2017-10-16 07:32:58 +00:00
|
|
|
/* WIP Under Really Heavy Construction */
|
|
|
|
package qgen
|
|
|
|
|
|
|
|
import (
|
2018-03-31 05:25:27 +00:00
|
|
|
"database/sql"
|
2017-10-16 07:32:58 +00:00
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-11-12 05:25:04 +00:00
|
|
|
Registry = append(Registry,
|
|
|
|
&MssqlAdapter{Name: "mssql", Buffer: make(map[string]DBStmt)},
|
2017-10-16 07:32:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
type MssqlAdapter struct {
|
2017-10-16 07:32:58 +00:00
|
|
|
Name string // ? - Do we really need this? Can't we hard-code this?
|
2017-11-12 05:25:04 +00:00
|
|
|
Buffer map[string]DBStmt
|
2017-10-16 07:32:58 +00:00
|
|
|
BufferOrder []string // Map iteration order is random, so we need this to track the order, so we don't get huge diffs every commit
|
|
|
|
keys map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName gives you the name of the database adapter. In this case, it's Mssql
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) GetName() string {
|
|
|
|
return a.Name
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) GetStmt(name string) DBStmt {
|
|
|
|
return a.Buffer[name]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) GetStmts() map[string]DBStmt {
|
|
|
|
return a.Buffer
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 05:25:27 +00:00
|
|
|
// TODO: Implement this
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) {
|
2018-03-31 05:25:27 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) DbVersion() string {
|
2018-03-31 05:25:27 +00:00
|
|
|
return "SELECT CONCAT(SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition'))"
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
func (a *MssqlAdapter) DropTable(name, table string) (string, error) {
|
2018-05-11 05:41:51 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "DROP TABLE IF EXISTS [" + table + "];"
|
|
|
|
a.pushStatement(name, "drop-table", q)
|
|
|
|
return q, nil
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 04:04:00 +00:00
|
|
|
// TODO: Add support for foreign keys?
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Convert any remaining stringy types to nvarchar
|
|
|
|
// We may need to change the CreateTable API to better suit Mssql and the other database drivers which are coming up
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (a *MssqlAdapter) CreateTable(name, table, charset, collation string, columns []DBTableColumn, keys []DBTableKey) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
|
|
|
return "", errors.New("You can't have a table with no columns")
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "CREATE TABLE [" + table + "] ("
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range columns {
|
2019-10-31 07:25:56 +00:00
|
|
|
column, size, end := a.parseColumn(column)
|
|
|
|
q += "\n\t[" + column.Name + "] " + column.Type + size + end + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) > 0 {
|
|
|
|
for _, key := range keys {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "\n\t" + key.Type
|
2017-10-16 07:32:58 +00:00
|
|
|
if key.Type != "unique" {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " key"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "("
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range strings.Split(key.Columns, ",") {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "[" + column + "],"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + "),"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + "\n);"
|
|
|
|
a.pushStatement(name, "create-table", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) parseColumn(column DBTableColumn) (col DBTableColumn, size string, end string) {
|
2018-05-27 09:36:35 +00:00
|
|
|
var max, createdAt bool
|
|
|
|
switch column.Type {
|
|
|
|
case "createdAt":
|
|
|
|
column.Type = "datetime"
|
|
|
|
createdAt = true
|
|
|
|
case "varchar":
|
|
|
|
column.Type = "nvarchar"
|
|
|
|
case "text":
|
|
|
|
column.Type = "nvarchar"
|
|
|
|
max = true
|
|
|
|
case "json":
|
|
|
|
column.Type = "nvarchar"
|
|
|
|
max = true
|
|
|
|
case "boolean":
|
|
|
|
column.Type = "bit"
|
|
|
|
}
|
|
|
|
if column.Size > 0 {
|
|
|
|
size = " (" + strconv.Itoa(column.Size) + ")"
|
|
|
|
}
|
|
|
|
if max {
|
|
|
|
size = " (MAX)"
|
|
|
|
}
|
|
|
|
|
|
|
|
if column.Default != "" {
|
|
|
|
end = " DEFAULT "
|
|
|
|
if createdAt {
|
|
|
|
end += "GETUTCDATE()" // TODO: Use GETUTCDATE() in updates instead of the neutral format
|
2019-10-31 07:25:56 +00:00
|
|
|
} else if a.stringyType(column.Type) && column.Default != "''" {
|
2018-05-27 09:36:35 +00:00
|
|
|
end += "'" + column.Default + "'"
|
|
|
|
} else {
|
|
|
|
end += column.Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !column.Null {
|
|
|
|
end += " not null"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Not exactly the meaning of auto increment...
|
|
|
|
if column.AutoIncrement {
|
|
|
|
end += " IDENTITY"
|
|
|
|
}
|
|
|
|
return column, size, end
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Test this, not sure if some things work
|
2019-01-21 12:27:59 +00:00
|
|
|
// TODO: Add support for keys
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (a *MssqlAdapter) AddColumn(name, table string, column DBTableColumn, key *DBTableKey) (string, error) {
|
2018-05-27 09:36:35 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
column, size, end := a.parseColumn(column)
|
|
|
|
q := "ALTER TABLE [" + table + "] ADD [" + column.Name + "] " + column.Type + size + end + ";"
|
|
|
|
a.pushStatement(name, "add-column", q)
|
|
|
|
return q, nil
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
func (a *MssqlAdapter) DropColumn(name, table, colName string) (string, error) {
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement this
|
|
|
|
func (a *MssqlAdapter) RenameColumn(name, table, oldName, newName string) (string, error) {
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement this
|
|
|
|
func (a *MssqlAdapter) ChangeColumn(name, table, colName string, col DBTableColumn) (string, error) {
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement this
|
|
|
|
func (a *MssqlAdapter) SetDefaultColumn(name, table, colName, colType, defaultStr string) (string, error) {
|
|
|
|
if colType == "text" {
|
|
|
|
return "", errors.New("text fields cannot have default values")
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2018-12-31 09:03:49 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
// TODO: Test to make sure everything works here
|
2019-12-31 21:57:54 +00:00
|
|
|
func (a *MssqlAdapter) AddIndex(name, table, iname, colname string) (string, error) {
|
2018-12-31 09:03:49 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if iname == "" {
|
|
|
|
return "", errors.New("You need a name for the index")
|
|
|
|
}
|
|
|
|
if colname == "" {
|
|
|
|
return "", errors.New("You need a name for the column")
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:29:19 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
// TODO: Test to make sure everything works here
|
2020-02-20 23:55:45 +00:00
|
|
|
func (a *MssqlAdapter) AddKey(name, table, column string, key DBTableKey) (string, error) {
|
2019-02-23 06:29:19 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if column == "" {
|
|
|
|
return "", errors.New("You need a name for the column")
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-05-06 04:04:00 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
// TODO: Test to make sure everything works here
|
2020-02-20 23:55:45 +00:00
|
|
|
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) {
|
2019-10-31 07:25:56 +00:00
|
|
|
c := func(str string, val bool) {
|
2019-05-06 04:04:00 +00:00
|
|
|
if e != nil || !val {
|
|
|
|
return
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
e = errors.New("You need a " + str + " for this table")
|
2019-05-06 04:04:00 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
c("name", table == "")
|
|
|
|
c("column", column == "")
|
|
|
|
c("ftable", ftable == "")
|
|
|
|
c("fcolumn", fcolumn == "")
|
2019-05-06 04:04:00 +00:00
|
|
|
if e != nil {
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
func (a *MssqlAdapter) SimpleInsert(name, table, cols, fields string) (string, error) {
|
2020-02-24 11:15:17 +00:00
|
|
|
q, err := a.simpleBulkInsert(name, table, cols, []string{fields})
|
|
|
|
a.pushStatement(name, "insert", q)
|
|
|
|
return q, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *MssqlAdapter) SimpleBulkInsert(name, table, cols string, fieldSet []string) (string, error) {
|
|
|
|
q, err := a.simpleBulkInsert(name, table, cols, fieldSet)
|
|
|
|
a.pushStatement(name, "bulk-insert", q)
|
|
|
|
return q, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *MssqlAdapter) simpleBulkInsert(name, table, cols string, fieldSet []string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "INSERT INTO [" + table + "] ("
|
2019-12-31 21:57:54 +00:00
|
|
|
if cols == "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += ") VALUES ()"
|
|
|
|
a.pushStatement(name, "insert", q)
|
|
|
|
return q, nil
|
2018-04-22 12:33:56 +00:00
|
|
|
}
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(cols) {
|
|
|
|
if col.Type == TokenFunc {
|
|
|
|
q += col.Left + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
q += "[" + col.Left + "],"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q += ") VALUES ("
|
2020-02-24 11:15:17 +00:00
|
|
|
for oi, fields := range fieldSet {
|
|
|
|
if oi != 0 {
|
|
|
|
q += ",("
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2020-02-24 11:15:17 +00:00
|
|
|
for _, field := range processFields(fields) {
|
|
|
|
field.Name = strings.Replace(field.Name, "UTC_TIMESTAMP()", "GETUTCDATE()", -1)
|
|
|
|
//log.Print("field.Name ", field.Name)
|
|
|
|
nameLen := len(field.Name)
|
|
|
|
if field.Name[0] == '"' && field.Name[nameLen-1] == '"' && nameLen >= 3 {
|
|
|
|
field.Name = "'" + field.Name[1:nameLen-1] + "'"
|
|
|
|
}
|
|
|
|
if field.Name[0] == '\'' && field.Name[nameLen-1] == '\'' && nameLen >= 3 {
|
|
|
|
field.Name = "'" + strings.Replace(field.Name[1:nameLen-1], "'", "''", -1) + "'"
|
|
|
|
}
|
|
|
|
q += field.Name + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2020-02-24 11:15:17 +00:00
|
|
|
q = q[0:len(q)-1] + ")"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ! DEPRECATED
|
2020-01-02 05:28:36 +00:00
|
|
|
func (a *MssqlAdapter) SimpleReplace(name, table, columns, fields string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("In SimpleReplace")
|
2019-10-31 07:25:56 +00:00
|
|
|
key, ok := a.keys[table]
|
2017-10-16 07:32:58 +00:00
|
|
|
if !ok {
|
|
|
|
return "", errors.New("Unable to elide key from table '" + table + "', please use SimpleUpsert (coming soon!) instead")
|
|
|
|
}
|
|
|
|
log.Print("After the key check")
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
var keyPosition int
|
|
|
|
for _, column := range processColumns(columns) {
|
|
|
|
if column.Left == key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keyPosition++
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyValue string
|
|
|
|
for fieldID, field := range processFields(fields) {
|
|
|
|
field.Name = strings.Replace(field.Name, "UTC_TIMESTAMP()", "GETUTCDATE()", -1)
|
|
|
|
nameLen := len(field.Name)
|
|
|
|
if field.Name[0] == '"' && field.Name[nameLen-1] == '"' && nameLen >= 3 {
|
|
|
|
field.Name = "'" + field.Name[1:nameLen-1] + "'"
|
|
|
|
}
|
|
|
|
if field.Name[0] == '\'' && field.Name[nameLen-1] == '\'' && nameLen >= 3 {
|
|
|
|
field.Name = "'" + strings.Replace(field.Name[1:nameLen-1], "'", "''", -1) + "'"
|
|
|
|
}
|
|
|
|
if keyPosition == fieldID {
|
|
|
|
keyValue = field.Name
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
return a.SimpleUpsert(name, table, columns, fields, "key = "+keyValue)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 05:28:36 +00:00
|
|
|
func (a *MssqlAdapter) SimpleUpsert(name, table, columns, fields, where string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
|
|
|
return "", errors.New("No columns found for SimpleInsert")
|
|
|
|
}
|
|
|
|
if len(fields) == 0 {
|
|
|
|
return "", errors.New("No input data found for SimpleInsert")
|
|
|
|
}
|
|
|
|
|
|
|
|
var fieldCount int
|
|
|
|
var fieldOutput string
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "MERGE [" + table + "] WITH(HOLDLOCK) as t1 USING (VALUES("
|
|
|
|
parsedFields := processFields(fields)
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, field := range parsedFields {
|
|
|
|
fieldCount++
|
|
|
|
field.Name = strings.Replace(field.Name, "UTC_TIMESTAMP()", "GETUTCDATE()", -1)
|
|
|
|
//log.Print("field.Name ", field.Name)
|
|
|
|
nameLen := len(field.Name)
|
|
|
|
if field.Name[0] == '"' && field.Name[nameLen-1] == '"' && nameLen >= 3 {
|
|
|
|
field.Name = "'" + field.Name[1:nameLen-1] + "'"
|
|
|
|
}
|
|
|
|
if field.Name[0] == '\'' && field.Name[nameLen-1] == '\'' && nameLen >= 3 {
|
|
|
|
field.Name = "'" + strings.Replace(field.Name[1:nameLen-1], "'", "''", -1) + "'"
|
|
|
|
}
|
|
|
|
fieldOutput += field.Name + ","
|
|
|
|
}
|
|
|
|
fieldOutput = fieldOutput[0 : len(fieldOutput)-1]
|
2019-10-31 07:25:56 +00:00
|
|
|
q += fieldOutput + ")) AS updates ("
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// nolint The linter wants this to be less readable
|
|
|
|
for fieldID, _ := range parsedFields {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "f" + strconv.Itoa(fieldID) + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + ") ON "
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
//querystr += "t1.[" + key + "] = "
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ?"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
matched := " WHEN MATCHED THEN UPDATE SET "
|
|
|
|
notMatched := "WHEN NOT MATCHED THEN INSERT("
|
2017-10-16 07:32:58 +00:00
|
|
|
var fieldList string
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
for columnID, col := range processColumns(columns) {
|
2017-10-16 07:32:58 +00:00
|
|
|
fieldList += "f" + strconv.Itoa(columnID) + ","
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Type == TokenFunc {
|
|
|
|
matched += col.Left + " = f" + strconv.Itoa(columnID) + ","
|
|
|
|
notMatched += col.Left + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
matched += "[" + col.Left + "] = f" + strconv.Itoa(columnID) + ","
|
|
|
|
notMatched += "[" + col.Left + "],"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matched = matched[0 : len(matched)-1]
|
|
|
|
notMatched = notMatched[0 : len(notMatched)-1]
|
|
|
|
fieldList = fieldList[0 : len(fieldList)-1]
|
|
|
|
|
|
|
|
notMatched += ") VALUES (" + fieldList + ");"
|
2019-10-31 07:25:56 +00:00
|
|
|
q += matched + " " + notMatched
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' {
|
2019-10-31 07:25:56 +00:00
|
|
|
log.Print(name+" query: ", q)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(name, "upsert", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error) {
|
2018-12-27 05:42:41 +00:00
|
|
|
if up.table == "" {
|
2017-10-16 07:32:58 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
2018-12-27 05:42:41 +00:00
|
|
|
if up.set == "" {
|
2017-10-16 07:32:58 +00:00
|
|
|
return "", errors.New("You need to set data in this update statement")
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "UPDATE [" + up.table + "] SET "
|
2018-12-27 05:42:41 +00:00
|
|
|
for _, item := range processSet(up.set) {
|
2019-12-31 21:57:54 +00:00
|
|
|
q += "[" + item.Column + "]="
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, token := range item.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ?"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
2018-12-27 05:42:41 +00:00
|
|
|
if len(up.where) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2018-12-27 05:42:41 +00:00
|
|
|
for _, loc := range processWhere(up.where) {
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenSub, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(up.name, "update", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleUpdateSelect(b *updatePrebuilder) (string, error) {
|
2018-12-27 05:42:41 +00:00
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleDelete(name string, table string, where string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if where == "" {
|
|
|
|
return "", errors.New("You need to specify what data you want to delete")
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "DELETE FROM [" + table + "] WHERE"
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ?"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = strings.TrimSpace(q[0 : len(q)-4])
|
|
|
|
a.pushStatement(name, "delete", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) ComplexDelete(b *deletePrebuilder) (string, error) {
|
2019-06-05 04:57:10 +00:00
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
// We don't want to accidentally wipe tables, so we'll have a separate method for purging tables instead
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) Purge(name string, table string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "DELETE FROM [" + table + "]"
|
|
|
|
a.pushStatement(name, "purge", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleSelect(name string, table string, columns string, where string, orderby string, limit string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
|
|
|
return "", errors.New("No columns found for SimpleSelect")
|
|
|
|
}
|
|
|
|
// TODO: Add this to the MySQL adapter in order to make this problem more discoverable?
|
|
|
|
if len(orderby) == 0 && limit != "" {
|
|
|
|
return "", errors.New("Orderby needs to be set to use limit on Mssql")
|
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount := 0
|
2019-10-31 07:25:56 +00:00
|
|
|
q := ""
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-10-31 07:25:56 +00:00
|
|
|
colslice := strings.Split(strings.TrimSpace(columns), ",")
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range colslice {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "[" + strings.TrimSpace(column) + "],"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + " FROM [" + table + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
|
|
|
subCount++
|
|
|
|
q += " ?" + strconv.Itoa(subCount)
|
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
2017-10-21 00:27:47 +00:00
|
|
|
// MSSQL seems to convert the formats? so we'll compare it with a regular date. Do this with the other methods too?
|
2017-10-16 07:32:58 +00:00
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
2017-10-21 00:27:47 +00:00
|
|
|
token.Contents = "GETDATE()"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(orderby) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY "
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range processOrderby(orderby) {
|
|
|
|
// TODO: We might want to escape this column
|
2019-10-31 07:25:56 +00:00
|
|
|
q += column.Column + " " + strings.ToUpper(column.Order) + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if limit != "" {
|
|
|
|
limiter := processLimit(limit)
|
|
|
|
log.Printf("limiter: %+v\n", limiter)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
q += " OFFSET ?" + strconv.Itoa(subCount) + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " OFFSET " + limiter.Offset + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(subCount)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = strings.TrimSpace("SELECT " + q)
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && limit == "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
log.Print(name+" query: ", q)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(name, "select", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
// TODO: ComplexSelect
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) ComplexSelect(preBuilder *selectPrebuilder) (string, error) {
|
2018-01-03 07:46:18 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleLeftJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table1 == "" {
|
|
|
|
return "", errors.New("You need a name for the left table")
|
|
|
|
}
|
|
|
|
if table2 == "" {
|
|
|
|
return "", errors.New("You need a name for the right table")
|
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
|
|
|
return "", errors.New("No columns found for SimpleLeftJoin")
|
|
|
|
}
|
|
|
|
if len(joiners) == 0 {
|
|
|
|
return "", errors.New("No joiners found for SimpleLeftJoin")
|
|
|
|
}
|
|
|
|
// TODO: Add this to the MySQL adapter in order to make this problem more discoverable?
|
|
|
|
if len(orderby) == 0 && limit != "" {
|
|
|
|
return "", errors.New("Orderby needs to be set to use limit on Mssql")
|
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount := 0
|
2019-10-31 07:25:56 +00:00
|
|
|
q := ""
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(columns) {
|
2017-10-16 07:32:58 +00:00
|
|
|
var source, alias string
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Table != "" {
|
|
|
|
source = "[" + col.Table + "].[" + col.Left + "]"
|
|
|
|
} else if col.Type == TokenFunc {
|
|
|
|
source = col.Left
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
source = "[" + col.Left + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Alias != "" {
|
|
|
|
alias = " AS '" + col.Alias + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += source + alias + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FROM [" + table1 + "] LEFT JOIN [" + table2 + "] ON "
|
|
|
|
for _, j := range processJoiner(joiners) {
|
|
|
|
q += "[" + j.LeftTable + "].[" + j.LeftColumn + "]" + j.Operator + "[" + j.RightTable + "].[" + j.RightColumn + "] AND "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
// Remove the trailing AND
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
|
|
|
subCount++
|
|
|
|
q += " ?" + strconv.Itoa(subCount)
|
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2017-10-16 07:32:58 +00:00
|
|
|
halves := strings.Split(token.Contents, ".")
|
|
|
|
if len(halves) == 2 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + halves[0] + "].[" + halves[1] + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(orderby) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY "
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range processOrderby(orderby) {
|
|
|
|
log.Print("column: ", column)
|
|
|
|
// TODO: We might want to escape this column
|
2019-10-31 07:25:56 +00:00
|
|
|
q += column.Column + " " + strings.ToUpper(column.Order) + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
} else if limit != "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
key, ok := a.keys[table1]
|
2017-10-16 07:32:58 +00:00
|
|
|
if ok {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY [" + table1 + "].[" + key + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != "" {
|
|
|
|
limiter := processLimit(limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
q += " OFFSET ?" + strconv.Itoa(subCount) + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " OFFSET " + limiter.Offset + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(subCount)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = strings.TrimSpace("SELECT " + q)
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && limit == "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
log.Print(name+" query: ", q)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(name, "select", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleInnerJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table1 == "" {
|
|
|
|
return "", errors.New("You need a name for the left table")
|
|
|
|
}
|
|
|
|
if table2 == "" {
|
|
|
|
return "", errors.New("You need a name for the right table")
|
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
|
|
|
return "", errors.New("No columns found for SimpleInnerJoin")
|
|
|
|
}
|
|
|
|
if len(joiners) == 0 {
|
|
|
|
return "", errors.New("No joiners found for SimpleInnerJoin")
|
|
|
|
}
|
|
|
|
// TODO: Add this to the MySQL adapter in order to make this problem more discoverable?
|
|
|
|
if len(orderby) == 0 && limit != "" {
|
|
|
|
return "", errors.New("Orderby needs to be set to use limit on Mssql")
|
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount := 0
|
2019-10-31 07:25:56 +00:00
|
|
|
q := ""
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(columns) {
|
2017-10-16 07:32:58 +00:00
|
|
|
var source, alias string
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Table != "" {
|
|
|
|
source = "[" + col.Table + "].[" + col.Left + "]"
|
|
|
|
} else if col.Type == TokenFunc {
|
|
|
|
source = col.Left
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
source = "[" + col.Left + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Alias != "" {
|
|
|
|
alias = " AS '" + col.Alias + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += source + alias + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FROM [" + table1 + "] INNER JOIN [" + table2 + "] ON "
|
|
|
|
for _, j := range processJoiner(joiners) {
|
|
|
|
q += "[" + j.LeftTable + "].[" + j.LeftColumn + "]" + j.Operator + "[" + j.RightTable + "].[" + j.RightColumn + "] AND "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
// Remove the trailing AND
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
|
|
|
subCount++
|
|
|
|
q += " ?" + strconv.Itoa(subCount)
|
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2017-10-16 07:32:58 +00:00
|
|
|
halves := strings.Split(token.Contents, ".")
|
|
|
|
if len(halves) == 2 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + halves[0] + "].[" + halves[1] + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(orderby) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY "
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range processOrderby(orderby) {
|
|
|
|
log.Print("column: ", column)
|
|
|
|
// TODO: We might want to escape this column
|
2019-10-31 07:25:56 +00:00
|
|
|
q += column.Column + " " + strings.ToUpper(column.Order) + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
} else if limit != "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
key, ok := a.keys[table1]
|
2017-10-16 07:32:58 +00:00
|
|
|
if ok {
|
|
|
|
log.Print("key: ", key)
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY [" + table1 + "].[" + key + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != "" {
|
|
|
|
limiter := processLimit(limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
q += " OFFSET ?" + strconv.Itoa(subCount) + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " OFFSET " + limiter.Offset + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(subCount)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = strings.TrimSpace("SELECT " + q)
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && limit == "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
log.Print(name+" query: ", q)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(name, "select", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleInsertSelect(name string, ins DBInsert, sel DBSelect) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: More errors.
|
|
|
|
// TODO: Add this to the MySQL adapter in order to make this problem more discoverable?
|
|
|
|
if len(sel.Orderby) == 0 && sel.Limit != "" {
|
|
|
|
return "", errors.New("Orderby needs to be set to use limit on Mssql")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert */
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "INSERT INTO [" + ins.Table + "] ("
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(ins.Columns) {
|
|
|
|
if col.Type == TokenFunc {
|
|
|
|
q += col.Left + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
q += "[" + col.Left + "],"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + ") SELECT "
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
/* Select */
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount := 0
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(sel.Columns) {
|
2017-10-21 00:27:47 +00:00
|
|
|
var source, alias string
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Type == TokenFunc || col.Type == TokenSub {
|
|
|
|
source = col.Left
|
2017-10-21 00:27:47 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
source = "[" + col.Left + "]"
|
2017-10-21 00:27:47 +00:00
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Alias != "" {
|
|
|
|
alias = " AS [" + col.Alias + "]"
|
2017-10-21 00:27:47 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + source + alias + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + " FROM [" + sel.Table + "] "
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(sel.Where) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, loc := range processWhere(sel.Where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
|
|
|
subCount++
|
|
|
|
q += " ?" + strconv.Itoa(subCount)
|
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(sel.Orderby) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY "
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range processOrderby(sel.Orderby) {
|
|
|
|
// TODO: We might want to escape this column
|
2019-10-31 07:25:56 +00:00
|
|
|
q += column.Column + " " + strings.ToUpper(column.Order) + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
} else if sel.Limit != "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
key, ok := a.keys[sel.Table]
|
2017-10-16 07:32:58 +00:00
|
|
|
if ok {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY [" + sel.Table + "].[" + key + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sel.Limit != "" {
|
|
|
|
limiter := processLimit(sel.Limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
q += " OFFSET ?" + strconv.Itoa(subCount) + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " OFFSET " + limiter.Offset + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(subCount)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = strings.TrimSpace(q)
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && sel.Limit == "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
log.Print(name+" query: ", q)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(name, "insert", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) simpleJoin(name string, ins DBInsert, sel DBJoin, joinType string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: More errors.
|
|
|
|
// TODO: Add this to the MySQL adapter in order to make this problem more discoverable?
|
|
|
|
if len(sel.Orderby) == 0 && sel.Limit != "" {
|
|
|
|
return "", errors.New("Orderby needs to be set to use limit on Mssql")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert */
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "INSERT INTO [" + ins.Table + "] ("
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(ins.Columns) {
|
|
|
|
if col.Type == TokenFunc {
|
|
|
|
q += col.Left + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
q += "[" + col.Left + "],"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + ") SELECT "
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
/* Select */
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount := 0
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(sel.Columns) {
|
2017-10-16 07:32:58 +00:00
|
|
|
var source, alias string
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Table != "" {
|
|
|
|
source = "[" + col.Table + "].[" + col.Left + "]"
|
|
|
|
} else if col.Type == TokenFunc {
|
|
|
|
source = col.Left
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
source = "[" + col.Left + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
if col.Alias != "" {
|
|
|
|
alias = " AS '" + col.Alias + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += source + alias + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FROM [" + sel.Table1 + "] " + joinType + " JOIN [" + sel.Table2 + "] ON "
|
|
|
|
for _, j := range processJoiner(sel.Joiners) {
|
|
|
|
q += "[" + j.LeftTable + "].[" + j.LeftColumn + "] " + j.Operator + " [" + j.RightTable + "].[" + j.RightColumn + "] AND "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(sel.Where) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, loc := range processWhere(sel.Where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenSub:
|
|
|
|
subCount++
|
|
|
|
q += " ?" + strconv.Itoa(subCount)
|
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Split the function case off to speed things up
|
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2017-10-16 07:32:58 +00:00
|
|
|
halves := strings.Split(token.Contents, ".")
|
|
|
|
if len(halves) == 2 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + halves[0] + "].[" + halves[1] + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(sel.Orderby) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY "
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, column := range processOrderby(sel.Orderby) {
|
|
|
|
log.Print("column: ", column)
|
|
|
|
// TODO: We might want to escape this column
|
2019-10-31 07:25:56 +00:00
|
|
|
q += column.Column + " " + strings.ToUpper(column.Order) + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-10-16 07:32:58 +00:00
|
|
|
} else if sel.Limit != "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
key, ok := a.keys[sel.Table1]
|
2017-10-16 07:32:58 +00:00
|
|
|
if ok {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " ORDER BY [" + sel.Table1 + "].[" + key + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sel.Limit != "" {
|
|
|
|
limiter := processLimit(sel.Limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
q += " OFFSET ?" + strconv.Itoa(subCount) + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " OFFSET " + limiter.Offset + " ROWS"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
2019-12-31 21:57:54 +00:00
|
|
|
subCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(subCount)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = strings.TrimSpace(q)
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && sel.Limit == "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
log.Print(name+" query: ", q)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(name, "insert", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleInsertLeftJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
|
|
|
|
return a.simpleJoin(name, ins, sel, "LEFT")
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SimpleInsertInnerJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
|
|
|
|
return a.simpleJoin(name, ins, sel, "INNER")
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
func (a *MssqlAdapter) SimpleCount(name, table, where, limit string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "SELECT COUNT(*) FROM [" + table + "]"
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// TODO: Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2017-10-16 07:32:58 +00:00
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenFunc, TokenOp, TokenNumber, TokenSub, TokenOr, TokenNot, TokenLike:
|
2017-10-16 07:32:58 +00:00
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " [" + token.Contents + "]"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-10-16 07:32:58 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
if limit != "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " LIMIT " + limit
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = strings.TrimSpace(q)
|
|
|
|
a.pushStatement(name, "select", q)
|
|
|
|
return q, nil
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) Builder() *prebuilder {
|
|
|
|
return &prebuilder{a}
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) Write() error {
|
2017-10-16 07:32:58 +00:00
|
|
|
var stmts, body string
|
2019-10-31 07:25:56 +00:00
|
|
|
for _, name := range a.BufferOrder {
|
2018-12-27 05:42:41 +00:00
|
|
|
if name == "" {
|
2017-10-16 07:32:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
stmt := a.Buffer[name]
|
2017-10-16 07:32:58 +00:00
|
|
|
// TODO: Add support for create-table? Table creation might be a little complex for Go to do outside a SQL file :(
|
|
|
|
if stmt.Type != "create-table" {
|
2017-11-05 09:55:34 +00:00
|
|
|
stmts += "\t" + name + " *sql.Stmt\n"
|
2017-10-16 07:32:58 +00:00
|
|
|
body += `
|
2018-02-26 09:07:00 +00:00
|
|
|
common.DebugLog("Preparing ` + name + ` statement.")
|
2017-11-05 09:55:34 +00:00
|
|
|
stmts.` + name + `, err = db.Prepare("` + stmt.Contents + `")
|
2017-10-16 07:32:58 +00:00
|
|
|
if err != nil {
|
2018-02-26 09:07:00 +00:00
|
|
|
log.Print("Error in ` + name + ` statement.")
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("Bad Query: ","` + stmt.Contents + `")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 09:55:34 +00:00
|
|
|
// TODO: Move these custom queries out of this file
|
2017-10-16 07:32:58 +00:00
|
|
|
out := `// +build mssql
|
|
|
|
|
|
|
|
// This file was generated by Gosora's Query Generator. Please try to avoid modifying this file, as it might change at any time.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "database/sql"
|
2018-10-27 03:21:02 +00:00
|
|
|
import "github.com/Azareal/Gosora/common"
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// nolint
|
2017-11-05 09:55:34 +00:00
|
|
|
type Stmts struct {
|
2017-10-16 07:32:58 +00:00
|
|
|
` + stmts + `
|
2017-11-05 09:55:34 +00:00
|
|
|
getActivityFeedByWatcher *sql.Stmt
|
|
|
|
getActivityCountByWatcher *sql.Stmt
|
|
|
|
|
|
|
|
Mocks bool
|
|
|
|
}
|
|
|
|
|
2017-10-16 07:32:58 +00:00
|
|
|
// nolint
|
|
|
|
func _gen_mssql() (err error) {
|
2018-02-19 04:26:01 +00:00
|
|
|
common.DebugLog("Building the generated statements")
|
2017-10-16 07:32:58 +00:00
|
|
|
` + body + `
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
`
|
|
|
|
return writeFile("./gen_mssql.go", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal methods, not exposed in the interface
|
2020-01-02 05:28:36 +00:00
|
|
|
func (a *MssqlAdapter) pushStatement(name, stype, q string) {
|
2018-12-31 09:03:49 +00:00
|
|
|
if name == "" {
|
2018-02-10 15:07:21 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-02 05:28:36 +00:00
|
|
|
a.Buffer[name] = DBStmt{q, stype}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.BufferOrder = append(a.BufferOrder, name)
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 05:28:36 +00:00
|
|
|
func (a *MssqlAdapter) stringyType(ct string) bool {
|
|
|
|
ct = strings.ToLower(ct)
|
|
|
|
return ct == "char" || ct == "varchar" || ct == "datetime" || ct == "text" || ct == "nvarchar"
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SetPrimaryKeys interface {
|
|
|
|
SetPrimaryKeys(keys map[string]string)
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *MssqlAdapter) SetPrimaryKeys(keys map[string]string) {
|
|
|
|
a.keys = keys
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|