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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) GetName() string {
|
|
|
|
return a.Name
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) GetStmt(name string) DBStmt {
|
|
|
|
return a.Buffer[name]
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) GetStmts() map[string]DBStmt {
|
|
|
|
return a.Buffer
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 05:25:27 +00:00
|
|
|
// TODO: Implement this
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) {
|
2018-03-31 05:25:27 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) DbVersion() string {
|
2018-05-11 05:41:51 +00:00
|
|
|
return "SELECT version()"
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) DropTable(name string, table string) (string, error) {
|
2018-05-11 05:41:51 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "DROP TABLE IF EXISTS \"" + table + "\";"
|
|
|
|
a.pushStatement(name, "drop-table", q)
|
|
|
|
return q, nil
|
2018-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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "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 "
|
2019-10-31 07:25:56 +00:00
|
|
|
if a.stringyType(column.Type) && column.Default != "''" {
|
2017-07-12 11:05:18 +00:00
|
|
|
end += "'" + column.Default + "'"
|
|
|
|
} else {
|
|
|
|
end += column.Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !column.Null {
|
|
|
|
end += " not null"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "\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 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "\n\t" + key.Type
|
2017-07-12 11:05:18 +00:00
|
|
|
if key.Type != "unique" {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " key"
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "("
|
2017-09-03 04:50:31 +00:00
|
|
|
for _, column := range strings.Split(key.Columns, ",") {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += "`" + column + "`,"
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + "),"
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0:len(q)-1] + "\n);"
|
|
|
|
a.pushStatement(name, "create-table", q)
|
|
|
|
return q, nil
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) AddIndex(name string, table string, iname string, colname string) (string, error) {
|
2018-12-31 09:03:49 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if iname == "" {
|
|
|
|
return "", errors.New("You need a name for the index")
|
|
|
|
}
|
|
|
|
if colname == "" {
|
|
|
|
return "", errors.New("You need a name for the column")
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:29:19 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
// TODO: Test to make sure everything works here
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) AddKey(name string, table string, column string, key DBTableKey) (string, error) {
|
2019-02-23 06:29:19 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if column == "" {
|
|
|
|
return "", errors.New("You need a name for the column")
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-05-06 04:04:00 +00:00
|
|
|
// TODO: Implement this
|
|
|
|
// TODO: Test to make sure everything works here
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) AddForeignKey(name string, table string, column string, ftable string, fcolumn string, cascade bool) (out string, e error) {
|
2019-05-06 04:04:00 +00:00
|
|
|
var c = func(str string, val bool) {
|
|
|
|
if e != nil || !val {
|
|
|
|
return
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
e = errors.New("You need a " + str + " for this table")
|
2019-05-06 04:04:00 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
c("name", table == "")
|
|
|
|
c("column", column == "")
|
|
|
|
c("ftable", ftable == "")
|
|
|
|
c("fcolumn", fcolumn == "")
|
2019-05-06 04:04:00 +00:00
|
|
|
if e != nil {
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
|
2018-05-27 09:36:35 +00:00
|
|
|
if table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "INSERT INTO \"" + table + "\"("
|
2018-05-27 09:36:35 +00:00
|
|
|
if columns != "" {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += a.buildColumns(columns) + ") VALUES ("
|
2018-05-27 09:36:35 +00:00
|
|
|
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) + "'"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += field.Name + ","
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2018-05-27 09:36:35 +00:00
|
|
|
} else {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += ") VALUES ("
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += ")"
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(name, "insert", q)
|
|
|
|
return q, nil
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
func (a *PgsqlAdapter) buildColumns(cols string) (q string) {
|
|
|
|
if cols == "" {
|
2018-05-27 09:36:35 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// Escape the column names, just in case we've used a reserved keyword
|
2019-12-31 21:57:54 +00:00
|
|
|
for _, col := range processColumns(cols) {
|
|
|
|
if col.Type == TokenFunc {
|
|
|
|
q += col.Left + ","
|
2018-05-27 09:36:35 +00:00
|
|
|
} else {
|
2019-12-31 21:57:54 +00:00
|
|
|
q += "\"" + col.Left + "\","
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
return q[0 : len(q)-1]
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2019-12-31 21:57:54 +00:00
|
|
|
func (a *PgsqlAdapter) SimpleReplace(name, table, columns, 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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) SimpleUpdate(up *updatePrebuilder) (string, error) {
|
2018-12-27 05:42:41 +00:00
|
|
|
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
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
q := "UPDATE \"" + up.table + "\" SET "
|
2018-12-27 05:42:41 +00:00
|
|
|
for _, item := range processSet(up.set) {
|
2019-12-31 21:57:54 +00:00
|
|
|
q += "`" + item.Column + "`="
|
2017-07-12 11:05:18 +00:00
|
|
|
for _, token := range item.Expr {
|
2017-09-03 04:50:31 +00:00
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenFunc:
|
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()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenOp, TokenNumber, TokenSub, TokenOr:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " `" + token.Contents + "`"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += ","
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-1]
|
2017-09-03 04:50:31 +00:00
|
|
|
|
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 {
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " WHERE"
|
2018-12-27 05:42:41 +00:00
|
|
|
for _, loc := range processWhere(up.where) {
|
2017-07-12 11:05:18 +00:00
|
|
|
for _, token := range loc.Expr {
|
2017-09-03 04:50:31 +00:00
|
|
|
switch token.Type {
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenFunc:
|
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()"
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenOp, TokenNumber, TokenSub, TokenOr, TokenNot, TokenLike:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " " + token.Contents
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenColumn:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " `" + token.Contents + "`"
|
2019-12-31 21:57:54 +00:00
|
|
|
case TokenString:
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " '" + token.Contents + "'"
|
2017-09-03 04:50:31 +00:00
|
|
|
default:
|
|
|
|
panic("This token doesn't exist o_o")
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q += " AND"
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
q = q[0 : len(q)-4]
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
a.pushStatement(up.name, "update", q)
|
|
|
|
return q, nil
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
// TODO: Implement this
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) SimpleUpdateSelect(up *updatePrebuilder) (string, error) {
|
2018-12-27 05:42:41 +00:00
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Implement this
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:57:10 +00:00
|
|
|
// TODO: Implement this
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) ComplexDelete(b *deletePrebuilder) (string, error) {
|
2019-06-05 04:57:10 +00:00
|
|
|
if b.table == "" {
|
|
|
|
return "", errors.New("You need a name for this table")
|
|
|
|
}
|
|
|
|
if b.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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) ComplexSelect(prebuilder *selectPrebuilder) (string, error) {
|
2018-01-03 07:46:18 +00:00
|
|
|
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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) Builder() *prebuilder {
|
|
|
|
return &prebuilder{a}
|
2017-11-12 11:29:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) Write() error {
|
2017-07-12 11:05:18 +00:00
|
|
|
var stmts, body string
|
2019-10-31 07:25:56 +00:00
|
|
|
for _, name := range a.BufferOrder {
|
2017-10-14 07:39:22 +00:00
|
|
|
if name[0] == '_' {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
stmt := a.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
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *PgsqlAdapter) pushStatement(name string, stype string, q string) {
|
2018-12-27 05:42:41 +00:00
|
|
|
if name == "" {
|
2018-02-10 15:07:21 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-31 07:25:56 +00:00
|
|
|
a.Buffer[name] = DBStmt{q, stype}
|
|
|
|
a.BufferOrder = append(a.BufferOrder, name)
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:25:56 +00:00
|
|
|
func (a *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"
|
|
|
|
}
|