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
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) GetName() string {
|
2017-10-16 07:32:58 +00:00
|
|
|
return adapter.Name
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *MssqlAdapter) GetStmt(name string) DBStmt {
|
2017-10-16 07:32:58 +00:00
|
|
|
return adapter.Buffer[name]
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *MssqlAdapter) GetStmts() map[string]DBStmt {
|
2017-10-16 07:32:58 +00:00
|
|
|
return adapter.Buffer
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:25:27 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
func (adapter *MssqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *MssqlAdapter) DbVersion() string {
|
|
|
|
return "SELECT CONCAT(SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition'))"
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:41:51 +00:00
|
|
|
func (adapter *MssqlAdapter) DropTable(name string, table string) (string, error) {
|
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
querystr := "DROP TABLE IF EXISTS [" + table + "];"
|
|
|
|
adapter.pushStatement(name, "drop-table", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
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
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *MssqlAdapter) CreateTable(name string, table string, charset string, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var querystr = "CREATE TABLE [" + table + "] ("
|
|
|
|
for _, column := range columns {
|
2018-05-27 09:36:35 +00:00
|
|
|
column, size, end := adapter.parseColumn(column)
|
2017-10-16 07:32:58 +00:00
|
|
|
querystr += "\n\t[" + column.Name + "] " + column.Type + size + end + ","
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) > 0 {
|
|
|
|
for _, key := range keys {
|
|
|
|
querystr += "\n\t" + key.Type
|
|
|
|
if key.Type != "unique" {
|
|
|
|
querystr += " key"
|
|
|
|
}
|
|
|
|
querystr += "("
|
|
|
|
for _, column := range strings.Split(key.Columns, ",") {
|
|
|
|
querystr += "[" + column + "],"
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr)-1] + "),"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = querystr[0:len(querystr)-1] + "\n);"
|
|
|
|
adapter.pushStatement(name, "create-table", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2018-05-27 09:36:35 +00:00
|
|
|
func (adapter *MssqlAdapter) parseColumn(column DBTableColumn) (col DBTableColumn, size string, end string) {
|
|
|
|
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
|
|
|
|
} else if adapter.stringyType(column.Type) && column.Default != "''" {
|
|
|
|
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
|
2019-02-23 06:29:19 +00:00
|
|
|
func (adapter *MssqlAdapter) AddColumn(name string, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
column, size, end := adapter.parseColumn(column)
|
|
|
|
querystr := "ALTER TABLE [" + table + "] ADD [" + column.Name + "] " + column.Type + size + end + ";"
|
|
|
|
adapter.pushStatement(name, "add-column", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2018-12-31 09:03:49 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
// TODO: Test to make sure everything works here
|
|
|
|
func (adapter *MssqlAdapter) AddIndex(name string, table string, iname string, colname 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")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
func (adapter *MssqlAdapter) AddKey(name string, table string, column string, key DBTableKey) (string, error) {
|
|
|
|
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
|
|
|
|
func (adapter *MssqlAdapter) AddForeignKey(name string, table string, column string, ftable string, fcolumn string, cascade bool) (out string, e error) {
|
|
|
|
var c = func(str string, val bool) {
|
|
|
|
if e != nil || !val {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e = errors.New("You need a "+str+" for this table")
|
|
|
|
}
|
|
|
|
c("name",table=="")
|
|
|
|
c("column",column=="")
|
|
|
|
c("ftable",ftable=="")
|
|
|
|
c("fcolumn",fcolumn=="")
|
|
|
|
if e != nil {
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
|
2018-04-23 21:08:31 +00:00
|
|
|
var querystr = "INSERT INTO [" + table + "] ("
|
2018-04-22 12:33:56 +00:00
|
|
|
if columns == "" {
|
2018-04-23 21:08:31 +00:00
|
|
|
querystr += ") VALUES ()"
|
2018-04-22 12:33:56 +00:00
|
|
|
adapter.pushStatement(name, "insert", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
2017-10-16 07:32:58 +00:00
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
for _, column := range processColumns(columns) {
|
|
|
|
if column.Type == "function" {
|
|
|
|
querystr += column.Left + ","
|
|
|
|
} else {
|
|
|
|
querystr += "[" + column.Left + "],"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
|
|
|
querystr += ") VALUES ("
|
|
|
|
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) + "'"
|
|
|
|
}
|
|
|
|
querystr += field.Name + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
|
|
|
adapter.pushStatement(name, "insert", querystr+")")
|
|
|
|
return querystr + ")", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! DEPRECATED
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleReplace(name string, table string, columns string, fields string) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
log.Print("In SimpleReplace")
|
|
|
|
key, ok := adapter.keys[table]
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adapter.SimpleUpsert(name, table, columns, fields, "key = "+keyValue)
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleUpsert(name string, table string, columns string, fields 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 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
|
|
|
|
var querystr = "MERGE [" + table + "] WITH(HOLDLOCK) as t1 USING (VALUES("
|
|
|
|
|
|
|
|
var parsedFields = processFields(fields)
|
|
|
|
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]
|
|
|
|
querystr += fieldOutput + ")) AS updates ("
|
|
|
|
|
|
|
|
// nolint The linter wants this to be less readable
|
|
|
|
for fieldID, _ := range parsedFields {
|
|
|
|
querystr += "f" + strconv.Itoa(fieldID) + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
querystr += ") ON "
|
|
|
|
|
|
|
|
//querystr += "t1.[" + key + "] = "
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
querystr += " ?"
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var matched = " WHEN MATCHED THEN UPDATE SET "
|
|
|
|
var notMatched = "WHEN NOT MATCHED THEN INSERT("
|
|
|
|
var fieldList string
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
for columnID, column := range processColumns(columns) {
|
|
|
|
fieldList += "f" + strconv.Itoa(columnID) + ","
|
|
|
|
if column.Type == "function" {
|
|
|
|
matched += column.Left + " = f" + strconv.Itoa(columnID) + ","
|
|
|
|
notMatched += column.Left + ","
|
|
|
|
} else {
|
|
|
|
matched += "[" + column.Left + "] = f" + strconv.Itoa(columnID) + ","
|
|
|
|
notMatched += "[" + column.Left + "],"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matched = matched[0 : len(matched)-1]
|
|
|
|
notMatched = notMatched[0 : len(notMatched)-1]
|
|
|
|
fieldList = fieldList[0 : len(fieldList)-1]
|
|
|
|
|
|
|
|
notMatched += ") VALUES (" + fieldList + ");"
|
|
|
|
querystr += matched + " " + notMatched
|
|
|
|
|
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' {
|
|
|
|
log.Print(name+" query: ", querystr)
|
|
|
|
}
|
|
|
|
adapter.pushStatement(name, "upsert", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error) {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
var querystr = "UPDATE [" + up.table + "] SET "
|
|
|
|
for _, item := range processSet(up.set) {
|
2017-10-16 07:32:58 +00:00
|
|
|
querystr += "[" + item.Column + "] ="
|
|
|
|
for _, token := range item.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
querystr += " ?"
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += ","
|
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
2018-12-27 05:42:41 +00:00
|
|
|
if len(up.where) != 0 {
|
2017-10-16 07:32:58 +00:00
|
|
|
querystr += " 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 {
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "substitute", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
adapter.pushStatement(up.name, "update", querystr)
|
2017-10-16 07:32:58 +00:00
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleUpdateSelect(b *updatePrebuilder) (string, error) {
|
2018-12-27 05:42:41 +00:00
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var querystr = "DELETE FROM [" + table + "] WHERE"
|
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
querystr += " ?"
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = strings.TrimSpace(querystr[0 : len(querystr)-4])
|
|
|
|
adapter.pushStatement(name, "delete", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
func (adapter *MssqlAdapter) ComplexDelete(b *deletePrebuilder) (string, error) {
|
|
|
|
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
|
|
|
|
func (adapter *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")
|
|
|
|
}
|
|
|
|
adapter.pushStatement(name, "purge", "DELETE FROM ["+table+"]")
|
|
|
|
return "DELETE FROM [" + table + "]", nil
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var substituteCount = 0
|
|
|
|
var querystr = ""
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
var colslice = strings.Split(strings.TrimSpace(columns), ",")
|
|
|
|
for _, column := range colslice {
|
|
|
|
querystr += "[" + strings.TrimSpace(column) + "],"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
|
|
|
querystr += " FROM [" + table + "]"
|
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
substituteCount++
|
|
|
|
querystr += " ?" + strconv.Itoa(substituteCount)
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
|
|
|
for _, column := range processOrderby(orderby) {
|
|
|
|
// TODO: We might want to escape this column
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != "" {
|
|
|
|
limiter := processLimit(limit)
|
|
|
|
log.Printf("limiter: %+v\n", limiter)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
|
|
|
substituteCount++
|
|
|
|
querystr += " OFFSET ?" + strconv.Itoa(substituteCount) + " ROWS"
|
|
|
|
} else {
|
|
|
|
querystr += " OFFSET " + limiter.Offset + " ROWS"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
|
|
|
substituteCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(substituteCount)
|
|
|
|
}
|
|
|
|
querystr += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = strings.TrimSpace("SELECT " + querystr)
|
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && limit == "" {
|
|
|
|
log.Print(name+" query: ", querystr)
|
|
|
|
}
|
|
|
|
adapter.pushStatement(name, "select", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
// TODO: ComplexSelect
|
|
|
|
func (adapter *MssqlAdapter) ComplexSelect(preBuilder *selectPrebuilder) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *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")
|
|
|
|
}
|
|
|
|
var substituteCount = 0
|
|
|
|
var querystr = ""
|
|
|
|
|
|
|
|
for _, column := range processColumns(columns) {
|
|
|
|
var source, alias string
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
if column.Table != "" {
|
|
|
|
source = "[" + column.Table + "].[" + column.Left + "]"
|
|
|
|
} else if column.Type == "function" {
|
|
|
|
source = column.Left
|
|
|
|
} else {
|
|
|
|
source = "[" + column.Left + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
if column.Alias != "" {
|
|
|
|
alias = " AS '" + column.Alias + "'"
|
|
|
|
}
|
|
|
|
querystr += source + alias + ","
|
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
|
|
|
querystr += " FROM [" + table1 + "] LEFT JOIN [" + table2 + "] ON "
|
|
|
|
for _, joiner := range processJoiner(joiners) {
|
|
|
|
querystr += "[" + joiner.LeftTable + "].[" + joiner.LeftColumn + "] " + joiner.Operator + " [" + joiner.RightTable + "].[" + joiner.RightColumn + "] AND "
|
|
|
|
}
|
|
|
|
// Remove the trailing AND
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
substituteCount++
|
|
|
|
querystr += " ?" + strconv.Itoa(substituteCount)
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
halves := strings.Split(token.Contents, ".")
|
|
|
|
if len(halves) == 2 {
|
|
|
|
querystr += " [" + halves[0] + "].[" + halves[1] + "]"
|
|
|
|
} else {
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
}
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
|
|
|
for _, column := range processOrderby(orderby) {
|
|
|
|
log.Print("column: ", column)
|
|
|
|
// TODO: We might want to escape this column
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
} else if limit != "" {
|
|
|
|
key, ok := adapter.keys[table1]
|
|
|
|
if ok {
|
|
|
|
querystr += " ORDER BY [" + table1 + "].[" + key + "]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != "" {
|
|
|
|
limiter := processLimit(limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
|
|
|
substituteCount++
|
|
|
|
querystr += " OFFSET ?" + strconv.Itoa(substituteCount) + " ROWS"
|
|
|
|
} else {
|
|
|
|
querystr += " OFFSET " + limiter.Offset + " ROWS"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
|
|
|
substituteCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(substituteCount)
|
|
|
|
}
|
|
|
|
querystr += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = strings.TrimSpace("SELECT " + querystr)
|
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && limit == "" {
|
|
|
|
log.Print(name+" query: ", querystr)
|
|
|
|
}
|
|
|
|
adapter.pushStatement(name, "select", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var substituteCount = 0
|
|
|
|
var querystr = ""
|
|
|
|
|
|
|
|
for _, column := range processColumns(columns) {
|
|
|
|
var source, alias string
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
if column.Table != "" {
|
|
|
|
source = "[" + column.Table + "].[" + column.Left + "]"
|
|
|
|
} else if column.Type == "function" {
|
|
|
|
source = column.Left
|
|
|
|
} else {
|
|
|
|
source = "[" + column.Left + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
if column.Alias != "" {
|
|
|
|
alias = " AS '" + column.Alias + "'"
|
|
|
|
}
|
|
|
|
querystr += source + alias + ","
|
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
|
|
|
querystr += " FROM [" + table1 + "] INNER JOIN [" + table2 + "] ON "
|
|
|
|
for _, joiner := range processJoiner(joiners) {
|
|
|
|
querystr += "[" + joiner.LeftTable + "].[" + joiner.LeftColumn + "] " + joiner.Operator + " [" + joiner.RightTable + "].[" + joiner.RightColumn + "] AND "
|
|
|
|
}
|
|
|
|
// Remove the trailing AND
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
substituteCount++
|
|
|
|
querystr += " ?" + strconv.Itoa(substituteCount)
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
halves := strings.Split(token.Contents, ".")
|
|
|
|
if len(halves) == 2 {
|
|
|
|
querystr += " [" + halves[0] + "].[" + halves[1] + "]"
|
|
|
|
} else {
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
}
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
|
|
|
for _, column := range processOrderby(orderby) {
|
|
|
|
log.Print("column: ", column)
|
|
|
|
// TODO: We might want to escape this column
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
} else if limit != "" {
|
|
|
|
key, ok := adapter.keys[table1]
|
|
|
|
if ok {
|
|
|
|
log.Print("key: ", key)
|
|
|
|
querystr += " ORDER BY [" + table1 + "].[" + key + "]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != "" {
|
|
|
|
limiter := processLimit(limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
|
|
|
substituteCount++
|
|
|
|
querystr += " OFFSET ?" + strconv.Itoa(substituteCount) + " ROWS"
|
|
|
|
} else {
|
|
|
|
querystr += " OFFSET " + limiter.Offset + " ROWS"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
|
|
|
substituteCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(substituteCount)
|
|
|
|
}
|
|
|
|
querystr += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = strings.TrimSpace("SELECT " + querystr)
|
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && limit == "" {
|
|
|
|
log.Print(name+" query: ", querystr)
|
|
|
|
}
|
|
|
|
adapter.pushStatement(name, "select", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *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 */
|
|
|
|
var querystr = "INSERT INTO [" + ins.Table + "] ("
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
for _, column := range processColumns(ins.Columns) {
|
|
|
|
if column.Type == "function" {
|
|
|
|
querystr += column.Left + ","
|
|
|
|
} else {
|
|
|
|
querystr += "[" + column.Left + "],"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr)-1] + ") SELECT "
|
|
|
|
|
|
|
|
/* Select */
|
|
|
|
var substituteCount = 0
|
|
|
|
|
2017-10-21 00:27:47 +00:00
|
|
|
for _, column := range processColumns(sel.Columns) {
|
|
|
|
var source, alias string
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
if column.Type == "function" || column.Type == "substitute" {
|
|
|
|
source = column.Left
|
|
|
|
} else {
|
|
|
|
source = "[" + column.Left + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
if column.Alias != "" {
|
|
|
|
alias = " AS [" + column.Alias + "]"
|
|
|
|
}
|
|
|
|
querystr += " " + source + alias + ","
|
2017-10-16 07:32:58 +00:00
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
querystr += " FROM [" + sel.Table + "] "
|
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(sel.Where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range processWhere(sel.Where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
substituteCount++
|
|
|
|
querystr += " ?" + strconv.Itoa(substituteCount)
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(sel.Orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
|
|
|
for _, column := range processOrderby(sel.Orderby) {
|
|
|
|
// TODO: We might want to escape this column
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
} else if sel.Limit != "" {
|
|
|
|
key, ok := adapter.keys[sel.Table]
|
|
|
|
if ok {
|
|
|
|
querystr += " ORDER BY [" + sel.Table + "].[" + key + "]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sel.Limit != "" {
|
|
|
|
limiter := processLimit(sel.Limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
|
|
|
substituteCount++
|
|
|
|
querystr += " OFFSET ?" + strconv.Itoa(substituteCount) + " ROWS"
|
|
|
|
} else {
|
|
|
|
querystr += " OFFSET " + limiter.Offset + " ROWS"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
|
|
|
substituteCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(substituteCount)
|
|
|
|
}
|
|
|
|
querystr += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = strings.TrimSpace(querystr)
|
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && sel.Limit == "" {
|
|
|
|
log.Print(name+" query: ", querystr)
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.pushStatement(name, "insert", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *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 */
|
|
|
|
var querystr = "INSERT INTO [" + ins.Table + "] ("
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
for _, column := range processColumns(ins.Columns) {
|
|
|
|
if column.Type == "function" {
|
|
|
|
querystr += column.Left + ","
|
|
|
|
} else {
|
|
|
|
querystr += "[" + column.Left + "],"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr)-1] + ") SELECT "
|
|
|
|
|
|
|
|
/* Select */
|
|
|
|
var substituteCount = 0
|
|
|
|
|
|
|
|
for _, column := range processColumns(sel.Columns) {
|
|
|
|
var source, alias string
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
if column.Table != "" {
|
|
|
|
source = "[" + column.Table + "].[" + column.Left + "]"
|
|
|
|
} else if column.Type == "function" {
|
|
|
|
source = column.Left
|
|
|
|
} else {
|
|
|
|
source = "[" + column.Left + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
if column.Alias != "" {
|
|
|
|
alias = " AS '" + column.Alias + "'"
|
|
|
|
}
|
|
|
|
querystr += source + alias + ","
|
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
|
|
|
querystr += " FROM [" + sel.Table1 + "] " + joinType + " JOIN [" + sel.Table2 + "] ON "
|
|
|
|
for _, joiner := range processJoiner(sel.Joiners) {
|
|
|
|
querystr += "[" + joiner.LeftTable + "].[" + joiner.LeftColumn + "] " + joiner.Operator + " [" + joiner.RightTable + "].[" + joiner.RightColumn + "] AND "
|
|
|
|
}
|
|
|
|
// Remove the trailing AND
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
|
|
|
|
// Add support for BETWEEN x.x
|
|
|
|
if len(sel.Where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range processWhere(sel.Where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
|
|
|
case "substitute":
|
|
|
|
substituteCount++
|
|
|
|
querystr += " ?" + strconv.Itoa(substituteCount)
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "or":
|
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()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
halves := strings.Split(token.Contents, ".")
|
|
|
|
if len(halves) == 2 {
|
|
|
|
querystr += " [" + halves[0] + "].[" + halves[1] + "]"
|
|
|
|
} else {
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
}
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MSSQL requires ORDER BY for LIMIT
|
|
|
|
if len(sel.Orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
|
|
|
for _, column := range processOrderby(sel.Orderby) {
|
|
|
|
log.Print("column: ", column)
|
|
|
|
// TODO: We might want to escape this column
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
} else if sel.Limit != "" {
|
|
|
|
key, ok := adapter.keys[sel.Table1]
|
|
|
|
if ok {
|
|
|
|
querystr += " ORDER BY [" + sel.Table1 + "].[" + key + "]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sel.Limit != "" {
|
|
|
|
limiter := processLimit(sel.Limit)
|
|
|
|
if limiter.Offset != "" {
|
|
|
|
if limiter.Offset == "?" {
|
|
|
|
substituteCount++
|
|
|
|
querystr += " OFFSET ?" + strconv.Itoa(substituteCount) + " ROWS"
|
|
|
|
} else {
|
|
|
|
querystr += " OFFSET " + limiter.Offset + " ROWS"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Does this work without an offset?
|
|
|
|
if limiter.MaxCount != "" {
|
|
|
|
if limiter.MaxCount == "?" {
|
|
|
|
substituteCount++
|
|
|
|
limiter.MaxCount = "?" + strconv.Itoa(substituteCount)
|
|
|
|
}
|
|
|
|
querystr += " FETCH NEXT " + limiter.MaxCount + " ROWS ONLY "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = strings.TrimSpace(querystr)
|
|
|
|
// TODO: Run this on debug mode?
|
|
|
|
if name[0] == '_' && sel.Limit == "" {
|
|
|
|
log.Print(name+" query: ", querystr)
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.pushStatement(name, "insert", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleInsertLeftJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
return adapter.simpleJoin(name, ins, sel, "LEFT")
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleInsertInnerJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
|
2017-10-16 07:32:58 +00:00
|
|
|
return adapter.simpleJoin(name, ins, sel, "INNER")
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) SimpleCount(name string, table string, where 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var querystr = "SELECT COUNT(*) AS [count] FROM [" + table + "]"
|
|
|
|
|
|
|
|
// TODO: Add support for BETWEEN x.x
|
|
|
|
if len(where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range processWhere(where) {
|
|
|
|
for _, token := range loc.Expr {
|
|
|
|
switch token.Type {
|
2018-03-31 05:25:27 +00:00
|
|
|
case "function", "operator", "number", "substitute", "or":
|
2017-10-16 07:32:58 +00:00
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "GETUTCDATE()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " [" + token.Contents + "]"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != "" {
|
|
|
|
querystr += " LIMIT " + limit
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr = strings.TrimSpace(querystr)
|
|
|
|
adapter.pushStatement(name, "select", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 00:31:46 +00:00
|
|
|
func (adapter *MssqlAdapter) Builder() *prebuilder {
|
|
|
|
return &prebuilder{adapter}
|
2017-11-12 05:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) Write() error {
|
2017-10-16 07:32:58 +00:00
|
|
|
var stmts, body string
|
|
|
|
for _, name := range adapter.BufferOrder {
|
2018-12-27 05:42:41 +00:00
|
|
|
if name == "" {
|
2017-10-16 07:32:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stmt := adapter.Buffer[name]
|
|
|
|
// 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
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) pushStatement(name string, stype string, querystr string) {
|
2018-12-31 09:03:49 +00:00
|
|
|
if name == "" {
|
2018-02-10 15:07:21 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-12 05:25:04 +00:00
|
|
|
adapter.Buffer[name] = DBStmt{querystr, stype}
|
2017-10-16 07:32:58 +00:00
|
|
|
adapter.BufferOrder = append(adapter.BufferOrder, name)
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) stringyType(ctype string) bool {
|
2017-10-16 07:32:58 +00:00
|
|
|
ctype = strings.ToLower(ctype)
|
|
|
|
return ctype == "char" || ctype == "varchar" || ctype == "datetime" || ctype == "text" || ctype == "nvarchar"
|
|
|
|
}
|
|
|
|
|
|
|
|
type SetPrimaryKeys interface {
|
|
|
|
SetPrimaryKeys(keys map[string]string)
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *MssqlAdapter) SetPrimaryKeys(keys map[string]string) {
|
2017-10-16 07:32:58 +00:00
|
|
|
adapter.keys = keys
|
|
|
|
}
|