<commit_before>package service

import (
	"database/sql"
	_ "github.com/lib/pq"
	"github.com/nleof/goyesql"
	"log"
	"os"
)

// PsqlDao is an entity that implements per-user connection pool limits and
// facilitates access to SQL queries loaded from disk.
type PsqlDao struct {
	// dao is a collection of *sql.DB identified by strings (usernames), the point
	// being that each user gets their own connection pool
	dao  map[string]*sql.DB
	ddl  goyesql.Queries
	dml  goyesql.Queries
	conn string
}

// AppDB is an identifier for a specific *sql.DB in our dao map
const AppDB = "_app"

func NewPsqlDao(conn string) *PsqlDao {
	db := PsqlDao{}
	db.conn = os.Getenv("DATABASE_URL")

	if db.conn == "" {
		log.Fatal("DATABASE_URL environment variable not set!")
	}

	db.dao = make(map[string]*sql.DB)

	// Create all the Tables, Views if they do not exist
	db.ddl = goyesql.MustParseFile("data/sql/ddl.sql")

	db.EnsurePool(AppDB)

	logExec(db.dao[AppDB], (string)(db.ddl["create-user-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-command-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-tag-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-invocation-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-invocationtag-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-commandhistory-view"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-timestamp-index"]))

	// Load all data-manipulation queries
	db.dml = goyesql.MustParseFile("data/sql/queries.sql")

	log.Println("storage init completed")
	return &db
}

// Query fetches an SQL query that matches the given identifier
func (r *PsqlDao) Query(identifier string) string {
	return (string)(r.dml[(goyesql.Tag)(identifier)])
}

func logExec(conn *sql.DB, query string) {
	log.Printf("executing query: '%s'\n", query)
	_, err := conn.Exec(query)
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("executed query: '%s'\n", query)
	}
}

// EnsurePool verifies that we have created a connection pool for the given user
func (r *PsqlDao) EnsurePool(user string) (db *sql.DB) {
	db, exists := r.dao[user]

	if !exists {
		var err error
		r.dao[user], err = sql.Open("postgres", r.conn)
		r.dao[user].SetMaxOpenConns(5)
		r.dao[user].SetMaxIdleConns(1)

		if err != nil {
			log.Fatal(err)
		}
	}
	return
}<commit_msg>fix bad pointer on first EnsurePool<commit_after>package service

import (
	"database/sql"
	_ "github.com/lib/pq"
	"github.com/nleof/goyesql"
	"log"
	"os"
)

// PsqlDao is an entity that implements per-user connection pool limits and
// facilitates access to SQL queries loaded from disk.
type PsqlDao struct {
	// dao is a collection of *sql.DB identified by strings (usernames), the point
	// being that each user gets their own connection pool
	dao  map[string]*sql.DB
	ddl  goyesql.Queries
	dml  goyesql.Queries
	conn string
}

// AppDB is an identifier for a specific *sql.DB in our dao map
const AppDB = "_app"

func NewPsqlDao(conn string) *PsqlDao {
	db := PsqlDao{}
	db.conn = os.Getenv("DATABASE_URL")

	if db.conn == "" {
		log.Fatal("DATABASE_URL environment variable not set!")
	}

	db.dao = make(map[string]*sql.DB)

	// Create all the Tables, Views if they do not exist
	db.ddl = goyesql.MustParseFile("data/sql/ddl.sql")

	db.EnsurePool(AppDB)

	logExec(db.dao[AppDB], (string)(db.ddl["create-user-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-command-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-tag-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-invocation-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-invocationtag-table"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-commandhistory-view"]))
	logExec(db.dao[AppDB], (string)(db.ddl["create-timestamp-index"]))

	// Load all data-manipulation queries
	db.dml = goyesql.MustParseFile("data/sql/queries.sql")

	log.Println("storage init completed")
	return &db
}

// Query fetches an SQL query that matches the given identifier
func (r *PsqlDao) Query(identifier string) string {
	return (string)(r.dml[(goyesql.Tag)(identifier)])
}

func logExec(conn *sql.DB, query string) {
	log.Printf("executing query: '%s'\n", query)
	_, err := conn.Exec(query)
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("executed query: '%s'\n", query)
	}
}

// EnsurePool verifies that we have created a connection pool for the given user
func (r *PsqlDao) EnsurePool(user string) *sql.DB {
	_, exists := r.dao[user]

	if !exists {
		var err error
		r.dao[user], err = sql.Open("postgres", r.conn)
		r.dao[user].SetMaxOpenConns(5)
		r.dao[user].SetMaxIdleConns(1)

		if err != nil {
			log.Fatal(err)
		}
	}
	return r.dao[user]
}
{'fraction_non_alphanumeric': 0.15555077452667815, 'fraction_numerical': 0.0008605851979345956, 'mean_word_length': 4.374566473988439, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4848919', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1789, 'n_words': 416}