{"_id":"57531435be31940e0035ad9c","parentDoc":null,"category":{"_id":"57531435be31940e0035ad98","__v":0,"project":"56e5982f9191742000ef204a","version":"57531435be31940e0035ad96","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-18T19:27:31.446Z","from_sync":false,"order":1,"slug":"the-session","title":"Orm"},"__v":18,"user":"543466ea0e8e2b0e00341818","version":{"_id":"57531435be31940e0035ad96","project":"56e5982f9191742000ef204a","__v":3,"createdAt":"2016-06-04T17:47:33.623Z","releaseDate":"2016-06-04T17:47:33.623Z","categories":["57531435be31940e0035ad97","57531435be31940e0035ad98","57531652d8fe070e00258a51","576c609fba92910e0099ad3f"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.2.0","version":"0.2"},"project":"56e5982f9191742000ef204a","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-18T19:27:57.105Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"In qb, the session object is the most useful & simplest object. It has all the building blocks of qb. A typical session object has the following dependencies;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Session is the composition of engine connection & orm mappings\\ntype Session struct {\\n\\tqueries  []*Query  // queries in the current transaction\\n\\tmapper   *Mapper   // mapper object for shortcut funcs\\n\\tmetadata *MetaData // metadata object to keep table registry\\n\\ttx       *sql.Tx   // active transaction\\n\\tbuilder  *Builder  // query builder\\n  mutex    *sync.Mutex // mutex for preventing race conditions while opening a transaction\\n}\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nThe following object is the only object required to use every single function in qb. The other structs that can also build sql statements such as `Builder` is transactionless.\n\nFrom this point, the following qb initialization is assumed.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package main\\n\\nimport (\\n\\t\\\"github.com/aacanakin/qb\\\"\\n)\\n\\ntype User struct {\\n  ID string `qb:\\\"constraints:primary_key\\\"`\\n  Name string `qb:\\\"constraints:not_null\\\"`\\n  Email string `qb:\\\"constraints:unique, not_null\\\"`\\n}\\n\\nfunc main() {\\n\\n\\tdb, err := qb.New(\\\"postgres\\\", \\\"user=postgres dbname=qb_test sslmode=disable\\\")\\n\\tif err != nil {\\n\\t\\tpanic(err)\\n\\t}\\n\\tdefer db.Close()\\n\\n\\tdb.Metadata().Add(User{})\\n\\tdb.Metadata().CreateAll()\\n}\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Inserting\"\n}\n[/block]\nTo insert rows on a table just add sample models and call `db.Add(model)`; \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// generate an insert statement and add it to current transaction\\nuser := User{Name: \\\"Al Pacino\\\"}\\ndb.Add(user)\\n\\n// commit\\ndb.Commit()\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nYou can add multiple models by calling Add function sequentially. This would generate the following sql statements and bindings, add it to the current transaction.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"INSERT INTO user\\n(name)\\nVALUES ($1);\\n[Al Pacino]\",\n      \"language\": \"sql\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Updating\"\n}\n[/block]\nSuppose you have inserted the user variable in the insert example above.To update rows, update the changes in user struct & use Add() function\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"user.Name = \\\"Robert Downey Jr.\\\"\\n\\ndb.Add(user)\\ndb.Commit()\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nAs it can be easily noticed `Update` statement is a little more unique for the flexibility of update statements.\n\nThis type of syntax is also supported in `Select` statements.\n\nThe following sql statement & bindings will be produced within a transaction;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# for sqlite\\nREPLACE INTO user(name) VALUES(?)\\n[Robert Downey Jr.]\\n\\n# for postgres\\nINSERT INTO user(name) VALUES($1) ON CONFLICT(id) DO UPDATE SET name = $2;\\n[Robert Downey Jr. Robert Downey Jr.]\\n\\n# for mysql\\nINSERT INTO user(name) VALUES(?) ON DUPLICATE KEY UPDATE name = ?;\\n[Robert Downey Jr. Robert Downey Jr.]\",\n      \"language\": \"sql\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Deleting\"\n}\n[/block]\nDeletes are done by the following session call;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// insert the row\\ndb.Add(User{Name: \\\"Aras Can Akin\\\"})\\ndb.Commit()\\n\\n// delete it\\ndb.Delete(User{Name: \\\"Aras Can Akin\\\"})\\ndb.Commit()\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nThe statement would produce the following sql statements & bindings within a transaction;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"DELETE FROM user\\nWHERE name = $1;\\n[Aras Can Akin]\",\n      \"language\": \"sql\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Selecting\"\n}\n[/block]\nThere are more than one way to build select queries. In `qb.Session`, there exists a shortcut namely `Find()` that finds a model that matches the struct values as in the following;\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Find.One()\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"db.Add(User{Name: \\\"Aras Can Akin\\\"})\\ndb.Commit()\\n\\nvar user User\\nerr = db.Find(User{Name: \\\"Aras Can Akin\\\"}).One(&user)\\nif err != nil {\\n  fmt.Println(err)\\n}\\n\\nfmt.Printf(\\\"id=%d, name=%s\\\\n\\\", user.ID, user.Name)\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nThe `Find()` call would produce the following sql statement with bindings;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SELECT name, id\\nFROM user\\nWHERE name = $1;\\n[Aras Can Akin]\",\n      \"language\": \"sql\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Find().All()\"\n}\n[/block]\n`Find(model interface{}).All(models interface{})` returns all rows that is matched by struct example as in the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"db.Add(User{Name: \\\"Aras Can Akin\\\", Email: \\\"aras:::at:::gmail.com\\\"})\\ndb.Add(User{Name: \\\"Aras Can Akin\\\", Email: \\\"aras@slicebit.com\\\"})\\ndb.Commit()\\n\\nvar users []User\\nerr = db.Find(User{Name: \\\"Aras Can Akin\\\"}).All(&users)\\nif err != nil {\\n  fmt.Println(err)\\n}\\n\\nfor _, u := range users {\\n  fmt.Printf(\\\"<User id=%d name=%s email=%s>\\\\n\\\", u.ID, u.Name, u.Email)\\n}\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\n`Find().All()` call would produce the following sql statements and bindings;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SELECT id, name, email\\nFROM user\\nWHERE name = $1;\\n[Aras Can Akin]\",\n      \"language\": \"sql\"\n    }\n  ]\n}\n[/block]\n[The Builder](doc:the-builder)] explained how sql statements are built by function chaining. The more complex select statements can be built by both builder and the session. Query building by func chaining can be also achieved using `qb.Session`. Lets make a complex selective query using joins;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"type User struct {\\n  ID    int64  `qb:\\\"type:bigserial; constraints:primary_key\\\"`\\n  Name  string `qb:\\\"constraints:not_null\\\"`\\n  Email string `qb:\\\"constraints:not_null, unique\\\"`\\n}\\n\\ntype Session struct {\\n  UserID    int64  `qb:\\\"constraints:ref(user.id)\\\"`\\n  AuthToken string `qb:\\\"type:uuid\\\"`\\n  Agent     string `qb:\\\"constraints:not_null\\\"`\\n}\\n\\ndb, err := qb.New(\\\"postgres\\\", \\\"user=postgres dbname=qb_test sslmode=disable\\\")\\nif err != nil {\\n  panic(err)\\n}\\ndefer db.Close()\\n\\ndb.Metadata().Add(User{})\\ndb.Metadata().Add(Session{})\\ndb.Metadata().CreateAll()\\n\\ndb.Add(User{Name: \\\"Aras Can Akin\\\", Email: \\\"aras@gmail.com\\\"})\\ndb.Add(User{Name: \\\"Aras Can Akin\\\", Email: \\\"aras@slicebit.com\\\"})\\n\\ndb.Add(Session{\\n  UserID:    1,\\n  AuthToken: \\\"f1d2a8af-b048-479a-99c8-3725805299cf\\\",\\n  Agent:     \\\"android\\\"})\\n\\ndb.Add(Session{\\n  UserID:    1,\\n  AuthToken: \\\"9bb95918-1cc1-4ab1-b3b1-29bf385d00bb\\\",\\n  Agent:     \\\"chrome\\\"})\\n\\ndb.Add(Session{\\n  UserID:    2,\\n  AuthToken: \\\"0470f8ae-3e36-4a83-a4d1-7562173c48c6\\\",\\n  Agent:     \\\"ios\\\"})\\n\\ndb.Commit()\\n\\nvar sessions []Session\\nerr = db.\\n  Select(\\\"s.user_id, s.auth_token, s.agent\\\").\\n  From(\\\"session s\\\").\\n  InnerJoin(\\\"user u\\\", \\\"s.user_id = u.id\\\").\\n  Where(\\\"u.name = ?\\\", \\\"Aras Can Akin\\\").\\n  OrderBy(\\\"s.agent\\\").\\n  All(&sessions)\\n\\nif err != nil {\\n  fmt.Println(err)\\n}\\n\\nfor _, s := range sessions {\\n  fmt.Printf(\\\"<Session user_id=%d auth_token=%s agent=%s>\\\\n\\\",\\n             s.UserID,\\n             s.AuthToken,\\n             s.Agent)\\n}\\n\\n// outputs\\n// <Session user_id=1 auth_token=f1d2a8af-b048-479a-99c8-3725805299cf agent=android>\\n// <Session user_id=1 auth_token=9bb95918-1cc1-4ab1-b3b1-29bf385d00bb agent=chrome>\\n// <Session user_id=2 auth_token=0470f8ae-3e36-4a83-a4d1-7562173c48c6 agent=ios>\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nLet's take a look at the last complex Select statement with use of `qb.Session`. The chain starting with `Select()` is just as the same as the builder way. However, builder doesn't have any `All()` functions which parses the struct and maps the values for each iteration in the result set.\n\n**So, the very first question comes to mind is;**\nWhy do I have to bother with the builder api?\n\nThe `One()` and `All()` function calls uses sqlx's mapper functions which uses a lot of reflection. Therefore, it may slow down your query calls when dealing with large & complex structs. Therefore, performance critical apis should use builder api.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Query().Filter()\"\n}\n[/block]\nThere is an optional way to build select statements in an ormish way. This way uses Query().Filter() functions to chain where statements and retrieve the result set using One() & All(). This feature can also be used for partial extraction.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"package main\\n\\nimport (\\n\\t\\\"fmt\\\"\\n\\t\\\"github.com/aacanakin/qb\\\"\\n)\\n\\nfunc main() {\\n\\n\\ttype User struct {\\n\\t\\tID   string `qb:\\\"type:uuid; constraints:primary_key\\\"`\\n\\t\\tName string `qb:\\\"constraints:not_null\\\"`\\n\\t}\\n\\n\\tdb, err := qb.New(\\\"postgres\\\", \\\"user=postgres dbname=qb_test sslmode=disable\\\")\\n\\tif err != nil {\\n\\t\\tpanic(err)\\n\\t}\\n\\tdefer db.Close()\\n\\n\\tdb.Dialect().SetEscaping(true)\\n\\n\\tdb.AddTable(User{})\\n\\tdb.CreateAll()\\n\\n\\tuser := &User{\\n\\t\\tID:   \\\"98d7fa05-72a3-466a-9a46-995d9f662312\\\",\\n\\t\\tName: \\\"Al Pacino\\\",\\n\\t}\\n\\n\\tdb.Add(user)\\n\\terr = db.Commit()\\n\\tcheckErr(err)\\n\\n\\tvar selUser User\\n\\tdb.Query(db.T(\\\"user\\\").C(\\\"name\\\")).\\n\\t\\tFilter(db.T(\\\"user\\\").C(\\\"id\\\").Eq(\\\"98d7fa05-72a3-466a-9a46-995d9f662312\\\")).\\n\\t\\tOne(&selUser)\\n\\n\\tfmt.Printf(\\\"User%+v\\\\n\\\", selUser)\\n\\n}\\n\\nfunc checkErr(err error) {\\n\\tif err != nil {\\n\\t\\tpanic(err)\\n\\t}\\n}\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nThe output would be the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"User{ID: Name:Al Pacino}\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nAs it might be noticed, the ID field is not initialized because in the example above, Query() function only takes \"name\" column as parameter. Moreover, db has T(name string) function for retrieving table object from metadata and inside the table object there exists C(name string) function for retrieving column object of the table.\n\nAll() is also available for slice of structs. No further example is required.","excerpt":"","slug":"the-session","type":"basic","title":"Session"}
In qb, the session object is the most useful & simplest object. It has all the building blocks of qb. A typical session object has the following dependencies; [block:code] { "codes": [ { "code": "// Session is the composition of engine connection & orm mappings\ntype Session struct {\n\tqueries []*Query // queries in the current transaction\n\tmapper *Mapper // mapper object for shortcut funcs\n\tmetadata *MetaData // metadata object to keep table registry\n\ttx *sql.Tx // active transaction\n\tbuilder *Builder // query builder\n mutex *sync.Mutex // mutex for preventing race conditions while opening a transaction\n}", "language": "go" } ] } [/block] The following object is the only object required to use every single function in qb. The other structs that can also build sql statements such as `Builder` is transactionless. From this point, the following qb initialization is assumed. [block:code] { "codes": [ { "code": "package main\n\nimport (\n\t\"github.com/aacanakin/qb\"\n)\n\ntype User struct {\n ID string `qb:\"constraints:primary_key\"`\n Name string `qb:\"constraints:not_null\"`\n Email string `qb:\"constraints:unique, not_null\"`\n}\n\nfunc main() {\n\n\tdb, err := qb.New(\"postgres\", \"user=postgres dbname=qb_test sslmode=disable\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer db.Close()\n\n\tdb.Metadata().Add(User{})\n\tdb.Metadata().CreateAll()\n}", "language": "go" } ] } [/block] [block:api-header] { "type": "basic", "title": "Inserting" } [/block] To insert rows on a table just add sample models and call `db.Add(model)`; [block:code] { "codes": [ { "code": "// generate an insert statement and add it to current transaction\nuser := User{Name: \"Al Pacino\"}\ndb.Add(user)\n\n// commit\ndb.Commit()", "language": "go" } ] } [/block] You can add multiple models by calling Add function sequentially. This would generate the following sql statements and bindings, add it to the current transaction. [block:code] { "codes": [ { "code": "INSERT INTO user\n(name)\nVALUES ($1);\n[Al Pacino]", "language": "sql" } ] } [/block] [block:api-header] { "type": "basic", "title": "Updating" } [/block] Suppose you have inserted the user variable in the insert example above.To update rows, update the changes in user struct & use Add() function [block:code] { "codes": [ { "code": "user.Name = \"Robert Downey Jr.\"\n\ndb.Add(user)\ndb.Commit()", "language": "go" } ] } [/block] As it can be easily noticed `Update` statement is a little more unique for the flexibility of update statements. This type of syntax is also supported in `Select` statements. The following sql statement & bindings will be produced within a transaction; [block:code] { "codes": [ { "code": "# for sqlite\nREPLACE INTO user(name) VALUES(?)\n[Robert Downey Jr.]\n\n# for postgres\nINSERT INTO user(name) VALUES($1) ON CONFLICT(id) DO UPDATE SET name = $2;\n[Robert Downey Jr. Robert Downey Jr.]\n\n# for mysql\nINSERT INTO user(name) VALUES(?) ON DUPLICATE KEY UPDATE name = ?;\n[Robert Downey Jr. Robert Downey Jr.]", "language": "sql" } ] } [/block] [block:api-header] { "type": "basic", "title": "Deleting" } [/block] Deletes are done by the following session call; [block:code] { "codes": [ { "code": "// insert the row\ndb.Add(User{Name: \"Aras Can Akin\"})\ndb.Commit()\n\n// delete it\ndb.Delete(User{Name: \"Aras Can Akin\"})\ndb.Commit()", "language": "go" } ] } [/block] The statement would produce the following sql statements & bindings within a transaction; [block:code] { "codes": [ { "code": "DELETE FROM user\nWHERE name = $1;\n[Aras Can Akin]", "language": "sql" } ] } [/block] [block:api-header] { "type": "basic", "title": "Selecting" } [/block] There are more than one way to build select queries. In `qb.Session`, there exists a shortcut namely `Find()` that finds a model that matches the struct values as in the following; [block:api-header] { "type": "basic", "title": "Find.One()" } [/block] [block:code] { "codes": [ { "code": "db.Add(User{Name: \"Aras Can Akin\"})\ndb.Commit()\n\nvar user User\nerr = db.Find(User{Name: \"Aras Can Akin\"}).One(&user)\nif err != nil {\n fmt.Println(err)\n}\n\nfmt.Printf(\"id=%d, name=%s\\n\", user.ID, user.Name)", "language": "go" } ] } [/block] The `Find()` call would produce the following sql statement with bindings; [block:code] { "codes": [ { "code": "SELECT name, id\nFROM user\nWHERE name = $1;\n[Aras Can Akin]", "language": "sql" } ] } [/block] [block:api-header] { "type": "basic", "title": "Find().All()" } [/block] `Find(model interface{}).All(models interface{})` returns all rows that is matched by struct example as in the following; [block:code] { "codes": [ { "code": "db.Add(User{Name: \"Aras Can Akin\", Email: \"aras@gmail.com\"})\ndb.Add(User{Name: \"Aras Can Akin\", Email: \"aras@slicebit.com\"})\ndb.Commit()\n\nvar users []User\nerr = db.Find(User{Name: \"Aras Can Akin\"}).All(&users)\nif err != nil {\n fmt.Println(err)\n}\n\nfor _, u := range users {\n fmt.Printf(\"<User id=%d name=%s email=%s>\\n\", u.ID, u.Name, u.Email)\n}", "language": "go" } ] } [/block] `Find().All()` call would produce the following sql statements and bindings; [block:code] { "codes": [ { "code": "SELECT id, name, email\nFROM user\nWHERE name = $1;\n[Aras Can Akin]", "language": "sql" } ] } [/block] [The Builder](doc:the-builder)] explained how sql statements are built by function chaining. The more complex select statements can be built by both builder and the session. Query building by func chaining can be also achieved using `qb.Session`. Lets make a complex selective query using joins; [block:code] { "codes": [ { "code": "type User struct {\n ID int64 `qb:\"type:bigserial; constraints:primary_key\"`\n Name string `qb:\"constraints:not_null\"`\n Email string `qb:\"constraints:not_null, unique\"`\n}\n\ntype Session struct {\n UserID int64 `qb:\"constraints:ref(user.id)\"`\n AuthToken string `qb:\"type:uuid\"`\n Agent string `qb:\"constraints:not_null\"`\n}\n\ndb, err := qb.New(\"postgres\", \"user=postgres dbname=qb_test sslmode=disable\")\nif err != nil {\n panic(err)\n}\ndefer db.Close()\n\ndb.Metadata().Add(User{})\ndb.Metadata().Add(Session{})\ndb.Metadata().CreateAll()\n\ndb.Add(User{Name: \"Aras Can Akin\", Email: \"aras@gmail.com\"})\ndb.Add(User{Name: \"Aras Can Akin\", Email: \"aras@slicebit.com\"})\n\ndb.Add(Session{\n UserID: 1,\n AuthToken: \"f1d2a8af-b048-479a-99c8-3725805299cf\",\n Agent: \"android\"})\n\ndb.Add(Session{\n UserID: 1,\n AuthToken: \"9bb95918-1cc1-4ab1-b3b1-29bf385d00bb\",\n Agent: \"chrome\"})\n\ndb.Add(Session{\n UserID: 2,\n AuthToken: \"0470f8ae-3e36-4a83-a4d1-7562173c48c6\",\n Agent: \"ios\"})\n\ndb.Commit()\n\nvar sessions []Session\nerr = db.\n Select(\"s.user_id, s.auth_token, s.agent\").\n From(\"session s\").\n InnerJoin(\"user u\", \"s.user_id = u.id\").\n Where(\"u.name = ?\", \"Aras Can Akin\").\n OrderBy(\"s.agent\").\n All(&sessions)\n\nif err != nil {\n fmt.Println(err)\n}\n\nfor _, s := range sessions {\n fmt.Printf(\"<Session user_id=%d auth_token=%s agent=%s>\\n\",\n s.UserID,\n s.AuthToken,\n s.Agent)\n}\n\n// outputs\n// <Session user_id=1 auth_token=f1d2a8af-b048-479a-99c8-3725805299cf agent=android>\n// <Session user_id=1 auth_token=9bb95918-1cc1-4ab1-b3b1-29bf385d00bb agent=chrome>\n// <Session user_id=2 auth_token=0470f8ae-3e36-4a83-a4d1-7562173c48c6 agent=ios>", "language": "go" } ] } [/block] Let's take a look at the last complex Select statement with use of `qb.Session`. The chain starting with `Select()` is just as the same as the builder way. However, builder doesn't have any `All()` functions which parses the struct and maps the values for each iteration in the result set. **So, the very first question comes to mind is;** Why do I have to bother with the builder api? The `One()` and `All()` function calls uses sqlx's mapper functions which uses a lot of reflection. Therefore, it may slow down your query calls when dealing with large & complex structs. Therefore, performance critical apis should use builder api. [block:api-header] { "type": "basic", "title": "Query().Filter()" } [/block] There is an optional way to build select statements in an ormish way. This way uses Query().Filter() functions to chain where statements and retrieve the result set using One() & All(). This feature can also be used for partial extraction. [block:code] { "codes": [ { "code": "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/aacanakin/qb\"\n)\n\nfunc main() {\n\n\ttype User struct {\n\t\tID string `qb:\"type:uuid; constraints:primary_key\"`\n\t\tName string `qb:\"constraints:not_null\"`\n\t}\n\n\tdb, err := qb.New(\"postgres\", \"user=postgres dbname=qb_test sslmode=disable\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer db.Close()\n\n\tdb.Dialect().SetEscaping(true)\n\n\tdb.AddTable(User{})\n\tdb.CreateAll()\n\n\tuser := &User{\n\t\tID: \"98d7fa05-72a3-466a-9a46-995d9f662312\",\n\t\tName: \"Al Pacino\",\n\t}\n\n\tdb.Add(user)\n\terr = db.Commit()\n\tcheckErr(err)\n\n\tvar selUser User\n\tdb.Query(db.T(\"user\").C(\"name\")).\n\t\tFilter(db.T(\"user\").C(\"id\").Eq(\"98d7fa05-72a3-466a-9a46-995d9f662312\")).\n\t\tOne(&selUser)\n\n\tfmt.Printf(\"User%+v\\n\", selUser)\n\n}\n\nfunc checkErr(err error) {\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}", "language": "go" } ] } [/block] The output would be the following; [block:code] { "codes": [ { "code": "User{ID: Name:Al Pacino}", "language": "text" } ] } [/block] As it might be noticed, the ID field is not initialized because in the example above, Query() function only takes "name" column as parameter. Moreover, db has T(name string) function for retrieving table object from metadata and inside the table object there exists C(name string) function for retrieving column object of the table. All() is also available for slice of structs. No further example is required.