Skip to content

Commit

Permalink
fix: fix insql
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed May 10, 2024
1 parent abe2588 commit 85facff
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
36 changes: 18 additions & 18 deletions driver/mysql/def_atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"ariga.io/atlas/sql/schema"
"google.golang.org/protobuf/reflect/protoreflect"

innersqlx "github.com/things-go/ens/internal/sqlx"
"github.com/things-go/ens/internal/insql"
"github.com/things-go/ens/proto"
"github.com/things-go/ens/rapier"
"github.com/things-go/ens/sqlx"
Expand All @@ -28,7 +28,7 @@ func intoColumnSql(col *schema.Column) string {
if autoIncrement {
b.WriteString(" AUTO_INCREMENT")
} else {
dv, ok := innersqlx.DefaultValue(col)
dv, ok := insql.DefaultValue(col)
if ok {
fmt.Fprintf(b, " DEFAULT '%s'", strings.Trim(dv, `"`))
} else if nullable {
Expand All @@ -39,10 +39,10 @@ func intoColumnSql(col *schema.Column) string {
}

func intoIndexSql(index *schema.Index) string {
fields := innersqlx.IndexPartColumnNames(index.Parts)
fields := insql.IndexPartColumnNames(index.Parts)
indexType := findIndexType(index.Attrs)
fieldList := "`" + strings.Join(fields, "`,`") + "`"
if innersqlx.IndexEqual(index.Table.PrimaryKey, index) {
if insql.IndexEqual(index.Table.PrimaryKey, index) {
return fmt.Sprintf("PRIMARY KEY (%s) USING %s", fieldList, indexType)
} else if index.Unique {
return fmt.Sprintf("UNIQUE KEY `%s` (%s) USING %s", index.Name, fieldList, indexType)
Expand All @@ -52,8 +52,8 @@ func intoIndexSql(index *schema.Index) string {
}

func intoForeignKeySql(fk *schema.ForeignKey) string {
columnNameList := "`" + strings.Join(innersqlx.ColumnNames(fk.Columns), "`,`") + "`"
refColumnNameList := "`" + strings.Join(innersqlx.ColumnNames(fk.RefColumns), "`,`") + "`"
columnNameList := "`" + strings.Join(insql.ColumnNames(fk.Columns), "`,`") + "`"
refColumnNameList := "`" + strings.Join(insql.ColumnNames(fk.RefColumns), "`,`") + "`"
return fmt.Sprintf(
"CONSTRAINT `%s` FOREIGN KEY (%s) REFERENCES `%s` (%s) ON DELETE %s ON UPDATE %s",
fk.Symbol, columnNameList, fk.RefTable.Name, refColumnNameList, fk.OnDelete, fk.OnUpdate,
Expand All @@ -79,7 +79,7 @@ func intoTableSql(tb *schema.Table) string {
for _, col := range tb.Columns {
remain--
suffix := suffixOrEmpty(remain)
comment, ok := innersqlx.Comment(col.Attrs)
comment, ok := insql.Comment(col.Attrs)
if ok {
comment = fmt.Sprintf(" COMMENT '%s'", comment)
}
Expand All @@ -93,7 +93,7 @@ func intoTableSql(tb *schema.Table) string {
}
for _, index := range tb.Indexes {
remain--
if innersqlx.IndexEqual(tb.PrimaryKey, index) { // ignore primary key, maybe include
if insql.IndexEqual(tb.PrimaryKey, index) { // ignore primary key, maybe include
continue
}
suffix := suffixOrEmpty(remain)
Expand Down Expand Up @@ -137,7 +137,7 @@ func intoTableSql(tb *schema.Table) string {
func intoGormTag(tb *schema.Table, col *schema.Column) string {
pkPriority, isPk := 0, false
if pk := tb.PrimaryKey; pk != nil {
pkPriority, isPk = innersqlx.FindIndexPartSeq(pk.Parts, col)
pkPriority, isPk = insql.FindIndexPartSeq(pk.Parts, col)
}
autoIncrement := autoIncrement(col.Attrs)

Expand All @@ -156,7 +156,7 @@ func intoGormTag(tb *schema.Table, col *schema.Column) string {
fmt.Fprintf(b, ";autoIncrement:true")
}
} else {
dv, ok := innersqlx.DefaultValue(col)
dv, ok := insql.DefaultValue(col)
if ok {
if dv == `""` || dv == "" {
dv = "''"
Expand All @@ -177,7 +177,7 @@ func intoGormTag(tb *schema.Table, col *schema.Column) string {
}
}
for _, val := range col.Indexes {
if innersqlx.IndexEqual(tb.PrimaryKey, val) { // ignore primary key, may be include
if insql.IndexEqual(tb.PrimaryKey, val) { // ignore primary key, may be include
continue
}
if val.Unique {
Expand All @@ -190,13 +190,13 @@ func intoGormTag(tb *schema.Table, col *schema.Column) string {
// }
}
if len(val.Parts) > 1 {
priority, ok := innersqlx.FindIndexPartSeq(val.Parts, col)
priority, ok := insql.FindIndexPartSeq(val.Parts, col)
if ok {
fmt.Fprintf(b, ",priority:%d", priority)
}
}
}
if comment, ok := innersqlx.Comment(col.Attrs); ok && comment != "" {
if comment, ok := insql.Comment(col.Attrs); ok && comment != "" {
fmt.Fprintf(b, ";comment:%s", utils.TrimFieldComment(comment))
}
b.WriteString(`"`)
Expand All @@ -219,13 +219,13 @@ func intoProto(tb *schema.Table) *proto.Message {
TypeName: n,
Name: col.Name,
ColumnName: col.Name,
Comment: innersqlx.MustComment(col.Attrs),
Comment: insql.MustComment(col.Attrs),
})
}
return &proto.Message{
Name: tb.Name,
TableName: tb.Name,
Comment: innersqlx.MustComment(tb.Attrs),
Comment: insql.MustComment(tb.Attrs),
Fields: fields,
}
}
Expand All @@ -243,13 +243,13 @@ func intoRapier(tb *schema.Table) *rapier.Struct {
GoName: utils.CamelCase(col.Name),
Nullable: col.Type.Null,
ColumnName: col.Name,
Comment: innersqlx.MustComment(col.Attrs),
Comment: insql.MustComment(col.Attrs),
})
}
return &rapier.Struct{
GoName: utils.CamelCase(tb.Name),
TableName: tb.Name,
Comment: innersqlx.MustComment(tb.Attrs),
Comment: insql.MustComment(tb.Attrs),
Fields: fields,
}
}
Expand All @@ -258,6 +258,6 @@ func intoSql(tb *schema.Table) *sqlx.Table {
return &sqlx.Table{
Name: tb.Name,
Sql: intoTableSql(tb),
Comment: innersqlx.MustComment(tb.Attrs),
Comment: insql.MustComment(tb.Attrs),
}
}
6 changes: 3 additions & 3 deletions driver/mysql/def_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"ariga.io/atlas/sql/schema"

"github.com/things-go/ens"
"github.com/things-go/ens/internal/sqlx"
"github.com/things-go/ens/internal/insql"
)

func autoIncrement(attrs []schema.Attr) bool {
return sqlx.Has(attrs, &mysql.AutoIncrement{})
return insql.Has(attrs, &mysql.AutoIncrement{})
}

func findIndexType(attrs []schema.Attr) string {
var t mysql.IndexType
if sqlx.Has(attrs, &t) && t.T != "" {
if insql.Has(attrs, &t) && t.T != "" {
return t.T
} else {
return "BTREE"
Expand Down
10 changes: 5 additions & 5 deletions driver/mysql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"ariga.io/atlas/sql/schema"
"github.com/things-go/ens"
"github.com/things-go/ens/driver"
innersqlx "github.com/things-go/ens/internal/sqlx"
"github.com/things-go/ens/internal/insql"
"github.com/things-go/ens/proto"
"github.com/things-go/ens/rapier"
"github.com/things-go/ens/sqlx"
Expand Down Expand Up @@ -296,7 +296,7 @@ func parseSqlColumnDefinition(col *sqlparser.ColumnDefinition) (*schema.Column,
raw := colType.Type
if colType.Length != nil {
length := parseInt(colType.Length)
size = innersqlx.P(int(length))
size = insql.P(int(length))
raw = fmt.Sprintf("%s(%d)", colType.Type, length)
}

Expand All @@ -322,10 +322,10 @@ func parseSqlColumnDefinition(col *sqlparser.ColumnDefinition) (*schema.Column,
var precision, scale *int

if colType.Length != nil {
precision = innersqlx.P(int(parseInt(colType.Length)))
precision = insql.P(int(parseInt(colType.Length)))
}
if colType.Scale != nil {
scale = innersqlx.P(int(parseInt(colType.Scale)))
scale = insql.P(int(parseInt(colType.Scale)))
}
coldef.Type = &schema.ColumnType{
Type: &schema.TimeType{
Expand Down Expand Up @@ -401,7 +401,7 @@ func parseSqlIndexDefinition(table *schema.Table, idx *sqlparser.IndexDefinition
cols := make([]*schema.Column, 0, len(idx.Columns))
for _, idxCol := range idx.Columns {
columnName := idxCol.Column.String()
col, ok := innersqlx.FindColumn(columns, columnName)
col, ok := insql.FindColumn(columns, columnName)
if ok {
cols = append(cols, col)
} else {
Expand Down
4 changes: 2 additions & 2 deletions driver/mysql/sql_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/things-go/ens"
"github.com/things-go/ens/driver"
innersqlx "github.com/things-go/ens/internal/sqlx"
"github.com/things-go/ens/internal/insql"
"github.com/things-go/ens/proto"
"github.com/things-go/ens/rapier"
"github.com/things-go/ens/sqlx"
Expand Down Expand Up @@ -399,7 +399,7 @@ func parseCreateTableStmtIndex(table *schema.Table, idx *ast.Constraint, columns
cols := make([]*schema.Column, 0, len(idx.Keys))
for _, idxCol := range idx.Keys {
columnName := idxCol.Column.String()
col, ok := innersqlx.FindColumn(columns, columnName)
col, ok := insql.FindColumn(columns, columnName)
if ok {
cols = append(cols, col)
} else {
Expand Down
4 changes: 2 additions & 2 deletions entity_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ens

import "github.com/things-go/ens/internal/sqlx"
import "github.com/things-go/ens/internal/insql"

var _ MixinEntity = (*EntityBuilder)(nil)

Expand All @@ -19,7 +19,7 @@ func EntityFromDef(def TableDef) *EntityBuilder {
tb := def.Table()
return &EntityBuilder{
name: tb.Name,
comment: sqlx.MustComment(tb.Attrs),
comment: insql.MustComment(tb.Attrs),
table: def,
fields: nil,
indexes: nil,
Expand Down
4 changes: 2 additions & 2 deletions field_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ens
import (
"database/sql/driver"

"github.com/things-go/ens/internal/sqlx"
"github.com/things-go/ens/internal/insql"
)

func Bool(name string) *FieldBuilder { return Field(BoolType(), name) }
Expand Down Expand Up @@ -50,7 +50,7 @@ func FieldFromDef(t *GoType, def ColumnDef) *FieldBuilder {
return &FieldBuilder{
inner: &FieldDescriptor{
Name: col.Name,
Comment: sqlx.MustComment(col.Attrs),
Comment: insql.MustComment(col.Attrs),
Nullable: col.Type.Null,
Column: def,
Type: t,
Expand Down
6 changes: 3 additions & 3 deletions foreign_key_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ens

import (
"ariga.io/atlas/sql/schema"
"github.com/things-go/ens/internal/sqlx"
"github.com/things-go/ens/internal/insql"
)

// ForeignKeyFromDef returns a new ForeignKey with the ForeignKeyDef.
Expand All @@ -28,9 +28,9 @@ func ForeignKeyFromDef(def ForeignKeyDef) *foreignKeyBuilder {
inner: &ForeignKeyDescriptor{
Symbol: fk.Symbol,
Table: fk.Table.Name,
Columns: sqlx.ColumnNames(fk.Columns),
Columns: insql.ColumnNames(fk.Columns),
RefTable: fk.RefTable.Name,
RefColumns: sqlx.ColumnNames(fk.RefColumns),
RefColumns: insql.ColumnNames(fk.RefColumns),
OnUpdate: fk.OnUpdate,
OnDelete: fk.OnDelete,
ForeignKey: def,
Expand Down
4 changes: 2 additions & 2 deletions index_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ens

import "github.com/things-go/ens/internal/sqlx"
import "github.com/things-go/ens/internal/insql"

type indexBuilder struct {
inner *IndexDescriptor
Expand All @@ -23,7 +23,7 @@ func IndexFromDef(e IndexDef) *indexBuilder {
return &indexBuilder{
inner: &IndexDescriptor{
Name: index.Name,
Fields: sqlx.IndexPartColumnNames(index.Parts),
Fields: insql.IndexPartColumnNames(index.Parts),
Index: e,
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/sqlx/sqlx.go → internal/insql/insqlx.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sqlx
package insql

import (
"fmt"
Expand Down

0 comments on commit 85facff

Please sign in to comment.