Skip to content

Commit

Permalink
chore: check basic go types are covered
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed Sep 26, 2024
1 parent a8e78a8 commit 4364687
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 176 deletions.
25 changes: 22 additions & 3 deletions codegen/code_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ import (
"golang.org/x/tools/imports"
)

// The minimum go type we need to map
var goTypes = []string{
"byte",
"rune",
"bool",
"string",
"float32", "float64",
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64",
"time.Time",
// "any", "sql.RawBytes", "json.RawMessage"
}

type Generator struct {
*bytes.Buffer
config *Config
Expand All @@ -34,7 +47,7 @@ type Generator struct {
errs []error
}

func newGenerator(cfg *Config, d dialect.Dialect) *Generator {
func newGenerator(cfg *Config, d dialect.Dialect) (*Generator, error) {
gen := new(Generator)
gen.Buffer = new(bytes.Buffer)
gen.config = cfg
Expand All @@ -44,11 +57,17 @@ func newGenerator(cfg *Config, d dialect.Dialect) *Generator {
case '`':
gen.quoteRune = '"'
default:
gen.quoteRune = '"'
return nil, fmt.Errorf(`sqlgen: invalid quote character %q for string`, d.QuoteRune())
}
gen.dialect = d
gen.staticVar = d.QuoteVar(1) == d.QuoteVar(0)
gen.defaultColumnTypes = d.ColumnDataTypes()
// Check the dialect cover the basic go types
for _, t := range goTypes {
if _, ok := gen.defaultColumnTypes[t]; !ok {
return nil, fmt.Errorf(`sqlgen: SQL dialect %q missing column type mapping for type %q`, d.Driver(), t)
}
}
gen.columnTypes = make(map[string]*dialect.ColumnType)
for k, decl := range cfg.DataTypes {
gen.columnTypes[k] = &dialect.ColumnType{
Expand All @@ -61,7 +80,7 @@ func newGenerator(cfg *Config, d dialect.Dialect) *Generator {
SQLValuer: decl.SQLValuer,
}
}
return gen
return gen, nil
}

func (g *Generator) LogError(err error) {
Expand Down
11 changes: 7 additions & 4 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ func Generate(c *Config) error {
return fmt.Errorf("sqlgen: missing dialect, please register your dialect first")
}

generator, err := newGenerator(cfg, dialect)
if err != nil {
return err
}

var (
srcDir string
sources = make([]string, len(cfg.Source))
generator = newGenerator(cfg, dialect)
srcDir string
sources = make([]string, len(cfg.Source))
)

copy(sources, cfg.Source)

// Resolve every source provided
Expand Down
12 changes: 6 additions & 6 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ func (c *Config) initIfEmpty() {
if c.Database.Operator.Filename == "" {
c.Database.Operator.Filename = "operator.go"
}
if c.Migration == nil {
c.Migration = new(MigrationConfig)
}
c.Migration.Dir = "migrate"
c.Migration.Package = "migrate"
c.Migration.Filename = "migrate.go"
// if c.Migration == nil {
// c.Migration = new(MigrationConfig)
// }
// c.Migration.Dir = "migrate"
// c.Migration.Package = "migrate"
// c.Migration.Filename = "migrate.go"
if c.DataTypes == nil {
c.DataTypes = make(map[string]DataType)
}
Expand Down
5 changes: 5 additions & 0 deletions codegen/dialect/mysql/column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func (s *mysqlDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
Valuer: "(string)({{goPath}})",
Scanner: "{{addrOfGoPath}}",
},
"byte": {
DataType: s.columnDataType("CHAR(1)"),
Valuer: "(string)({{goPath}})",
Scanner: "{{addrOfGoPath}}",
},
"string": {
DataType: s.columnDataType("VARCHAR(255)", ""),
Valuer: "(string)({{goPath}})",
Expand Down
5 changes: 5 additions & 0 deletions codegen/dialect/postgres/column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func (s *postgresDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
Valuer: "(string)({{goPath}})",
Scanner: "{{addrOfGoPath}}",
},
"byte": {
DataType: s.columnDataType("char(1)"),
Valuer: "(string)({{goPath}})",
Scanner: "{{addrOfGoPath}}",
},
"string": {
DataType: func(col dialect.GoColumn) string {
size := 255
Expand Down
29 changes: 26 additions & 3 deletions codegen/dialect/sqlite/data_type.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
package sqlite

import (
"fmt"

"github.com/si3nloong/sqlgen/codegen/dialect"
)

func (s *sqliteDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
return map[string]*dialect.ColumnType{
"rune": {
DataType: s.columnDataType("CHAR(1)"),
Valuer: "(string)({{goPath}})",
Scanner: "{{addrOfGoPath}}",
},
"byte": {
DataType: s.columnDataType("CHAR(1)"),
Valuer: "(string)({{goPath}})",
Scanner: "{{addrOfGoPath}}",
},
"string": {
DataType: s.columnDataType("TEXT"),
Valuer: "string({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.String({{addrOfGoPath}})",
DataType: func(col dialect.GoColumn) string {
size := 255
if n := col.Size(); n > 0 {
size = n
}
return fmt.Sprintf("VARCHAR(%d)", size)
},
Valuer: "string({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.String({{addrOfGoPath}})",
},
"bool": {
DataType: s.columnDataType("INTEGER"),
Expand Down Expand Up @@ -76,6 +94,11 @@ func (s *sqliteDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
Valuer: "float64({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Float({{addrOfGoPath}})",
},
"time.Time": {
DataType: s.columnDataType("TEXT"),
Valuer: "(time.Time)({{goPath}})",
Scanner: "(*time.Time)({{addrOfGoPath}})",
},
}
}

Expand Down
159 changes: 0 additions & 159 deletions examples/db/mysql/migrate.go

This file was deleted.

1 change: 0 additions & 1 deletion examples/testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func TestAll(t *testing.T) {
Package: "mysqldb",
Dir: "./db/mysql",
},
Migration: &codegen.MigrationConfig{},
// QuoteIdentifier: true,
Exec: codegen.ExecConfig{
SkipEmpty: false,
Expand Down

0 comments on commit 4364687

Please sign in to comment.