{"_id":"576e8ba94d787c0e006241db","__v":7,"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":"","project":"56e5982f9191742000ef204a","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"},"parentDoc":null,"user":"543466ea0e8e2b0e00341818","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-06-25T13:48:25.430Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"Although qb has a great support for building many different queries, there might be cases where it may be needed to run raw sql statements. qb provides that too. Here's an example\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\\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\\tstatement := qb.Statement()\\n\\n\\tsql := `\\n\\n\\t\\t\\tSELECT id, name, email\\nFROM user\\n\\t\\t\\t\\tWHERE id = ?\\n\\n\\t`\\n\\n\\tstatement.Text(sql)\\n\\n\\tfmt.Println(statement.SQL())\\n\\n  // execute the select\\n\\tvar id int\\n\\tvar name, email string\\n\\tdb.Engine().DB().QueryRow(statement.SQL(), 5).Scan(&id, &name, &email)\\n\\n}\",\n      \"language\": \"go\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"The indendation is intentional\",\n  \"body\": \"The example intentionally defines sql with shitty indentation to make a point.\"\n}\n[/block]\nThe output would be;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SELECT id, name, email\\nFROM user\\nWHERE id = ?;\",\n      \"language\": \"sql\"\n    }\n  ]\n}\n[/block]\nHere, you can build the same sql without using Statement() call. However, if you don't use Statement() constructor, your query will have \\t \\n characters that is not needed. Moreover, Statement() checks if you added \";\" character and ensures the sql has \";\". You can put any tabs at the start of sql statement. Statement().Text() would clean them for you.","excerpt":"","slug":"raw-sql-statements","type":"basic","title":"Raw sql statements"}

Raw sql statements


Although qb has a great support for building many different queries, there might be cases where it may be needed to run raw sql statements. qb provides that too. Here's an example [block:code] { "codes": [ { "code": "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/aacanakin/qb\"\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\tstatement := qb.Statement()\n\n\tsql := `\n\n\t\t\tSELECT id, name, email\nFROM user\n\t\t\t\tWHERE id = ?\n\n\t`\n\n\tstatement.Text(sql)\n\n\tfmt.Println(statement.SQL())\n\n // execute the select\n\tvar id int\n\tvar name, email string\n\tdb.Engine().DB().QueryRow(statement.SQL(), 5).Scan(&id, &name, &email)\n\n}", "language": "go" } ] } [/block] [block:callout] { "type": "info", "title": "The indendation is intentional", "body": "The example intentionally defines sql with shitty indentation to make a point." } [/block] The output would be; [block:code] { "codes": [ { "code": "SELECT id, name, email\nFROM user\nWHERE id = ?;", "language": "sql" } ] } [/block] Here, you can build the same sql without using Statement() call. However, if you don't use Statement() constructor, your query will have \t \n characters that is not needed. Moreover, Statement() checks if you added ";" character and ensures the sql has ";". You can put any tabs at the start of sql statement. Statement().Text() would clean them for you.