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
}
2019-10-06 22:20:37 +00:00
func ( ins * installer ) SetAdapter ( name string ) error {
2017-07-12 11:05:18 +00:00
adap , err := GetAdapter ( name )
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
ins . SetAdapterInstance ( adap )
2017-07-12 11:05:18 +00:00
return nil
}
2019-10-06 22:20:37 +00:00
func ( ins * installer ) SetAdapterInstance ( adapter Adapter ) {
ins . adapter = adapter
ins . instructions = [ ] DBInstallInstruction { }
2017-07-12 11:05:18 +00:00
}
2019-10-06 22:20:37 +00:00
func ( ins * installer ) AddPlugins ( plugins ... QueryPlugin ) {
ins . plugins = append ( ins . plugins , plugins ... )
2017-10-16 07:32:58 +00:00
}
2019-10-06 22:20:37 +00:00
func ( ins * 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 }
2019-10-06 22:20:37 +00:00
err := ins . RunHook ( "CreateTableStart" , tableStruct )
2017-11-23 05:37:08 +00:00
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2019-10-06 22:20:37 +00:00
res , err := ins . adapter . CreateTable ( "" , table , charset , collation , columns , keys )
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
err = ins . RunHook ( "CreateTableAfter" , tableStruct )
2017-11-23 05:37:08 +00:00
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2019-10-06 22:20:37 +00:00
ins . instructions = append ( ins . instructions , DBInstallInstruction { table , res , "create-table" } )
ins . tables = append ( ins . 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
2019-10-06 22:20:37 +00:00
func ( ins * installer ) AddIndex ( table string , iname string , colname string ) error {
err := ins . RunHook ( "AddIndexStart" , table , iname , colname )
2018-12-31 09:03:49 +00:00
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
res , err := ins . adapter . AddIndex ( "" , table , iname , colname )
2018-12-31 09:03:49 +00:00
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
err = ins . RunHook ( "AddIndexAfter" , table , iname , colname )
2018-12-31 09:03:49 +00:00
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
ins . instructions = append ( ins . 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
2019-10-06 22:20:37 +00:00
func ( ins * installer ) SimpleInsert ( table string , columns string , fields string ) error {
err := ins . RunHook ( "SimpleInsertStart" , table , columns , fields )
2017-11-23 05:37:08 +00:00
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2019-10-06 22:20:37 +00:00
res , err := ins . adapter . SimpleInsert ( "" , table , columns , fields )
2017-10-14 07:39:22 +00:00
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
err = ins . RunHook ( "SimpleInsertAfter" , table , columns , fields , res )
2017-11-23 05:37:08 +00:00
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
ins . instructions = append ( ins . instructions , DBInstallInstruction { table , res , "insert" } )
2017-11-23 05:37:08 +00:00
return nil
}
2019-10-06 22:20:37 +00:00
func ( ins * installer ) RunHook ( name string , args ... interface { } ) error {
for _ , plugin := range ins . 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
}
2019-10-06 22:20:37 +00:00
func ( ins * installer ) Write ( ) error {
2017-07-12 11:05:18 +00:00
var inserts string
// We can't escape backticks, so we have to dump it out a file at a time
2019-10-06 22:20:37 +00:00
for _ , instr := range ins . instructions {
2017-07-12 11:05:18 +00:00
if instr . Type == "create-table" {
2019-10-06 22:20:37 +00:00
err := writeFile ( "./schema/" + ins . 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
2019-10-06 22:20:37 +00:00
err := writeFile ( "./schema/" + ins . adapter . GetName ( ) + "/inserts.sql" , inserts )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
2019-10-06 22:20:37 +00:00
for _ , plugin := range ins . plugins {
2017-10-16 07:32:58 +00:00
err := plugin . Write ( )
if err != nil {
return err
}
}
return nil
2017-07-12 11:05:18 +00:00
}