支持sqlite表结构自动生成
This commit is contained in:
parent
24aa0d3f9b
commit
572ed2b0f3
@ -115,6 +115,9 @@ func (f *Field) ToString() (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
fieldTpl := f.getFieldTpl()
|
fieldTpl := f.getFieldTpl()
|
||||||
|
if f.PrimaryKey && DatabaseDriver == consts.DatabaseDriverSqlite3 {
|
||||||
|
f.Type = "INTEGER PRIMARY KEY AUTOINCREMENT"
|
||||||
|
}
|
||||||
dataReplaceTable := map[string]string{
|
dataReplaceTable := map[string]string{
|
||||||
FieldName: f.Name, // 字段名
|
FieldName: f.Name, // 字段名
|
||||||
FieldType: f.Type, // 字段类型
|
FieldType: f.Type, // 字段类型
|
||||||
|
@ -8,7 +8,9 @@
|
|||||||
package define
|
package define
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -76,24 +78,43 @@ func (i *Index) Validate() error {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 17:47 2023/9/17
|
// Date : 17:47 2023/9/17
|
||||||
func (i *Index) ToString() (string, error) {
|
func (i *Index) ToString(tableName string) (string, error) {
|
||||||
if err := i.Validate(); nil != err {
|
if err := i.Validate(); nil != err {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 拼接sql
|
switch DatabaseDriver {
|
||||||
switch i.Type {
|
case consts.DatabaseDriverMysql:
|
||||||
case TableIndexPrimary:
|
// 拼接sql
|
||||||
return fmt.Sprintf("PRIMARY KEY (%v)", i.FieldList[0]), nil
|
switch i.Type {
|
||||||
case TableIndexUnique:
|
case TableIndexPrimary:
|
||||||
return fmt.Sprintf("UNIQUE KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
|
return fmt.Sprintf("ALTER TABLE `%v` ADD PRIMARY KEY (%v)", tableName, i.FieldList[0]), nil
|
||||||
case TableIndexKey:
|
case TableIndexUnique:
|
||||||
return fmt.Sprintf("KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
|
return fmt.Sprintf("ALTER TABLE `%v` ADD UNIQUE KEY %v (%v)", tableName, i.Name, strings.Join(i.FieldList, ",")), nil
|
||||||
case TableIndexIndex:
|
case TableIndexKey:
|
||||||
return fmt.Sprintf("INDEX %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
|
return fmt.Sprintf("ALTER TABLE `%v` ADD KEY %v (%v)", tableName, i.Name, strings.Join(i.FieldList, ",")), nil
|
||||||
default:
|
case TableIndexIndex:
|
||||||
return fmt.Sprintf("%v %v (%v)", i.Type, i.Name, strings.Join(i.FieldList, ",")), nil
|
return fmt.Sprintf("ALTER TABLE `%v` ADD INDEX %v (%v)", tableName, i.Name, strings.Join(i.FieldList, ",")), nil
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("ALTER TABLE `%v` ADD %v %v (%v)", tableName, i.Type, i.Name, strings.Join(i.FieldList, ",")), nil
|
||||||
|
}
|
||||||
|
case consts.DatabaseDriverSqlite3:
|
||||||
|
// 拼接sql
|
||||||
|
switch i.Type {
|
||||||
|
case TableIndexPrimary:
|
||||||
|
return "", nil
|
||||||
|
// return fmt.Sprintf("ALTER TABLE %v ADD CONSTRAINT %v PRIMARY KEY AUTOINCREMENT (%v)", tableName, i.FieldList[0], i.FieldList[0]), nil
|
||||||
|
case TableIndexUnique:
|
||||||
|
return fmt.Sprintf("CREATE UNIQUE INDEX %v ON %v (%v)", i.Name, tableName, strings.Join(i.FieldList, ",")), nil
|
||||||
|
case TableIndexKey:
|
||||||
|
fallthrough
|
||||||
|
case TableIndexIndex:
|
||||||
|
return fmt.Sprintf("CREATE INDEX %v ON %v (%v)", i.Name, tableName, strings.Join(i.FieldList, ",")), nil
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("CREATE %v %v ON %v (%v)", i.Type, i.Name, tableName, strings.Join(i.FieldList, ",")), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return "", errors.New(DatabaseDriver + " : database driver is not support")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToStringIgnoreError ...
|
// ToStringIgnoreError ...
|
||||||
@ -101,7 +122,7 @@ func (i *Index) ToString() (string, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 15:59 2023/9/18
|
// Date : 15:59 2023/9/18
|
||||||
func (i *Index) ToStringIgnoreError() string {
|
func (i *Index) ToStringIgnoreError(tableName string) string {
|
||||||
sql, _ := i.ToString()
|
sql, _ := i.ToString(tableName)
|
||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ func (t *Table) Validate() error {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:46 2023/9/17
|
// Date : 18:46 2023/9/17
|
||||||
func (t *Table) ToString() (string, error) {
|
func (t *Table) ToString() ([]string, error) {
|
||||||
tableTpl := t.getTableSqlTpl()
|
tableTpl := t.getTableSqlTpl()
|
||||||
replaceTable := map[string]string{
|
replaceTable := map[string]string{
|
||||||
TableName: t.Name, // 表名
|
TableName: t.Name, // 表名
|
||||||
@ -111,23 +111,22 @@ func (t *Table) ToString() (string, error) {
|
|||||||
for _, item := range t.FieldList {
|
for _, item := range t.FieldList {
|
||||||
fieldStr, err := item.ToString()
|
fieldStr, err := item.ToString()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
fieldList = append(fieldList, fieldStr)
|
fieldList = append(fieldList, fieldStr)
|
||||||
}
|
}
|
||||||
indexList := make([]string, 0)
|
indexList := make([]string, 0)
|
||||||
for _, item := range t.IndexList {
|
for _, item := range t.IndexList {
|
||||||
indexStr, err := item.ToString()
|
indexStr, err := item.ToString(t.Name)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return "", err
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(indexStr) == 0 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
indexList = append(indexList, indexStr)
|
indexList = append(indexList, indexStr)
|
||||||
}
|
}
|
||||||
if len(indexList) == 0 || consts.DatabaseDriverSqlite3 == DatabaseDriver {
|
replaceTable[TableFieldList] = strings.Join(fieldList, ",")
|
||||||
replaceTable[TableFieldList] = strings.Join(fieldList, ",")
|
|
||||||
} else {
|
|
||||||
replaceTable[TableFieldList] = strings.Join(fieldList, ",") + ","
|
|
||||||
}
|
|
||||||
replaceTable[TableIndexList] = strings.Join(indexList, ",")
|
replaceTable[TableIndexList] = strings.Join(indexList, ",")
|
||||||
|
|
||||||
if len(t.Engine) > 0 {
|
if len(t.Engine) > 0 {
|
||||||
@ -153,7 +152,8 @@ func (t *Table) ToString() (string, error) {
|
|||||||
for source, target := range replaceTable {
|
for source, target := range replaceTable {
|
||||||
tableTpl = strings.ReplaceAll(tableTpl, source, target)
|
tableTpl = strings.ReplaceAll(tableTpl, source, target)
|
||||||
}
|
}
|
||||||
return tableTpl, nil
|
|
||||||
|
return append([]string{tableTpl}, indexList...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToStringIgnoreError ...
|
// ToStringIgnoreError ...
|
||||||
@ -161,7 +161,7 @@ func (t *Table) ToString() (string, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 15:59 2023/9/18
|
// Date : 15:59 2023/9/18
|
||||||
func (t *Table) ToStringIgnoreError() string {
|
func (t *Table) ToStringIgnoreError() []string {
|
||||||
sql, _ := t.ToString()
|
sql, _ := t.ToString()
|
||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user