gosora/query_gen/install.go

178 lines
5.0 KiB
Go
Raw Normal View History

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
}
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
}
Added the AboutSegment feature, you can see this in use on Cosora, it's a little raw right now, but I'm planning to polish it in the next commit. Refactored the code to use switches instead of if blocks in some places. Refactored the Dashboard to make it easier to add icons to it like I did with Cosora. You can now use maps in transpiled templates. Made progress on Cosora's footer. Swapped out the ThemeName property in the HeaderVars struct for a more general and flexible Theme property. Added the colstack CSS class to make it easier to style the layouts for the Control Panel and profile. Renamed the FStore variable to Forums. Renamed the Fpstore variable to FPStore. Renamed the Gstore variable to Groups. Split the MemoryTopicStore into DefaultTopicStore and MemoryTopicCache. Split the MemoryUserStore into DefaultUserStore and MemoryUserCache. Removed the NullUserStore, SQLUserStore, and SQLTopicStore. Added the NullTopicCache and NullUserCache. Moved the Reload method out of the TopicCache interface and into the TopicStore one. Moved the Reload method out of the UserCache interface and into the UserStore one. Added the SetCache and GetCache methods to the TopicStore and UserStore. Added the BypassGetAll method to the WordFilterMap type. Renamed routePanelSetting to routePanelSettingEdit. Renamed routePanelSettingEdit to routePanelSettingEditSubmit. Moved the page titles into the english language pack. Split main() into main and afterDBInit to avoid code duplication in general_test.go Added the ReqIsJson method so that we don't have to sniff the headers every time. Added the LogStore interface. Added the SQLModLogStore and the SQLAdminLogStore. Refactored the phrase system to use getPhrasePlaceholder instead of hard-coding the string to return in a bunch of functions. Removed a redundant rank check. Added the GuildStore to plugin_guilds. Added the about_segment_title and about_segment_body settings. Refactored the setting system to use predefined errors to make it easier for an upstream caller to filter out sensitive error messages as opposed to safe errors. Added the BypassGetAll method to the SettingMap type. Added the Update method to the SettingMap type. BulkGet is now exposed via the MemoryUserCache. Refactored more logs in the template transpiler to reduce the amount of indentation. Refactored the tests to take up fewer lines. Further improved the Cosora theme's colours, padding, and profiles. Added styling for the Control Panel Dashboard to the Cosora Theme. Reduced the amount of code duplication in the installer query generator and opened the door to certain types of auto-migrations. Refactored the Control Panel Dashboard to reduce the amount of code duplication. Refactored the modlog route to reduce the amount of code duplication and string concatenation.
2017-11-23 05:37:08 +00:00
// TODO: Add methods to this to construct it OO-like
type DBInstallTable struct {
2022-02-21 03:32:53 +00:00
Name string
Charset string
Collation string
Columns []DBTableColumn
Keys []DBTableKey
Added the AboutSegment feature, you can see this in use on Cosora, it's a little raw right now, but I'm planning to polish it in the next commit. Refactored the code to use switches instead of if blocks in some places. Refactored the Dashboard to make it easier to add icons to it like I did with Cosora. You can now use maps in transpiled templates. Made progress on Cosora's footer. Swapped out the ThemeName property in the HeaderVars struct for a more general and flexible Theme property. Added the colstack CSS class to make it easier to style the layouts for the Control Panel and profile. Renamed the FStore variable to Forums. Renamed the Fpstore variable to FPStore. Renamed the Gstore variable to Groups. Split the MemoryTopicStore into DefaultTopicStore and MemoryTopicCache. Split the MemoryUserStore into DefaultUserStore and MemoryUserCache. Removed the NullUserStore, SQLUserStore, and SQLTopicStore. Added the NullTopicCache and NullUserCache. Moved the Reload method out of the TopicCache interface and into the TopicStore one. Moved the Reload method out of the UserCache interface and into the UserStore one. Added the SetCache and GetCache methods to the TopicStore and UserStore. Added the BypassGetAll method to the WordFilterMap type. Renamed routePanelSetting to routePanelSettingEdit. Renamed routePanelSettingEdit to routePanelSettingEditSubmit. Moved the page titles into the english language pack. Split main() into main and afterDBInit to avoid code duplication in general_test.go Added the ReqIsJson method so that we don't have to sniff the headers every time. Added the LogStore interface. Added the SQLModLogStore and the SQLAdminLogStore. Refactored the phrase system to use getPhrasePlaceholder instead of hard-coding the string to return in a bunch of functions. Removed a redundant rank check. Added the GuildStore to plugin_guilds. Added the about_segment_title and about_segment_body settings. Refactored the setting system to use predefined errors to make it easier for an upstream caller to filter out sensitive error messages as opposed to safe errors. Added the BypassGetAll method to the SettingMap type. Added the Update method to the SettingMap type. BulkGet is now exposed via the MemoryUserCache. Refactored more logs in the template transpiler to reduce the amount of indentation. Refactored the tests to take up fewer lines. Further improved the Cosora theme's colours, padding, and profiles. Added styling for the Control Panel Dashboard to the Cosora Theme. Reduced the amount of code duplication in the installer query generator and opened the door to certain types of auto-migrations. Refactored the Control Panel Dashboard to reduce the amount of code duplication. Refactored the modlog route to reduce the amount of code duplication and string concatenation.
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
// TODO: Re-implement the query generation, query builder and installer adapters as layers on-top of a query text adapter
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
}
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
}
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
}
func (i *installer) AddPlugins(plugins ...QueryPlugin) {
2022-02-21 03:32:53 +00:00
i.plugins = append(i.plugins, plugins...)
}
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
}
// TODO: Let plugins manipulate the parameters like in CreateTable
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
}
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
}
Added the AboutSegment feature, you can see this in use on Cosora, it's a little raw right now, but I'm planning to polish it in the next commit. Refactored the code to use switches instead of if blocks in some places. Refactored the Dashboard to make it easier to add icons to it like I did with Cosora. You can now use maps in transpiled templates. Made progress on Cosora's footer. Swapped out the ThemeName property in the HeaderVars struct for a more general and flexible Theme property. Added the colstack CSS class to make it easier to style the layouts for the Control Panel and profile. Renamed the FStore variable to Forums. Renamed the Fpstore variable to FPStore. Renamed the Gstore variable to Groups. Split the MemoryTopicStore into DefaultTopicStore and MemoryTopicCache. Split the MemoryUserStore into DefaultUserStore and MemoryUserCache. Removed the NullUserStore, SQLUserStore, and SQLTopicStore. Added the NullTopicCache and NullUserCache. Moved the Reload method out of the TopicCache interface and into the TopicStore one. Moved the Reload method out of the UserCache interface and into the UserStore one. Added the SetCache and GetCache methods to the TopicStore and UserStore. Added the BypassGetAll method to the WordFilterMap type. Renamed routePanelSetting to routePanelSettingEdit. Renamed routePanelSettingEdit to routePanelSettingEditSubmit. Moved the page titles into the english language pack. Split main() into main and afterDBInit to avoid code duplication in general_test.go Added the ReqIsJson method so that we don't have to sniff the headers every time. Added the LogStore interface. Added the SQLModLogStore and the SQLAdminLogStore. Refactored the phrase system to use getPhrasePlaceholder instead of hard-coding the string to return in a bunch of functions. Removed a redundant rank check. Added the GuildStore to plugin_guilds. Added the about_segment_title and about_segment_body settings. Refactored the setting system to use predefined errors to make it easier for an upstream caller to filter out sensitive error messages as opposed to safe errors. Added the BypassGetAll method to the SettingMap type. Added the Update method to the SettingMap type. BulkGet is now exposed via the MemoryUserCache. Refactored more logs in the template transpiler to reduce the amount of indentation. Refactored the tests to take up fewer lines. Further improved the Cosora theme's colours, padding, and profiles. Added styling for the Control Panel Dashboard to the Cosora Theme. Reduced the amount of code duplication in the installer query generator and opened the door to certain types of auto-migrations. Refactored the Control Panel Dashboard to reduce the amount of code duplication. Refactored the modlog route to reduce the amount of code duplication and string concatenation.
2017-11-23 05:37:08 +00:00
// TODO: Let plugins manipulate the parameters like in CreateTable
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
Added the AboutSegment feature, you can see this in use on Cosora, it's a little raw right now, but I'm planning to polish it in the next commit. Refactored the code to use switches instead of if blocks in some places. Refactored the Dashboard to make it easier to add icons to it like I did with Cosora. You can now use maps in transpiled templates. Made progress on Cosora's footer. Swapped out the ThemeName property in the HeaderVars struct for a more general and flexible Theme property. Added the colstack CSS class to make it easier to style the layouts for the Control Panel and profile. Renamed the FStore variable to Forums. Renamed the Fpstore variable to FPStore. Renamed the Gstore variable to Groups. Split the MemoryTopicStore into DefaultTopicStore and MemoryTopicCache. Split the MemoryUserStore into DefaultUserStore and MemoryUserCache. Removed the NullUserStore, SQLUserStore, and SQLTopicStore. Added the NullTopicCache and NullUserCache. Moved the Reload method out of the TopicCache interface and into the TopicStore one. Moved the Reload method out of the UserCache interface and into the UserStore one. Added the SetCache and GetCache methods to the TopicStore and UserStore. Added the BypassGetAll method to the WordFilterMap type. Renamed routePanelSetting to routePanelSettingEdit. Renamed routePanelSettingEdit to routePanelSettingEditSubmit. Moved the page titles into the english language pack. Split main() into main and afterDBInit to avoid code duplication in general_test.go Added the ReqIsJson method so that we don't have to sniff the headers every time. Added the LogStore interface. Added the SQLModLogStore and the SQLAdminLogStore. Refactored the phrase system to use getPhrasePlaceholder instead of hard-coding the string to return in a bunch of functions. Removed a redundant rank check. Added the GuildStore to plugin_guilds. Added the about_segment_title and about_segment_body settings. Refactored the setting system to use predefined errors to make it easier for an upstream caller to filter out sensitive error messages as opposed to safe errors. Added the BypassGetAll method to the SettingMap type. Added the Update method to the SettingMap type. BulkGet is now exposed via the MemoryUserCache. Refactored more logs in the template transpiler to reduce the amount of indentation. Refactored the tests to take up fewer lines. Further improved the Cosora theme's colours, padding, and profiles. Added styling for the Control Panel Dashboard to the Cosora Theme. Reduced the amount of code duplication in the installer query generator and opened the door to certain types of auto-migrations. Refactored the Control Panel Dashboard to reduce the amount of code duplication. Refactored the modlog route to reduce the amount of code duplication and string concatenation.
2017-11-23 05:37:08 +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
}
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
}
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
}