mirror of
https://github.com/geektutu/7days-golang.git
synced 2024-04-21 12:32:11 +00:00
fix Drop table
This commit is contained in:
@@ -2,7 +2,6 @@ package session
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"geeorm/log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -11,18 +10,10 @@ import (
|
||||
|
||||
var TestDB *sql.DB
|
||||
|
||||
func setup() {
|
||||
TestDB, _ = sql.Open("sqlite3", "gee.db")
|
||||
log.SetLevel(log.ErrorLevel)
|
||||
}
|
||||
|
||||
func teardown() {
|
||||
_ = TestDB.Close()
|
||||
}
|
||||
func TestMain(m *testing.M) {
|
||||
setup()
|
||||
TestDB, _ = sql.Open("sqlite3", "gee.db")
|
||||
code := m.Run()
|
||||
teardown()
|
||||
_ = TestDB.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
@@ -31,20 +22,18 @@ func NewSession() *Session {
|
||||
}
|
||||
|
||||
func TestSession_Exec(t *testing.T) {
|
||||
_, _ = NewSession().Raw("DROP TABLE USER;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE USER(name text);").Exec()
|
||||
result, _ := NewSession().
|
||||
Raw("INSERT INTO USER(`name`) values (?), (?)", "Tom", "Sam").Exec()
|
||||
_, _ = NewSession().Raw("DROP TABLE IF EXISTS User;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE User(name text);").Exec()
|
||||
result, _ := NewSession().Raw("INSERT INTO User(`name`) values (?), (?)", "Tom", "Sam").Exec()
|
||||
if count, err := result.RowsAffected(); err != nil || count != 2 {
|
||||
t.Fatal("expect 2, but got", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSession_QueryRow(t *testing.T) {
|
||||
_, _ = NewSession().Raw("DROP TABLE USER;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE USER(name text);").Exec()
|
||||
row := NewSession().Raw("SELECT count(*) FROM USER").QueryRow()
|
||||
|
||||
func TestSession_QueryRows(t *testing.T) {
|
||||
_, _ = NewSession().Raw("DROP TABLE IF EXISTS User;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE User(name text);").Exec()
|
||||
row := NewSession().Raw("SELECT count(*) FROM User").QueryRow()
|
||||
var count int
|
||||
if err := row.Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("failed to query db", err)
|
||||
|
||||
@@ -6,27 +6,19 @@ import (
|
||||
"testing"
|
||||
|
||||
"geeorm/dialect"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var (
|
||||
TestDB *sql.DB
|
||||
TestDial dialect.Dialect
|
||||
TestDB *sql.DB
|
||||
TestDial, _ = dialect.GetDialect("sqlite3")
|
||||
)
|
||||
|
||||
func setup() {
|
||||
TestDB, _ = sql.Open("sqlite3", "gee.db")
|
||||
TestDial, _ = dialect.GetDialect("sqlite3")
|
||||
}
|
||||
|
||||
func teardown() {
|
||||
_ = TestDB.Close()
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
setup()
|
||||
TestDB, _ = sql.Open("sqlite3", "gee.db")
|
||||
code := m.Run()
|
||||
teardown()
|
||||
_ = TestDB.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
@@ -35,18 +27,18 @@ func NewSession() *Session {
|
||||
}
|
||||
|
||||
func TestSession_Exec(t *testing.T) {
|
||||
_, _ = NewSession().Raw("DROP TABLE USER;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE USER(name text);").Exec()
|
||||
result, _ := NewSession().Raw("INSERT INTO USER(`name`) values (?), (?)", "Tom", "Sam").Exec()
|
||||
_, _ = NewSession().Raw("DROP TABLE IF EXISTS User;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE User(name text);").Exec()
|
||||
result, _ := NewSession().Raw("INSERT INTO User(`name`) values (?), (?)", "Tom", "Sam").Exec()
|
||||
if count, err := result.RowsAffected(); err != nil || count != 2 {
|
||||
t.Fatal("expect 2, but got", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSession_QueryRows(t *testing.T) {
|
||||
_, _ = NewSession().Raw("DROP TABLE USER;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE USER(name text);").Exec()
|
||||
row := NewSession().Raw("SELECT count(*) FROM USER").QueryRow()
|
||||
_, _ = NewSession().Raw("DROP TABLE IF EXISTS User;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE User(name text);").Exec()
|
||||
row := NewSession().Raw("SELECT count(*) FROM User").QueryRow()
|
||||
var count int
|
||||
if err := row.Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("failed to query db", err)
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"geeorm/log"
|
||||
"geeorm/schema"
|
||||
)
|
||||
|
||||
@@ -38,7 +37,7 @@ func (s *Session) CreateTable(value interface{}) error {
|
||||
// DropTable drops a table with the name of model
|
||||
func (s *Session) DropTable(value interface{}) error {
|
||||
table := s.RefTable(value)
|
||||
_, err := s.Raw(fmt.Sprintf("DROP TABLE %s", table.TableName)).Exec()
|
||||
_, err := s.Raw(fmt.Sprintf("DROP TABLE IF EXISTS %s", table.TableName)).Exec()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -52,8 +51,6 @@ func (s *Session) HasTable(value interface{}) bool {
|
||||
sql, values := s.dialect.TableExistSQL(tableName)
|
||||
row := s.Raw(sql, values...).QueryRow()
|
||||
var tmp string
|
||||
if err := row.Scan(&tmp); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
_ = row.Scan(&tmp)
|
||||
return tmp == tableName
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func TestClause_Set(t *testing.T) {
|
||||
var clause Clause
|
||||
clause.Set(INSERT, "User", "Name,Age")
|
||||
clause.Set(INSERT, "User", []string{"Name", "Age"})
|
||||
sql := clause.sql[INSERT]
|
||||
vars := clause.sqlVars[INSERT]
|
||||
t.Log(sql, vars)
|
||||
@@ -19,7 +19,7 @@ func TestClause_Set(t *testing.T) {
|
||||
func TestClause_Build(t *testing.T) {
|
||||
var clause Clause
|
||||
clause.Set(LIMIT, 3)
|
||||
clause.Set(SELECT, "User", "*")
|
||||
clause.Set(SELECT, "User", []string{"*"})
|
||||
orders := []Type{SELECT, LIMIT}
|
||||
sql, vars := clause.Build(orders)
|
||||
t.Log(sql, vars)
|
||||
|
||||
@@ -2,7 +2,6 @@ package clause
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -29,7 +28,7 @@ func genBindVars(num int) string {
|
||||
func _insert(values ...interface{}) (string, []interface{}) {
|
||||
// INSERT INTO $tableName ($fields)
|
||||
tableName := values[0]
|
||||
fields := values[1]
|
||||
fields := strings.Join(values[1].([]string), ",")
|
||||
return fmt.Sprintf("INSERT INTO %s (%v)", tableName, fields), []interface{}{}
|
||||
}
|
||||
|
||||
@@ -57,12 +56,11 @@ func _values(values ...interface{}) (string, []interface{}) {
|
||||
func _select(values ...interface{}) (string, []interface{}) {
|
||||
// SELECT $fields FROM $tableName
|
||||
tableName := values[0]
|
||||
fields := values[1]
|
||||
fields := strings.Join(values[1].([]string), ",")
|
||||
return fmt.Sprintf("SELECT %v FROM %s", fields, tableName), []interface{}{}
|
||||
}
|
||||
|
||||
func _limit(values ...interface{}) (string, []interface{}) {
|
||||
// LIMIT $num
|
||||
log.Println(values...)
|
||||
return "LIMIT ?", values
|
||||
}
|
||||
|
||||
@@ -6,27 +6,19 @@ import (
|
||||
"testing"
|
||||
|
||||
"geeorm/dialect"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var (
|
||||
TestDB *sql.DB
|
||||
TestDial dialect.Dialect
|
||||
TestDB *sql.DB
|
||||
TestDial, _ = dialect.GetDialect("sqlite3")
|
||||
)
|
||||
|
||||
func setup() {
|
||||
TestDB, _ = sql.Open("sqlite3", "gee.db")
|
||||
TestDial, _ = dialect.GetDialect("sqlite3")
|
||||
}
|
||||
|
||||
func teardown() {
|
||||
_ = TestDB.Close()
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
setup()
|
||||
TestDB, _ = sql.Open("sqlite3", "gee.db")
|
||||
code := m.Run()
|
||||
teardown()
|
||||
_ = TestDB.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
@@ -35,18 +27,18 @@ func NewSession() *Session {
|
||||
}
|
||||
|
||||
func TestSession_Exec(t *testing.T) {
|
||||
_, _ = NewSession().Raw("DROP TABLE USER;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE USER(name text);").Exec()
|
||||
result, _ := NewSession().Raw("INSERT INTO USER(`name`) values (?), (?)", "Tom", "Sam").Exec()
|
||||
_, _ = NewSession().Raw("DROP TABLE IF EXISTS User;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE User(name text);").Exec()
|
||||
result, _ := NewSession().Raw("INSERT INTO User(`name`) values (?), (?)", "Tom", "Sam").Exec()
|
||||
if count, err := result.RowsAffected(); err != nil || count != 2 {
|
||||
t.Fatal("expect 2, but got", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSession_QueryRows(t *testing.T) {
|
||||
_, _ = NewSession().Raw("DROP TABLE USER;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE USER(name text);").Exec()
|
||||
row := NewSession().Raw("SELECT count(*) FROM USER").QueryRow()
|
||||
_, _ = NewSession().Raw("DROP TABLE IF EXISTS User;").Exec()
|
||||
_, _ = NewSession().Raw("CREATE TABLE User(name text);").Exec()
|
||||
row := NewSession().Raw("SELECT count(*) FROM User").QueryRow()
|
||||
var count int
|
||||
if err := row.Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("failed to query db", err)
|
||||
|
||||
@@ -3,20 +3,14 @@ package session
|
||||
import (
|
||||
"geeorm/clause"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Create one or more records in database
|
||||
func (s *Session) Create(values ...interface{}) (int64, error) {
|
||||
var flag bool
|
||||
recordValues := make([]interface{}, 0)
|
||||
for _, value := range values {
|
||||
table := s.RefTable(value)
|
||||
if !flag {
|
||||
flag = true
|
||||
fieldSQL := strings.Join(table.FieldNames, ", ")
|
||||
s.clause.Set(clause.INSERT, table.TableName, fieldSQL)
|
||||
}
|
||||
s.clause.Set(clause.INSERT, table.TableName, table.FieldNames)
|
||||
recordValues = append(recordValues, table.Values(value))
|
||||
}
|
||||
|
||||
@@ -34,9 +28,7 @@ func (s *Session) Create(values ...interface{}) (int64, error) {
|
||||
func (s *Session) First(value interface{}) error {
|
||||
table := s.RefTable(value)
|
||||
|
||||
fieldSQL := strings.Join(table.FieldNames, ", ")
|
||||
|
||||
s.clause.Set(clause.SELECT, table.TableName, fieldSQL)
|
||||
s.clause.Set(clause.SELECT, table.TableName, table.FieldNames)
|
||||
s.clause.Set(clause.LIMIT, 1)
|
||||
|
||||
sql, vars := s.clause.Build([]clause.Type{clause.SELECT, clause.LIMIT})
|
||||
@@ -57,8 +49,7 @@ func (s *Session) Find(values interface{}) error {
|
||||
destType := destSlice.Type().Elem()
|
||||
table := s.RefTable(reflect.New(destType).Elem().Interface())
|
||||
|
||||
fieldSQL := strings.Join(table.FieldNames, ", ")
|
||||
s.clause.Set(clause.SELECT, table.TableName, fieldSQL)
|
||||
s.clause.Set(clause.SELECT, table.TableName, table.FieldNames)
|
||||
sql, vars := s.clause.Build([]clause.Type{clause.SELECT})
|
||||
rows, err := s.Raw(sql, vars...).QueryRows()
|
||||
if err != nil {
|
||||
@@ -76,8 +67,5 @@ func (s *Session) Find(values interface{}) error {
|
||||
}
|
||||
destSlice.Set(reflect.Append(destSlice, dest))
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return rows.Close()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"geeorm/log"
|
||||
"geeorm/schema"
|
||||
)
|
||||
|
||||
@@ -38,7 +37,7 @@ func (s *Session) CreateTable(value interface{}) error {
|
||||
// DropTable drops a table with the name of model
|
||||
func (s *Session) DropTable(value interface{}) error {
|
||||
table := s.RefTable(value)
|
||||
_, err := s.Raw(fmt.Sprintf("DROP TABLE %s", table.TableName)).Exec()
|
||||
_, err := s.Raw(fmt.Sprintf("DROP TABLE IF EXISTS %s", table.TableName)).Exec()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -52,8 +51,6 @@ func (s *Session) HasTable(value interface{}) bool {
|
||||
sql, values := s.dialect.TableExistSQL(tableName)
|
||||
row := s.Raw(sql, values...).QueryRow()
|
||||
var tmp string
|
||||
if err := row.Scan(&tmp); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
_ = row.Scan(&tmp)
|
||||
return tmp == tableName
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user