{"action":"created","comment":{"url":"https://api.github.com/repos/carbonsrv/carbon/pulls/comments/67617469","id":67617469,"diff_hunk":"@@ -0,0 +1,109 @@\n+-- SQL library\n+sql = {}\n+\n+-- Helpers\n+local function do_ping(con)\n+\tlocal err = con.Ping()\n+\tif err then\n+\t\terror(\"SQL: Database connection ping failed.\", 0)\n+\tend\n+end\n+local function convert_rows(rows)\n+\tif not rows then\n+\t\terror(\"SQL: Can't convert nil to row!\")\n+\tend\n+\n+\tlocal rows, err = carbon._sql_rows(rows)\n+\tif err then\n+\t\terror(\"SQL: Row conversion failed: \"..err) \n+\tend\n+\n+\tlocal final = {}\n+\tfinal.n = rows.Len\n+\tfor i=1, final.n do\n+\t\tfinal[i] = luar.map2table(carbon._sql_getrow(rows, i))\n+\tend\n+\n+\treturn final\n+end\n+\n+-- API\n+function sql.drivers() -- returns list of drivers available\n+\treturn luar.slice2table(carbon._sql_drivers())\n+end\n+\n+function sql.open(driver, src) -- generates a database wrapper\n+\tif not driver or not src then\n+\t\terror(\"SQL: open needs driver and src\", 0)\n+\tend\n+\n+\tlocal db, err = carbon._sql_open(driver, src)\n+\tif err then\n+\t\treturn nil, err\n+\tend\n+\t\n+\treturn {\n+\t\tcon = db,\n+\t\tclose = function(self)\n+\t\t\tlocal err = self.con.Close()\n+\t\t\tif err then\n+\t\t\t\treturn false, err\n+\t\t\tend\n+\t\t\treturn true, nil\n+\t\tend,\n+\t\texec = function(self, stmt, ...)\n+\t\t\tif not stmt then\n+\t\t\t\terror(\"SQL: exec needs statemement!\", 0)\n+\t\t\tend\n+\n+\t\t\tdo_ping(self.con)\n+\t\t\treturn self.con.Exec(stmt, ...)\n+\t\tend,\n+\t\tquery = function(self, stmt, ...)\n+\t\t\tif not stmt then\n+\t\t\t\terror(\"SQL: query needs statement!\", 0)\n+\t\t\tend\n+\n+\t\t\tdo_ping(self.con)\n+\t\t\tlocal rows, err = self.con.Query(stmt, ...)\n+\t\t\tif err then\n+\t\t\t\treturn nil, err\n+\t\t\tend\n+\n+\t\t\treturn convert_rows(rows)\n+\t\tend,\n+\t\tprepare = function(origself, stmt)\n+\t\t\tif not stmt then\n+\t\t\t\terror(\"SQL: prepare needs statement!\", 0)\n+\t\t\tend\n+\n+\t\t\tdo_ping(self.con)","path":"builtin/libs/wrappers/sql.lua","position":80,"original_position":80,"commit_id":"95aff05ad9a44ed3f5b3120076567ce7f0e6d11a","original_commit_id":"95aff05ad9a44ed3f5b3120076567ce7f0e6d11a","user":{"login":"vifino","id":5837359,"avatar_url":"https://avatars.githubusercontent.com/u/5837359?v=3","gravatar_id":"","url":"https://api.github.com/users/vifino","html_url":"https://github.com/vifino","followers_url":"https://api.github.com/users/vifino/followers","following_url":"https://api.github.com/users/vifino/following{/other_user}","gists_url":"https://api.github.com/users/vifino/gists{/gist_id}","starred_url":"https://api.github.com/users/vifino/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vifino/subscriptions","organizations_url":"https://api.github.com/users/vifino/orgs","repos_url":"https://api.github.com/users/vifino/repos","events_url":"https://api.github.com/users/vifino/events{/privacy}","received_events_url":"https://api.github.com/users/vifino/received_events","type":"User","site_admin":false},"body":"Fixed in master.","created_at":"2016-06-19T13:59:51Z","updated_at":"2016-06-19T13:59:51Z","html_url":"https://github.com/carbonsrv/carbon/pull/7#discussion_r67617469","pull_request_url":"https://api.github.com/repos/carbonsrv/carbon/pulls/7","_links":{"self":{"href":"https://api.github.com/repos/carbonsrv/carbon/pulls/comments/67617469"},"html":{"href":"https://github.com/carbonsrv/carbon/pull/7#discussion_r67617469"},"pull_request":{"href":"https://api.github.com/repos/carbonsrv/carbon/pulls/7"}}},"pull_request":{"url":"https://api.github.com/repos/carbonsrv/carbon/pulls/7","id":74362416,"html_url":"https://github.com/carbonsrv/carbon/pull/7","diff_url":"https://github.com/carbonsrv/carbon/pull/7.diff","patch_url":"https://github.com/carbonsrv/carbon/pull/7.patch","issue_url":"https://api.github.com/repos/carbonsrv/carbon/issues/7","number":7,"state":"closed","locked":false,"title":"Database support","user":{"login":"vifino","id":5837359,"avatar_url":"https://avatars.githubusercontent.com/u/5837359?v=3","gravatar_id":"","url":"https://api.github.com/users/vifino","html_url":"https://github.com/vifino","followers_url":"https://api.github.com/users/vifino/followers","following_url":"https://api.github.com/users/vifino/following{/other_user}","gists_url":"https://api.github.com/users/vifino/gists{/gist_id}","starred_url":"https://api.github.com/users/vifino/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vifino/subscriptions","organizations_url":"https://api.github.com/users/vifino/orgs","repos_url":"https://api.github.com/users/vifino/repos","events_url":"https://api.github.com/users/vifino/events{/privacy}","received_events_url":"https://api.github.com/users/vifino/received_events","type":"User","site_admin":false},"body":"This PR adds database support:\r\n\r\n`sql.drivers()` returns the available drivers in table form, should return `{\"mysql\", \"postgres\", \"sqlite3\"}`.\r\n\r\n`sql.open(drivername, source)` creates a new DB wrapper. A sqlite3 instance in memory would be as simple as the following Example:\r\n```lua\r\nlocal db = sql.open(\"sqlite3\", \":memory:\")\r\n```\r\nThe DB wrapper has a few fields:\r\n\r\n* `con`\r\n  - Please don't touch. This is the internal connection object, iterating it doesn't work, calling things wrong crashes carbon, etc..\r\n* `close`\r\n  - Closes automatically opened connections, however, this should be barely useful, since db wrappers are supposed to be long lasting and used by multiple threads.\r\n* `exec`\r\n  - Executes statements:\r\n    - `db:exec(\"CREATE TABLE users (name text, age int)\")`\r\n  - Can also take extra arguments and replace `$1`, `$2`, etc.. respectively.\r\n* `query`\r\n  - Probably the most useful.\r\n  - Also takes format arguments similar to `exec`.\r\n  - Example query:\r\n```lua\r\nlocal rows = assert(db:query(\"SELECT * FROM users\"))\r\nfor rowno=1, rows.n do\r\n  print(\"Showing row no. \"..tostring(rowno))\r\n  for k, v in pairs(rows[rowno]) do\r\n    print(\"> \"..k, v)\r\n  end\r\nend\r\n```\r\n* `prepare`\r\n  - Pretty much `query`, but you prepare a statement beforehand, then execute it repeatedly.\r\n```lua\r\nlocal tables = {\"users\", \"administrators\"}\r\nlocal statement = db:prepare(\"SELECT * FROM $1\")\r\nfor _, tbl in pairs(tables) do\r\n  local rows = statement:query(tbl)\r\n  for rowno=1, rows.n do\r\n    print(tbl..\": Showing row no. \"..tostring(rowno))\r\n    for k, v in pairs(rows[rowno]) do\r\n      print(\"> \"..k, v)\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nSee also: `tests/sqltest.lua`\r\n\r\n---\r\n\r\n@bauen1 made a wiki entry for this PR: https://github.com/carbonsrv/carbon/wiki/API:-sql","created_at":"2016-06-19T12:35:29Z","updated_at":"2016-06-19T13:59:51Z","closed_at":"2016-06-19T13:52:37Z","merged_at":"2016-06-19T13:52:37Z","merge_commit_sha":"2953f8e56c6d90efddee1de9d02dcd3815974b0a","assignee":{"login":"vifino","id":5837359,"avatar_url":"https://avatars.githubusercontent.com/u/5837359?v=3","gravatar_id":"","url":"https://api.github.com/users/vifino","html_url":"https://github.com/vifino","followers_url":"https://api.github.com/users/vifino/followers","following_url":"https://api.github.com/users/vifino/following{/other_user}","gists_url":"https://api.github.com/users/vifino/gists{/gist_id}","starred_url":"https://api.github.com/users/vifino/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vifino/subscriptions","organizations_url":"https://api.github.com/users/vifino/orgs","repos_url":"https://api.github.com/users/vifino/repos","events_url":"https://api.github.com/users/vifino/events{/privacy}","received_events_url":"https://api.github.com/users/vifino/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/carbonsrv/carbon/pulls/7/commits","review_comments_url":"https://api.github.com/repos/carbonsrv/carbon/pulls/7/comments","review_comment_url":"https://api.github.com/repos/carbonsrv/carbon/pulls/comments{/number}","comments_url":"https://api.github.com/repos/carbonsrv/carbon/issues/7/comments","statuses_url":"https://api.github.com/repos/carbonsrv/carbon/statuses/95aff05ad9a44ed3f5b3120076567ce7f0e6d11a","head":{"label":"carbonsrv:database-new","ref":"database-new","sha":"95aff05ad9a44ed3f5b3120076567ce7f0e6d11a","user":{"login":"carbonsrv","id":15823292,"avatar_url":"https://avatars.githubusercontent.com/u/15823292?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonsrv","html_url":"https://github.com/carbonsrv","followers_url":"https://api.github.com/users/carbonsrv/followers","following_url":"https://api.github.com/users/carbonsrv/following{/other_user}","gists_url":"https://api.github.com/users/carbonsrv/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonsrv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonsrv/subscriptions","organizations_url":"https://api.github.com/users/carbonsrv/orgs","repos_url":"https://api.github.com/users/carbonsrv/repos","events_url":"https://api.github.com/users/carbonsrv/events{/privacy}","received_events_url":"https://api.github.com/users/carbonsrv/received_events","type":"Organization","site_admin":false},"repo":{"id":34277542,"name":"carbon","full_name":"carbonsrv/carbon","owner":{"login":"carbonsrv","id":15823292,"avatar_url":"https://avatars.githubusercontent.com/u/15823292?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonsrv","html_url":"https://github.com/carbonsrv","followers_url":"https://api.github.com/users/carbonsrv/followers","following_url":"https://api.github.com/users/carbonsrv/following{/other_user}","gists_url":"https://api.github.com/users/carbonsrv/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonsrv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonsrv/subscriptions","organizations_url":"https://api.github.com/users/carbonsrv/orgs","repos_url":"https://api.github.com/users/carbonsrv/repos","events_url":"https://api.github.com/users/carbonsrv/events{/privacy}","received_events_url":"https://api.github.com/users/carbonsrv/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/carbonsrv/carbon","description":"Carbon is a Lua Application Toolkit with focus on Web Servers written in Go","fork":false,"url":"https://api.github.com/repos/carbonsrv/carbon","forks_url":"https://api.github.com/repos/carbonsrv/carbon/forks","keys_url":"https://api.github.com/repos/carbonsrv/carbon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/carbonsrv/carbon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/carbonsrv/carbon/teams","hooks_url":"https://api.github.com/repos/carbonsrv/carbon/hooks","issue_events_url":"https://api.github.com/repos/carbonsrv/carbon/issues/events{/number}","events_url":"https://api.github.com/repos/carbonsrv/carbon/events","assignees_url":"https://api.github.com/repos/carbonsrv/carbon/assignees{/user}","branches_url":"https://api.github.com/repos/carbonsrv/carbon/branches{/branch}","tags_url":"https://api.github.com/repos/carbonsrv/carbon/tags","blobs_url":"https://api.github.com/repos/carbonsrv/carbon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/carbonsrv/carbon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/carbonsrv/carbon/git/refs{/sha}","trees_url":"https://api.github.com/repos/carbonsrv/carbon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/carbonsrv/carbon/statuses/{sha}","languages_url":"https://api.github.com/repos/carbonsrv/carbon/languages","stargazers_url":"https://api.github.com/repos/carbonsrv/carbon/stargazers","contributors_url":"https://api.github.com/repos/carbonsrv/carbon/contributors","subscribers_url":"https://api.github.com/repos/carbonsrv/carbon/subscribers","subscription_url":"https://api.github.com/repos/carbonsrv/carbon/subscription","commits_url":"https://api.github.com/repos/carbonsrv/carbon/commits{/sha}","git_commits_url":"https://api.github.com/repos/carbonsrv/carbon/git/commits{/sha}","comments_url":"https://api.github.com/repos/carbonsrv/carbon/comments{/number}","issue_comment_url":"https://api.github.com/repos/carbonsrv/carbon/issues/comments{/number}","contents_url":"https://api.github.com/repos/carbonsrv/carbon/contents/{+path}","compare_url":"https://api.github.com/repos/carbonsrv/carbon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/carbonsrv/carbon/merges","archive_url":"https://api.github.com/repos/carbonsrv/carbon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/carbonsrv/carbon/downloads","issues_url":"https://api.github.com/repos/carbonsrv/carbon/issues{/number}","pulls_url":"https://api.github.com/repos/carbonsrv/carbon/pulls{/number}","milestones_url":"https://api.github.com/repos/carbonsrv/carbon/milestones{/number}","notifications_url":"https://api.github.com/repos/carbonsrv/carbon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/carbonsrv/carbon/labels{/name}","releases_url":"https://api.github.com/repos/carbonsrv/carbon/releases{/id}","deployments_url":"https://api.github.com/repos/carbonsrv/carbon/deployments","created_at":"2015-04-20T18:09:46Z","updated_at":"2016-06-05T10:47:49Z","pushed_at":"2016-06-19T13:57:00Z","git_url":"git://github.com/carbonsrv/carbon.git","ssh_url":"git@github.com:carbonsrv/carbon.git","clone_url":"https://github.com/carbonsrv/carbon.git","svn_url":"https://github.com/carbonsrv/carbon","homepage":"","size":18522,"stargazers_count":10,"watchers_count":10,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":10,"default_branch":"master"}},"base":{"label":"carbonsrv:master","ref":"master","sha":"e93a2e7dfbfc38294ee1a590a61b7d3ec6cbba2f","user":{"login":"carbonsrv","id":15823292,"avatar_url":"https://avatars.githubusercontent.com/u/15823292?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonsrv","html_url":"https://github.com/carbonsrv","followers_url":"https://api.github.com/users/carbonsrv/followers","following_url":"https://api.github.com/users/carbonsrv/following{/other_user}","gists_url":"https://api.github.com/users/carbonsrv/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonsrv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonsrv/subscriptions","organizations_url":"https://api.github.com/users/carbonsrv/orgs","repos_url":"https://api.github.com/users/carbonsrv/repos","events_url":"https://api.github.com/users/carbonsrv/events{/privacy}","received_events_url":"https://api.github.com/users/carbonsrv/received_events","type":"Organization","site_admin":false},"repo":{"id":34277542,"name":"carbon","full_name":"carbonsrv/carbon","owner":{"login":"carbonsrv","id":15823292,"avatar_url":"https://avatars.githubusercontent.com/u/15823292?v=3","gravatar_id":"","url":"https://api.github.com/users/carbonsrv","html_url":"https://github.com/carbonsrv","followers_url":"https://api.github.com/users/carbonsrv/followers","following_url":"https://api.github.com/users/carbonsrv/following{/other_user}","gists_url":"https://api.github.com/users/carbonsrv/gists{/gist_id}","starred_url":"https://api.github.com/users/carbonsrv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carbonsrv/subscriptions","organizations_url":"https://api.github.com/users/carbonsrv/orgs","repos_url":"https://api.github.com/users/carbonsrv/repos","events_url":"https://api.github.com/users/carbonsrv/events{/privacy}","received_events_url":"https://api.github.com/users/carbonsrv/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/carbonsrv/carbon","description":"Carbon is a Lua Application Toolkit with focus on Web Servers written in Go","fork":false,"url":"https://api.github.com/repos/carbonsrv/carbon","forks_url":"https://api.github.com/repos/carbonsrv/carbon/forks","keys_url":"https://api.github.com/repos/carbonsrv/carbon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/carbonsrv/carbon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/carbonsrv/carbon/teams","hooks_url":"https://api.github.com/repos/carbonsrv/carbon/hooks","issue_events_url":"https://api.github.com/repos/carbonsrv/carbon/issues/events{/number}","events_url":"https://api.github.com/repos/carbonsrv/carbon/events","assignees_url":"https://api.github.com/repos/carbonsrv/carbon/assignees{/user}","branches_url":"https://api.github.com/repos/carbonsrv/carbon/branches{/branch}","tags_url":"https://api.github.com/repos/carbonsrv/carbon/tags","blobs_url":"https://api.github.com/repos/carbonsrv/carbon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/carbonsrv/carbon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/carbonsrv/carbon/git/refs{/sha}","trees_url":"https://api.github.com/repos/carbonsrv/carbon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/carbonsrv/carbon/statuses/{sha}","languages_url":"https://api.github.com/repos/carbonsrv/carbon/languages","stargazers_url":"https://api.github.com/repos/carbonsrv/carbon/stargazers","contributors_url":"https://api.github.com/repos/carbonsrv/carbon/contributors","subscribers_url":"https://api.github.com/repos/carbonsrv/carbon/subscribers","subscription_url":"https://api.github.com/repos/carbonsrv/carbon/subscription","commits_url":"https://api.github.com/repos/carbonsrv/carbon/commits{/sha}","git_commits_url":"https://api.github.com/repos/carbonsrv/carbon/git/commits{/sha}","comments_url":"https://api.github.com/repos/carbonsrv/carbon/comments{/number}","issue_comment_url":"https://api.github.com/repos/carbonsrv/carbon/issues/comments{/number}","contents_url":"https://api.github.com/repos/carbonsrv/carbon/contents/{+path}","compare_url":"https://api.github.com/repos/carbonsrv/carbon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/carbonsrv/carbon/merges","archive_url":"https://api.github.com/repos/carbonsrv/carbon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/carbonsrv/carbon/downloads","issues_url":"https://api.github.com/repos/carbonsrv/carbon/issues{/number}","pulls_url":"https://api.github.com/repos/carbonsrv/carbon/pulls{/number}","milestones_url":"https://api.github.com/repos/carbonsrv/carbon/milestones{/number}","notifications_url":"https://api.github.com/repos/carbonsrv/carbon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/carbonsrv/carbon/labels{/name}","releases_url":"https://api.github.com/repos/carbonsrv/carbon/releases{/id}","deployments_url":"https://api.github.com/repos/carbonsrv/carbon/deployments","created_at":"2015-04-20T18:09:46Z","updated_at":"2016-06-05T10:47:49Z","pushed_at":"2016-06-19T13:57:00Z","git_url":"git://github.com/carbonsrv/carbon.git","ssh_url":"git@github.com:carbonsrv/carbon.git","clone_url":"https://github.com/carbonsrv/carbon.git","svn_url":"https://github.com/carbonsrv/carbon","homepage":"","size":18522,"stargazers_count":10,"watchers_count":10,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":10,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/carbonsrv/carbon/pulls/7"},"html":{"href":"https://github.com/carbonsrv/carbon/pull/7"},"issue":{"href":"https://api.github.com/repos/carbonsrv/carbon/issues/7"},"comments":{"href":"https://api.github.com/repos/carbonsrv/carbon/issues/7/comments"},"review_comments":{"href":"https://api.github.com/repos/carbonsrv/carbon/pulls/7/comments"},"review_comment":{"href":"https://api.github.com/repos/carbonsrv/carbon/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/carbonsrv/carbon/pulls/7/commits"},"statuses":{"href":"https://api.github.com/repos/carbonsrv/carbon/statuses/95aff05ad9a44ed3f5b3120076567ce7f0e6d11a"}}}}
{"actor":{"display_login":"vifino"}}