add RemoveIndex adapter method

add AddKey installer method
This commit is contained in:
Azareal 2020-02-21 09:55:45 +10:00
parent 79546715a1
commit 3d1fd807d3
7 changed files with 117 additions and 51 deletions

View File

@ -136,6 +136,10 @@ func (b *builder) AddKey(table, column string, key DBTableKey) (stmt *sql.Stmt,
return b.prepare(b.adapter.AddKey("", table, column, key))
}
func (b *builder) RemoveIndex(table, iname string) (stmt *sql.Stmt, err error) {
return b.prepare(b.adapter.RemoveIndex("", table, iname))
}
func (b *builder) AddForeignKey(table, column, ftable, fcolumn string, cascade bool) (stmt *sql.Stmt, err error) {
return b.prepare(b.adapter.AddForeignKey("", table, column, ftable, fcolumn, cascade))
}

View File

@ -30,81 +30,98 @@ type installer struct {
plugins []QueryPlugin
}
func (ins *installer) SetAdapter(name string) error {
func (i *installer) SetAdapter(name string) error {
adap, err := GetAdapter(name)
if err != nil {
return err
}
ins.SetAdapterInstance(adap)
i.SetAdapterInstance(adap)
return nil
}
func (ins *installer) SetAdapterInstance(adapter Adapter) {
ins.adapter = adapter
ins.instructions = []DBInstallInstruction{}
func (i *installer) SetAdapterInstance(adap Adapter) {
i.adapter = adap
i.instructions = []DBInstallInstruction{}
}
func (ins *installer) AddPlugins(plugins ...QueryPlugin) {
ins.plugins = append(ins.plugins, plugins...)
func (i *installer) AddPlugins(plugins ...QueryPlugin) {
i.plugins = append(i.plugins, plugins...)
}
func (ins *installer) CreateTable(table string, charset string, collation string, columns []DBTableColumn, keys []DBTableKey) error {
func (i *installer) CreateTable(table, charset, collation string, columns []DBTableColumn, keys []DBTableKey) error {
tableStruct := &DBInstallTable{table, charset, collation, columns, keys}
err := ins.RunHook("CreateTableStart", tableStruct)
err := i.RunHook("CreateTableStart", tableStruct)
if err != nil {
return err
}
res, err := ins.adapter.CreateTable("", table, charset, collation, columns, keys)
res, err := i.adapter.CreateTable("", table, charset, collation, columns, keys)
if err != nil {
return err
}
err = ins.RunHook("CreateTableAfter", tableStruct)
err = i.RunHook("CreateTableAfter", tableStruct)
if err != nil {
return err
}
ins.instructions = append(ins.instructions, DBInstallInstruction{table, res, "create-table"})
ins.tables = append(ins.tables, tableStruct)
i.instructions = append(i.instructions, DBInstallInstruction{table, res, "create-table"})
i.tables = append(i.tables, tableStruct)
return nil
}
// TODO: Let plugins manipulate the parameters like in CreateTable
func (ins *installer) AddIndex(table string, iname string, colname string) error {
err := ins.RunHook("AddIndexStart", table, iname, colname)
func (i *installer) AddIndex(table, iname, colname string) error {
err := i.RunHook("AddIndexStart", table, iname, colname)
if err != nil {
return err
}
res, err := ins.adapter.AddIndex("", table, iname, colname)
res, err := i.adapter.AddIndex("", table, iname, colname)
if err != nil {
return err
}
err = ins.RunHook("AddIndexAfter", table, iname, colname)
err = i.RunHook("AddIndexAfter", table, iname, colname)
if err != nil {
return err
}
ins.instructions = append(ins.instructions, DBInstallInstruction{table, res, "index"})
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"})
return nil
}
// TODO: Let plugins manipulate the parameters like in CreateTable
func (ins *installer) SimpleInsert(table string, columns string, fields string) error {
err := ins.RunHook("SimpleInsertStart", table, columns, fields)
func (i *installer) SimpleInsert(table, columns, fields string) error {
err := i.RunHook("SimpleInsertStart", table, columns, fields)
if err != nil {
return err
}
res, err := ins.adapter.SimpleInsert("", table, columns, fields)
res, err := i.adapter.SimpleInsert("", table, columns, fields)
if err != nil {
return err
}
err = ins.RunHook("SimpleInsertAfter", table, columns, fields, res)
err = i.RunHook("SimpleInsertAfter", table, columns, fields, res)
if err != nil {
return err
}
ins.instructions = append(ins.instructions, DBInstallInstruction{table, res, "insert"})
i.instructions = append(i.instructions, DBInstallInstruction{table, res, "insert"})
return nil
}
func (ins *installer) RunHook(name string, args ...interface{}) error {
for _, plugin := range ins.plugins {
func (i *installer) RunHook(name string, args ...interface{}) error {
for _, plugin := range i.plugins {
err := plugin.Hook(name, args...)
if err != nil {
return err
@ -113,12 +130,12 @@ func (ins *installer) RunHook(name string, args ...interface{}) error {
return nil
}
func (ins *installer) Write() error {
func (i *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 ins.instructions {
for _, instr := range i.instructions {
if instr.Type == "create-table" {
err := writeFile("./schema/"+ins.adapter.GetName()+"/query_"+instr.Table+".sql", instr.Contents)
err := writeFile("./schema/"+i.adapter.GetName()+"/query_"+instr.Table+".sql", instr.Contents)
if err != nil {
return err
}
@ -127,12 +144,12 @@ func (ins *installer) Write() error {
}
}
err := writeFile("./schema/"+ins.adapter.GetName()+"/inserts.sql", inserts)
err := writeFile("./schema/"+i.adapter.GetName()+"/inserts.sql", inserts)
if err != nil {
return err
}
for _, plugin := range ins.plugins {
for _, plugin := range i.plugins {
err := plugin.Write()
if err != nil {
return err

View File

@ -188,7 +188,7 @@ func (a *MssqlAdapter) AddIndex(name, table, iname, colname string) (string, err
// TODO: Implement this
// TODO: Test to make sure everything works here
func (a *MssqlAdapter) AddKey(name string, table string, column string, key DBTableKey) (string, error) {
func (a *MssqlAdapter) AddKey(name, table, column string, key DBTableKey) (string, error) {
if table == "" {
return "", errors.New("You need a name for this table")
}
@ -200,7 +200,19 @@ func (a *MssqlAdapter) AddKey(name string, table string, column string, key DBTa
// TODO: Implement this
// TODO: Test to make sure everything works here
func (a *MssqlAdapter) AddForeignKey(name string, table string, column string, ftable string, fcolumn string, cascade bool) (out string, e error) {
func (a *MssqlAdapter) RemoveIndex(name, table, iname string) (string, error) {
if table == "" {
return "", errors.New("You need a name for this table")
}
if iname == "" {
return "", errors.New("You need a name for the index")
}
return "", errors.New("not implemented")
}
// TODO: Implement this
// TODO: Test to make sure everything works here
func (a *MssqlAdapter) AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error) {
c := func(str string, val bool) {
if e != nil || !val {
return

View File

@ -283,6 +283,20 @@ func (a *MysqlAdapter) AddKey(name, table, column string, key DBTableKey) (strin
return q, nil
}
func (a *MysqlAdapter) RemoveIndex(name, table, iname string) (string, error) {
if table == "" {
return "", errors.New("You need a name for this table")
}
if iname == "" {
return "", errors.New("You need a name for the index")
}
q := "ALTER TABLE `" + table + "` DROP INDEX `" + iname + "`"
// TODO: Shunt the table name logic and associated stmt list up to the a higher layer to reduce the amount of unnecessary overhead in the builder / accumulator
a.pushStatement(name, "remove-index", q)
return q, nil
}
func (a *MysqlAdapter) AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error) {
c := func(str string, val bool) {
if e != nil || !val {
@ -587,9 +601,12 @@ func (a *MysqlAdapter) buildFlexiWhere(where string, dateCutoff *dateCutoff) (q
}
q = " WHERE"
if dateCutoff != nil {
if dateCutoff.Type == 0 {
switch dateCutoff.Type {
case 0:
q += " " + dateCutoff.Column + " BETWEEN (UTC_TIMESTAMP() - interval " + strconv.Itoa(dateCutoff.Quantity) + " " + dateCutoff.Unit + ") AND UTC_TIMESTAMP() AND"
} else {
case 11:
q += " " + dateCutoff.Column + " < UTC_TIMESTAMP() - interval ? " + dateCutoff.Unit + " AND"
default:
q += " " + dateCutoff.Column + " < UTC_TIMESTAMP() - interval " + strconv.Itoa(dateCutoff.Quantity) + " " + dateCutoff.Unit + " AND"
}
}

View File

@ -53,43 +53,43 @@ func (a *PgsqlAdapter) DropTable(name, table string) (string, error) {
// TODO: Implement this
// We may need to change the CreateTable API to better suit PGSQL and the other database drivers which are coming up
func (a *PgsqlAdapter) CreateTable(name, table, charset, collation string, columns []DBTableColumn, keys []DBTableKey) (string, error) {
func (a *PgsqlAdapter) CreateTable(name, table, charset, collation string, cols []DBTableColumn, keys []DBTableKey) (string, error) {
if table == "" {
return "", errors.New("You need a name for this table")
}
if len(columns) == 0 {
if len(cols) == 0 {
return "", errors.New("You can't have a table with no columns")
}
q := "CREATE TABLE \"" + table + "\" ("
for _, column := range columns {
if column.AutoIncrement {
column.Type = "serial"
} else if column.Type == "createdAt" {
column.Type = "timestamp"
} else if column.Type == "datetime" {
column.Type = "timestamp"
for _, col := range cols {
if col.AutoIncrement {
col.Type = "serial"
} else if col.Type == "createdAt" {
col.Type = "timestamp"
} else if col.Type == "datetime" {
col.Type = "timestamp"
}
var size string
if column.Size > 0 {
size = " (" + strconv.Itoa(column.Size) + ")"
if col.Size > 0 {
size = " (" + strconv.Itoa(col.Size) + ")"
}
var end string
if column.Default != "" {
if col.Default != "" {
end = " DEFAULT "
if a.stringyType(column.Type) && column.Default != "''" {
end += "'" + column.Default + "'"
if a.stringyType(col.Type) && col.Default != "''" {
end += "'" + col.Default + "'"
} else {
end += column.Default
end += col.Default
}
}
if !column.Null {
if !col.Null {
end += " not null"
}
q += "\n\t`" + column.Name + "` " + column.Type + size + end + ","
q += "\n\t`" + col.Name + "` " + col.Type + size + end + ","
}
if len(keys) > 0 {
@ -169,6 +169,18 @@ func (a *PgsqlAdapter) AddKey(name, table, column string, key DBTableKey) (strin
return "", errors.New("not implemented")
}
// TODO: Implement this
// TODO: Test to make sure everything works here
func (a *PgsqlAdapter) RemoveIndex(name, table, iname string) (string, error) {
if table == "" {
return "", errors.New("You need a name for this table")
}
if iname == "" {
return "", errors.New("You need a name for the index")
}
return "", errors.New("not implemented")
}
// TODO: Implement this
// TODO: Test to make sure everything works here
func (a *PgsqlAdapter) AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error) {

View File

@ -141,6 +141,7 @@ type Adapter interface {
SetDefaultColumn(name, table, colName, colType, defaultStr string) (string, error)
AddIndex(name, table, iname, colname string) (string, error)
AddKey(name, table, column string, key DBTableKey) (string, error)
RemoveIndex(name, table, column string) (string, error)
AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error)
SimpleInsert(name, table, columns, fields string) (string, error)
SimpleUpdate(b *updatePrebuilder) (string, error)

View File

@ -6,6 +6,9 @@ ALTER TABLE `emails` ADD INDEX `i_uid` (`uid`);;
ALTER TABLE `attachments` ADD INDEX `i_originID` (`originID`);;
ALTER TABLE `attachments` ADD INDEX `i_path` (`path`);;
ALTER TABLE `activity_stream_matches` ADD INDEX `i_watcher` (`watcher`);;
ALTER TABLE `topics` ADD FULLTEXT(`title`);
ALTER TABLE `topics` ADD FULLTEXT(`content`);
ALTER TABLE `replies` ADD FULLTEXT(`content`);
INSERT INTO `sync`(`last_update`) VALUES (UTC_TIMESTAMP());
INSERT INTO `settings`(`name`,`content`,`type`,`constraints`) VALUES ('activation_type','1','list','1-3');
INSERT INTO `settings`(`name`,`content`,`type`) VALUES ('bigpost_min_words','250','int');