2017-08-06 15:22:18 +00:00
|
|
|
/* WIP Under Really Heavy Construction */
|
2017-07-12 11:05:18 +00:00
|
|
|
package qgen
|
|
|
|
|
2018-03-31 05:25:27 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
2017-07-12 11:05:18 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-11-12 05:25:04 +00:00
|
|
|
Registry = append(Registry,
|
|
|
|
&PgsqlAdapter{Name: "pgsql", Buffer: make(map[string]DBStmt)},
|
2017-07-12 11:05:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
type PgsqlAdapter struct {
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +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-07-12 11:05:18 +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
|
|
|
|
}
|
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
// GetName gives you the name of the database adapter. In this case, it's pgsql
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) GetName() string {
|
2017-07-12 11:05:18 +00:00
|
|
|
return adapter.Name
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *PgsqlAdapter) GetStmt(name string) DBStmt {
|
2017-07-12 11:05:18 +00:00
|
|
|
return adapter.Buffer[name]
|
|
|
|
}
|
|
|
|
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *PgsqlAdapter) GetStmts() map[string]DBStmt {
|
2017-07-12 11:05:18 +00:00
|
|
|
return adapter.Buffer
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:25:27 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
func (adapter *PgsqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *PgsqlAdapter) DbVersion() string {
|
2018-05-11 05:41:51 +00:00
|
|
|
return "SELECT version()"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *PgsqlAdapter) 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
|
2018-03-31 05:25:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-07-12 11:05:18 +00:00
|
|
|
// We may need to change the CreateTable API to better suit PGSQL and the other database drivers which are coming up
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *PgsqlAdapter) CreateTable(name string, table string, charset string, collation string, columns []DBTableColumn, keys []DBTableKey) (string, error) {
|
2017-07-12 11:05:18 +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")
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2018-08-15 07:02:57 +00:00
|
|
|
var querystr = "CREATE TABLE \"" + table + "\" ("
|
2017-07-12 11:05:18 +00:00
|
|
|
for _, column := range columns {
|
2017-11-12 05:25:04 +00:00
|
|
|
if column.AutoIncrement {
|
2017-07-12 11:05:18 +00:00
|
|
|
column.Type = "serial"
|
|
|
|
} else if column.Type == "createdAt" {
|
|
|
|
column.Type = "timestamp"
|
|
|
|
} else if column.Type == "datetime" {
|
|
|
|
column.Type = "timestamp"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
var size string
|
|
|
|
if column.Size > 0 {
|
|
|
|
size = " (" + strconv.Itoa(column.Size) + ")"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
var end string
|
|
|
|
if column.Default != "" {
|
|
|
|
end = " DEFAULT "
|
2017-09-03 04:50:31 +00:00
|
|
|
if adapter.stringyType(column.Type) && column.Default != "''" {
|
2017-07-12 11:05:18 +00:00
|
|
|
end += "'" + column.Default + "'"
|
|
|
|
} else {
|
|
|
|
end += column.Default
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
if !column.Null {
|
|
|
|
end += " not null"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
|
|
|
querystr += "\n\t`" + column.Name + "` " + column.Type + size + end + ","
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
if len(keys) > 0 {
|
|
|
|
for _, key := range keys {
|
|
|
|
querystr += "\n\t" + key.Type
|
|
|
|
if key.Type != "unique" {
|
|
|
|
querystr += " key"
|
|
|
|
}
|
|
|
|
querystr += "("
|
2017-09-03 04:50:31 +00:00
|
|
|
for _, column := range strings.Split(key.Columns, ",") {
|
2017-07-12 11:05:18 +00:00
|
|
|
querystr += "`" + column + "`,"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
querystr = querystr[0:len(querystr)-1] + "),"
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
|
|
|
querystr = querystr[0:len(querystr)-1] + "\n);"
|
|
|
|
adapter.pushStatement(name, "create-table", querystr)
|
2017-07-12 11:05:18 +00:00
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2019-02-23 06:29:19 +00:00
|
|
|
func (adapter *PgsqlAdapter) AddColumn(name string, table string, column DBTableColumn, key *DBTableKey) (string, error) {
|
2017-07-12 11:05:18 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-12-31 09:03:49 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
// TODO: Test to make sure everything works here
|
|
|
|
func (adapter *PgsqlAdapter) 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 *PgsqlAdapter) 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 *PgsqlAdapter) 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")
|
|
|
|
}
|
|
|
|
|
2018-05-27 09:36:35 +00:00
|
|
|
// TODO: Test this
|
|
|
|
// ! We need to get the last ID out of this somehow, maybe add returning to every query? Might require some sort of wrapper over the sql statements
|
|
|
|
func (adapter *PgsqlAdapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
|
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var querystr = "INSERT INTO \"" + table + "\"("
|
|
|
|
if columns != "" {
|
|
|
|
querystr += adapter.buildColumns(columns) + ") VALUES ("
|
|
|
|
for _, field := range processFields(fields) {
|
|
|
|
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]
|
|
|
|
} else {
|
|
|
|
querystr += ") VALUES ("
|
|
|
|
}
|
|
|
|
querystr += ")"
|
|
|
|
|
|
|
|
adapter.pushStatement(name, "insert", querystr)
|
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *PgsqlAdapter) buildColumns(columns string) (querystr string) {
|
|
|
|
if columns == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// 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 + "\","
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return querystr[0 : len(querystr)-1]
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleReplace(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")
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement this
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleUpsert(name string, table string, columns string, fields string, where string) (string, error) {
|
2017-07-12 11:05:18 +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")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implemented, but we need CreateTable and a better installer to *test* it
|
2018-12-27 05:42:41 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error) {
|
|
|
|
if up.table == "" {
|
2017-07-12 11:05:18 +00:00
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
2018-12-27 05:42:41 +00:00
|
|
|
if up.set == "" {
|
2017-07-12 11:05:18 +00:00
|
|
|
return "", errors.New("You need to set data in this update statement")
|
|
|
|
}
|
2018-08-15 07:02:57 +00:00
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
var querystr = "UPDATE \"" + up.table + "\" SET "
|
|
|
|
for _, item := range processSet(up.set) {
|
2017-07-12 11:05:18 +00:00
|
|
|
querystr += "`" + item.Column + "` ="
|
|
|
|
for _, token := range item.Expr {
|
2017-09-03 04:50:31 +00:00
|
|
|
switch token.Type {
|
|
|
|
case "function":
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Write a more sophisticated function parser on the utils side.
|
2017-09-03 04:50:31 +00:00
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "LOCALTIMESTAMP()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
2018-03-31 05:25:27 +00:00
|
|
|
case "operator", "number", "substitute", "or":
|
2017-09-03 04:50:31 +00:00
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " `" + token.Contents + "`"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += ","
|
|
|
|
}
|
|
|
|
// Remove the trailing comma
|
2017-09-03 04:50:31 +00:00
|
|
|
querystr = querystr[0 : len(querystr)-1]
|
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
// Add support for BETWEEN x.x
|
2018-12-27 05:42:41 +00:00
|
|
|
if len(up.where) != 0 {
|
2017-07-12 11:05:18 +00:00
|
|
|
querystr += " WHERE"
|
2018-12-27 05:42:41 +00:00
|
|
|
for _, loc := range processWhere(up.where) {
|
2017-07-12 11:05:18 +00:00
|
|
|
for _, token := range loc.Expr {
|
2017-09-03 04:50:31 +00:00
|
|
|
switch token.Type {
|
|
|
|
case "function":
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Write a more sophisticated function parser on the utils side. What's the situation in regards to case sensitivity?
|
2017-09-03 04:50:31 +00:00
|
|
|
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
|
|
|
|
token.Contents = "LOCALTIMESTAMP()"
|
|
|
|
}
|
|
|
|
querystr += " " + token.Contents
|
2018-03-31 05:25:27 +00:00
|
|
|
case "operator", "number", "substitute", "or":
|
2017-09-03 04:50:31 +00:00
|
|
|
querystr += " " + token.Contents
|
|
|
|
case "column":
|
|
|
|
querystr += " `" + token.Contents + "`"
|
|
|
|
case "string":
|
|
|
|
querystr += " '" + token.Contents + "'"
|
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
querystr += " AND"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
querystr = querystr[0 : len(querystr)-4]
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
adapter.pushStatement(up.name, "update", querystr)
|
2017-07-12 11:05:18 +00:00
|
|
|
return querystr, nil
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
func (adapter *PgsqlAdapter) SimpleUpdateSelect(up *updatePrebuilder) (string, error) {
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleDelete(name string, table string, where string) (string, error) {
|
2017-07-12 11:05:18 +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")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
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 *PgsqlAdapter) Purge(name string, table string) (string, error) {
|
2017-07-12 11:05:18 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleSelect(name string, table string, columns string, where string, orderby string, limit string) (string, error) {
|
2017-07-12 11:05:18 +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")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:46:18 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
func (adapter *PgsqlAdapter) ComplexSelect(prebuilder *selectPrebuilder) (string, error) {
|
|
|
|
if prebuilder.table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if len(prebuilder.columns) == 0 {
|
|
|
|
return "", errors.New("No columns found for ComplexSelect")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleLeftJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (string, error) {
|
2017-07-12 11:05:18 +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")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleInnerJoin(name string, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (string, error) {
|
2017-07-12 11:05:18 +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")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleInsertSelect(name string, ins DBInsert, sel DBSelect) (string, error) {
|
2017-07-12 11:05:18 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleInsertLeftJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
|
2017-07-12 11:05:18 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-11-12 05:25:04 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleInsertInnerJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
|
2017-07-12 11:05:18 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) SimpleCount(name string, table string, where string, limit string) (string, error) {
|
2017-07-12 11:05:18 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 00:31:46 +00:00
|
|
|
func (adapter *PgsqlAdapter) Builder() *prebuilder {
|
|
|
|
return &prebuilder{adapter}
|
2017-11-12 11:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) Write() error {
|
2017-07-12 11:05:18 +00:00
|
|
|
var stmts, body string
|
|
|
|
for _, name := range adapter.BufferOrder {
|
2017-10-14 07:39:22 +00:00
|
|
|
if name[0] == '_' {
|
|
|
|
continue
|
|
|
|
}
|
2017-07-12 11:05:18 +00:00
|
|
|
stmt := adapter.Buffer[name]
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Add support for create-table? Table creation might be a little complex for Go to do outside a SQL file :(
|
2017-07-12 11:05:18 +00:00
|
|
|
if stmt.Type != "create-table" {
|
2017-11-05 09:55:34 +00:00
|
|
|
stmts += "\t" + name + " *sql.Stmt\n"
|
2017-07-12 11:05:18 +00:00
|
|
|
body += `
|
2018-02-26 09:07:00 +00:00
|
|
|
common.DebugLog("Preparing ` + name + ` statement.")
|
2018-08-15 07:02:57 +00:00
|
|
|
stmts.` + name + `, err = db.Prepare("` + strings.Replace(stmt.Contents, "\"", "\\\"", -1) + `")
|
2017-07-12 11:05:18 +00:00
|
|
|
if err != nil {
|
2018-02-26 09:07:00 +00:00
|
|
|
log.Print("Error in ` + name + ` statement.")
|
2017-07-12 11:05:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-11-05 09:55:34 +00:00
|
|
|
// TODO: Move these custom queries out of this file
|
2017-07-12 11:05:18 +00:00
|
|
|
out := `// +build pgsql
|
|
|
|
|
|
|
|
// 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-07-12 11:05:18 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
// nolint
|
2017-11-05 09:55:34 +00:00
|
|
|
type Stmts struct {
|
2017-07-12 11:05:18 +00:00
|
|
|
` + stmts + `
|
2017-11-05 09:55:34 +00:00
|
|
|
getActivityFeedByWatcher *sql.Stmt
|
|
|
|
getActivityCountByWatcher *sql.Stmt
|
|
|
|
|
|
|
|
Mocks bool
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
// nolint
|
2017-07-12 11:05:18 +00:00
|
|
|
func _gen_pgsql() (err error) {
|
2018-02-19 04:26:01 +00:00
|
|
|
common.DebugLog("Building the generated statements")
|
2017-07-12 11:05:18 +00:00
|
|
|
` + body + `
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
`
|
2017-09-03 04:50:31 +00:00
|
|
|
return writeFile("./gen_pgsql.go", out)
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Internal methods, not exposed in the interface
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) pushStatement(name string, stype string, querystr string) {
|
2018-12-27 05:42:41 +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-09-03 04:50:31 +00:00
|
|
|
adapter.BufferOrder = append(adapter.BufferOrder, name)
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 09:57:08 +00:00
|
|
|
func (adapter *PgsqlAdapter) stringyType(ctype string) bool {
|
2017-07-12 11:05:18 +00:00
|
|
|
ctype = strings.ToLower(ctype)
|
|
|
|
return ctype == "char" || ctype == "varchar" || ctype == "timestamp" || ctype == "text"
|
|
|
|
}
|