2017-07-12 11:05:18 +00:00
package qgen
var Install * installer
func init ( ) {
2019-05-06 04:04:00 +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 {
2017-09-03 04:50:31 +00:00
Table string
2017-07-12 11:05:18 +00:00
Contents string
2017-09-03 04:50:31 +00:00
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 {
2017-11-23 05:37:08 +00:00
Name string
Charset string
Collation string
Columns [ ] DBTableColumn
Keys [ ] DBTableKey
}
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 {
2017-11-12 05:25:04 +00:00
adapter Adapter
2019-05-06 04:04:00 +00:00
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
2017-10-16 07:32:58 +00:00
plugins [ ] QueryPlugin
2017-07-12 11:05:18 +00:00
}
func ( install * installer ) SetAdapter ( name string ) error {
adap , err := GetAdapter ( name )
if err != nil {
return err
}
2017-11-23 05:37:08 +00:00
install . SetAdapterInstance ( adap )
2017-07-12 11:05:18 +00:00
return nil
}
2017-11-12 05:25:04 +00:00
func ( install * installer ) SetAdapterInstance ( adapter Adapter ) {
2017-07-12 11:05:18 +00:00
install . adapter = adapter
2019-05-06 04:04:00 +00:00
install . instructions = [ ] DBInstallInstruction { }
2017-07-12 11:05:18 +00:00
}
2017-11-23 05:37:08 +00:00
func ( install * installer ) AddPlugins ( plugins ... QueryPlugin ) {
install . plugins = append ( install . plugins , plugins ... )
2017-10-16 07:32:58 +00:00
}
2017-11-12 05:25:04 +00:00
func ( install * installer ) CreateTable ( table string , charset string , collation string , columns [ ] DBTableColumn , keys [ ] DBTableKey ) error {
2019-05-06 04:04:00 +00:00
tableStruct := & DBInstallTable { table , charset , collation , columns , keys }
2017-11-23 05:37:08 +00:00
err := install . RunHook ( "CreateTableStart" , tableStruct )
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2018-12-31 09:03:49 +00:00
res , err := install . adapter . CreateTable ( "" , table , charset , collation , columns , keys )
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
2017-11-23 05:37:08 +00:00
err = install . RunHook ( "CreateTableAfter" , tableStruct )
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2019-05-06 04:04:00 +00:00
install . instructions = append ( install . instructions , DBInstallInstruction { table , res , "create-table" } )
2017-11-23 05:37:08 +00:00
install . tables = append ( install . tables , tableStruct )
2017-07-12 11:05:18 +00:00
return nil
}
2018-12-31 09:03:49 +00:00
// TODO: Let plugins manipulate the parameters like in CreateTable
func ( install * installer ) AddIndex ( table string , iname string , colname string ) error {
err := install . RunHook ( "AddIndexStart" , table , iname , colname )
if err != nil {
return err
}
res , err := install . adapter . AddIndex ( "" , table , iname , colname )
if err != nil {
return err
}
err = install . RunHook ( "AddIndexAfter" , table , iname , colname )
if err != nil {
return err
}
2019-05-06 04:04:00 +00:00
install . instructions = append ( install . instructions , DBInstallInstruction { table , res , "index" } )
2018-12-31 09:03:49 +00:00
return nil
}
2017-11-23 05:37:08 +00:00
// TODO: Let plugins manipulate the parameters like in CreateTable
2017-10-14 07:39:22 +00:00
func ( install * installer ) SimpleInsert ( table string , columns string , fields string ) error {
2017-11-23 05:37:08 +00:00
err := install . RunHook ( "SimpleInsertStart" , table , columns , fields )
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2018-12-31 09:03:49 +00:00
res , err := install . adapter . SimpleInsert ( "" , table , columns , fields )
2017-10-14 07:39:22 +00:00
if err != nil {
return err
}
2017-11-23 05:37:08 +00:00
err = install . RunHook ( "SimpleInsertAfter" , table , columns , fields , res )
if err != nil {
return err
}
2019-05-06 04:04:00 +00:00
install . instructions = append ( install . instructions , DBInstallInstruction { table , res , "insert" } )
2017-11-23 05:37:08 +00:00
return nil
}
func ( install * installer ) RunHook ( name string , args ... interface { } ) error {
2017-10-16 07:32:58 +00:00
for _ , plugin := range install . plugins {
2017-11-23 05:37:08 +00:00
err := plugin . Hook ( name , args ... )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
}
2017-10-14 07:39:22 +00:00
return nil
}
2017-07-12 11:05:18 +00:00
func ( install * installer ) Write ( ) error {
var inserts string
// We can't escape backticks, so we have to dump it out a file at a time
for _ , instr := range install . instructions {
if instr . Type == "create-table" {
2017-09-03 04:50:31 +00:00
err := writeFile ( "./schema/" + install . adapter . GetName ( ) + "/query_" + instr . Table + ".sql" , instr . Contents )
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
} else {
2017-10-14 07:39:22 +00:00
inserts += instr . Contents + ";\n"
2017-07-12 11:05:18 +00:00
}
}
2017-10-16 07:32:58 +00:00
err := writeFile ( "./schema/" + install . adapter . GetName ( ) + "/inserts.sql" , inserts )
if err != nil {
return err
}
for _ , plugin := range install . plugins {
err := plugin . Write ( )
if err != nil {
return err
}
}
return nil
2017-07-12 11:05:18 +00:00
}