{"action":"created","comment":{"url":"https://api.github.com/repos/mmmries/sqlitex/pulls/comments/28563833","id":28563833,"diff_hunk":"@@ -7,19 +7,47 @@ defmodule Sqlitex do\n     :esqlite3.open(path)\n   end\n \n-  def query(db, query, opts \\\\ []) do\n-    {:ok, statement} = :esqlite3.prepare(query, db)\n-    types = :esqlite3.column_types(statement) |> Tuple.to_list\n-    columns = :esqlite3.column_names(statement) |> Tuple.to_list\n-    rows = :esqlite3.fetchall(statement)\n-    into = Keyword.get(opts, :into, [])\n-    Sqlitex.Row.from(types, columns, rows, into)\n-  end\n-\n   def with_db(path, fun) do\n     {:ok, db} = open(path)\n     res = fun.(db)\n     close(db)\n     res\n   end\n+\n+  def exec(db, sql) do\n+    :esqlite3.exec(sql, db)\n+  end\n+\n+  def query(db, sql, opts \\\\ []) do\n+    {params, into} = query_options(opts)\n+    {:ok, statement} = :esqlite3.prepare(sql, db)\n+    :ok = :esqlite3.bind(statement, params)\n+    types = :esqlite3.column_types(statement)\n+    columns = :esqlite3.column_names(statement)\n+    rows = :esqlite3.fetchall(statement)\n+    return_rows_or_error(types, columns, rows, into)\n+  end\n+\n+  defp query_options(opts) do\n+    params = Keyword.get(opts, :bind, [])\n+    into = Keyword.get(opts, :into, [])\n+    {params, into}\n+  end\n+\n+  defp return_rows_or_error({:error, kind} = error, columns, rows, into) do\n+    case kind do\n+      :no_columns -> return_rows_or_error({}, columns, rows, into)\n+      _ -> error\n+    end\n+  end\n+  defp return_rows_or_error(types, {:error, kind} = error, rows, into) do\n+    case kind do\n+      :no_columns -> return_rows_or_error(types, {}, rows, into)\n+      _ -> error\n+    end\n+  end","path":"lib/sqlitex.ex","position":51,"original_position":51,"commit_id":"ac75d13bdfbe8aea49d3ba9b2ebfb788132a7815","original_commit_id":"ac75d13bdfbe8aea49d3ba9b2ebfb788132a7815","user":{"login":"jazzyb","id":52593,"avatar_url":"https://avatars.githubusercontent.com/u/52593?v=3","gravatar_id":"","url":"https://api.github.com/users/jazzyb","html_url":"https://github.com/jazzyb","followers_url":"https://api.github.com/users/jazzyb/followers","following_url":"https://api.github.com/users/jazzyb/following{/other_user}","gists_url":"https://api.github.com/users/jazzyb/gists{/gist_id}","starred_url":"https://api.github.com/users/jazzyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jazzyb/subscriptions","organizations_url":"https://api.github.com/users/jazzyb/orgs","repos_url":"https://api.github.com/users/jazzyb/repos","events_url":"https://api.github.com/users/jazzyb/events{/privacy}","received_events_url":"https://api.github.com/users/jazzyb/received_events","type":"User","site_admin":false},"body":"I'll look into writing a fix for the esqlite code.\r\n\r\nI kind of like having a separate `query` and `exec`.  The first implies that you care about the output while the latter is more like a fire-and-forget -- it only returns `:ok` or an error.  As a wrapper around `sqlite3_exec`, the `exec` call can't take parametrized statements.  In [sqlite_ecto](https://github.com/jazzyb/sqlite_ecto) I use `exec` internally for imperative commands like transactions and triggers -- things for which bound variables wouldn't make much sense anyway.\r\n\r\nEcto uses `query` because it first builds statements dynamically and then passes these SQL strings back to the adapter to be executed.  The adapter is agnostic about the statement being passed in so using `query` for everything is a safe bet.","created_at":"2015-04-17T01:08:08Z","updated_at":"2015-04-17T01:08:08Z","html_url":"https://github.com/mmmries/sqlitex/pull/4#discussion_r28563833","pull_request_url":"https://api.github.com/repos/mmmries/sqlitex/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/mmmries/sqlitex/pulls/comments/28563833"},"html":{"href":"https://github.com/mmmries/sqlitex/pull/4#discussion_r28563833"},"pull_request":{"href":"https://api.github.com/repos/mmmries/sqlitex/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/mmmries/sqlitex/pulls/4","id":33311962,"html_url":"https://github.com/mmmries/sqlitex/pull/4","diff_url":"https://github.com/mmmries/sqlitex/pull/4.diff","patch_url":"https://github.com/mmmries/sqlitex/pull/4.patch","issue_url":"https://api.github.com/repos/mmmries/sqlitex/issues/4","number":4,"state":"open","locked":false,"title":"Additional Features","user":{"login":"jazzyb","id":52593,"avatar_url":"https://avatars.githubusercontent.com/u/52593?v=3","gravatar_id":"","url":"https://api.github.com/users/jazzyb","html_url":"https://github.com/jazzyb","followers_url":"https://api.github.com/users/jazzyb/followers","following_url":"https://api.github.com/users/jazzyb/following{/other_user}","gists_url":"https://api.github.com/users/jazzyb/gists{/gist_id}","starred_url":"https://api.github.com/users/jazzyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jazzyb/subscriptions","organizations_url":"https://api.github.com/users/jazzyb/orgs","repos_url":"https://api.github.com/users/jazzyb/repos","events_url":"https://api.github.com/users/jazzyb/events{/privacy}","received_events_url":"https://api.github.com/users/jazzyb/received_events","type":"User","site_admin":false},"body":"I'm playing around with SQLite and Elixir, and I added some additional functionality to your library that I needed.  Namely:\r\n* stop() call for the gen_server\r\n* exec() calls for writing to the database\r\n* parametrized queries\r\n\r\nNote that there is a dependency on my fork of the esqlite repo.  I fixed a bug to get some of the new sqlitex functionality to work.  The fix was merged with the upstream repo, but it apparently hasn't been added to hex.pm yet.  When it is, the dependency should be fixed.\r\n\r\nThanks and keep up the good work!","created_at":"2015-04-15T00:05:44Z","updated_at":"2015-04-17T01:08:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"438eb523f13d0ee0864b8be0baadd3b1142060fc","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mmmries/sqlitex/pulls/4/commits","review_comments_url":"https://api.github.com/repos/mmmries/sqlitex/pulls/4/comments","review_comment_url":"https://api.github.com/repos/mmmries/sqlitex/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mmmries/sqlitex/issues/4/comments","statuses_url":"https://api.github.com/repos/mmmries/sqlitex/statuses/ac75d13bdfbe8aea49d3ba9b2ebfb788132a7815","head":{"label":"jazzyb:master","ref":"master","sha":"ac75d13bdfbe8aea49d3ba9b2ebfb788132a7815","user":{"login":"jazzyb","id":52593,"avatar_url":"https://avatars.githubusercontent.com/u/52593?v=3","gravatar_id":"","url":"https://api.github.com/users/jazzyb","html_url":"https://github.com/jazzyb","followers_url":"https://api.github.com/users/jazzyb/followers","following_url":"https://api.github.com/users/jazzyb/following{/other_user}","gists_url":"https://api.github.com/users/jazzyb/gists{/gist_id}","starred_url":"https://api.github.com/users/jazzyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jazzyb/subscriptions","organizations_url":"https://api.github.com/users/jazzyb/orgs","repos_url":"https://api.github.com/users/jazzyb/repos","events_url":"https://api.github.com/users/jazzyb/events{/privacy}","received_events_url":"https://api.github.com/users/jazzyb/received_events","type":"User","site_admin":false},"repo":{"id":31464188,"name":"sqlitex","full_name":"jazzyb/sqlitex","owner":{"login":"jazzyb","id":52593,"avatar_url":"https://avatars.githubusercontent.com/u/52593?v=3","gravatar_id":"","url":"https://api.github.com/users/jazzyb","html_url":"https://github.com/jazzyb","followers_url":"https://api.github.com/users/jazzyb/followers","following_url":"https://api.github.com/users/jazzyb/following{/other_user}","gists_url":"https://api.github.com/users/jazzyb/gists{/gist_id}","starred_url":"https://api.github.com/users/jazzyb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jazzyb/subscriptions","organizations_url":"https://api.github.com/users/jazzyb/orgs","repos_url":"https://api.github.com/users/jazzyb/repos","events_url":"https://api.github.com/users/jazzyb/events{/privacy}","received_events_url":"https://api.github.com/users/jazzyb/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jazzyb/sqlitex","description":"An Elixir wrapper around esqlite. Allows access to sqlite3 databases.","fork":true,"url":"https://api.github.com/repos/jazzyb/sqlitex","forks_url":"https://api.github.com/repos/jazzyb/sqlitex/forks","keys_url":"https://api.github.com/repos/jazzyb/sqlitex/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jazzyb/sqlitex/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jazzyb/sqlitex/teams","hooks_url":"https://api.github.com/repos/jazzyb/sqlitex/hooks","issue_events_url":"https://api.github.com/repos/jazzyb/sqlitex/issues/events{/number}","events_url":"https://api.github.com/repos/jazzyb/sqlitex/events","assignees_url":"https://api.github.com/repos/jazzyb/sqlitex/assignees{/user}","branches_url":"https://api.github.com/repos/jazzyb/sqlitex/branches{/branch}","tags_url":"https://api.github.com/repos/jazzyb/sqlitex/tags","blobs_url":"https://api.github.com/repos/jazzyb/sqlitex/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jazzyb/sqlitex/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jazzyb/sqlitex/git/refs{/sha}","trees_url":"https://api.github.com/repos/jazzyb/sqlitex/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jazzyb/sqlitex/statuses/{sha}","languages_url":"https://api.github.com/repos/jazzyb/sqlitex/languages","stargazers_url":"https://api.github.com/repos/jazzyb/sqlitex/stargazers","contributors_url":"https://api.github.com/repos/jazzyb/sqlitex/contributors","subscribers_url":"https://api.github.com/repos/jazzyb/sqlitex/subscribers","subscription_url":"https://api.github.com/repos/jazzyb/sqlitex/subscription","commits_url":"https://api.github.com/repos/jazzyb/sqlitex/commits{/sha}","git_commits_url":"https://api.github.com/repos/jazzyb/sqlitex/git/commits{/sha}","comments_url":"https://api.github.com/repos/jazzyb/sqlitex/comments{/number}","issue_comment_url":"https://api.github.com/repos/jazzyb/sqlitex/issues/comments{/number}","contents_url":"https://api.github.com/repos/jazzyb/sqlitex/contents/{+path}","compare_url":"https://api.github.com/repos/jazzyb/sqlitex/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jazzyb/sqlitex/merges","archive_url":"https://api.github.com/repos/jazzyb/sqlitex/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jazzyb/sqlitex/downloads","issues_url":"https://api.github.com/repos/jazzyb/sqlitex/issues{/number}","pulls_url":"https://api.github.com/repos/jazzyb/sqlitex/pulls{/number}","milestones_url":"https://api.github.com/repos/jazzyb/sqlitex/milestones{/number}","notifications_url":"https://api.github.com/repos/jazzyb/sqlitex/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jazzyb/sqlitex/labels{/name}","releases_url":"https://api.github.com/repos/jazzyb/sqlitex/releases{/id}","created_at":"2015-02-28T14:01:18Z","updated_at":"2015-04-16T22:18:59Z","pushed_at":"2015-04-16T22:18:59Z","git_url":"git://github.com/jazzyb/sqlitex.git","ssh_url":"git@github.com:jazzyb/sqlitex.git","clone_url":"https://github.com/jazzyb/sqlitex.git","svn_url":"https://github.com/jazzyb/sqlitex","homepage":"https://hex.pm/packages/sqlitex","size":112,"stargazers_count":1,"watchers_count":1,"language":"Elixir","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"mmmries:master","ref":"master","sha":"69caa9d82924238475e7667abf932248fe9a1695","user":{"login":"mmmries","id":80008,"avatar_url":"https://avatars.githubusercontent.com/u/80008?v=3","gravatar_id":"","url":"https://api.github.com/users/mmmries","html_url":"https://github.com/mmmries","followers_url":"https://api.github.com/users/mmmries/followers","following_url":"https://api.github.com/users/mmmries/following{/other_user}","gists_url":"https://api.github.com/users/mmmries/gists{/gist_id}","starred_url":"https://api.github.com/users/mmmries/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mmmries/subscriptions","organizations_url":"https://api.github.com/users/mmmries/orgs","repos_url":"https://api.github.com/users/mmmries/repos","events_url":"https://api.github.com/users/mmmries/events{/privacy}","received_events_url":"https://api.github.com/users/mmmries/received_events","type":"User","site_admin":false},"repo":{"id":29835967,"name":"sqlitex","full_name":"mmmries/sqlitex","owner":{"login":"mmmries","id":80008,"avatar_url":"https://avatars.githubusercontent.com/u/80008?v=3","gravatar_id":"","url":"https://api.github.com/users/mmmries","html_url":"https://github.com/mmmries","followers_url":"https://api.github.com/users/mmmries/followers","following_url":"https://api.github.com/users/mmmries/following{/other_user}","gists_url":"https://api.github.com/users/mmmries/gists{/gist_id}","starred_url":"https://api.github.com/users/mmmries/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mmmries/subscriptions","organizations_url":"https://api.github.com/users/mmmries/orgs","repos_url":"https://api.github.com/users/mmmries/repos","events_url":"https://api.github.com/users/mmmries/events{/privacy}","received_events_url":"https://api.github.com/users/mmmries/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mmmries/sqlitex","description":"An Elixir wrapper around esqlite. Allows access to sqlite3 databases.","fork":false,"url":"https://api.github.com/repos/mmmries/sqlitex","forks_url":"https://api.github.com/repos/mmmries/sqlitex/forks","keys_url":"https://api.github.com/repos/mmmries/sqlitex/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mmmries/sqlitex/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mmmries/sqlitex/teams","hooks_url":"https://api.github.com/repos/mmmries/sqlitex/hooks","issue_events_url":"https://api.github.com/repos/mmmries/sqlitex/issues/events{/number}","events_url":"https://api.github.com/repos/mmmries/sqlitex/events","assignees_url":"https://api.github.com/repos/mmmries/sqlitex/assignees{/user}","branches_url":"https://api.github.com/repos/mmmries/sqlitex/branches{/branch}","tags_url":"https://api.github.com/repos/mmmries/sqlitex/tags","blobs_url":"https://api.github.com/repos/mmmries/sqlitex/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mmmries/sqlitex/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mmmries/sqlitex/git/refs{/sha}","trees_url":"https://api.github.com/repos/mmmries/sqlitex/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mmmries/sqlitex/statuses/{sha}","languages_url":"https://api.github.com/repos/mmmries/sqlitex/languages","stargazers_url":"https://api.github.com/repos/mmmries/sqlitex/stargazers","contributors_url":"https://api.github.com/repos/mmmries/sqlitex/contributors","subscribers_url":"https://api.github.com/repos/mmmries/sqlitex/subscribers","subscription_url":"https://api.github.com/repos/mmmries/sqlitex/subscription","commits_url":"https://api.github.com/repos/mmmries/sqlitex/commits{/sha}","git_commits_url":"https://api.github.com/repos/mmmries/sqlitex/git/commits{/sha}","comments_url":"https://api.github.com/repos/mmmries/sqlitex/comments{/number}","issue_comment_url":"https://api.github.com/repos/mmmries/sqlitex/issues/comments{/number}","contents_url":"https://api.github.com/repos/mmmries/sqlitex/contents/{+path}","compare_url":"https://api.github.com/repos/mmmries/sqlitex/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mmmries/sqlitex/merges","archive_url":"https://api.github.com/repos/mmmries/sqlitex/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mmmries/sqlitex/downloads","issues_url":"https://api.github.com/repos/mmmries/sqlitex/issues{/number}","pulls_url":"https://api.github.com/repos/mmmries/sqlitex/pulls{/number}","milestones_url":"https://api.github.com/repos/mmmries/sqlitex/milestones{/number}","notifications_url":"https://api.github.com/repos/mmmries/sqlitex/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mmmries/sqlitex/labels{/name}","releases_url":"https://api.github.com/repos/mmmries/sqlitex/releases{/id}","created_at":"2015-01-25T23:19:33Z","updated_at":"2015-02-25T04:22:57Z","pushed_at":"2015-04-16T22:19:00Z","git_url":"git://github.com/mmmries/sqlitex.git","ssh_url":"git@github.com:mmmries/sqlitex.git","clone_url":"https://github.com/mmmries/sqlitex.git","svn_url":"https://github.com/mmmries/sqlitex","homepage":"https://hex.pm/packages/sqlitex","size":198,"stargazers_count":4,"watchers_count":4,"language":"Elixir","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":2,"forks":3,"open_issues":2,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mmmries/sqlitex/pulls/4"},"html":{"href":"https://github.com/mmmries/sqlitex/pull/4"},"issue":{"href":"https://api.github.com/repos/mmmries/sqlitex/issues/4"},"comments":{"href":"https://api.github.com/repos/mmmries/sqlitex/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/mmmries/sqlitex/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/mmmries/sqlitex/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mmmries/sqlitex/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/mmmries/sqlitex/statuses/ac75d13bdfbe8aea49d3ba9b2ebfb788132a7815"}}}}