Skip to content

Commit

Permalink
null対応
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Jun 22, 2020
1 parent d268b7a commit ed44245
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 143 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ build:
statik -src template -f
go build -o sqlboiler
code: ./sample/sqlboiler.yaml
./sqlboiler code -c sample/models -y sample/docs/sqlboiler.yaml
./sqlboiler code -c sample/models -y sample/sqlboiler.yaml
schema: ./sample/sqlboiler.yaml
./sqlboiler schema -s sample/docs -y sample/docs/sqlboiler.yaml
./sqlboiler schema -s sample/docs -y sample/sqlboiler.yaml
all:
make code
make schema
81 changes: 58 additions & 23 deletions boiler/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func NewCode(basePath string, yaml *model.Yaml) (*Code, error) {
if err != nil {
return nil, fmt.Errorf("Name Detail Constructor(%s.%s) Error: %w", key, v.Name, err)
}
goType, err := typeParser(v.Type)
goType, err := typeParser(v.Type, !v.NoNull)
if err != nil {
return nil, fmt.Errorf("Type Parse Error(%s.%s): %w", key, v.Name, err)
}

column := &model.CodeColumn{
Name: name,
Type: goType,
Null: v.Null,
Null: !v.NoNull,
ReadOnly: v.AutoIncrement,
}
columns = append(columns, column)
Expand Down Expand Up @@ -97,26 +97,58 @@ func (c *Code) BoilCode() error {
return nil
}

func typeParser(sqlType string) (string, error) {
typeMap := map[string]string{
"boolean": "bool",
"char": "string",
"varchar": "string",
"binary": "string",
"varbinary": "string",
"blob": "blob",
"text": "string",
"integer": "int32",
"int": "int32",
"bigint": "int64",
"mediumint": "int32",
"smallint": "int16",
"tinyint": "int8",
"date": "timeTime",
"datetime": "timeTime",
"timestamp": "timeTime",
"time": "timeTime",
"year": "timeTime",
func typeParser(sqlType string, isNullable bool) (string, error) {
type sqlTypes struct {
nonNull string
null string
}
sqlBool := &sqlTypes{
nonNull: "bool",
null: "nullBool",
}
sqlString := &sqlTypes{
nonNull: "string",
null: "nullString",
}
sqlInt8 := &sqlTypes{
nonNull: "int8",
null: "nullInt32",
}
sqlInt16 := &sqlTypes{
nonNull: "int16",
null: "nullInt32",
}
sqlInt32 := &sqlTypes{
nonNull: "int32",
null: "nullInt32",
}
sqlInt64 := &sqlTypes{
nonNull: "int64",
null: "nullInt64",
}
sqlTime := &sqlTypes{
nonNull: "timeTime",
null: "nullTime",
}
typeMap := map[string]*sqlTypes{
"boolean": sqlBool,
"char": sqlString,
"varchar": sqlString,
"binary": sqlString,
"varbinary": sqlString,
"blob": sqlString,
"text": sqlString,
"integer": sqlInt32,
"int": sqlInt32,
"bigint": sqlInt64,
"mediumint": sqlInt32,
"smallint": sqlInt16,
"tinyint": sqlInt8,
"date": sqlTime,
"datetime": sqlTime,
"timestamp": sqlTime,
"time": sqlTime,
"year": sqlTime,
}

var buf bytes.Buffer
Expand All @@ -141,5 +173,8 @@ func typeParser(sqlType string) (string, error) {
return "", fmt.Errorf("Invalid Type %s", sqlType)
}

return goType, nil
if isNullable {
return goType.null, nil
}
return goType.nonNull, nil
}
4 changes: 2 additions & 2 deletions boiler/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func TestCode(t *testing.T) {
}

expectedType := "bool"
goType, err := typeParser("boolean")
goType, err := typeParser("boolean", false)
if err != nil {
t.Fatalf("Unexpected TypeParser Error: %#v", err)
}
if goType != expectedType {
t.Fatalf("Invalid Parsed Type %s, Expected %s", goType, expectedType)
}

_, err = typeParser("test")
_, err = typeParser("test", false)
if err == nil {
t.Fatalf("Unexpected No Error: %s", goType)
}
Expand Down
2 changes: 1 addition & 1 deletion boiler/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewSchema(basePath string, yaml *model.Yaml) *Schema {
column := model.SchemaColumn{
Name: v.Name,
Type: v.Type,
Null: v.Null,
Null: !v.NoNull,
Key: v.Key,
Default: v.Default,
Extra: extra,
Expand Down
4 changes: 2 additions & 2 deletions boiler/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func newTestYaml() *Yaml {
{
Name: "testa",
Type: "int(1)",
Null: true,
NoNull: false,
AutoIncrement: true,
Key: "PRI",
Default: "default",
},
{
Name: "testb",
Type: "int",
Null: false,
NoNull: true,
AutoIncrement: false,
Key: "",
Default: "",
Expand Down
2 changes: 1 addition & 1 deletion boiler/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestReadYaml(t *testing.T) {
{
Name: "user_id",
Type: "int(11)",
Null: false,
NoNull: false,
AutoIncrement: false,
Key: "",
Default: "",
Expand Down
6 changes: 3 additions & 3 deletions model/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ type yamlColumns = []*YamlColumn
type YamlColumn struct {
Name string
Type string
Null bool
AutoIncrement bool
NoNull bool `yaml:"no_null"`
AutoIncrement bool `yaml:"auto_increment"`
Key string
Default string
}

// Check 同一か確認
func (y *YamlColumn) Check(yc *YamlColumn) bool {
return y.Name == yc.Name || y.Type == yc.Type && y.Null == yc.Null && y.AutoIncrement == yc.AutoIncrement && y.Key == yc.Key && y.Default == yc.Default
return y.Name == yc.Name || y.Type == yc.Type && y.NoNull == yc.NoNull && y.AutoIncrement == yc.AutoIncrement && y.Key == yc.Key && y.Default == yc.Default
}
91 changes: 0 additions & 91 deletions sample/docs/sqlboiler.yaml

This file was deleted.

28 changes: 14 additions & 14 deletions sample/sqlboiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ tables:
key: PRI
- name: name
type: varchar(32)
null: false
no_null: true
- name: password
type: char(128)
null: false
no_null: true
- name: created_at
type: datetime(6)
default: CURRENT_TIMESTAMP
null: false
no_null: true
- name: updated_at
type: datetime(6)
default: CURRENT_TIMESTAMP
null: false
no_null: true
- name: deleted_at
type: datetime(6)
default: null
Expand All @@ -30,20 +30,20 @@ tables:
key: PRI
- name: user_id
type: int(11)
null: false
no_null: true
foreign_key:
users: id
- name: content
type: text
null: false
no_null: true
- name: is_pinned
type: boolean
null: false
no_null: true
default: false
- name: created_at
type: datetime(6)
default: CURRENT_TIMESTAMP
null: false
no_null: true
- name: updated_at
type: datetime(6)
default: null
Expand All @@ -57,18 +57,18 @@ tables:
key: PRI
- name: user_id
type: int(11)
null: false
no_null: true
foreign_key:
users: id
- name: target_user_id
type: int(11)
null: false
no_null: true
foreign_key:
users: id
- name: created_at
type: datetime(6)
default: CURRENT_TIMESTAMP
null: false
no_null: true
- name: deleted_at
type: datetime(6)
default: null
Expand All @@ -79,16 +79,16 @@ tables:
key: PRI
- name: user_id
type: int(11)
null: false
no_null: true
foreign_key:
users: id
- name: target_message_id
type: varchar(36)
null: false
no_null: true
- name: created_at
type: datetime(6)
default: CURRENT_TIMESTAMP
null: false
no_null: true
- name: deleted_at
type: datetime(6)
default: null
Loading

0 comments on commit ed44245

Please sign in to comment.