2017-06-05 11:57:27 +00:00
|
|
|
/* WIP Under Construction */
|
2017-06-13 07:12:58 +00:00
|
|
|
package qgen
|
2017-06-05 11:57:27 +00:00
|
|
|
|
2017-06-06 08:47:33 +00:00
|
|
|
//import "fmt"
|
2017-06-05 11:57:27 +00:00
|
|
|
import "strings"
|
|
|
|
import "errors"
|
|
|
|
|
|
|
|
func init() {
|
2017-06-13 07:12:58 +00:00
|
|
|
DB_Registry = append(DB_Registry,
|
|
|
|
&Mysql_Adapter{Name:"mysql",Buffer:make(map[string]string)},
|
|
|
|
)
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mysql_Adapter struct
|
|
|
|
{
|
|
|
|
Name string
|
2017-06-13 07:12:58 +00:00
|
|
|
Buffer map[string]string
|
|
|
|
BufferOrder []string // Map iteration order is random, so we need this to track the order, so we don't get huge diffs every commit
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 07:12:58 +00:00
|
|
|
func (adapter *Mysql_Adapter) GetName() string {
|
2017-06-05 11:57:27 +00:00
|
|
|
return adapter.Name
|
|
|
|
}
|
|
|
|
|
2017-06-13 07:12:58 +00:00
|
|
|
func (adapter *Mysql_Adapter) GetStmt(name string) string {
|
|
|
|
return adapter.Buffer[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *Mysql_Adapter) GetStmts() map[string]string {
|
|
|
|
return adapter.Buffer
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
|
2017-06-05 11:57:27 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if table == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No columns found for SimpleInsert")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if len(fields) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No input data found for SimpleInsert")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 10:07:40 +00:00
|
|
|
var querystr string = "INSERT INTO `" + table + "`("
|
2017-06-05 11:57:27 +00:00
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2017-06-06 14:41:06 +00:00
|
|
|
for _, column := range _process_columns(columns) {
|
|
|
|
if column.Type == "function" {
|
|
|
|
querystr += column.Left + ","
|
|
|
|
} else {
|
|
|
|
querystr += "`" + column.Left + "`,"
|
|
|
|
}
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
|
|
|
|
|
|
|
querystr += ") VALUES ("
|
2017-06-06 14:41:06 +00:00
|
|
|
for _, field := range _process_fields(fields) {
|
|
|
|
querystr += field.Name + ","
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
|
|
|
|
2017-06-13 07:12:58 +00:00
|
|
|
adapter.push_statement(name,querystr + ")")
|
2017-06-13 08:56:48 +00:00
|
|
|
return querystr + ")", nil
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleReplace(name string, table string, columns string, fields string) (string, error) {
|
2017-06-10 07:58:15 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|
|
|
|
if table == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No columns found for SimpleInsert")
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|
|
|
|
if len(fields) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No input data found for SimpleInsert")
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var querystr string = "REPLACE INTO `" + table + "`("
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
for _, column := range _process_columns(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 _process_fields(fields) {
|
|
|
|
querystr += field.Name + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
|
|
|
|
2017-06-13 07:12:58 +00:00
|
|
|
adapter.push_statement(name,querystr + ")")
|
2017-06-13 08:56:48 +00:00
|
|
|
return querystr + ")", nil
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleUpdate(name string, table string, set string, where string) (string, error) {
|
2017-06-07 10:07:40 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-07 10:07:40 +00:00
|
|
|
}
|
|
|
|
if table == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
2017-06-07 10:07:40 +00:00
|
|
|
}
|
|
|
|
if set == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need to set data in this update statement")
|
2017-06-07 10:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var querystr string = "UPDATE `" + table + "` SET "
|
|
|
|
for _, item := range _process_set(set) {
|
|
|
|
querystr += "`" + item.Column + "` ="
|
|
|
|
for _, token := range item.Expr {
|
|
|
|
switch(token.Type) {
|
|
|
|
case "function","operator","number","substitute":
|
|
|
|
querystr += " " + token.Contents + ""
|
|
|
|
case "column":
|
|
|
|
querystr += " `" + token.Contents + "`"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += ","
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
|
|
|
|
|
|
|
if len(where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range _process_where(where) {
|
|
|
|
var left, right string
|
|
|
|
|
|
|
|
if loc.LeftType == "column" {
|
|
|
|
left = "`" + loc.LeftColumn + "`"
|
|
|
|
} else {
|
|
|
|
left = loc.LeftColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
if loc.RightType == "column" {
|
|
|
|
right = "`" + loc.RightColumn + "`"
|
|
|
|
} else {
|
|
|
|
right = loc.RightColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr += " " + left + " " + loc.Operator + " " + right + " AND "
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr) - 4]
|
|
|
|
}
|
|
|
|
|
2017-06-13 07:12:58 +00:00
|
|
|
adapter.push_statement(name,querystr)
|
2017-06-13 08:56:48 +00:00
|
|
|
return querystr, nil
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleDelete(name string, table string, where string) (string, error) {
|
2017-06-12 09:03:14 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
if table == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
if where == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need to specify what data you want to delete")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var querystr string = "DELETE FROM `" + table + "` WHERE"
|
|
|
|
for _, loc := range _process_where(where) {
|
|
|
|
var left, right string
|
|
|
|
|
|
|
|
if loc.LeftType == "column" {
|
|
|
|
left = "`" + loc.LeftColumn + "`"
|
|
|
|
} else {
|
|
|
|
left = loc.LeftColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
if loc.RightType == "column" {
|
|
|
|
right = "`" + loc.RightColumn + "`"
|
|
|
|
} else {
|
|
|
|
right = loc.RightColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr += " " + left + " " + loc.Operator + " " + right + " AND "
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
querystr = strings.TrimSpace(querystr[0:len(querystr) - 4])
|
|
|
|
adapter.push_statement(name,querystr)
|
|
|
|
return querystr, nil
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to accidentally wipe tables, so we'll have a seperate method for purging tables instead
|
2017-06-13 08:56:48 +00:00
|
|
|
func (adapter *Mysql_Adapter) Purge(name string, table string) (string, error) {
|
2017-06-12 09:03:14 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
if table == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
2017-06-13 07:12:58 +00:00
|
|
|
adapter.push_statement(name,"DELETE FROM `" + table + "`")
|
2017-06-13 08:56:48 +00:00
|
|
|
return "DELETE FROM `" + table + "`", nil
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleSelect(name string, table string, columns string, where string, orderby string, limit string) (string, error) {
|
2017-06-05 11:57:27 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if table == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No columns found for SimpleSelect")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Slice up the user friendly strings into something easier to process
|
|
|
|
var colslice []string = strings.Split(strings.TrimSpace(columns),",")
|
|
|
|
|
|
|
|
var querystr string = "SELECT "
|
|
|
|
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
|
|
|
for _, column := range colslice {
|
|
|
|
querystr += "`" + strings.TrimSpace(column) + "`,"
|
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
|
|
|
|
2017-06-07 10:07:40 +00:00
|
|
|
querystr += " FROM `" + table + "`"
|
2017-06-05 11:57:27 +00:00
|
|
|
if len(where) != 0 {
|
2017-06-07 10:07:40 +00:00
|
|
|
querystr += " WHERE"
|
2017-06-05 11:57:27 +00:00
|
|
|
for _, loc := range _process_where(where) {
|
2017-06-06 14:41:06 +00:00
|
|
|
var left, right string
|
|
|
|
|
2017-06-05 11:57:27 +00:00
|
|
|
if loc.LeftType == "column" {
|
2017-06-06 14:41:06 +00:00
|
|
|
left = "`" + loc.LeftColumn + "`"
|
|
|
|
} else {
|
|
|
|
left = loc.LeftColumn
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2017-06-05 11:57:27 +00:00
|
|
|
if loc.RightType == "column" {
|
2017-06-06 14:41:06 +00:00
|
|
|
right = "`" + loc.RightColumn + "`"
|
|
|
|
} else {
|
|
|
|
right = loc.RightColumn
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
|
|
|
querystr += " " + left + " " + loc.Operator + " " + right + " AND "
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 08:47:33 +00:00
|
|
|
querystr = querystr[0:len(querystr) - 4]
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
2017-06-06 08:47:33 +00:00
|
|
|
for _, column := range _process_orderby(orderby) {
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 08:47:33 +00:00
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
if limit != "" {
|
|
|
|
querystr += " LIMIT " + limit
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
querystr = strings.TrimSpace(querystr)
|
|
|
|
adapter.push_statement(name,querystr)
|
|
|
|
return querystr, nil
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleLeftJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (string, error) {
|
2017-06-05 11:57:27 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if table1 == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for the left table")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if table2 == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for the right table")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No columns found for SimpleLeftJoin")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
if len(joiners) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No joiners found for SimpleLeftJoin")
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var querystr string = "SELECT "
|
|
|
|
|
2017-06-06 08:47:33 +00:00
|
|
|
for _, column := range _process_columns(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 + ","
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the trailing comma
|
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
|
|
|
|
2017-06-07 10:07:40 +00:00
|
|
|
querystr += " FROM `" + table1 + "` LEFT JOIN `" + table2 + "` ON "
|
2017-06-06 08:47:33 +00:00
|
|
|
for _, joiner := range _process_joiner(joiners) {
|
2017-06-12 09:03:14 +00:00
|
|
|
querystr += "`" + joiner.LeftTable + "`.`" + joiner.LeftColumn + "` " + joiner.Operator + " `" + joiner.RightTable + "`.`" + joiner.RightColumn + "` AND "
|
|
|
|
}
|
|
|
|
// Remove the trailing AND
|
|
|
|
querystr = querystr[0:len(querystr) - 4]
|
|
|
|
|
|
|
|
if len(where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range _process_where(where) {
|
|
|
|
var left, right string
|
|
|
|
|
|
|
|
if loc.LeftTable != "" {
|
|
|
|
left = "`" + loc.LeftTable + "`.`" + loc.LeftColumn + "`"
|
|
|
|
} else if loc.LeftType == "column" {
|
|
|
|
left = "`" + loc.LeftColumn + "`"
|
|
|
|
} else {
|
|
|
|
left = loc.LeftColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
if loc.RightTable != "" {
|
|
|
|
right = "`" + loc.RightTable + "`.`" + loc.RightColumn + "`"
|
|
|
|
} else if loc.RightType == "column" {
|
|
|
|
right = "`" + loc.RightColumn + "`"
|
|
|
|
} else {
|
|
|
|
right = loc.RightColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr += " " + left + " " + loc.Operator + " " + right + " AND "
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr) - 4]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
|
|
|
for _, column := range _process_orderby(orderby) {
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
if limit != "" {
|
|
|
|
querystr += " LIMIT " + limit
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
querystr = strings.TrimSpace(querystr)
|
|
|
|
adapter.push_statement(name,querystr)
|
|
|
|
return querystr, nil
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleInnerJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (string, error) {
|
2017-06-12 09:03:14 +00:00
|
|
|
if name == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for this statement")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
if table1 == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for the left table")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
if table2 == "" {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("You need a name for the right table")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
if len(columns) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No columns found for SimpleInnerJoin")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
if len(joiners) == 0 {
|
2017-06-13 08:56:48 +00:00
|
|
|
return "", errors.New("No joiners found for SimpleInnerJoin")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var querystr string = "SELECT "
|
|
|
|
|
|
|
|
for _, column := range _process_columns(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 _process_joiner(joiners) {
|
|
|
|
querystr += "`" + joiner.LeftTable + "`.`" + joiner.LeftColumn + "` " + joiner.Operator + " `" + joiner.RightTable + "`.`" + joiner.RightColumn + "` AND "
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
// Remove the trailing AND
|
|
|
|
querystr = querystr[0:len(querystr) - 4]
|
|
|
|
|
|
|
|
if len(where) != 0 {
|
2017-06-07 10:07:40 +00:00
|
|
|
querystr += " WHERE"
|
2017-06-05 11:57:27 +00:00
|
|
|
for _, loc := range _process_where(where) {
|
2017-06-06 14:41:06 +00:00
|
|
|
var left, right string
|
|
|
|
|
|
|
|
if loc.LeftTable != "" {
|
|
|
|
left = "`" + loc.LeftTable + "`.`" + loc.LeftColumn + "`"
|
|
|
|
} else if loc.LeftType == "column" {
|
|
|
|
left = "`" + loc.LeftColumn + "`"
|
|
|
|
} else {
|
|
|
|
left = loc.LeftColumn
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
|
|
|
if loc.RightTable != "" {
|
|
|
|
right = "`" + loc.RightTable + "`.`" + loc.RightColumn + "`"
|
|
|
|
} else if loc.RightType == "column" {
|
|
|
|
right = "`" + loc.RightColumn + "`"
|
|
|
|
} else {
|
|
|
|
right = loc.RightColumn
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
|
|
|
querystr += " " + left + " " + loc.Operator + " " + right + " AND "
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
querystr = querystr[0:len(querystr) - 4]
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(orderby) != 0 {
|
|
|
|
querystr += " ORDER BY "
|
2017-06-06 08:47:33 +00:00
|
|
|
for _, column := range _process_orderby(orderby) {
|
|
|
|
querystr += column.Column + " " + strings.ToUpper(column.Order) + ","
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
2017-06-06 08:47:33 +00:00
|
|
|
querystr = querystr[0:len(querystr) - 1]
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
if limit != "" {
|
|
|
|
querystr += " LIMIT " + limit
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
querystr = strings.TrimSpace(querystr)
|
|
|
|
adapter.push_statement(name,querystr)
|
2017-06-14 07:09:44 +00:00
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (adapter *Mysql_Adapter) SimpleCount(name string, table string, where string, limit string) (string, error) {
|
2017-06-14 07:09:44 +00:00
|
|
|
if name == "" {
|
|
|
|
return "", errors.New("You need a name for this statement")
|
|
|
|
}
|
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var querystr string = "SELECT COUNT(*) AS `count` FROM `" + table + "`"
|
|
|
|
if len(where) != 0 {
|
|
|
|
querystr += " WHERE"
|
|
|
|
for _, loc := range _process_where(where) {
|
|
|
|
var left, right string
|
|
|
|
|
|
|
|
if loc.LeftType == "column" {
|
|
|
|
left = "`" + loc.LeftColumn + "`"
|
|
|
|
} else {
|
|
|
|
left = loc.LeftColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
if loc.RightType == "column" {
|
|
|
|
right = "`" + loc.RightColumn + "`"
|
|
|
|
} else {
|
|
|
|
right = loc.RightColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
querystr += " " + left + " " + loc.Operator + " " + right + " AND "
|
|
|
|
}
|
|
|
|
querystr = querystr[0:len(querystr) - 4]
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
if limit != "" {
|
|
|
|
querystr += " LIMIT " + limit
|
|
|
|
}
|
|
|
|
|
2017-06-14 07:09:44 +00:00
|
|
|
querystr = strings.TrimSpace(querystr)
|
|
|
|
adapter.push_statement(name,querystr)
|
2017-06-13 08:56:48 +00:00
|
|
|
return querystr, nil
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 07:12:58 +00:00
|
|
|
func (adapter *Mysql_Adapter) Write() error {
|
|
|
|
var stmts, body string
|
|
|
|
|
|
|
|
for _, name := range adapter.BufferOrder {
|
|
|
|
stmts += "var " + name + "_stmt *sql.Stmt\n"
|
|
|
|
body += `
|
|
|
|
log.Print("Preparing ` + name + ` statement.")
|
|
|
|
` + name + `_stmt, err = db.Prepare("` + adapter.Buffer[name] + `")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2017-06-14 09:55:47 +00:00
|
|
|
out := `// Code generated by Gosora. More below:
|
|
|
|
/* This file was generated by Gosora's Query Generator. Please try to avoid modifying this file, as it might change at any time. */
|
2017-06-06 14:41:06 +00:00
|
|
|
// +build !pgsql !sqlite !mssql
|
2017-06-05 11:57:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "database/sql"
|
|
|
|
|
2017-06-13 07:12:58 +00:00
|
|
|
` + stmts + `
|
2017-06-05 11:57:27 +00:00
|
|
|
func gen_mysql() (err error) {
|
|
|
|
if debug {
|
|
|
|
log.Print("Building the generated statements")
|
|
|
|
}
|
2017-06-13 07:12:58 +00:00
|
|
|
` + body + `
|
2017-06-05 11:57:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
`
|
|
|
|
return write_file("./gen_mysql.go", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal method, not exposed in the interface
|
2017-06-13 07:12:58 +00:00
|
|
|
func (adapter *Mysql_Adapter) push_statement(name string, querystr string ) {
|
|
|
|
adapter.Buffer[name] = querystr
|
|
|
|
adapter.BufferOrder = append(adapter.BufferOrder,name)
|
2017-06-05 11:57:27 +00:00
|
|
|
}
|