forked from mirror/gorm
Support create table with struct
This commit is contained in:
parent
73cac58998
commit
3c68efc94a
|
@ -4,7 +4,6 @@ Yet Another ORM library for Go, aims for developer friendly
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
* Perimary key
|
* Perimary key
|
||||||
* Create table
|
|
||||||
* Save and fill the record
|
* Save and fill the record
|
||||||
* Update
|
* Update
|
||||||
* Delete
|
* Delete
|
||||||
|
@ -21,7 +20,6 @@ Yet Another ORM library for Go, aims for developer friendly
|
||||||
* Soft Delete
|
* Soft Delete
|
||||||
* After/Before Save/Update/Create/Delete
|
* After/Before Save/Update/Create/Delete
|
||||||
* FindOrInitialize / FindOrCreate
|
* FindOrInitialize / FindOrCreate
|
||||||
* Exec run SQL directly
|
|
||||||
* SQL Log
|
* SQL Log
|
||||||
* Auto Migration
|
* Auto Migration
|
||||||
* Index, Unique, Valiations
|
* Index, Unique, Valiations
|
||||||
|
|
6
main.go
6
main.go
|
@ -6,17 +6,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
Db *sql.DB
|
Db *sql.DB
|
||||||
|
Driver string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open(driver, source string) (db DB, err error) {
|
func Open(driver, source string) (db DB, err error) {
|
||||||
db.Db, err = sql.Open(driver, source)
|
db.Db, err = sql.Open(driver, source)
|
||||||
|
db.Driver = driver
|
||||||
// SetMaxIdleConns pools
|
// SetMaxIdleConns pools
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) buildORM() *Orm {
|
func (s *DB) buildORM() *Orm {
|
||||||
orm := &Orm{db: s.Db}
|
orm := &Orm{db: s.Db, driver: s.Driver}
|
||||||
return orm
|
return orm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
65
model.go
65
model.go
|
@ -1,19 +1,43 @@
|
||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"fmt"
|
||||||
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Data interface{}
|
Data interface{}
|
||||||
|
driver string
|
||||||
}
|
}
|
||||||
|
|
||||||
func toModel(value interface{}) *Model {
|
type Field struct {
|
||||||
var model Model
|
Name string
|
||||||
model.Data = value
|
Value interface{}
|
||||||
return &model
|
SqlType string
|
||||||
|
DbName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Orm) toModel(value interface{}) *Model {
|
||||||
|
return &Model{Data: value, driver: s.driver}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) Fields() (fields []Field) {
|
||||||
|
typ := reflect.TypeOf(m.Data).Elem()
|
||||||
|
|
||||||
|
for i := 0; i < typ.NumField(); i++ {
|
||||||
|
p := typ.Field(i)
|
||||||
|
if !p.Anonymous {
|
||||||
|
var field Field
|
||||||
|
field.Name = p.Name
|
||||||
|
field.DbName = toSnake(p.Name)
|
||||||
|
field.Value = reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Interface()
|
||||||
|
field.SqlType = getSqlType(m.driver, field.Value, 0)
|
||||||
|
fields = append(fields, field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) ColumnsAndValues() (columns []string, values []interface{}) {
|
func (m *Model) ColumnsAndValues() (columns []string, values []interface{}) {
|
||||||
|
@ -47,19 +71,6 @@ func (m *Model) TableName() string {
|
||||||
return reg.ReplaceAllString(toSnake(t.Name()), "s")
|
return reg.ReplaceAllString(toSnake(t.Name()), "s")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) Columns() (columns []string) {
|
|
||||||
typ := reflect.TypeOf(m.Data).Elem()
|
|
||||||
|
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
|
||||||
p := typ.Field(i)
|
|
||||||
if !p.Anonymous {
|
|
||||||
columns = append(columns, toSnake(p.Name))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (model *Model) MissingColumns() (results []string) {
|
func (model *Model) MissingColumns() (results []string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -67,3 +78,17 @@ func (model *Model) MissingColumns() (results []string) {
|
||||||
func (model *Model) ColumnType(column string) (result string) {
|
func (model *Model) ColumnType(column string) (result string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (model *Model) CreateTable() (sql string) {
|
||||||
|
var sqls []string
|
||||||
|
for _, field := range model.Fields() {
|
||||||
|
sqls = append(sqls, field.DbName+" "+field.SqlType)
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = fmt.Sprintf(
|
||||||
|
"CREATE TABLE \"%v\" (%v)",
|
||||||
|
model.TableName(),
|
||||||
|
strings.Join(sqls, ","),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
4
orm.go
4
orm.go
|
@ -16,6 +16,7 @@ type Orm struct {
|
||||||
Model *Model
|
Model *Model
|
||||||
|
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
|
driver string
|
||||||
whereClause []map[string]interface{}
|
whereClause []map[string]interface{}
|
||||||
selectStr string
|
selectStr string
|
||||||
orderStr string
|
orderStr string
|
||||||
|
@ -25,7 +26,7 @@ type Orm struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) setModel(model interface{}) (err error) {
|
func (s *Orm) setModel(model interface{}) (err error) {
|
||||||
s.Model = toModel(model)
|
s.Model = s.toModel(model)
|
||||||
s.TableName = s.Model.TableName()
|
s.TableName = s.Model.TableName()
|
||||||
s.PrimaryKey = "id"
|
s.PrimaryKey = "id"
|
||||||
return
|
return
|
||||||
|
@ -130,5 +131,6 @@ func (s *Orm) Not(querystring interface{}, args ...interface{}) *Orm {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) CreateTable(value interface{}) *Orm {
|
func (s *Orm) CreateTable(value interface{}) *Orm {
|
||||||
|
s.explain(value, "CreateTable").Exec()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
11
orm_test.go
11
orm_test.go
|
@ -4,6 +4,7 @@ import "testing"
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Name string
|
Name string
|
||||||
|
Id int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDB() DB {
|
func getDB() DB {
|
||||||
|
@ -55,3 +56,13 @@ func TestWhere(t *testing.T) {
|
||||||
t.Errorf("Shouldn't return error when looking for unexist records, %+v", users)
|
t.Errorf("Shouldn't return error when looking for unexist records, %+v", users)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateTable(t *testing.T) {
|
||||||
|
db := getDB()
|
||||||
|
db.Exec("drop table users;")
|
||||||
|
|
||||||
|
orm := db.CreateTable(&User{})
|
||||||
|
if orm.Error != nil {
|
||||||
|
t.Errorf("No error should raise when create table, but got %+v", orm.Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
11
sql.go
11
sql.go
|
@ -18,7 +18,7 @@ func (s *Orm) explain(value interface{}, operation string) *Orm {
|
||||||
case "Query":
|
case "Query":
|
||||||
s.querySql(value)
|
s.querySql(value)
|
||||||
case "CreateTable":
|
case "CreateTable":
|
||||||
s.createTableSql(value)
|
s.Sql = s.Model.CreateTable()
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
@ -103,12 +103,3 @@ func (s *Orm) whereSql() (sql string) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) createTableSql(value interface{}) {
|
|
||||||
s.Sql = fmt.Sprintf(
|
|
||||||
"CREATE TABLE \"%v\" (%v)",
|
|
||||||
s.TableName,
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue