func TestStructWithPointerLoad(t *testing.T) {

	db := getMoviesDb()

	msql := &Movie{}
	if res, err := db.Exec("INSERT INTO movies (title, genre, budget) VALUES ('2001: A Space Odyssey', 'Science-Fiction', 1500000)"); err != nil {
		t.Fatalf("Sqlite Exec failed: %s", err)
	} else if msql.Id, err = res.LastInsertId(); err != nil {
		t.Fatalf("Sqlite LastInsertId failed: %s", err)
	}

	m := &Movie{Id: msql.Id, Genre: new(string)}
	m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
	if err := m.Load(); err != nil {
		t.Fatalf("Failed Load: %s", err)
	}

	if !CompareStringPtr(m.Genre, stringPtr("Science-Fiction")) {
		t.Fatal("Load should load pointer fields")
	}

	m.Genre = nil
	m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
	if err := m.Load(); err != nil {
		t.Fatalf("Failed Load: %s", err)
	}

	if !CompareStringPtr(m.Genre, stringPtr("Science-Fiction")) {
		t.Fatal("Load should instantiate nil pointers")
	}
}
func TestStructWithPointerInsert(t *testing.T) {

	db := getMoviesDb()

	m := &Movie{
		Id:     -1,
		Title:  "2001: A Space Odyssey",
		Genre:  stringPtr("Science-Fiction"),
		Budget: 1500000}
	m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)

	if err := m.Insert(); err != nil {
		t.Fatalf("Failed Insert: %s", err)
	}

	msql := new(Movie)
	msql.loadFromSql(m.Id, db)

	if *msql.Genre != "Science-Fiction" {
		t.Fatal("Insert should dereference allocated pointers")
	}

	m.Genre = nil
	if err := m.Insert(); err != nil {
		t.Fatalf("Failed Insert: %s", err)
	}

	msql.loadFromSql(m.Id, db)

	if *msql.Genre != "unclassifiable" {
		t.Fatal("Insert should ignore nil pointers")
	}
}
Esempio n. 3
0
// Open returns a wrapped *sql.DB and starts services
func Open(connectionString string) (*DB, error) {
	db, err := sql.Open("postgres", connectionString)
	if err != nil {
		return nil, err
	}
	return &DB{db, squirrel.NewStmtCacheProxy(db)}, nil
}
func TestStructWithPointerUpdate(t *testing.T) {

	db := getMoviesDb()

	var lastId int64
	if res, err := db.Exec("INSERT INTO movies (title, genre, budget) VALUES ('2001: A Space Odyssey', 'Science-Fiction', 1500000)"); err != nil {
		t.Fatalf("Sqlite Exec failed: %s", err)
	} else if lastId, err = res.LastInsertId(); err != nil {
		t.Fatalf("Sqlite LastInsertId failed: %s", err)
	}

	m := &Movie{
		Id:     lastId,
		Title:  "The Usual Suspects",
		Genre:  nil,
		Budget: 6000000}

	m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
	if err := m.Update(); err != nil {
		t.Fatalf("Failed Update: %s", err)
	}

	msql := new(Movie)
	msql.loadFromSql(lastId, db)

	if !CompareStringPtr(msql.Genre, stringPtr("Science-Fiction")) {
		t.Fatal("Update should ignore nil pointers")
	}

	m.Genre = stringPtr("Crime Thriller")
	m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
	if err := m.Update(); err != nil {
		t.Fatalf("Failed Update: %s", err)
	}

	msql.loadFromSql(lastId, db)
	if !CompareStringPtr(msql.Genre, stringPtr("Crime Thriller")) {
		t.Log("msql.Genre: %v\n", *msql.Genre)
		t.Fatal("Update should ignore nil pointers")
	}
}
Esempio n. 5
0
func main() {

	// Boilerplate DB setup.
	// First, we need to know the database driver.
	driver := "postgres"
	// Second, we need a database connection.
	con, _ := sql.Open(driver, "dbname=structable_test sslmode=disable")
	// Third, we wrap in a prepared statement cache for better performance.
	cache := squirrel.NewStmtCacheProxy(con)

	// Create an empty new user and give it some properties.
	user := NewUser(cache, driver)
	user.Name = "Matt"
	user.Email = "*****@*****.**"

	// Insert this as a new record.
	if err := user.Insert(); err != nil {
		panic(err.Error())
	}
	fmt.Printf("Initial insert has ID %d, name %s, and email %s\n", user.Id, user.Name, user.Email)

	// Now create another empty User and set the user's Name.
	again := NewUser(cache, driver)
	again.Name = "Matt"

	// Load using our custom loader.
	if err := again.LoadByName(); err != nil {
		panic(err.Error())
	}
	fmt.Printf("User by name has ID %d and email %s\n", again.Id, again.Email)

	again.Email = "*****@*****.**"
	if err := again.Update(); err != nil {
		panic(err.Error())
	}
	fmt.Printf("Updated user has ID %d and email %s\n", again.Id, again.Email)

	// Delete using the built-in Deleter. (delete by Id.)
	if err := again.Delete(); err != nil {
		panic(err.Error())
	}
	fmt.Printf("Deleted user %d\n", again.Id)
}
Esempio n. 6
0
func Connect(user string, password string, database string, host string) error {
	c, err := sql.Open("mysql", fmt.Sprintf("%v:%v@%v/%v", user, password, host, database))
	Conn = c
	Lua = squirrel.NewStmtCacheProxy(Conn)
	return err
}