add RemoveIndex adapter method
add AddKey installer method
This commit is contained in:
parent
79546715a1
commit
3d1fd807d3
|
@ -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))
|
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) {
|
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))
|
return b.prepare(b.adapter.AddForeignKey("", table, column, ftable, fcolumn, cascade))
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,81 +30,98 @@ type installer struct {
|
||||||
plugins []QueryPlugin
|
plugins []QueryPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ins *installer) SetAdapter(name string) error {
|
func (i *installer) SetAdapter(name string) error {
|
||||||
adap, err := GetAdapter(name)
|
adap, err := GetAdapter(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ins.SetAdapterInstance(adap)
|
i.SetAdapterInstance(adap)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ins *installer) SetAdapterInstance(adapter Adapter) {
|
func (i *installer) SetAdapterInstance(adap Adapter) {
|
||||||
ins.adapter = adapter
|
i.adapter = adap
|
||||||
ins.instructions = []DBInstallInstruction{}
|
i.instructions = []DBInstallInstruction{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ins *installer) AddPlugins(plugins ...QueryPlugin) {
|
func (i *installer) AddPlugins(plugins ...QueryPlugin) {
|
||||||
ins.plugins = append(ins.plugins, plugins...)
|
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}
|
tableStruct := &DBInstallTable{table, charset, collation, columns, keys}
|
||||||
err := ins.RunHook("CreateTableStart", tableStruct)
|
err := i.RunHook("CreateTableStart", tableStruct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ins.RunHook("CreateTableAfter", tableStruct)
|
err = i.RunHook("CreateTableAfter", tableStruct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ins.instructions = append(ins.instructions, DBInstallInstruction{table, res, "create-table"})
|
i.instructions = append(i.instructions, DBInstallInstruction{table, res, "create-table"})
|
||||||
ins.tables = append(ins.tables, tableStruct)
|
i.tables = append(i.tables, tableStruct)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Let plugins manipulate the parameters like in CreateTable
|
// TODO: Let plugins manipulate the parameters like in CreateTable
|
||||||
func (ins *installer) AddIndex(table string, iname string, colname string) error {
|
func (i *installer) AddIndex(table, iname, colname string) error {
|
||||||
err := ins.RunHook("AddIndexStart", table, iname, colname)
|
err := i.RunHook("AddIndexStart", table, iname, colname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
res, err := ins.adapter.AddIndex("", table, iname, colname)
|
res, err := i.adapter.AddIndex("", table, iname, colname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ins.RunHook("AddIndexAfter", table, iname, colname)
|
err = i.RunHook("AddIndexAfter", table, iname, colname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Let plugins manipulate the parameters like in CreateTable
|
// TODO: Let plugins manipulate the parameters like in CreateTable
|
||||||
func (ins *installer) SimpleInsert(table string, columns string, fields string) error {
|
func (i *installer) SimpleInsert(table, columns, fields string) error {
|
||||||
err := ins.RunHook("SimpleInsertStart", table, columns, fields)
|
err := i.RunHook("SimpleInsertStart", table, columns, fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
res, err := ins.adapter.SimpleInsert("", table, columns, fields)
|
res, err := i.adapter.SimpleInsert("", table, columns, fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ins.RunHook("SimpleInsertAfter", table, columns, fields, res)
|
err = i.RunHook("SimpleInsertAfter", table, columns, fields, res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ins.instructions = append(ins.instructions, DBInstallInstruction{table, res, "insert"})
|
i.instructions = append(i.instructions, DBInstallInstruction{table, res, "insert"})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ins *installer) RunHook(name string, args ...interface{}) error {
|
func (i *installer) RunHook(name string, args ...interface{}) error {
|
||||||
for _, plugin := range ins.plugins {
|
for _, plugin := range i.plugins {
|
||||||
err := plugin.Hook(name, args...)
|
err := plugin.Hook(name, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -113,12 +130,12 @@ func (ins *installer) RunHook(name string, args ...interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ins *installer) Write() error {
|
func (i *installer) Write() error {
|
||||||
var inserts string
|
var inserts string
|
||||||
// We can't escape backticks, so we have to dump it out a file at a time
|
// 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" {
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, plugin := range ins.plugins {
|
for _, plugin := range i.plugins {
|
||||||
err := plugin.Write()
|
err := plugin.Write()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -188,7 +188,7 @@ func (a *MssqlAdapter) AddIndex(name, table, iname, colname string) (string, err
|
||||||
|
|
||||||
// TODO: Implement this
|
// TODO: Implement this
|
||||||
// TODO: Test to make sure everything works here
|
// 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 == "" {
|
if table == "" {
|
||||||
return "", errors.New("You need a name for this 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: Implement this
|
||||||
// TODO: Test to make sure everything works here
|
// 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) {
|
c := func(str string, val bool) {
|
||||||
if e != nil || !val {
|
if e != nil || !val {
|
||||||
return
|
return
|
||||||
|
|
|
@ -283,6 +283,20 @@ func (a *MysqlAdapter) AddKey(name, table, column string, key DBTableKey) (strin
|
||||||
return q, nil
|
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) {
|
func (a *MysqlAdapter) AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error) {
|
||||||
c := func(str string, val bool) {
|
c := func(str string, val bool) {
|
||||||
if e != nil || !val {
|
if e != nil || !val {
|
||||||
|
@ -587,9 +601,12 @@ func (a *MysqlAdapter) buildFlexiWhere(where string, dateCutoff *dateCutoff) (q
|
||||||
}
|
}
|
||||||
q = " WHERE"
|
q = " WHERE"
|
||||||
if dateCutoff != nil {
|
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"
|
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"
|
q += " " + dateCutoff.Column + " < UTC_TIMESTAMP() - interval " + strconv.Itoa(dateCutoff.Quantity) + " " + dateCutoff.Unit + " AND"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,43 +53,43 @@ func (a *PgsqlAdapter) DropTable(name, table string) (string, error) {
|
||||||
|
|
||||||
// TODO: Implement this
|
// TODO: Implement this
|
||||||
// We may need to change the CreateTable API to better suit PGSQL and the other database drivers which are coming up
|
// 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 == "" {
|
if table == "" {
|
||||||
return "", errors.New("You need a name for this 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")
|
return "", errors.New("You can't have a table with no columns")
|
||||||
}
|
}
|
||||||
|
|
||||||
q := "CREATE TABLE \"" + table + "\" ("
|
q := "CREATE TABLE \"" + table + "\" ("
|
||||||
for _, column := range columns {
|
for _, col := range cols {
|
||||||
if column.AutoIncrement {
|
if col.AutoIncrement {
|
||||||
column.Type = "serial"
|
col.Type = "serial"
|
||||||
} else if column.Type == "createdAt" {
|
} else if col.Type == "createdAt" {
|
||||||
column.Type = "timestamp"
|
col.Type = "timestamp"
|
||||||
} else if column.Type == "datetime" {
|
} else if col.Type == "datetime" {
|
||||||
column.Type = "timestamp"
|
col.Type = "timestamp"
|
||||||
}
|
}
|
||||||
|
|
||||||
var size string
|
var size string
|
||||||
if column.Size > 0 {
|
if col.Size > 0 {
|
||||||
size = " (" + strconv.Itoa(column.Size) + ")"
|
size = " (" + strconv.Itoa(col.Size) + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
var end string
|
var end string
|
||||||
if column.Default != "" {
|
if col.Default != "" {
|
||||||
end = " DEFAULT "
|
end = " DEFAULT "
|
||||||
if a.stringyType(column.Type) && column.Default != "''" {
|
if a.stringyType(col.Type) && col.Default != "''" {
|
||||||
end += "'" + column.Default + "'"
|
end += "'" + col.Default + "'"
|
||||||
} else {
|
} else {
|
||||||
end += column.Default
|
end += col.Default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !column.Null {
|
if !col.Null {
|
||||||
end += " not 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 {
|
if len(keys) > 0 {
|
||||||
|
@ -169,6 +169,18 @@ func (a *PgsqlAdapter) AddKey(name, table, column string, key DBTableKey) (strin
|
||||||
return "", errors.New("not implemented")
|
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: Implement this
|
||||||
// TODO: Test to make sure everything works here
|
// TODO: Test to make sure everything works here
|
||||||
func (a *PgsqlAdapter) AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error) {
|
func (a *PgsqlAdapter) AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error) {
|
||||||
|
|
|
@ -141,6 +141,7 @@ type Adapter interface {
|
||||||
SetDefaultColumn(name, table, colName, colType, defaultStr string) (string, error)
|
SetDefaultColumn(name, table, colName, colType, defaultStr string) (string, error)
|
||||||
AddIndex(name, table, iname, colname string) (string, error)
|
AddIndex(name, table, iname, colname string) (string, error)
|
||||||
AddKey(name, table, column string, key DBTableKey) (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)
|
AddForeignKey(name, table, column, ftable, fcolumn string, cascade bool) (out string, e error)
|
||||||
SimpleInsert(name, table, columns, fields string) (string, error)
|
SimpleInsert(name, table, columns, fields string) (string, error)
|
||||||
SimpleUpdate(b *updatePrebuilder) (string, error)
|
SimpleUpdate(b *updatePrebuilder) (string, error)
|
||||||
|
|
|
@ -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_originID` (`originID`);;
|
||||||
ALTER TABLE `attachments` ADD INDEX `i_path` (`path`);;
|
ALTER TABLE `attachments` ADD INDEX `i_path` (`path`);;
|
||||||
ALTER TABLE `activity_stream_matches` ADD INDEX `i_watcher` (`watcher`);;
|
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 `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`,`constraints`) VALUES ('activation_type','1','list','1-3');
|
||||||
INSERT INTO `settings`(`name`,`content`,`type`) VALUES ('bigpost_min_words','250','int');
|
INSERT INTO `settings`(`name`,`content`,`type`) VALUES ('bigpost_min_words','250','int');
|
||||||
|
|
Loading…
Reference in New Issue