Skip to content

Commit

Permalink
feat(database): add sqlite config
Browse files Browse the repository at this point in the history
  • Loading branch information
lianxmfor committed Dec 21, 2021
1 parent 1cf4751 commit d3a5236
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ require (
github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.10.4
github.com/mattn/go-sqlite3 v1.14.6
github.com/mitchellh/mapstructure v1.4.2
github.com/olekukonko/tablewriter v0.0.5
github.com/orlangure/gnomock v0.18.2
github.com/snowflakedb/gosnowflake v1.6.4
github.com/spf13/cast v1.4.1
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
google.golang.org/api v0.44.0
google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1
google.golang.org/grpc v1.42.0
google.golang.org/protobuf v1.27.1
Expand Down Expand Up @@ -103,7 +105,6 @@ require (
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.44.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
)
5 changes: 5 additions & 0 deletions internal/database/dbutil/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import (
"github.com/jackc/pgerrcode"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
)

func OpenSQLite(dbFile string) (*sqlx.DB, error) {
return sqlx.Open("sqlite3", dbFile)
}

func OpenMysqlDB(host, port, user, password, database string) (*sqlx.DB, error) {
cfg := mysql.NewConfig()
cfg.Net = fmt.Sprintf("(%s:%s)", host, port)
Expand Down
10 changes: 10 additions & 0 deletions internal/database/dbutil/db_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func GetDbTypeFrom(backend types.BackendType, valueType string) (string, error)
switch backend {
case types.POSTGRES:
mp = postgresType
case types.SQLite:
mp = sqliteType
case types.MYSQL:
mp = mysqlType
case types.CASSANDRA:
Expand All @@ -32,6 +34,14 @@ func GetDbTypeFrom(backend types.BackendType, valueType string) (string, error)
}

var (
sqliteType = map[string]string{
types.STRING: "TEXT",
types.INT64: "INTEGER",
types.FLOAT64: "FLOAT",
types.BOOL: "INTEGER",
types.BYTES: "BLOB",
types.TIME: "TIMESTAMP",
}
mysqlType = map[string]string{
types.STRING: "text",
types.INT64: "bigint",
Expand Down
2 changes: 1 addition & 1 deletion internal/database/dbutil/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
createSchemaFuncs = template.FuncMap{
"entity": func(entity types.Entity, backend types.BackendType) string {
switch backend {
case types.CASSANDRA:
case types.CASSANDRA, types.SQLite:
return fmt.Sprintf(`"%s" TEXT PRIMARY KEY`, entity.Name)
case types.POSTGRES, types.SNOWFLAKE:
return fmt.Sprintf(`"%s" VARCHAR(%d) PRIMARY KEY`, entity.Name, entity.Length)
Expand Down
6 changes: 3 additions & 3 deletions internal/database/dbutil/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func buildQueryAndArgsForInsertRecords(tableName string, records []interface{},
case types.POSTGRES, types.SNOWFLAKE:
columnStr = Quote(`"`, columns...)
tableName = fmt.Sprintf(`"%s"`, tableName)
case types.MYSQL:
case types.MYSQL, types.SQLite:
columnStr = Quote("`", columns...)
tableName = fmt.Sprintf("`%s`", tableName)
}
Expand All @@ -91,7 +91,7 @@ func GetColumnFormat(backendType types.BackendType) (string, error) {
switch backendType {
case types.POSTGRES, types.SNOWFLAKE:
columnFormat = `"%s" %s`
case types.MYSQL:
case types.MYSQL, types.SQLite:
columnFormat = "`%s` %s"
default:
return "", fmt.Errorf("unsupported backend type %s", backendType)
Expand All @@ -104,7 +104,7 @@ func QuoteFn(backendType types.BackendType) (func(...string) string, error) {
switch backendType {
case types.POSTGRES, types.SNOWFLAKE:
quote = `"`
case types.MYSQL:
case types.MYSQL, types.SQLite:
quote = "`"
default:
return nil, fmt.Errorf("unsupported backend type %s", backendType)
Expand Down
5 changes: 5 additions & 0 deletions pkg/oomstore/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
POSTGRES BackendType = "postgres"
REDIS BackendType = "redis"
MYSQL BackendType = "mysql"
SQLite BackendType = "sqlite"
SNOWFLAKE BackendType = "snowflake"
DYNAMODB BackendType = "dynamodb"
CASSANDRA BackendType = "cassandra"
Expand Down Expand Up @@ -58,6 +59,10 @@ type PostgresOpt struct {
Database string `yaml:"database"`
}

type SQLiteOpt struct {
DBFile string `yaml:"db-file"`
}

type MySQLOpt struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Expand Down

0 comments on commit d3a5236

Please sign in to comment.