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
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) SetAdapter ( name string ) error {
2017-07-12 11:05:18 +00:00
adap , err := GetAdapter ( name )
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
i . SetAdapterInstance ( adap )
2017-07-12 11:05:18 +00:00
return nil
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) SetAdapterInstance ( adap Adapter ) {
i . adapter = adap
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 ) {
i . plugins = append ( i . plugins , plugins ... )
2017-10-16 07:32:58 +00:00
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) CreateTable ( table , charset , collation string , columns [ ] DBTableColumn , keys [ ] DBTableKey ) error {
2019-05-06 04:04:00 +00:00
tableStruct := & DBInstallTable { table , charset , collation , columns , keys }
2020-02-20 23:55:45 +00:00
err := i . RunHook ( "CreateTableStart" , tableStruct )
2017-11-23 05:37:08 +00:00
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2020-02-20 23:55:45 +00:00
res , err := i . adapter . CreateTable ( "" , table , charset , collation , columns , keys )
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
err = i . RunHook ( "CreateTableAfter" , tableStruct )
2017-11-23 05:37:08 +00:00
if err != nil {
return err
2017-10-16 07:32:58 +00:00
}
2020-02-20 23:55:45 +00:00
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "create-table" } )
i . tables = append ( i . 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
2020-02-20 23:55:45 +00:00
func ( i * installer ) AddIndex ( table , iname , colname string ) error {
err := i . RunHook ( "AddIndexStart" , table , iname , colname )
2018-12-31 09:03:49 +00:00
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
res , err := i . adapter . AddIndex ( "" , table , iname , colname )
2018-12-31 09:03:49 +00:00
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
err = i . RunHook ( "AddIndexAfter" , table , iname , colname )
2018-12-31 09:03:49 +00:00
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "index" } )
return nil
}
func ( i * installer ) AddKey ( table , column string , key DBTableKey ) error {
err := i . RunHook ( "AddKeyStart" , table , column , key )
if err != nil {
return err
}
res , err := i . adapter . AddKey ( "" , table , column , key )
if err != nil {
return err
}
err = i . RunHook ( "AddKeyAfter" , table , column , key )
if err != nil {
return err
}
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "key" } )
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
2020-02-20 23:55:45 +00:00
func ( i * installer ) SimpleInsert ( table , columns , fields string ) error {
err := i . 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
}
2020-02-20 23:55:45 +00:00
res , err := i . adapter . SimpleInsert ( "" , table , columns , fields )
2017-10-14 07:39:22 +00:00
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
err = i . RunHook ( "SimpleInsertAfter" , table , columns , fields , res )
2017-11-23 05:37:08 +00:00
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
i . instructions = append ( i . instructions , DBInstallInstruction { table , res , "insert" } )
2017-11-23 05:37:08 +00:00
return nil
}
2020-02-20 23:55:45 +00:00
func ( i * installer ) RunHook ( name string , args ... interface { } ) error {
for _ , plugin := range i . 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
}
2020-02-20 23:55:45 +00:00
func ( i * 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
2020-02-20 23:55:45 +00:00
for _ , instr := range i . instructions {
2017-07-12 11:05:18 +00:00
if instr . Type == "create-table" {
2020-02-20 23:55:45 +00:00
err := writeFile ( "./schema/" + i . 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
2020-02-20 23:55:45 +00:00
err := writeFile ( "./schema/" + i . adapter . GetName ( ) + "/inserts.sql" , inserts )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
2020-02-20 23:55:45 +00:00
for _ , plugin := range i . 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
}