Add sqlite schema
This commit is contained in:
parent
f2f5133fc0
commit
0ec761558c
47
dialects/sqlite/schema.go
Normal file
47
dialects/sqlite/schema.go
Normal file
@ -0,0 +1,47 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AutoMigrate auto migrate database
|
||||
func (dialect *Dialect) AutoMigrate(value interface{}) (err error) {
|
||||
// create table
|
||||
|
||||
// create missed column
|
||||
|
||||
// safe upgrade some fields (like size, change data type)
|
||||
|
||||
// create missed foreign key
|
||||
|
||||
// create missed index
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasTable check if has table in current schema
|
||||
func (dialect *Dialect) HasTable(tableName string) bool {
|
||||
var count int
|
||||
currentDatabase, tableName := currentDatabaseAndTable(dialect, tableName)
|
||||
_ = dialect.DB.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = ? AND table_name = ?", currentDatabase, tableName).Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// CreateTable create table for value
|
||||
func (dialect *Dialect) CreateTable(value interface{}) error {
|
||||
// s := schema.Parse(value)
|
||||
return nil
|
||||
}
|
||||
|
||||
// CurrentDatabase get current database name
|
||||
func (dialect *Dialect) CurrentDatabase() (name string) {
|
||||
_ = dialect.DB.QueryRow("SELECT DATABASE()").Scan(&name)
|
||||
return
|
||||
}
|
||||
|
||||
func currentDatabaseAndTable(dialect *Dialect, tableName string) (string, string) {
|
||||
if strings.Contains(tableName, ".") {
|
||||
splitStrings := strings.SplitN(tableName, ".", 2)
|
||||
return splitStrings[0], splitStrings[1]
|
||||
}
|
||||
return dialect.CurrentDatabase(), tableName
|
||||
}
|
@ -10,7 +10,6 @@ import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/jinzhu/gorm/dialects/common/sqlbuilder"
|
||||
"github.com/jinzhu/gorm/model"
|
||||
"github.com/jinzhu/gorm/schema"
|
||||
)
|
||||
|
||||
// Dialect Sqlite3 Dialect for GORM
|
||||
@ -320,26 +319,3 @@ func (dialect *Dialect) Delete(tx *gorm.DB) (err error) {
|
||||
_, err = dialect.DB.Exec(s.String(), args...)
|
||||
return
|
||||
}
|
||||
|
||||
// AutoMigrate auto migrate database
|
||||
func (dialect *Dialect) AutoMigrate(value interface{}) (err error) {
|
||||
// create table
|
||||
|
||||
// create missed column
|
||||
|
||||
// safe upgrade some fields (like size, change data type)
|
||||
|
||||
// create missed foreign key
|
||||
|
||||
// create missed index
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dialect *Dialect) HasTable(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (dialect *Dialect) CreateTable(value interface{}) error {
|
||||
s := schema.Parse(value)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user