Skip to content

Commit

Permalink
Add sqlite3
Browse files Browse the repository at this point in the history
* All tests now also pass with sqlite3
* Used go-sqlite3 repository by @mattn available at
  https://github.com/mattn/go-sqlite3
  • Loading branch information
olivere committed Dec 1, 2013
1 parent 4cbfe21 commit ad3a18f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.db
66 changes: 40 additions & 26 deletions dapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package dapper
import (
"database/sql"
"fmt"
"os"
"reflect"
"testing"
"time"

_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
_ "github.com/ziutek/mymysql/godrv"
)

Expand All @@ -18,7 +20,7 @@ const (
)

var (
drivers = []string{"mymysql", "mysql"}
drivers = []string{"mymysql", "mysql", "sqlite3"}
)

// ---- Test tables ----------------------------------------------------------
Expand Down Expand Up @@ -166,11 +168,18 @@ func setup(driver string, t *testing.T) (db *sql.DB) {
if err != nil {
t.Fatalf("error connection to database: %v", err)
}
case "sqlite3":
os.Remove("./" + testDBName + ".db")
connectionString := fmt.Sprintf("./%s.db", testDBName)
db, err = sql.Open("sqlite3", connectionString)
if err != nil {
t.Fatalf("error connection to database: %v", err)
}
}
return seed(t, db)
return seed(driver, t, db)
}

func seed(t *testing.T, db *sql.DB) *sql.DB {
func seed(driver string, t *testing.T, db *sql.DB) *sql.DB {
// Drop tables
_, err := db.Exec("DROP TABLE IF EXISTS tweets")
if err != nil {
Expand Down Expand Up @@ -198,53 +207,60 @@ func seed(t *testing.T, db *sql.DB) *sql.DB {
}

// Create tables
autoinc := "AUTO_INCREMENT"
if driver == "sqlite3" {
autoinc = "AUTOINCREMENT"
}
timestampCol := "timestamp NOT NULL " +
"DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
if driver == "sqlite3" {
timestampCol = "datetime NOT NULL " +
"DEFAULT CURRENT_TIMESTAMP"
}
_, err = db.Exec(`
CREATE TABLE cruddy (
id int(11) not null auto_increment,
c_int int(11),
c_int32 int(11),
c_int64 int(11),
c_uint int(11),
c_uint32 int(11),
c_uint64 int(11),
id integer not null primary key ` + autoinc + `,
c_int integer,
c_int32 integer,
c_int64 integer,
c_uint integer,
c_uint32 integer,
c_uint64 integer,
c_float32 float,
c_float64 float,
c_decimal decimal(19,5),
c_date date,
c_date_ptr date,
c_datetime datetime,
c_datetime_ptr datetime,
c_timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
c_timestamp ` + timestampCol + `,
c_bool bool,
c_char char(3),
c_varchar varchar(20),
c_text text,
primary key (id)
c_text text
)`)
if err != nil {
t.Fatalf("error creating cruddy table: %v", err)
}

_, err = db.Exec(`
CREATE TABLE users (
id int(11) not null auto_increment,
id integer not null primary key ` + autoinc + `,
name varchar(100) not null,
karma decimal(19,5),
suspended tinyint(1) default '0',
primary key (id)
suspended tinyint(1) default '0'
)`)
if err != nil {
t.Fatalf("error creating users table: %v", err)
}

_, err = db.Exec(`
CREATE TABLE tweets (
id int(11) not null auto_increment,
user_id int(11) not null,
id integer not null primary key ` + autoinc + `,
user_id integer not null,
message text,
retweets int,
retweets integer,
created timestamp not null default current_timestamp,
primary key (id),
foreign key (user_id) references users (id) on delete cascade
)`)
if err != nil {
Expand All @@ -253,22 +269,20 @@ CREATE TABLE tweets (

_, err = db.Exec(`
CREATE TABLE orders (
id int(11) not null auto_increment,
ref_id varchar(100) not null,
primary key (id)
id integer not null primary key ` + autoinc + `,
ref_id varchar(100) not null
)`)
if err != nil {
t.Fatalf("error creating orders table: %v", err)
}

_, err = db.Exec(`
CREATE TABLE order_items (
id int(11) not null auto_increment,
order_id int(11) not null,
id integer not null primary key ` + autoinc + `,
order_id integer not null,
name varchar(100) not null,
price decimal(15,3) not null,
qty decimal(10,3) not null,
primary key (id),
foreign key (order_id) references orders (id) on delete cascade
)`)
if err != nil {
Expand Down

0 comments on commit ad3a18f

Please sign in to comment.