2017-07-12 11:05:18 +00:00
|
|
|
/* WIP Under Construction */
|
|
|
|
package qgen
|
|
|
|
|
|
|
|
var Install *installer
|
|
|
|
|
|
|
|
func init() {
|
2017-09-03 04:50:31 +00:00
|
|
|
Install = &installer{instructions: []DB_Install_Instruction{}}
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type DB_Install_Instruction struct {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
adapter DB_Adapter
|
2017-07-12 11:05:18 +00:00
|
|
|
instructions []DB_Install_Instruction
|
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
|
|
|
|
}
|
|
|
|
install.adapter = adap
|
2017-10-14 07:39:22 +00:00
|
|
|
install.instructions = []DB_Install_Instruction{}
|
2017-07-12 11:05:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (install *installer) SetAdapterInstance(adapter DB_Adapter) {
|
|
|
|
install.adapter = adapter
|
2017-10-14 07:39:22 +00:00
|
|
|
install.instructions = []DB_Install_Instruction{}
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 07:32:58 +00:00
|
|
|
func (install *installer) RegisterPlugin(plugin QueryPlugin) {
|
|
|
|
install.plugins = append(install.plugins, plugin)
|
|
|
|
}
|
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
func (install *installer) CreateTable(table string, charset string, collation string, columns []DB_Table_Column, keys []DB_Table_Key) error {
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, plugin := range install.plugins {
|
|
|
|
err := plugin.Hook("CreateTableStart", table, charset, collation, columns, keys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 11:05:18 +00:00
|
|
|
res, err := install.adapter.CreateTable("_installer", table, charset, collation, columns, keys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, plugin := range install.plugins {
|
|
|
|
err := plugin.Hook("CreateTableAfter", table, charset, collation, columns, keys, res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
install.instructions = append(install.instructions, DB_Install_Instruction{table, res, "create-table"})
|
2017-07-12 11:05:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:39:22 +00:00
|
|
|
func (install *installer) SimpleInsert(table string, columns string, fields string) error {
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, plugin := range install.plugins {
|
|
|
|
err := plugin.Hook("SimpleInsertStart", table, columns, fields)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-14 07:39:22 +00:00
|
|
|
res, err := install.adapter.SimpleInsert("_installer", table, columns, fields)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, plugin := range install.plugins {
|
|
|
|
err := plugin.Hook("SimpleInsertAfter", table, columns, fields, res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-14 07:39:22 +00:00
|
|
|
install.instructions = append(install.instructions, DB_Install_Instruction{table, res, "insert"})
|
|
|
|
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
|
|
|
}
|