Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CockroachDB Support #359

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type Dialect interface {
IfSchemaNotExists(command, schema string) string
IfTableExists(command, schema, table string) string
IfTableNotExists(command, schema, table string) string

// The command to create a new database/schema
CreateSchemaCommand() string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this would force a major version bump, because custom dialects would no longer implement the updated Dialect type. Can we do this as an optional type that dialects are not required to implement (and then use the old logic as a fallback)? Maybe something like type NonstandardSchemaDialect interface { CreateSchemaCommand() string }?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent idea. I'd like to change it slightly, though, since even a NonstandardSchemaDialect could vary amongst custom implementations. Maybe a type SchemaCreator() interface { CreateSchemaCommand() string } instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think SchemaCreator is a little misleading, since all other dialects will still be able to create schemas. What we're providing is a way for dialects to inform gorp that they are unable to execute create schema operations per the SQL standard.

For those reasons, I still think something along the lines of "nonstandard" should be part of the name. It is accurate - these are dialects that cannot handle the SQL standard for create schema, so they should be labeled as such. NonstandardSchemaCreator sounds fine to me, too.

Also, we might want to pass the schema name in to CreateSchemaCommand so that dialects have more power over the resulting SQL command.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the sound of that. I'll get that implemented and try to sort out testing after the holidays 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zikes Hey, just checking in; have you had any more time to work on this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't, sorry. It's something I'd still be interested in implementing sometime, but it's been less of a priority for me lately.

}

// IntegerAutoIncrInserter is implemented by dialects that can perform
Expand Down
156 changes: 156 additions & 0 deletions dialect_cockroachdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

// Package gorp provides a simple way to marshal Go structs to and from
// SQL databases. It uses the database/sql package, and should work with any
// compliant database/sql driver.
//
// Source code and project home:
// https://github.com/go-gorp/gorp

package gorp

import (
"fmt"
"reflect"
"strings"
"time"
)

type CockroachdbDialect struct {
suffix string
}

func (d CockroachdbDialect) QuerySuffix() string { return ";" }

func (d CockroachdbDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string {
switch val.Kind() {
case reflect.Ptr:
return d.ToSqlType(val.Elem(), maxsize, isAutoIncr)
case reflect.Bool:
return "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
if isAutoIncr {
return "serial"
}
return "integer"
case reflect.Int64, reflect.Uint64:
if isAutoIncr {
return "bigserial"
}
return "bigint"
case reflect.Float64:
return "double precision"
case reflect.Float32:
return "real"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "bytea"
}
}

switch val.Name() {
case "NullInt64":
return "bigint"
case "NullFloat64":
return "double precision"
case "NullBool":
return "boolean"
case "Time", "NullTime":
return "timestamp with time zone"
}

if maxsize > 0 {
return fmt.Sprintf("varchar(%d)", maxsize)
} else {
return "text"
}

}

// Returns empty string
func (d CockroachdbDialect) AutoIncrStr() string {
return ""
}

func (d CockroachdbDialect) AutoIncrBindValue() string {
return "default"
}

func (d CockroachdbDialect) AutoIncrInsertSuffix(col *ColumnMap) string {
return " returning " + d.QuoteField(col.ColumnName)
}

// Returns suffix
func (d CockroachdbDialect) CreateTableSuffix() string {
return d.suffix
}

func (d CockroachdbDialect) CreateIndexSuffix() string {
return "using"
}

func (d CockroachdbDialect) DropIndexSuffix() string {
return ""
}

func (d CockroachdbDialect) TruncateClause() string {
return "truncate"
}

func (d CockroachdbDialect) SleepClause(s time.Duration) string {
return fmt.Sprintf("pg_sleep(%f)", s.Seconds())
}

// Returns "$(i+1)"
func (d CockroachdbDialect) BindVar(i int) string {
return fmt.Sprintf("$%d", i+1)
}

func (d CockroachdbDialect) InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error {
rows, err := exec.Query(insertSql, params...)
if err != nil {
return err
}
defer rows.Close()

if !rows.Next() {
return fmt.Errorf("No serial value returned for insert: %s Encountered error: %s", insertSql, rows.Err())
}
if err := rows.Scan(target); err != nil {
return err
}
if rows.Next() {
return fmt.Errorf("more than two serial value returned for insert: %s", insertSql)
}
return rows.Err()
}

func (d CockroachdbDialect) QuoteField(f string) string {
return `"` + f + `"`
}

func (d CockroachdbDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return d.QuoteField(table)
}

return schema + "." + d.QuoteField(table)
}

func (d CockroachdbDialect) IfSchemaNotExists(command, schema string) string {
return fmt.Sprintf("%s if not exists", command)
}

func (d CockroachdbDialect) IfTableExists(command, schema, table string) string {
return fmt.Sprintf("%s if exists", command)
}

func (d CockroachdbDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s if not exists", command)
}

func (d CockroachdbDialect) CreateSchemaCommand() string {
return "create database"
}
4 changes: 4 additions & 0 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,7 @@ func (d MySQLDialect) IfTableExists(command, schema, table string) string {
func (d MySQLDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s if not exists", command)
}

func (d MySQLDialect) CreateSchemaCommand() string {
return "create schema"
}
4 changes: 4 additions & 0 deletions dialect_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,7 @@ func (d OracleDialect) IfTableExists(command, schema, table string) string {
func (d OracleDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s if not exists", command)
}

func (d OracleDialect) CreateSchemaCommand() string {
return "create schema"
}
4 changes: 4 additions & 0 deletions dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ func (d PostgresDialect) IfTableExists(command, schema, table string) string {
func (d PostgresDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s if not exists", command)
}

func (d PostgresDialect) CreateSchemaCommand() string {
return "create schema"
}
4 changes: 4 additions & 0 deletions dialect_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ func (d SqliteDialect) IfTableExists(command, schema, table string) string {
func (d SqliteDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s if not exists", command)
}

func (d SqliteDialect) CreateSchemaCommand() string {
return "create schema"
}
4 changes: 4 additions & 0 deletions dialect_sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ func (d SqlServerDialect) IfTableNotExists(command, schema, table string) string

func (d SqlServerDialect) CreateIndexSuffix() string { return "" }
func (d SqlServerDialect) DropIndexSuffix() string { return "" }

func (d SqlServerDialect) CreateSchemaCommand() string {
return "create schema"
}
2 changes: 1 addition & 1 deletion table.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (t *TableMap) SqlForCreate(ifNotExists bool) string {
dialect := t.dbmap.Dialect

if strings.TrimSpace(t.SchemaName) != "" {
schemaCreate := "create schema"
schemaCreate := dialect.CreateSchemaCommand()
if ifNotExists {
s.WriteString(dialect.IfSchemaNotExists(schemaCreate, t.SchemaName))
} else {
Expand Down