diff --git a/mysql/database_test.go b/mysql/database_test.go index 9f395fb883ee66b8c6803310899b8c0ce3bd80db..3a22d56be60fc9fb7691243cab307668f23614ee 100644 --- a/mysql/database_test.go +++ b/mysql/database_test.go @@ -39,6 +39,7 @@ import ( "menteslibres.net/gosexy/to" "upper.io/db" + "upper.io/db/util/sqlutil" ) const ( @@ -767,6 +768,53 @@ func TestRawRelations(t *testing.T) { } +func TestRawQuery(t *testing.T) { + var sess db.Database + var rows *sql.Rows + var err error + var drv *sql.DB + + type publication_t struct { + Id int64 `db:"id,omitempty"` + Title string `db:"title"` + AuthorId int64 `db:"author_id"` + } + + if sess, err = db.Open(Adapter, settings); err != nil { + t.Fatal(err) + } + + defer sess.Close() + + drv = sess.Driver().(*sql.DB) + + rows, err = drv.Query(` + SELECT + p.id, + p.title AS publication_title, + a.name AS artist_name + FROM + artist AS a, + publication AS p + WHERE + a.id = p.author_id + `) + + if err != nil { + t.Fatal(err) + } + + var all []publication_t + + if err = sqlutil.FetchRows(rows, &all); err != nil { + t.Fatal(err) + } + + if len(all) != 9 { + t.Fatalf("Expecting some rows.") + } +} + // Attempts to test database transactions. func TestTransactionsAndRollback(t *testing.T) { var sess db.Database diff --git a/postgresql/database_test.go b/postgresql/database_test.go index ce1b11c140a45d320479c4268469225b799a426f..1480d1e35a981d849605890fc1f457b670a3833d 100644 --- a/postgresql/database_test.go +++ b/postgresql/database_test.go @@ -39,6 +39,7 @@ import ( "menteslibres.net/gosexy/to" "upper.io/db" + "upper.io/db/util/sqlutil" ) const ( @@ -790,6 +791,53 @@ func TestRawRelations(t *testing.T) { } +func TestRawQuery(t *testing.T) { + var sess db.Database + var rows *sql.Rows + var err error + var drv *sql.DB + + type publication_t struct { + Id int64 `db:"id,omitempty"` + Title string `db:"title"` + AuthorId int64 `db:"author_id"` + } + + if sess, err = db.Open(Adapter, settings); err != nil { + t.Fatal(err) + } + + defer sess.Close() + + drv = sess.Driver().(*sql.DB) + + rows, err = drv.Query(` + SELECT + p.id, + p.title AS publication_title, + a.name AS artist_name + FROM + artist AS a, + publication AS p + WHERE + a.id = p.author_id + `) + + if err != nil { + t.Fatal(err) + } + + var all []publication_t + + if err = sqlutil.FetchRows(rows, &all); err != nil { + t.Fatal(err) + } + + if len(all) != 9 { + t.Fatalf("Expecting some rows.") + } +} + // Attempts to test database transactions. func TestTransactionsAndRollback(t *testing.T) { var sess db.Database diff --git a/util/sqlutil/main.go b/util/sqlutil/main.go index 35e55eeeb87d1089230b45c885a51ad6bff7a731..c28f99b0b88f59ee44687e93d42c0cae81287f60 100644 --- a/util/sqlutil/main.go +++ b/util/sqlutil/main.go @@ -182,6 +182,8 @@ func getRowColumns(rows *sql.Rows) ([]string, error) { return columns, nil } +// FetchRow() receives a *sql.Rows value and tries to map all the rows into a +// single struct given by the pointer `dst`. func FetchRow(rows *sql.Rows, dst interface{}) error { dstv := reflect.ValueOf(dst) @@ -220,6 +222,8 @@ func FetchRow(rows *sql.Rows, dst interface{}) error { return nil } +// FetchRow() receives a *sql.Rows value and tries to map all the rows into a +// slice of structs given by the pointer `dst`. func FetchRows(rows *sql.Rows, dst interface{}) error { // Destination.