{"_id":"576c5aa55738570e00ff0706","__v":18,"parentDoc":null,"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"},"category":{"_id":"57531652d8fe070e00258a51","__v":0,"project":"56e5982f9191742000ef204a","version":"57531435be31940e0035ad96","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-06-04T17:56:34.972Z","from_sync":false,"order":2,"slug":"expression-api","title":"Expression API"},"githubsync":"","user":"543466ea0e8e2b0e00341818","project":"56e5982f9191742000ef204a","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-06-23T21:54:45.856Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":5,"body":"Conditionals are restrictions of where clause in qb. There are many conditional builders inside the WhereClause.\n\nThe following initializations are assumed;\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\\tdb, _ := qb.New(\\\"sqlite3\\\", \\\":memory:\\\")\\n\\tdefer db.Close()\\n\\n\\tdb.Dialect().SetEscaping(true)\\n\\n\\tactors := qb.Table(\\n\\t\\t\\\"actor\\\",\\n\\t\\tqb.Column(\\\"id\\\", qb.Varchar().Size(36)),\\n\\t\\tqb.Column(\\\"name\\\", qb.Varchar().NotNull()),\\n\\t\\tqb.Column(\\\"nomination_count\\\", qb.Int().Default(0)),\\n\\t\\tqb.Column(\\\"oscar_count\\\", qb.Int().Default(0)),\\n\\t\\tqb.PrimaryKey(\\\"id\\\"),\\n\\t)\\n\\n}\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nA typical Select ... Where ... clause is build by conditionals especially inside the where clause. In this example, you can reach the column object by users.C(\"column-name\"). The conditionals can be generated.\n\nA simple Select ... Where ... using Conditionals would be the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"sel := actors.\\n\\t\\tSelect(actors.C(\\\"name\\\")).\\n\\t\\tWhere(actors.C(\\\"nomination_count\\\").Gt(0)).\\n\\t\\tBuild(db.Dialect())\\n\\nfmt.Println(sel.SQL())\\nfmt.Println(sel.Bindings())\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\nHere, the query asks db where nomination_count field is greater than 0. Output is;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SELECT name\\nFROM actor\\nWHERE actor.nomination_count > ?;\\n[0]\",\n      \"language\": \"sql\"\n    }\n  ]\n}\n[/block]\nThe \">\" character builder here is the Gt() function inside the ColumnElem object.\n\nHere is the full list of conditionals;\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Conditional\",\n    \"h-1\": \"Description\",\n    \"0-0\": \"Like(pattern string)\",\n    \"0-1\": \"Builds a LIKE conditional using pattern such as %user%\",\n    \"1-0\": \"NotIn(values ...interface{})\",\n    \"1-1\": \"Builds a NOT IN(...) conditional given values such as \\\"al:::at:::pacino.com\\\", \\\"robert@downey.com\\\"\",\n    \"2-0\": \"In(values ...interface{})\",\n    \"2-1\": \"Builds an IN(...) conditional given values such as \\\"al@pacino.com\\\", \\\"robert@downey.com\\\"\",\n    \"3-0\": \"NotEq(value interface{})\",\n    \"3-1\": \"Builds a :var != :placeholder conditional given value.\",\n    \"4-0\": \"Eq(value interface{})\",\n    \"4-1\": \"Builds a :var = :placeholder conditional given value.\",\n    \"5-0\": \"Gt(value interface{})\",\n    \"6-0\": \"Gte(value interface{})\",\n    \"7-0\": \"St(value interface{})\",\n    \"8-0\": \"Ste(value interface{})\",\n    \"5-1\": \"Builds a :var > :placeholder conditional given value.\",\n    \"6-1\": \"Builds a :var >= :placeholder conditional given value.\",\n    \"7-1\": \"Builds a :var < :placeholder conditional given value.\",\n    \"8-1\": \"Builds a :var <= :placeholder conditional given value.\"\n  },\n  \"cols\": 2,\n  \"rows\": 9\n}\n[/block]\nYou can also build custom Conditionals using Condition(col ColumnElem, op string, values ...interface{}) function.","excerpt":"","slug":"conditionals","type":"basic","title":"Conditionals"}
Conditionals are restrictions of where clause in qb. There are many conditional builders inside the WhereClause. The following initializations are assumed; [block:code] { "codes": [ { "code": "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/aacanakin/qb\"\n)\n\nfunc main() {\n\tdb, _ := qb.New(\"sqlite3\", \":memory:\")\n\tdefer db.Close()\n\n\tdb.Dialect().SetEscaping(true)\n\n\tactors := qb.Table(\n\t\t\"actor\",\n\t\tqb.Column(\"id\", qb.Varchar().Size(36)),\n\t\tqb.Column(\"name\", qb.Varchar().NotNull()),\n\t\tqb.Column(\"nomination_count\", qb.Int().Default(0)),\n\t\tqb.Column(\"oscar_count\", qb.Int().Default(0)),\n\t\tqb.PrimaryKey(\"id\"),\n\t)\n\n}", "language": "go" } ] } [/block] A typical Select ... Where ... clause is build by conditionals especially inside the where clause. In this example, you can reach the column object by users.C("column-name"). The conditionals can be generated. A simple Select ... Where ... using Conditionals would be the following; [block:code] { "codes": [ { "code": "sel := actors.\n\t\tSelect(actors.C(\"name\")).\n\t\tWhere(actors.C(\"nomination_count\").Gt(0)).\n\t\tBuild(db.Dialect())\n\nfmt.Println(sel.SQL())\nfmt.Println(sel.Bindings())", "language": "go" } ] } [/block] Here, the query asks db where nomination_count field is greater than 0. Output is; [block:code] { "codes": [ { "code": "SELECT name\nFROM actor\nWHERE actor.nomination_count > ?;\n[0]", "language": "sql" } ] } [/block] The ">" character builder here is the Gt() function inside the ColumnElem object. Here is the full list of conditionals; [block:parameters] { "data": { "h-0": "Conditional", "h-1": "Description", "0-0": "Like(pattern string)", "0-1": "Builds a LIKE conditional using pattern such as %user%", "1-0": "NotIn(values ...interface{})", "1-1": "Builds a NOT IN(...) conditional given values such as \"al@pacino.com\", \"robert@downey.com\"", "2-0": "In(values ...interface{})", "2-1": "Builds an IN(...) conditional given values such as \"al@pacino.com\", \"robert@downey.com\"", "3-0": "NotEq(value interface{})", "3-1": "Builds a :var != :placeholder conditional given value.", "4-0": "Eq(value interface{})", "4-1": "Builds a :var = :placeholder conditional given value.", "5-0": "Gt(value interface{})", "6-0": "Gte(value interface{})", "7-0": "St(value interface{})", "8-0": "Ste(value interface{})", "5-1": "Builds a :var > :placeholder conditional given value.", "6-1": "Builds a :var >= :placeholder conditional given value.", "7-1": "Builds a :var < :placeholder conditional given value.", "8-1": "Builds a :var <= :placeholder conditional given value." }, "cols": 2, "rows": 9 } [/block] You can also build custom Conditionals using Condition(col ColumnElem, op string, values ...interface{}) function.