2016-09-15 05:45:09 +02:00
|
|
|
package queries
|
2016-08-06 23:42:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2016-08-12 19:08:09 +02:00
|
|
|
"sort"
|
2016-08-06 23:42:22 +02:00
|
|
|
"strings"
|
|
|
|
|
2017-05-08 19:25:15 +02:00
|
|
|
"github.com/lbryio/sqlboiler/strmangle"
|
2016-08-06 23:42:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-08-07 23:09:56 +02:00
|
|
|
rgxIdentifier = regexp.MustCompile(`^(?i)"?[a-z_][_a-z0-9]*"?(?:\."?[_a-z][_a-z0-9]*"?)*$`)
|
2016-08-18 19:21:53 +02:00
|
|
|
rgxInClause = regexp.MustCompile(`^(?i)(.*[\s|\)|\?])IN([\s|\(|\?].*)$`)
|
2016-08-06 23:42:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func buildQuery(q *Query) (string, []interface{}) {
|
|
|
|
var buf *bytes.Buffer
|
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
switch {
|
2016-09-15 05:57:07 +02:00
|
|
|
case len(q.rawSQL.sql) != 0:
|
|
|
|
return q.rawSQL.sql, q.rawSQL.args
|
2016-08-06 23:42:22 +02:00
|
|
|
case q.delete:
|
|
|
|
buf, args = buildDeleteQuery(q)
|
|
|
|
case len(q.update) > 0:
|
|
|
|
buf, args = buildUpdateQuery(q)
|
|
|
|
default:
|
|
|
|
buf, args = buildSelectQuery(q)
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:20:13 +02:00
|
|
|
defer strmangle.PutBuffer(buf)
|
|
|
|
|
2016-09-01 03:28:35 +02:00
|
|
|
// Cache the generated query for query object re-use
|
|
|
|
bufStr := buf.String()
|
2016-09-15 05:57:07 +02:00
|
|
|
q.rawSQL.sql = bufStr
|
|
|
|
q.rawSQL.args = args
|
2016-09-01 03:28:35 +02:00
|
|
|
|
|
|
|
return bufStr, args
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
2016-08-13 16:20:13 +02:00
|
|
|
buf := strmangle.GetBuffer()
|
2016-08-12 19:59:50 +02:00
|
|
|
var args []interface{}
|
2016-08-06 23:42:22 +02:00
|
|
|
|
|
|
|
buf.WriteString("SELECT ")
|
|
|
|
|
2017-03-14 09:13:56 +01:00
|
|
|
if q.dialect.UseTopClause {
|
|
|
|
if q.limit != 0 && q.offset == 0 {
|
2017-03-14 12:31:52 +01:00
|
|
|
fmt.Fprintf(buf, " TOP (%d) ", q.limit)
|
2017-03-14 09:13:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 14:08:27 +02:00
|
|
|
if q.count {
|
|
|
|
buf.WriteString("COUNT(")
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 00:10:35 +02:00
|
|
|
hasSelectCols := len(q.selectCols) != 0
|
2016-08-07 22:37:51 +02:00
|
|
|
hasJoins := len(q.joins) != 0
|
2016-08-26 14:08:27 +02:00
|
|
|
if hasJoins && hasSelectCols && !q.count {
|
2016-08-07 22:37:51 +02:00
|
|
|
selectColsWithAs := writeAsStatements(q)
|
|
|
|
// Don't identQuoteSlice - writeAsStatements does this
|
2016-08-09 12:19:42 +02:00
|
|
|
buf.WriteString(strings.Join(selectColsWithAs, ", "))
|
2016-08-07 00:10:35 +02:00
|
|
|
} else if hasSelectCols {
|
2016-09-09 21:15:50 +02:00
|
|
|
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.selectCols), ", "))
|
2016-09-13 09:48:14 +02:00
|
|
|
} else if hasJoins && !q.count {
|
2016-08-07 22:37:51 +02:00
|
|
|
selectColsWithStars := writeStars(q)
|
2016-08-09 12:19:42 +02:00
|
|
|
buf.WriteString(strings.Join(selectColsWithStars, ", "))
|
2016-08-06 23:42:22 +02:00
|
|
|
} else {
|
2016-08-07 00:10:35 +02:00
|
|
|
buf.WriteByte('*')
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 14:08:27 +02:00
|
|
|
// close SQL COUNT function
|
|
|
|
if q.count {
|
2016-08-09 12:19:42 +02:00
|
|
|
buf.WriteByte(')')
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 17:30:23 +02:00
|
|
|
if len(q.forceindex) > 0 {
|
|
|
|
fmt.Fprintf(buf, " FROM %s FORCE INDEX (%s)", strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.from), ", "),q.forceindex)
|
|
|
|
|
|
|
|
}else{
|
|
|
|
fmt.Fprintf(buf, " FROM %s", strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.from), ", "))
|
|
|
|
|
|
|
|
}
|
2016-08-07 22:37:51 +02:00
|
|
|
|
2016-08-12 19:59:50 +02:00
|
|
|
if len(q.joins) > 0 {
|
|
|
|
argsLen := len(args)
|
2016-08-13 16:20:13 +02:00
|
|
|
joinBuf := strmangle.GetBuffer()
|
2016-08-12 19:59:50 +02:00
|
|
|
for _, j := range q.joins {
|
|
|
|
if j.kind != JoinInner {
|
|
|
|
panic("only inner joins are supported")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(joinBuf, " INNER JOIN %s", j.clause)
|
|
|
|
args = append(args, j.args...)
|
2016-08-07 22:37:51 +02:00
|
|
|
}
|
2016-09-09 21:15:50 +02:00
|
|
|
var resp string
|
|
|
|
if q.dialect.IndexPlaceholders {
|
|
|
|
resp, _ = convertQuestionMarks(joinBuf.String(), argsLen+1)
|
|
|
|
} else {
|
|
|
|
resp = joinBuf.String()
|
|
|
|
}
|
2016-08-18 19:21:53 +02:00
|
|
|
fmt.Fprintf(buf, resp)
|
2016-08-13 16:20:13 +02:00
|
|
|
strmangle.PutBuffer(joinBuf)
|
2016-08-07 22:37:51 +02:00
|
|
|
}
|
2016-08-06 23:42:22 +02:00
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
where, whereArgs := whereClause(q, len(args)+1)
|
2016-08-06 23:42:22 +02:00
|
|
|
buf.WriteString(where)
|
2016-08-17 07:19:23 +02:00
|
|
|
if len(whereArgs) != 0 {
|
|
|
|
args = append(args, whereArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
in, inArgs := inClause(q, len(args)+1)
|
|
|
|
buf.WriteString(in)
|
|
|
|
if len(inArgs) != 0 {
|
|
|
|
args = append(args, inArgs...)
|
|
|
|
}
|
2016-08-06 23:42:22 +02:00
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
writeModifiers(q, buf, &args)
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
buf.WriteByte(';')
|
|
|
|
return buf, args
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildDeleteQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
2016-08-17 07:19:23 +02:00
|
|
|
var args []interface{}
|
2016-08-13 16:20:13 +02:00
|
|
|
buf := strmangle.GetBuffer()
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
buf.WriteString("DELETE FROM ")
|
2016-09-09 21:15:50 +02:00
|
|
|
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.from), ", "))
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
where, whereArgs := whereClause(q, 1)
|
|
|
|
if len(whereArgs) != 0 {
|
2016-09-19 11:30:50 +02:00
|
|
|
args = append(args, whereArgs...)
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
buf.WriteString(where)
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
in, inArgs := inClause(q, len(args)+1)
|
|
|
|
if len(inArgs) != 0 {
|
|
|
|
args = append(args, inArgs...)
|
|
|
|
}
|
|
|
|
buf.WriteString(in)
|
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
writeModifiers(q, buf, &args)
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
buf.WriteByte(';')
|
|
|
|
|
|
|
|
return buf, args
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildUpdateQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
2016-08-13 16:20:13 +02:00
|
|
|
buf := strmangle.GetBuffer()
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
buf.WriteString("UPDATE ")
|
2016-09-09 21:15:50 +02:00
|
|
|
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.from), ", "))
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
cols := make(sort.StringSlice, len(q.update))
|
|
|
|
var args []interface{}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
count := 0
|
2016-08-12 19:08:09 +02:00
|
|
|
for name := range q.update {
|
|
|
|
cols[count] = name
|
2016-08-11 10:23:47 +02:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
cols.Sort()
|
|
|
|
|
|
|
|
for i := 0; i < len(cols); i++ {
|
|
|
|
args = append(args, q.update[cols[i]])
|
2016-09-09 21:15:50 +02:00
|
|
|
cols[i] = strmangle.IdentQuote(q.dialect.LQ, q.dialect.RQ, cols[i])
|
2016-08-12 19:08:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(
|
2016-08-11 14:26:49 +02:00
|
|
|
" SET (%s) = (%s)",
|
2016-08-11 10:23:47 +02:00
|
|
|
strings.Join(cols, ", "),
|
2016-09-09 21:15:50 +02:00
|
|
|
strmangle.Placeholders(q.dialect.IndexPlaceholders, len(cols), 1, 1)),
|
2016-08-11 10:23:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
where, whereArgs := whereClause(q, len(args)+1)
|
2016-08-17 07:19:23 +02:00
|
|
|
if len(whereArgs) != 0 {
|
|
|
|
args = append(args, whereArgs...)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
buf.WriteString(where)
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
in, inArgs := inClause(q, len(args)+1)
|
|
|
|
if len(inArgs) != 0 {
|
|
|
|
args = append(args, inArgs...)
|
|
|
|
}
|
|
|
|
buf.WriteString(in)
|
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
writeModifiers(q, buf, &args)
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
buf.WriteByte(';')
|
|
|
|
|
|
|
|
return buf, args
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
// BuildUpsertQueryMySQL builds a SQL statement string using the upsertData provided.
|
2017-06-12 20:02:04 +02:00
|
|
|
func BuildUpsertQueryMySQL(dia Dialect, tableName string, update, whitelist []string, autoIncrementCol string) string {
|
2016-09-14 10:08:30 +02:00
|
|
|
whitelist = strmangle.IdentQuoteSlice(dia.LQ, dia.RQ, whitelist)
|
|
|
|
|
|
|
|
buf := strmangle.GetBuffer()
|
|
|
|
defer strmangle.PutBuffer(buf)
|
|
|
|
|
2017-03-02 03:18:47 +01:00
|
|
|
var columns string
|
|
|
|
if len(whitelist) != 0 {
|
|
|
|
columns = strings.Join(whitelist, ", ")
|
|
|
|
}
|
|
|
|
|
2016-09-19 09:51:06 +02:00
|
|
|
if len(update) == 0 {
|
|
|
|
fmt.Fprintf(
|
|
|
|
buf,
|
|
|
|
"INSERT IGNORE INTO %s (%s) VALUES (%s)",
|
|
|
|
tableName,
|
2017-03-02 03:18:47 +01:00
|
|
|
columns,
|
2016-09-19 09:51:06 +02:00
|
|
|
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
|
|
|
|
)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
fmt.Fprintf(
|
|
|
|
buf,
|
|
|
|
"INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE ",
|
|
|
|
tableName,
|
2017-03-02 03:18:47 +01:00
|
|
|
columns,
|
2016-09-14 10:08:30 +02:00
|
|
|
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
|
|
|
|
)
|
|
|
|
|
2017-06-12 20:02:04 +02:00
|
|
|
// https://stackoverflow.com/questions/778534/mysql-on-duplicate-key-last-insert-id
|
|
|
|
if autoIncrementCol != "" {
|
|
|
|
buf.WriteString(autoIncrementCol + " = LAST_INSERT_ID(" + autoIncrementCol + "), ")
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
for i, v := range update {
|
|
|
|
if i != 0 {
|
|
|
|
buf.WriteByte(',')
|
|
|
|
}
|
|
|
|
quoted := strmangle.IdentQuote(dia.LQ, dia.RQ, v)
|
|
|
|
buf.WriteString(quoted)
|
|
|
|
buf.WriteString(" = VALUES(")
|
|
|
|
buf.WriteString(quoted)
|
|
|
|
buf.WriteByte(')')
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildUpsertQueryPostgres builds a SQL statement string using the upsertData provided.
|
|
|
|
func BuildUpsertQueryPostgres(dia Dialect, tableName string, updateOnConflict bool, ret, update, conflict, whitelist []string) string {
|
2016-09-09 21:15:50 +02:00
|
|
|
conflict = strmangle.IdentQuoteSlice(dia.LQ, dia.RQ, conflict)
|
|
|
|
whitelist = strmangle.IdentQuoteSlice(dia.LQ, dia.RQ, whitelist)
|
|
|
|
ret = strmangle.IdentQuoteSlice(dia.LQ, dia.RQ, ret)
|
2016-09-01 03:20:16 +02:00
|
|
|
|
|
|
|
buf := strmangle.GetBuffer()
|
|
|
|
defer strmangle.PutBuffer(buf)
|
|
|
|
|
2017-03-02 03:18:47 +01:00
|
|
|
columns := "DEFAULT VALUES"
|
|
|
|
if len(whitelist) != 0 {
|
|
|
|
columns = fmt.Sprintf("(%s) VALUES (%s)",
|
|
|
|
strings.Join(whitelist, ", "),
|
|
|
|
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1))
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:20:16 +02:00
|
|
|
fmt.Fprintf(
|
|
|
|
buf,
|
2017-03-02 03:18:47 +01:00
|
|
|
"INSERT INTO %s %s ON CONFLICT ",
|
2016-09-01 03:20:16 +02:00
|
|
|
tableName,
|
2017-03-02 03:18:47 +01:00
|
|
|
columns,
|
2016-09-01 03:20:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if !updateOnConflict || len(update) == 0 {
|
|
|
|
buf.WriteString("DO NOTHING")
|
|
|
|
} else {
|
|
|
|
buf.WriteByte('(')
|
|
|
|
buf.WriteString(strings.Join(conflict, ", "))
|
|
|
|
buf.WriteString(") DO UPDATE SET ")
|
|
|
|
|
|
|
|
for i, v := range update {
|
|
|
|
if i != 0 {
|
|
|
|
buf.WriteByte(',')
|
|
|
|
}
|
2016-09-09 21:15:50 +02:00
|
|
|
quoted := strmangle.IdentQuote(dia.LQ, dia.RQ, v)
|
2016-09-01 03:20:16 +02:00
|
|
|
buf.WriteString(quoted)
|
|
|
|
buf.WriteString(" = EXCLUDED.")
|
|
|
|
buf.WriteString(quoted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ret) != 0 {
|
|
|
|
buf.WriteString(" RETURNING ")
|
|
|
|
buf.WriteString(strings.Join(ret, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2017-03-21 11:04:07 +01:00
|
|
|
// BuildUpsertQueryMSSQL builds a SQL statement string using the upsertData provided.
|
|
|
|
func BuildUpsertQueryMSSQL(dia Dialect, tableName string, primary, update, insert []string, output []string) string {
|
|
|
|
insert = strmangle.IdentQuoteSlice(dia.LQ, dia.RQ, insert)
|
|
|
|
|
|
|
|
buf := strmangle.GetBuffer()
|
|
|
|
defer strmangle.PutBuffer(buf)
|
|
|
|
|
|
|
|
startIndex := 1
|
|
|
|
|
|
|
|
fmt.Fprintf(buf, "MERGE INTO %s as [t]\n", tableName)
|
|
|
|
fmt.Fprintf(buf, "USING (SELECT %s) as [s] ([%s])\n",
|
|
|
|
strmangle.Placeholders(dia.IndexPlaceholders, len(primary), startIndex, 1),
|
|
|
|
strings.Join(primary, string(dia.RQ)+","+string(dia.LQ)))
|
|
|
|
fmt.Fprint(buf, "ON (")
|
|
|
|
for i, v := range primary {
|
|
|
|
if i != 0 {
|
|
|
|
fmt.Fprint(buf, " AND ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(buf, "[s].[%s] = [t].[%s]", v, v)
|
|
|
|
}
|
|
|
|
fmt.Fprint(buf, ")\n")
|
|
|
|
|
2017-03-21 11:15:37 +01:00
|
|
|
startIndex += len(primary)
|
2017-03-21 11:04:07 +01:00
|
|
|
|
|
|
|
fmt.Fprint(buf, "WHEN MATCHED THEN ")
|
|
|
|
fmt.Fprintf(buf, "UPDATE SET %s\n", strmangle.SetParamNames(string(dia.LQ), string(dia.RQ), startIndex, update))
|
|
|
|
|
2017-03-21 11:15:37 +01:00
|
|
|
startIndex += len(update)
|
2017-03-21 11:04:07 +01:00
|
|
|
|
|
|
|
fmt.Fprint(buf, "WHEN NOT MATCHED THEN ")
|
|
|
|
fmt.Fprintf(buf, "INSERT (%s) VALUES (%s)",
|
|
|
|
strings.Join(insert, ", "),
|
|
|
|
strmangle.Placeholders(dia.IndexPlaceholders, len(insert), startIndex, 1))
|
|
|
|
|
|
|
|
if len(output) > 0 {
|
|
|
|
fmt.Fprintf(buf, "\nOUTPUT INSERTED.[%s];", strings.Join(output, "],INSERTED.["))
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(buf, ";")
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:08:09 +02:00
|
|
|
func writeModifiers(q *Query, buf *bytes.Buffer, args *[]interface{}) {
|
2016-08-08 09:28:01 +02:00
|
|
|
if len(q.groupBy) != 0 {
|
2016-08-09 12:19:42 +02:00
|
|
|
fmt.Fprintf(buf, " GROUP BY %s", strings.Join(q.groupBy, ", "))
|
2016-08-08 09:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.having) != 0 {
|
2016-08-12 19:59:50 +02:00
|
|
|
argsLen := len(*args)
|
2016-08-13 16:20:13 +02:00
|
|
|
havingBuf := strmangle.GetBuffer()
|
2016-08-12 19:59:50 +02:00
|
|
|
fmt.Fprintf(havingBuf, " HAVING ")
|
2016-08-12 19:08:09 +02:00
|
|
|
for i, j := range q.having {
|
|
|
|
if i > 0 {
|
2016-08-12 19:59:50 +02:00
|
|
|
fmt.Fprintf(havingBuf, ", ")
|
2016-08-12 19:08:09 +02:00
|
|
|
}
|
2016-08-12 19:59:50 +02:00
|
|
|
fmt.Fprintf(havingBuf, j.clause)
|
2016-08-12 19:08:09 +02:00
|
|
|
*args = append(*args, j.args...)
|
|
|
|
}
|
2016-09-09 21:15:50 +02:00
|
|
|
var resp string
|
|
|
|
if q.dialect.IndexPlaceholders {
|
|
|
|
resp, _ = convertQuestionMarks(havingBuf.String(), argsLen+1)
|
|
|
|
} else {
|
|
|
|
resp = havingBuf.String()
|
|
|
|
}
|
2016-08-18 19:21:53 +02:00
|
|
|
fmt.Fprintf(buf, resp)
|
2016-08-13 16:20:13 +02:00
|
|
|
strmangle.PutBuffer(havingBuf)
|
2016-08-08 09:28:01 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 23:42:22 +02:00
|
|
|
if len(q.orderBy) != 0 {
|
|
|
|
buf.WriteString(" ORDER BY ")
|
2016-08-09 12:19:42 +02:00
|
|
|
buf.WriteString(strings.Join(q.orderBy, ", "))
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 09:13:56 +01:00
|
|
|
if !q.dialect.UseTopClause {
|
|
|
|
if q.limit != 0 {
|
|
|
|
fmt.Fprintf(buf, " LIMIT %d", q.limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.offset != 0 {
|
|
|
|
fmt.Fprintf(buf, " OFFSET %d", q.offset)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// From MS SQL 2012 and above: https://technet.microsoft.com/en-us/library/ms188385(v=sql.110).aspx
|
|
|
|
// ORDER BY ...
|
|
|
|
// OFFSET N ROWS
|
|
|
|
// FETCH NEXT M ROWS ONLY
|
|
|
|
if q.offset != 0 {
|
2017-03-14 12:24:03 +01:00
|
|
|
|
|
|
|
// Hack from https://www.microsoftpressstore.com/articles/article.aspx?p=2314819
|
|
|
|
// ...
|
|
|
|
// As mentioned, the OFFSET-FETCH filter requires an ORDER BY clause. If you want to use arbitrary order,
|
|
|
|
// like TOP without an ORDER BY clause, you can use the trick with ORDER BY (SELECT NULL)
|
|
|
|
// ...
|
|
|
|
if len(q.orderBy) == 0 {
|
|
|
|
buf.WriteString(" ORDER BY (SELECT NULL)")
|
|
|
|
}
|
|
|
|
|
2017-03-14 09:13:56 +01:00
|
|
|
fmt.Fprintf(buf, " OFFSET %d", q.offset)
|
|
|
|
|
|
|
|
if q.limit != 0 {
|
|
|
|
fmt.Fprintf(buf, " FETCH NEXT %d ROWS ONLY", q.limit)
|
|
|
|
}
|
|
|
|
}
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
2016-08-30 05:13:00 +02:00
|
|
|
|
|
|
|
if len(q.forlock) != 0 {
|
|
|
|
fmt.Fprintf(buf, " FOR %s", q.forlock)
|
|
|
|
}
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 23:09:56 +02:00
|
|
|
func writeStars(q *Query) []string {
|
2016-09-01 05:14:35 +02:00
|
|
|
cols := make([]string, len(q.from))
|
|
|
|
for i, f := range q.from {
|
2016-08-07 23:09:56 +02:00
|
|
|
toks := strings.Split(f, " ")
|
|
|
|
if len(toks) == 1 {
|
2016-09-09 21:15:50 +02:00
|
|
|
cols[i] = fmt.Sprintf(`%s.*`, strmangle.IdentQuote(q.dialect.LQ, q.dialect.RQ, toks[0]))
|
2016-08-07 23:09:56 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
alias, name, ok := parseFromClause(toks)
|
|
|
|
if !ok {
|
2016-09-01 05:14:35 +02:00
|
|
|
return nil
|
2016-08-07 23:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(alias) != 0 {
|
|
|
|
name = alias
|
|
|
|
}
|
2016-09-09 21:15:50 +02:00
|
|
|
cols[i] = fmt.Sprintf(`%s.*`, strmangle.IdentQuote(q.dialect.LQ, q.dialect.RQ, name))
|
2016-08-07 23:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cols
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeAsStatements(q *Query) []string {
|
2016-08-07 00:10:35 +02:00
|
|
|
cols := make([]string, len(q.selectCols))
|
2016-08-07 23:09:56 +02:00
|
|
|
for i, col := range q.selectCols {
|
|
|
|
if !rgxIdentifier.MatchString(col) {
|
|
|
|
cols[i] = col
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
toks := strings.Split(col, ".")
|
|
|
|
if len(toks) == 1 {
|
2016-09-09 21:15:50 +02:00
|
|
|
cols[i] = strmangle.IdentQuote(q.dialect.LQ, q.dialect.RQ, col)
|
2016-08-07 23:09:56 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
asParts := make([]string, len(toks))
|
|
|
|
for j, tok := range toks {
|
|
|
|
asParts[j] = strings.Trim(tok, `"`)
|
2016-08-07 00:10:35 +02:00
|
|
|
}
|
2016-08-07 23:09:56 +02:00
|
|
|
|
2016-09-09 21:15:50 +02:00
|
|
|
cols[i] = fmt.Sprintf(`%s as "%s"`, strmangle.IdentQuote(q.dialect.LQ, q.dialect.RQ, col), strings.Join(asParts, "."))
|
2016-08-07 00:10:35 +02:00
|
|
|
}
|
2016-08-07 23:09:56 +02:00
|
|
|
|
|
|
|
return cols
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
// whereClause parses a where slice and converts it into a
|
|
|
|
// single WHERE clause like:
|
|
|
|
// WHERE (a=$1) AND (b=$2)
|
|
|
|
//
|
|
|
|
// startAt specifies what number placeholders start at
|
|
|
|
func whereClause(q *Query, startAt int) (string, []interface{}) {
|
2016-08-06 23:42:22 +02:00
|
|
|
if len(q.where) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:20:13 +02:00
|
|
|
buf := strmangle.GetBuffer()
|
|
|
|
defer strmangle.PutBuffer(buf)
|
2016-08-06 23:42:22 +02:00
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
buf.WriteString(" WHERE ")
|
2016-08-18 19:21:53 +02:00
|
|
|
for i, where := range q.where {
|
|
|
|
if i != 0 {
|
|
|
|
if where.orSeparator {
|
|
|
|
buf.WriteString(" OR ")
|
|
|
|
} else {
|
|
|
|
buf.WriteString(" AND ")
|
|
|
|
}
|
2016-08-11 08:44:15 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("(%s)", where.clause))
|
|
|
|
args = append(args, where.args...)
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 21:15:50 +02:00
|
|
|
var resp string
|
|
|
|
if q.dialect.IndexPlaceholders {
|
|
|
|
resp, _ = convertQuestionMarks(buf.String(), startAt)
|
|
|
|
} else {
|
|
|
|
resp = buf.String()
|
|
|
|
}
|
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
return resp, args
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
// inClause parses an in slice and converts it into a
|
|
|
|
// single IN clause, like:
|
|
|
|
// WHERE ("a", "b") IN (($1,$2),($3,$4)).
|
2016-08-17 07:19:23 +02:00
|
|
|
func inClause(q *Query, startAt int) (string, []interface{}) {
|
|
|
|
if len(q.in) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := strmangle.GetBuffer()
|
|
|
|
defer strmangle.PutBuffer(buf)
|
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
if len(q.where) == 0 {
|
|
|
|
buf.WriteString(" WHERE ")
|
|
|
|
}
|
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
for i, in := range q.in {
|
|
|
|
ln := len(in.args)
|
|
|
|
// We only prefix the OR and AND separators after the first
|
|
|
|
// clause has been generated UNLESS there is already a where
|
|
|
|
// clause that we have to add on to.
|
|
|
|
if i != 0 || len(q.where) > 0 {
|
|
|
|
if in.orSeparator {
|
|
|
|
buf.WriteString(" OR ")
|
|
|
|
} else {
|
|
|
|
buf.WriteString(" AND ")
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 07:19:23 +02:00
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
matches := rgxInClause.FindStringSubmatch(in.clause)
|
|
|
|
// If we can't find any matches attempt a simple replace with 1 group.
|
|
|
|
// Clauses that fit this criteria will not be able to contain ? in their
|
|
|
|
// column name side, however if this case is being hit then the regexp
|
|
|
|
// probably needs adjustment, or the user is passing in invalid clauses.
|
|
|
|
if matches == nil {
|
2016-09-09 21:15:50 +02:00
|
|
|
clause, count := convertInQuestionMarks(q.dialect.IndexPlaceholders, in.clause, startAt, 1, ln)
|
2016-08-18 19:21:53 +02:00
|
|
|
buf.WriteString(clause)
|
|
|
|
startAt = startAt + count
|
|
|
|
} else {
|
|
|
|
leftSide := strings.TrimSpace(matches[1])
|
|
|
|
rightSide := strings.TrimSpace(matches[2])
|
|
|
|
// If matches are found, we have to parse the left side (column side)
|
|
|
|
// of the clause to determine how many columns they are using.
|
|
|
|
// This number determines the groupAt for the convert function.
|
|
|
|
cols := strings.Split(leftSide, ",")
|
2016-09-09 21:15:50 +02:00
|
|
|
cols = strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, cols)
|
2016-08-18 19:21:53 +02:00
|
|
|
groupAt := len(cols)
|
|
|
|
|
2016-09-09 21:15:50 +02:00
|
|
|
var leftClause string
|
|
|
|
var leftCount int
|
|
|
|
if q.dialect.IndexPlaceholders {
|
|
|
|
leftClause, leftCount = convertQuestionMarks(strings.Join(cols, ","), startAt)
|
|
|
|
} else {
|
|
|
|
// Count the number of cols that are question marks, so we know
|
|
|
|
// how much to offset convertInQuestionMarks by
|
|
|
|
for _, v := range cols {
|
|
|
|
if v == "?" {
|
|
|
|
leftCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
leftClause = strings.Join(cols, ",")
|
|
|
|
}
|
|
|
|
rightClause, rightCount := convertInQuestionMarks(q.dialect.IndexPlaceholders, rightSide, startAt+leftCount, groupAt, ln-leftCount)
|
2016-08-18 19:21:53 +02:00
|
|
|
buf.WriteString(leftClause)
|
|
|
|
buf.WriteString(" IN ")
|
|
|
|
buf.WriteString(rightClause)
|
|
|
|
startAt = startAt + leftCount + rightCount
|
|
|
|
}
|
2016-08-17 07:19:23 +02:00
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
args = append(args, in.args...)
|
|
|
|
}
|
2016-08-17 07:19:23 +02:00
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
return buf.String(), args
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 13:28:58 +02:00
|
|
|
// convertInQuestionMarks finds the first unescaped occurrence of ? and swaps it
|
2016-08-18 19:21:53 +02:00
|
|
|
// with a list of numbered placeholders, starting at startAt.
|
|
|
|
// It uses groupAt to determine how many placeholders should be in each group,
|
|
|
|
// for example, groupAt 2 would result in: (($1,$2),($3,$4))
|
|
|
|
// and groupAt 1 would result in ($1,$2,$3,$4)
|
2016-09-09 21:15:50 +02:00
|
|
|
func convertInQuestionMarks(indexPlaceholders bool, clause string, startAt, groupAt, total int) (string, int) {
|
2016-08-18 19:21:53 +02:00
|
|
|
if startAt == 0 || len(clause) == 0 {
|
|
|
|
panic("Not a valid start number.")
|
|
|
|
}
|
|
|
|
|
|
|
|
paramBuf := strmangle.GetBuffer()
|
|
|
|
defer strmangle.PutBuffer(paramBuf)
|
|
|
|
|
|
|
|
foundAt := -1
|
|
|
|
for i := 0; i < len(clause); i++ {
|
|
|
|
if (clause[i] == '?' && i == 0) || (clause[i] == '?' && clause[i-1] != '\\') {
|
|
|
|
foundAt = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundAt == -1 {
|
|
|
|
return strings.Replace(clause, `\?`, "?", -1), 0
|
|
|
|
}
|
|
|
|
|
|
|
|
paramBuf.WriteString(clause[:foundAt])
|
|
|
|
paramBuf.WriteByte('(')
|
2016-09-09 21:15:50 +02:00
|
|
|
paramBuf.WriteString(strmangle.Placeholders(indexPlaceholders, total, startAt, groupAt))
|
2016-08-18 19:21:53 +02:00
|
|
|
paramBuf.WriteByte(')')
|
|
|
|
paramBuf.WriteString(clause[foundAt+1:])
|
|
|
|
|
|
|
|
// Remove all backslashes from escaped question-marks
|
|
|
|
ret := strings.Replace(paramBuf.String(), `\?`, "?", -1)
|
|
|
|
return ret, total
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 13:28:58 +02:00
|
|
|
// convertQuestionMarks converts each occurrence of ? with $<number>
|
2016-08-17 07:19:23 +02:00
|
|
|
// where <number> is an incrementing digit starting at startAt.
|
|
|
|
// If question-mark (?) is escaped using back-slash (\), it will be ignored.
|
2016-08-18 19:21:53 +02:00
|
|
|
func convertQuestionMarks(clause string, startAt int) (string, int) {
|
2016-08-11 10:23:47 +02:00
|
|
|
if startAt == 0 {
|
|
|
|
panic("Not a valid start number.")
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:20:13 +02:00
|
|
|
paramBuf := strmangle.GetBuffer()
|
|
|
|
defer strmangle.PutBuffer(paramBuf)
|
2016-08-11 10:23:47 +02:00
|
|
|
paramIndex := 0
|
2016-08-18 19:21:53 +02:00
|
|
|
total := 0
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-13 16:54:46 +02:00
|
|
|
for {
|
2016-08-11 10:23:47 +02:00
|
|
|
if paramIndex >= len(clause) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
clause = clause[paramIndex:]
|
|
|
|
paramIndex = strings.IndexByte(clause, '?')
|
|
|
|
|
|
|
|
if paramIndex == -1 {
|
|
|
|
paramBuf.WriteString(clause)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:54:46 +02:00
|
|
|
escapeIndex := strings.Index(clause, `\?`)
|
|
|
|
if escapeIndex != -1 && paramIndex > escapeIndex {
|
|
|
|
paramBuf.WriteString(clause[:escapeIndex] + "?")
|
|
|
|
paramIndex++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
paramBuf.WriteString(clause[:paramIndex] + fmt.Sprintf("$%d", startAt))
|
2016-08-18 19:21:53 +02:00
|
|
|
total++
|
2016-08-13 16:54:46 +02:00
|
|
|
startAt++
|
2016-08-11 10:23:47 +02:00
|
|
|
paramIndex++
|
|
|
|
}
|
|
|
|
|
2016-08-18 19:21:53 +02:00
|
|
|
return paramBuf.String(), total
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 05:15:41 +02:00
|
|
|
// parseFromClause will parse something that looks like
|
|
|
|
// a
|
2016-08-06 23:42:22 +02:00
|
|
|
// a b
|
|
|
|
// a as b
|
2016-08-07 23:09:56 +02:00
|
|
|
func parseFromClause(toks []string) (alias, name string, ok bool) {
|
|
|
|
if len(toks) > 3 {
|
|
|
|
toks = toks[:3]
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 23:09:56 +02:00
|
|
|
sawIdent, sawAs := false, false
|
|
|
|
for _, tok := range toks {
|
2016-08-06 23:42:22 +02:00
|
|
|
if t := strings.ToLower(tok); sawIdent && t == "as" {
|
|
|
|
sawAs = true
|
|
|
|
continue
|
|
|
|
} else if sawIdent && t == "on" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rgxIdentifier.MatchString(tok) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if sawIdent || sawAs {
|
|
|
|
alias = strings.Trim(tok, `"`)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strings.Trim(tok, `"`)
|
|
|
|
sawIdent = true
|
2016-08-07 23:09:56 +02:00
|
|
|
ok = true
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 23:09:56 +02:00
|
|
|
return alias, name, ok
|
2016-08-06 23:42:22 +02:00
|
|
|
}
|