2017-07-12 11:05:18 +00:00
package qgen
var Install * installer
func init ( ) {
2022-02-21 03:32:53 +00:00
Install = & installer { instructions : [ ] DBInstallInstruction { } }
2017-07-12 11:05:18 +00:00
}
2019-05-06 04:04:00 +00:00
type DBInstallInstruction struct {
2022-02-21 03:32:53 +00:00
Table string
Contents string
Type string
2017-07-12 11:05:18 +00:00
}
2017-11-23 05:37:08 +00:00
// TODO: Add methods to this to construct it OO-like
2019-05-06 04:04:00 +00:00
type DBInstallTable struct {
2022-02-21 03:32:53 +00:00
Name string
Charset string
Collation string
Columns [ ] DBTableColumn
Keys [ ] DBTableKey
2017-11-23 05:37:08 +00:00
}
2017-07-12 11:05:18 +00:00
// A set of wrappers around the generator methods, so we can use this in the installer
2017-09-10 16:57:22 +00:00
// TODO: Re-implement the query generation, query builder and installer adapters as layers on-top of a query text adapter
2017-09-03 04:50:31 +00:00
type installer struct {
2022-02-21 03:32:53 +00:00
adapter Adapter
instructions [ ] DBInstallInstruction
tables [ ] * DBInstallTable // TODO: Use this in Record() in the next commit to allow us to auto-migrate settings rather than manually patching them in on upgrade
plugins [ ] QueryPlugin
2017-07-12 11:05:18 +00:00
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) SetAdapter ( name string ) error {
2022-02-21 03:32:53 +00:00
a , err := GetAdapter ( name )
if err != nil {
return err
}
i . SetAdapterInstance ( a )
return nil
2017-07-12 11:05:18 +00:00
}
2021-04-29 22:33:02 +00:00
func ( i * installer ) SetAdapterInstance ( a Adapter ) {
2022-02-21 03:32:53 +00:00
i . adapter = a
i . instructions = [ ] DBInstallInstruction { }
2017-07-12 11:05:18 +00:00
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) AddPlugins ( plugins ... QueryPlugin ) {
2022-02-21 03:32:53 +00:00
i . plugins = append ( i . plugins , plugins ... )
2017-10-16 07:32:58 +00:00
}
2021-04-29 22:33:02 +00:00
func ( i * installer ) CreateTable ( table , charset , collation string , cols [ ] DBTableColumn , keys [ ] DBTableKey ) error {
2022-02-21 03:32:53 +00:00
tableStruct := & DBInstallTable { table , charset , collation , cols , keys }
err := i . RunHook ( "CreateTableStart" , tableStruct )
if err != nil {
return err
}
res , err := i . adapter . CreateTable ( "" , table , charset , collation , cols , keys )
if err != nil {
return err
}
err = i . RunHook ( "CreateTableAfter" , tableStruct )
if err != nil {
return err
}
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "create-table" } )
i . tables = append ( i . tables , tableStruct )
return nil
2017-07-12 11:05:18 +00:00
}
2018-12-31 09:03:49 +00:00
// TODO: Let plugins manipulate the parameters like in CreateTable
2021-04-29 22:33:02 +00:00
func ( i * installer ) AddIndex ( table , iname , colName string ) error {
2022-02-21 03:32:53 +00:00
err := i . RunHook ( "AddIndexStart" , table , iname , colName )
if err != nil {
return err
}
res , err := i . adapter . AddIndex ( "" , table , iname , colName )
if err != nil {
return err
}
err = i . RunHook ( "AddIndexAfter" , table , iname , colName )
if err != nil {
return err
}
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "index" } )
return nil
2020-02-20 23:55:45 +00:00
}
2021-04-29 22:33:02 +00:00
func ( i * installer ) AddKey ( table , col string , key DBTableKey ) error {
2022-02-21 03:32:53 +00:00
err := i . RunHook ( "AddKeyStart" , table , col , key )
if err != nil {
return err
}
res , err := i . adapter . AddKey ( "" , table , col , key )
if err != nil {
return err
}
err = i . RunHook ( "AddKeyAfter" , table , col , key )
if err != nil {
return err
}
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "key" } )
return nil
2018-12-31 09:03:49 +00:00
}
2017-11-23 05:37:08 +00:00
// TODO: Let plugins manipulate the parameters like in CreateTable
2020-02-20 23:55:45 +00:00
func ( i * installer ) SimpleInsert ( table , columns , fields string ) error {
2022-02-21 03:32:53 +00:00
err := i . RunHook ( "SimpleInsertStart" , table , columns , fields )
if err != nil {
return err
}
res , err := i . adapter . SimpleInsert ( "" , table , columns , fields )
if err != nil {
return err
}
err = i . RunHook ( "SimpleInsertAfter" , table , columns , fields , res )
if err != nil {
return err
}
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "insert" } )
return nil
2017-11-23 05:37:08 +00:00
}
2021-04-29 22:33:02 +00:00
func ( i * installer ) SimpleBulkInsert ( table , cols string , fieldSet [ ] string ) error {
2022-02-21 03:32:53 +00:00
err := i . RunHook ( "SimpleBulkInsertStart" , table , cols , fieldSet )
if err != nil {
return err
}
res , err := i . adapter . SimpleBulkInsert ( "" , table , cols , fieldSet )
if err != nil {
return err
}
err = i . RunHook ( "SimpleBulkInsertAfter" , table , cols , fieldSet , res )
if err != nil {
return err
}
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "bulk-insert" } )
return nil
2021-04-29 22:33:02 +00:00
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) RunHook ( name string , args ... interface { } ) error {
2022-02-21 03:32:53 +00:00
for _ , pl := range i . plugins {
err := pl . Hook ( name , args ... )
if err != nil {
return err
}
}
return nil
2017-10-14 07:39:22 +00:00
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) Write ( ) error {
2022-02-21 03:32:53 +00:00
var inserts string
// We can't escape backticks, so we have to dump it out a file at a time
for _ , instr := range i . instructions {
if instr . Type == "create-table" {
err := writeFile ( "./schema/" + i . adapter . GetName ( ) + "/query_" + instr . Table + ".sql" , instr . Contents )
if err != nil {
return err
}
} else {
inserts += instr . Contents + ";\n"
}
}
err := writeFile ( "./schema/" + i . adapter . GetName ( ) + "/inserts.sql" , inserts )
if err != nil {
return err
}
for _ , plugin := range i . plugins {
err := plugin . Write ( )
if err != nil {
return err
}
}
return nil
2017-07-12 11:05:18 +00:00
}