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

sqlite-support #3

Merged
merged 2 commits into from
Sep 11, 2020
Merged
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
10 changes: 10 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions cli/gnorm.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# ConnStr is the connection string for the database. Any environment variables
# in this string will be expanded, so for example dbname=$MY_DDB will do the
# right thing.
# MySQL example:
# ConnStr = "root:admin@tcp/"
# Mssql example:
# ConnStr = "sqlserver://username:password@server:1433?database=dbname&connection+timeout=30"
# Postgres example:
# sqlite: ConnStr = "./sqlite.db"
# MySQL: ConnStr = "root:admin@tcp/"
# Postgres: ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"
ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"

# DBType holds the type of db you're connecting to. Possible values are
# "postgres" "mssql" or "mysql".
# "postgres", "mysql" or "sqlite".
DBType = "postgres"

# Schemas holds the names of schemas to generate code for.
# postgres: "public"
# sqlite: "main"
# Mssql example :
# Schemas = ["dbo"]
Schemas = ["public"]
Expand Down Expand Up @@ -41,6 +41,7 @@ IncludeTables = []
# generation. You cannot set ExcludeTables if IncludeTables is set. By
# default, tables will be excluded from all schemas. To specify tables for
# a specific schema only, use the schema.tablenmae format.
# sqlite: ExcludeTables = ["sqlite_sequence", "sqlite_master"]
# Mssql example:
# ExcludeTables = ["sysdiagrams"]
ExcludeTables = ["xyzzx"]
Expand Down Expand Up @@ -125,6 +126,16 @@ NoOverwriteGlobs = ["*.perm.go"]
# not in this mapping, Column.Type will be an empty string. Note that because
# of the way tables in TOML work, TypeMap and NullableTypeMap must be at the end
# of your configuration file.
#
# Example for mapping sqlite types to Go types:
# https://www.sqlite.org/datatype3.html
# [TypeMap]
# "INTEGER" = "int"
# "NUMERIC" = "float64"
# "REAL" = "float64"
# "TEXT" = "string"
# "BLOB" = "byte[]"
#
# Example for mapping postgres types to Go types:
[TypeMap]
"timestamp with time zone" = "time.Time"
Expand Down
3 changes: 3 additions & 0 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"gnorm.org/gnorm/database"
"gnorm.org/gnorm/database/drivers/mysql"
"gnorm.org/gnorm/database/drivers/postgres"
"gnorm.org/gnorm/database/drivers/sqlite"
"gnorm.org/gnorm/environ"
"gnorm.org/gnorm/run"
"gnorm.org/gnorm/run/data"
Expand Down Expand Up @@ -140,6 +141,8 @@ func getDriver(name string) (database.Driver, error) {
return postgres.PG{}, nil
case "mysql":
return mysql.MySQL{}, nil
case "sqlite":
return sqlite.Sqlite{}, nil
case "mssql":
return mssql.Mssql{}, nil
default:
Expand Down
23 changes: 17 additions & 6 deletions cli/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ package cli
const sample = `# ConnStr is the connection string for the database. Any environment variables
# in this string will be expanded, so for example dbname=$MY_DDB will do the
# right thing.
# MySQL example:
# ConnStr = "root:admin@tcp/"
# Mssql example:
# ConnStr = "sqlserver://username:password@server:1433?database=dbname&connection+timeout=30"
# Postgres example:
# sqlite: ConnStr = "./sqlite.db"
# MySQL: ConnStr = "root:admin@tcp/"
# Postgres: ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"
ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"

# DBType holds the type of db you're connecting to. Possible values are
# "postgres" "mssql" or "mysql".
# "postgres", "mysql" or "sqlite".
DBType = "postgres"

# Schemas holds the names of schemas to generate code for.
# postgres: "public"
# sqlite: "main"
# Mssql example :
# Schemas = ["dbo"]
Schemas = ["public"]
Expand Down Expand Up @@ -67,6 +67,7 @@ IncludeTables = []
# generation. You cannot set ExcludeTables if IncludeTables is set. By
# default, tables will be excluded from all schemas. To specify tables for
# a specific schema only, use the schema.tablenmae format.
# sqlite: ExcludeTables = ["sqlite_sequence", "sqlite_master"]
# Mssql example:
# ExcludeTables = ["sysdiagrams"]
ExcludeTables = ["xyzzx"]
Expand Down Expand Up @@ -151,6 +152,16 @@ NoOverwriteGlobs = ["*.perm.go"]
# not in this mapping, Column.Type will be an empty string. Note that because
# of the way tables in TOML work, TypeMap and NullableTypeMap must be at the end
# of your configuration file.
#
# Example for mapping sqlite types to Go types:
# https://www.sqlite.org/datatype3.html
# [TypeMap]
# "INTEGER" = "int"
# "NUMERIC" = "float64"
# "REAL" = "float64"
# "TEXT" = "string"
# "BLOB" = "byte[]"
#
# Example for mapping postgres types to Go types:
[TypeMap]
"timestamp with time zone" = "time.Time"
Expand Down
57 changes: 57 additions & 0 deletions database/drivers/sqlite/gnorm/columns/columns.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions database/drivers/sqlite/gnorm/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gnorm // import "gnorm.org/gnorm/database/drivers/sqlite/gnorm"

// Note that this file is *NOT* generated. :)

import (
"database/sql"
)

// DB is the common interface for database operations.
//
// This should work with database/sql.DB and database/sql.Tx.
type DB interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
}

// WhereClause has a String function should return a properly formatted where
// clause (not including the WHERE) for positional arguments starting at idx.
type WhereClause interface {
String() string
Values() []interface{}
}

type comparison string

const (
compEqual comparison = " = "
compGreater comparison = " > "
compLess comparison = " < "
compGTE comparison = " >= "
compLTE comparison = " <= "
compNE comparison = " <> "
)

type inClause struct {
field string
values []interface{}
}

func (in inClause) String() string {
ret := in.field + " in ("
for x := range in.values {
if x != 0 {
ret += ", "
}
ret += "?"
}
ret += ")"
return ret
}

func (in inClause) Values() []interface{} {
return in.values
}

type whereClause struct {
field string
comp comparison
value interface{}
}

func (w whereClause) String() string {
ret := w.field + string(w.comp) + "?"
return ret
}

func (w whereClause) Values() []interface{} {
return []interface{}{w.value}
}
111 changes: 111 additions & 0 deletions database/drivers/sqlite/gnorm/statistics/statistics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading