diff --git a/README.md b/README.md index 0180babb..4f354620 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,11 @@ type Address struct { // TableName: `addresses` import "github.com/jinzhu/gorm" import _ "github.com/lib/pq" // import _ "github.com/go-sql-driver/mysql" +// import _ "github.com/mattn/go-sqlite3" db, err := Open("postgres", "user=gorm dbname=gorm sslmode=disable") // db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") +// db, err = Open("sqlite3", "/tmp/gorm.db") // Set the maximum idle database connections @@ -620,7 +622,6 @@ db.Where("email = ?", "x@example.org").Attrs(User{FromIp: "111.111.111.111"}).Fi * Auto Migration * SQL Log * SQL Query with goroutines -* Only tested with postgres and mysql, confirm works with other database adaptors # Author diff --git a/gorm_test.go b/gorm_test.go index c1204da6..1e6261e1 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -5,6 +5,7 @@ import ( "fmt" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" + _ "github.com/mattn/go-sqlite3" "reflect" "strconv" "testing" @@ -68,7 +69,8 @@ func init() { // CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm'; // CREATE DATABASE 'gorm'; // GRANT ALL ON gorm.* TO 'gorm'@'localhost'; - db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") + // db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") + // db, err = Open("sqlite3", "/tmp/gorm.db") if err != nil { panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err)) diff --git a/sql_type.go b/sql_type.go index 2f57ba1d..31ff880d 100644 --- a/sql_type.go +++ b/sql_type.go @@ -7,6 +7,8 @@ import ( func getPrimaryKeySqlType(adaptor string, column interface{}, size int) string { switch adaptor { + case "sqlite3": + return "INTEGER PRIMARY KEY" case "mysql": suffix_str := " NOT NULL AUTO_INCREMENT PRIMARY KEY" switch column.(type) { @@ -28,6 +30,26 @@ func getPrimaryKeySqlType(adaptor string, column interface{}, size int) string { func getSqlType(adaptor string, column interface{}, size int) string { switch adaptor { + case "sqlite3": + switch column.(type) { + case time.Time: + return "datetime" + case bool: + return "bool" + case int, int8, int16, int32, uint, uint8, uint16, uint32: + return "integer" + case int64, uint64: + return "bigint" + case float32, float64: + return "real" + case string: + if size > 0 && size < 65532 { + return fmt.Sprintf("varchar(%d)", size) + } + return "text" + default: + panic("invalid sql type") + } case "mysql": switch column.(type) { case time.Time: