gosora/query_gen/lib/pgsql.go

404 lines
11 KiB
Go
Raw Normal View History

Added support for HTTP/2 Push. The themes are still going to be a little broken for a while, but here's the progress I've made. Renamed the topics_trow_assign hook to topics_topic_row_assign. Added more debug code to the generated router. Added a robots.txt file. Gosora now responds to favicon.ico requests with a 404 rather than the topic list. Fixed the tests and some of the benchmarks. Changed the default UserCacheCapacity from 100 to 120. Changed the default TopicCacheCapacity from 100 to 200. Added the last replyer to the topics list and the forum pages. Added the BulkCascadeGetMap method to the UserStore. Refactored the topics list and forum page to load the users with a call to the UserStore rather than via a join. WebSockets now work on SSL. Fixed a race condition when the user permissions are initialised at the start of a request. Fixed a race condition when the user permissions for the OP of a topic are initialised. The rows.Close() calls are deferred once more, so that we can catch problems with recover() Improved support for struct pointers in the template compiler. Added a pin emoji to pinned topics to make them stand-out on the Shadow theme, we have some other ideas in mind for this, but I'd like to get Shadow fully functional for this commit. Fixed a bug an issue with Chrome not detecting XHTML style closes on <form>s. Fixed a visual issue with `color` not being set for textarea elements for the Shadow theme. Fixed a function which wasn't getting renamed for PGSQL. Added seven new UserStore tests.
2017-08-06 15:22:18 +00:00
/* WIP Under Really Heavy Construction */
2017-07-12 11:05:18 +00:00
package qgen
import "strings"
import "strconv"
import "errors"
func init() {
Registry = append(Registry,
&PgsqlAdapter{Name: "pgsql", Buffer: make(map[string]DBStmt)},
2017-07-12 11:05:18 +00:00
)
}
type PgsqlAdapter struct {
Name string // ? - Do we really need this? Can't we hard-code this?
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
}
// GetName gives you the name of the database adapter. In this case, it's pgsql
func (adapter *PgsqlAdapter) GetName() string {
2017-07-12 11:05:18 +00:00
return adapter.Name
}
func (adapter *PgsqlAdapter) GetStmt(name string) DBStmt {
2017-07-12 11:05:18 +00:00
return adapter.Buffer[name]
}
func (adapter *PgsqlAdapter) GetStmts() map[string]DBStmt {
2017-07-12 11:05:18 +00:00
return adapter.Buffer
}
// 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
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 name == "" {
return "", errors.New("You need a name for this statement")
}
if table == "" {
return "", errors.New("You need a name for this table")
}
if len(columns) == 0 {
return "", errors.New("You can't have a table with no columns")
}
var querystr = "CREATE TABLE `" + table + "` ("
2017-07-12 11:05:18 +00:00
for _, column := range columns {
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-07-12 11:05:18 +00:00
var size string
if column.Size > 0 {
size = " (" + strconv.Itoa(column.Size) + ")"
}
2017-07-12 11:05:18 +00:00
var end string
if column.Default != "" {
end = " DEFAULT "
if adapter.stringyType(column.Type) && column.Default != "''" {
2017-07-12 11:05:18 +00:00
end += "'" + column.Default + "'"
} else {
end += column.Default
}
}
2017-07-12 11:05:18 +00:00
if !column.Null {
end += " not null"
}
querystr += "\n\t`" + column.Name + "` " + column.Type + size + end + ","
2017-07-12 11:05:18 +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 += "("
for _, column := range strings.Split(key.Columns, ",") {
2017-07-12 11:05:18 +00:00
querystr += "`" + column + "`,"
}
querystr = querystr[0:len(querystr)-1] + "),"
2017-07-12 11:05:18 +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
}
// TODO: Implement this
func (adapter *PgsqlAdapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
2017-07-12 11:05:18 +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")
}
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
func (adapter *PgsqlAdapter) SimpleReplace(name string, table string, columns string, fields string) (string, error) {
2017-07-12 11:05:18 +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")
}
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
func (adapter *PgsqlAdapter) SimpleUpsert(name string, table string, columns string, fields string, where string) (string, error) {
if name == "" {
2017-07-12 11:05:18 +00:00
return "", errors.New("You need a name for this statement")
}
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: Implemented, but we need CreateTable and a better installer to *test* it
func (adapter *PgsqlAdapter) SimpleUpdate(name string, table string, set string, where string) (string, error) {
2017-07-12 11:05:18 +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")
}
if set == "" {
return "", errors.New("You need to set data in this update statement")
}
var querystr = "UPDATE `" + table + "` SET "
for _, item := range processSet(set) {
2017-07-12 11:05:18 +00:00
querystr += "`" + item.Column + "` ="
for _, token := range item.Expr {
switch token.Type {
case "function":
// TODO: Write a more sophisticated function parser on the utils side.
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
token.Contents = "LOCALTIMESTAMP()"
}
querystr += " " + token.Contents
case "operator", "number", "substitute":
querystr += " " + token.Contents
case "column":
querystr += " `" + token.Contents + "`"
case "string":
querystr += " '" + token.Contents + "'"
2017-07-12 11:05:18 +00:00
}
}
querystr += ","
}
2017-07-12 11:05:18 +00:00
// Remove the trailing comma
querystr = querystr[0 : len(querystr)-1]
2017-07-12 11:05:18 +00:00
// Add support for BETWEEN x.x
if len(where) != 0 {
querystr += " WHERE"
for _, loc := range processWhere(where) {
2017-07-12 11:05:18 +00:00
for _, token := range loc.Expr {
switch token.Type {
case "function":
// TODO: Write a more sophisticated function parser on the utils side. What's the situation in regards to case sensitivity?
if strings.ToUpper(token.Contents) == "UTC_TIMESTAMP()" {
token.Contents = "LOCALTIMESTAMP()"
}
querystr += " " + token.Contents
case "operator", "number", "substitute":
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"
}
querystr = querystr[0 : len(querystr)-4]
2017-07-12 11:05:18 +00:00
}
adapter.pushStatement(name, "update", querystr)
2017-07-12 11:05:18 +00:00
return querystr, nil
}
// TODO: Implement this
func (adapter *PgsqlAdapter) SimpleDelete(name string, table string, where string) (string, error) {
2017-07-12 11:05:18 +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")
}
if where == "" {
return "", errors.New("You need to specify what data you want to delete")
}
return "", nil
}
// TODO: Implement this
// 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 name == "" {
return "", errors.New("You need a name for this statement")
}
if table == "" {
return "", errors.New("You need a name for this table")
}
return "", nil
}
// TODO: Implement this
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 name == "" {
return "", errors.New("You need a name for this statement")
}
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
}
// TODO: Implement this
func (adapter *PgsqlAdapter) ComplexSelect(prebuilder *selectPrebuilder) (string, error) {
if prebuilder.name == "" {
return "", errors.New("You need a name for this statement")
}
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
}
// TODO: Implement this
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 name == "" {
return "", errors.New("You need a name for this statement")
}
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
}
// TODO: Implement this
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 name == "" {
return "", errors.New("You need a name for this statement")
}
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
}
// TODO: Implement this
func (adapter *PgsqlAdapter) SimpleInsertSelect(name string, ins DBInsert, sel DBSelect) (string, error) {
2017-07-12 11:05:18 +00:00
return "", nil
}
// TODO: Implement this
func (adapter *PgsqlAdapter) SimpleInsertLeftJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
2017-07-12 11:05:18 +00:00
return "", nil
}
// TODO: Implement this
func (adapter *PgsqlAdapter) SimpleInsertInnerJoin(name string, ins DBInsert, sel DBJoin) (string, error) {
2017-07-12 11:05:18 +00:00
return "", nil
}
// TODO: Implement this
func (adapter *PgsqlAdapter) SimpleCount(name string, table string, where string, limit string) (string, error) {
2017-07-12 11:05:18 +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")
}
return "", nil
}
2017-11-13 00:31:46 +00:00
func (adapter *PgsqlAdapter) Builder() *prebuilder {
return &prebuilder{adapter}
}
func (adapter *PgsqlAdapter) Write() error {
2017-07-12 11:05:18 +00:00
var stmts, body string
for _, name := range adapter.BufferOrder {
if name[0] == '_' {
continue
}
2017-07-12 11:05:18 +00:00
stmt := adapter.Buffer[name]
// TODO: Add support for create-table? Table creation might be a little complex for Go to do outside a SQL file :(
2017-07-12 11:05:18 +00:00
if stmt.Type != "create-table" {
stmts += "\t" + name + " *sql.Stmt\n"
2017-07-12 11:05:18 +00:00
body += `
log.Print("Preparing ` + name + ` statement.")
stmts.` + name + `, err = db.Prepare("` + stmt.Contents + `")
2017-07-12 11:05:18 +00:00
if err != nil {
return err
}
`
}
}
// 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"
import "./common"
2017-07-12 11:05:18 +00:00
// nolint
type Stmts struct {
2017-07-12 11:05:18 +00:00
` + stmts + `
getActivityFeedByWatcher *sql.Stmt
getActivityCountByWatcher *sql.Stmt
todaysPostCount *sql.Stmt
todaysTopicCount *sql.Stmt
todaysReportCount *sql.Stmt
todaysNewUserCount *sql.Stmt
Mocks bool
}
// nolint
2017-07-12 11:05:18 +00:00
func _gen_pgsql() (err error) {
if common.Dev.DebugMode {
2017-07-12 11:05:18 +00:00
log.Print("Building the generated statements")
}
` + body + `
return nil
}
`
return writeFile("./gen_pgsql.go", out)
2017-07-12 11:05:18 +00:00
}
// Internal methods, not exposed in the interface
func (adapter *PgsqlAdapter) pushStatement(name string, stype string, querystr string) {
adapter.Buffer[name] = DBStmt{querystr, stype}
adapter.BufferOrder = append(adapter.BufferOrder, name)
2017-07-12 11:05:18 +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"
}