-
Notifications
You must be signed in to change notification settings - Fork 374
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
zikes
wants to merge
2
commits into
go-gorp:main
Choose a base branch
from
zikes:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 liketype NonstandardSchemaDialect interface { CreateSchemaCommand() string }
?There was a problem hiding this comment.
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 atype SchemaCreator() interface { CreateSchemaCommand() string }
instead?There was a problem hiding this comment.
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 informgorp
that they are unable to executecreate 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.There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.