Refactor sqlite schema
This commit is contained in:
parent
0ec761558c
commit
1d6ee71300
@ -1,7 +1,7 @@
|
|||||||
package sqlite
|
package sqlite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AutoMigrate auto migrate database
|
// AutoMigrate auto migrate database
|
||||||
@ -18,30 +18,65 @@ func (dialect *Dialect) AutoMigrate(value interface{}) (err error) {
|
|||||||
return nil
|
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
|
// CreateTable create table for value
|
||||||
func (dialect *Dialect) CreateTable(value interface{}) error {
|
func (dialect *Dialect) CreateTable(value interface{}) error {
|
||||||
// s := schema.Parse(value)
|
// s := schema.Parse(value)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurrentDatabase get current database name
|
// HasTable check if has table
|
||||||
func (dialect *Dialect) CurrentDatabase() (name string) {
|
func (dialect *Dialect) HasTable(tableName string) bool {
|
||||||
_ = dialect.DB.QueryRow("SELECT DATABASE()").Scan(&name)
|
var count int
|
||||||
return
|
_ = dialect.DB.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", tableName).Scan(&count)
|
||||||
|
return count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentDatabaseAndTable(dialect *Dialect, tableName string) (string, string) {
|
// HasColumn check if has column in table
|
||||||
if strings.Contains(tableName, ".") {
|
func (dialect *Dialect) HasColumn(tableName string, columnName string) bool {
|
||||||
splitStrings := strings.SplitN(tableName, ".", 2)
|
var count int
|
||||||
return splitStrings[0], splitStrings[1]
|
_ = dialect.DB.QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND (sql LIKE '%%\"%v\" %%' OR sql LIKE '%%%v %%');\n", columnName, columnName), tableName).Scan(&count)
|
||||||
|
return count > 0
|
||||||
}
|
}
|
||||||
return dialect.CurrentDatabase(), tableName
|
|
||||||
|
// HasIndex check if has index in table
|
||||||
|
func (dialect *Dialect) HasIndex(tableName string, indexName string) bool {
|
||||||
|
var count int
|
||||||
|
_ = dialect.DB.QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND sql LIKE '%%INDEX %v ON%%'", indexName), tableName).Scan(&count)
|
||||||
|
return count > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasForeignKey check if has foreign key in table
|
||||||
|
func (dialect *Dialect) HasForeignKey(tableName string, foreignKeyName string) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveIndex remove index from table
|
||||||
|
func (dialect *Dialect) RemoveIndex(tableName string, indexName string) error {
|
||||||
|
_, err := dialect.DB.Exec(fmt.Sprintf("DROP INDEX %v", indexName))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModifyColumn modify column
|
||||||
|
func (dialect *Dialect) ModifyColumn(tableName string, columnName string, typ string) error {
|
||||||
|
_, err := dialect.DB.Exec(fmt.Sprintf("ALTER TABLE %v ALTER COLUMN %v TYPE %v", tableName, columnName, typ))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentDatabase get current database name
|
||||||
|
func (dialect *Dialect) CurrentDatabase() (name string) {
|
||||||
|
var (
|
||||||
|
ifaces = make([]interface{}, 3)
|
||||||
|
pointers = make([]*string, 3)
|
||||||
|
i int
|
||||||
|
)
|
||||||
|
for i = 0; i < 3; i++ {
|
||||||
|
ifaces[i] = &pointers[i]
|
||||||
|
}
|
||||||
|
if err := dialect.DB.QueryRow("PRAGMA database_list").Scan(ifaces...); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pointers[1] != nil {
|
||||||
|
name = *pointers[1]
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user