forked from mirror/gorm
Add support for sqlite3
This commit is contained in:
parent
7a23685e0b
commit
0b22775dd7
|
@ -60,9 +60,11 @@ type Address struct { // TableName: `addresses`
|
||||||
import "github.com/jinzhu/gorm"
|
import "github.com/jinzhu/gorm"
|
||||||
import _ "github.com/lib/pq"
|
import _ "github.com/lib/pq"
|
||||||
// import _ "github.com/go-sql-driver/mysql"
|
// 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("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||||
// 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")
|
||||||
|
|
||||||
|
|
||||||
// Set the maximum idle database connections
|
// 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
|
* Auto Migration
|
||||||
* SQL Log
|
* SQL Log
|
||||||
* SQL Query with goroutines
|
* SQL Query with goroutines
|
||||||
* Only tested with postgres and mysql, confirm works with other database adaptors
|
|
||||||
|
|
||||||
# Author
|
# Author
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -68,7 +69,8 @@ func init() {
|
||||||
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
|
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
|
||||||
// CREATE DATABASE 'gorm';
|
// CREATE DATABASE 'gorm';
|
||||||
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
|
// 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 {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err))
|
panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err))
|
||||||
|
|
22
sql_type.go
22
sql_type.go
|
@ -7,6 +7,8 @@ import (
|
||||||
|
|
||||||
func getPrimaryKeySqlType(adaptor string, column interface{}, size int) string {
|
func getPrimaryKeySqlType(adaptor string, column interface{}, size int) string {
|
||||||
switch adaptor {
|
switch adaptor {
|
||||||
|
case "sqlite3":
|
||||||
|
return "INTEGER PRIMARY KEY"
|
||||||
case "mysql":
|
case "mysql":
|
||||||
suffix_str := " NOT NULL AUTO_INCREMENT PRIMARY KEY"
|
suffix_str := " NOT NULL AUTO_INCREMENT PRIMARY KEY"
|
||||||
switch column.(type) {
|
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 {
|
func getSqlType(adaptor string, column interface{}, size int) string {
|
||||||
switch adaptor {
|
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":
|
case "mysql":
|
||||||
switch column.(type) {
|
switch column.(type) {
|
||||||
case time.Time:
|
case time.Time:
|
||||||
|
|
Loading…
Reference in New Issue