|
|
|
|
@ -19,12 +19,12 @@ const (
|
|
|
|
|
MercuryDatabaseName = "mercury"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//<editor-fold name="DBConnectorGeneric">
|
|
|
|
|
//<editor-fold name="ConnectorGeneric">
|
|
|
|
|
/*======================================================================================
|
|
|
|
|
DBConnectorGeneric
|
|
|
|
|
ConnectorGeneric
|
|
|
|
|
======================================================================================*/
|
|
|
|
|
|
|
|
|
|
type DBConnectorGeneric struct {
|
|
|
|
|
type ConnectorGeneric struct {
|
|
|
|
|
cachedConnection *sql.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -33,7 +33,7 @@ type sqlScriptRunner struct {
|
|
|
|
|
DatabaseName string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *DBConnectorGeneric) ExecuteSqlScript(runner *sqlScriptRunner) {
|
|
|
|
|
func (c *ConnectorGeneric) ExecuteSqlScript(runner *sqlScriptRunner) {
|
|
|
|
|
c.startConnection(runner.DatabaseName)
|
|
|
|
|
defer c.returnConnection()
|
|
|
|
|
queryWhole := *loadSqlFile(runner.ScriptName)
|
|
|
|
|
@ -54,13 +54,13 @@ func (c *DBConnectorGeneric) ExecuteSqlScript(runner *sqlScriptRunner) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// startConnection this initializes, caches, and returns a connection for the desired database
|
|
|
|
|
func (c *DBConnectorGeneric) startConnection(dataBase string) {
|
|
|
|
|
func (c *ConnectorGeneric) startConnection(dataBase string) {
|
|
|
|
|
db := createDbConnection(dataBase)
|
|
|
|
|
c.cachedConnection = db
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returnConnection This just closes the cached db connection
|
|
|
|
|
func (c *DBConnectorGeneric) returnConnection() {
|
|
|
|
|
func (c *ConnectorGeneric) returnConnection() {
|
|
|
|
|
err := c.cachedConnection.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panic(err)
|
|
|
|
|
@ -68,7 +68,7 @@ func (c *DBConnectorGeneric) returnConnection() {
|
|
|
|
|
c.cachedConnection = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *DBConnectorGeneric) QueryFromScript(scriptName string) *sql.Rows {
|
|
|
|
|
func (c *ConnectorGeneric) QueryFromScript(scriptName string) *sql.Rows {
|
|
|
|
|
query := loadSqlFile(scriptName)
|
|
|
|
|
rows, err := c.cachedConnection.Query(*query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -77,7 +77,7 @@ func (c *DBConnectorGeneric) QueryFromScript(scriptName string) *sql.Rows {
|
|
|
|
|
return rows
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *DBConnectorGeneric) CreateTables() {
|
|
|
|
|
func (c *ConnectorGeneric) CreateTables() {
|
|
|
|
|
tableCreationRunners := []*sqlScriptRunner{
|
|
|
|
|
NewRunner("create-any-database.sql", ""),
|
|
|
|
|
NewRunner("create-insight-user-table.sql", InsightDatabaseName),
|
|
|
|
|
@ -124,7 +124,7 @@ func loadSqlFile(scriptName string) *string {
|
|
|
|
|
return &str
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func QueryForObjects[K any](db *DBConnectorGeneric, dbName string, queryScript string, mapping func(rows *sql.Rows) *K) *[]*K {
|
|
|
|
|
func QueryForObjects[K any](db *ConnectorGeneric, dbName string, queryScript string, mapping func(rows *sql.Rows) *K) *[]*K {
|
|
|
|
|
rs := make([]*K, 0, 10000)
|
|
|
|
|
db.startConnection(dbName)
|
|
|
|
|
defer db.returnConnection()
|
|
|
|
|
@ -138,7 +138,7 @@ func QueryForObjects[K any](db *DBConnectorGeneric, dbName string, queryScript s
|
|
|
|
|
return &rs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BulkUpdate[K any](db *DBConnectorGeneric, dbName string, updateScript string, items *[]*K, mapping func(s *sql.Stmt, item *K)) {
|
|
|
|
|
func BulkUpdate[K any](db *ConnectorGeneric, dbName string, updateScript string, items *[]*K, mapping func(s *sql.Stmt, item *K)) {
|
|
|
|
|
db.startConnection(dbName)
|
|
|
|
|
defer db.returnConnection()
|
|
|
|
|
statement := loadSqlFile(updateScript)
|
|
|
|
|
|