{"action":"created","comment":{"url":"https://api.github.com/repos/koppen/csv_query/pulls/comments/31379253","id":31379253,"diff_hunk":"@@ -1,37 +1,62 @@\n require 'sqlite3'\n+require 'csv_query/table'\n \n module CsvQuery\n-  # Wraps a SQLite in-memory database with a single table named csv.\n+  # Wraps a SQLite in-memory database with a single table named csv by default if you pass in only a csv to initialize.\n+  # Don't pass in a CSV and use #create_table and #import_data_from_csv to to perform more complex multi-table loads.\n   class Database\n-    attr_reader :database\n+    attr_accessor :sqlite_database\n+    alias_method :database, :sqlite_database # TODO: Can be removed by repo maintainer at version upgrade?\n \n-    def initialize(csv)\n-      @database = SQLite3::Database.new(':memory:')\n-      @columns = csv.headers\n-      create_table(@columns)\n+    def initialize(default_csv_to_create_table_for=nil)\n+      @tables = []\n+      @sqlite_database = SQLite3::Database.new(':memory:')\n+\n+      create_table(default_csv_to_create_table_for, 'csv') if !default_csv_to_create_table_for.nil?\n     end\n \n-    def import_data_from_csv(csv)\n-      columns = csv.headers\n+    def create_table(csv, table_name)\n+      CsvQuery::Table.new(self, csv, table_name).create_table\n+    end\n \n-      sql = \"INSERT INTO csv VALUES (#{(['?'] * columns.size).join(',')})\"\n-      statement = database.prepare(sql)\n+    def import_data_from_csv(csv, table_name='csv')\n+      CsvQuery::Table.new(self, csv, table_name).import_data_from_csv\n+    end\n \n-      csv.each do |row|\n-        statement.execute(row.fields)\n-      end\n+    def load_csv(csv, table_name)\n+      table = CsvQuery::Table.new(self, csv, table_name)\n+      table.create_table\n+      table.import_data_from_csv\n     end\n \n     # Returns the results of sql. First row of the resultset contains the column names\n     def query(sql)\n-      database.execute2(sql)\n+      @sqlite_database.execute2(sql)\n+    end\n+\n+    def query_hashes(sql)","path":"lib/csv_query/database.rb","position":51,"original_position":51,"commit_id":"4cc6954d43d9f8c47a318b540a8fd5b4ecc6936a","original_commit_id":"4cc6954d43d9f8c47a318b540a8fd5b4ecc6936a","user":{"login":"koppen","id":6480,"avatar_url":"https://avatars.githubusercontent.com/u/6480?v=3","gravatar_id":"","url":"https://api.github.com/users/koppen","html_url":"https://github.com/koppen","followers_url":"https://api.github.com/users/koppen/followers","following_url":"https://api.github.com/users/koppen/following{/other_user}","gists_url":"https://api.github.com/users/koppen/gists{/gist_id}","starred_url":"https://api.github.com/users/koppen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koppen/subscriptions","organizations_url":"https://api.github.com/users/koppen/orgs","repos_url":"https://api.github.com/users/koppen/repos","events_url":"https://api.github.com/users/koppen/events{/privacy}","received_events_url":"https://api.github.com/users/koppen/received_events","type":"User","site_admin":false},"body":"Also, this method looks like it could really use some tests; it's part of the public API and has some fairly complex behaviors.","created_at":"2015-05-30T09:30:39Z","updated_at":"2015-05-30T09:30:39Z","html_url":"https://github.com/koppen/csv_query/pull/1#discussion_r31379253","pull_request_url":"https://api.github.com/repos/koppen/csv_query/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/koppen/csv_query/pulls/comments/31379253"},"html":{"href":"https://github.com/koppen/csv_query/pull/1#discussion_r31379253"},"pull_request":{"href":"https://api.github.com/repos/koppen/csv_query/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/koppen/csv_query/pulls/1","id":35957987,"html_url":"https://github.com/koppen/csv_query/pull/1","diff_url":"https://github.com/koppen/csv_query/pull/1.diff","patch_url":"https://github.com/koppen/csv_query/pull/1.patch","issue_url":"https://api.github.com/repos/koppen/csv_query/issues/1","number":1,"state":"open","locked":false,"title":"Add support for multiple tables and more","user":{"login":"IntractableQuery","id":122086,"avatar_url":"https://avatars.githubusercontent.com/u/122086?v=3","gravatar_id":"","url":"https://api.github.com/users/IntractableQuery","html_url":"https://github.com/IntractableQuery","followers_url":"https://api.github.com/users/IntractableQuery/followers","following_url":"https://api.github.com/users/IntractableQuery/following{/other_user}","gists_url":"https://api.github.com/users/IntractableQuery/gists{/gist_id}","starred_url":"https://api.github.com/users/IntractableQuery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IntractableQuery/subscriptions","organizations_url":"https://api.github.com/users/IntractableQuery/orgs","repos_url":"https://api.github.com/users/IntractableQuery/repos","events_url":"https://api.github.com/users/IntractableQuery/events{/privacy}","received_events_url":"https://api.github.com/users/IntractableQuery/received_events","type":"User","site_admin":false},"body":"I am using your gem to load dozens of CSV tables and query them easily, so made it easier to do that via ::Database.  Converted VARCHAR(255) to TEXT.  All bulk inserts are via transaction now, which has some performance benefit when I override the sqlite database to be stored on disk.  Added #query_hashes, my favorite-er way to get rows.  Renamed accessor to sqlite_database because it is confusing to init ::Database and then call database.database.\r\n\r\nNot sure if my Gemfile.lock being committed is a best-practice for gems, though it makes sense for Rails projects.  May want to take this out and .gitignore it?","created_at":"2015-05-21T17:32:14Z","updated_at":"2015-05-30T09:30:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c0685909f5358b94c75e545ffc252cb71576fe84","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/koppen/csv_query/pulls/1/commits","review_comments_url":"https://api.github.com/repos/koppen/csv_query/pulls/1/comments","review_comment_url":"https://api.github.com/repos/koppen/csv_query/pulls/comments{/number}","comments_url":"https://api.github.com/repos/koppen/csv_query/issues/1/comments","statuses_url":"https://api.github.com/repos/koppen/csv_query/statuses/4cc6954d43d9f8c47a318b540a8fd5b4ecc6936a","head":{"label":"TrumanDev:master","ref":"master","sha":"4cc6954d43d9f8c47a318b540a8fd5b4ecc6936a","user":{"login":"TrumanDev","id":10408243,"avatar_url":"https://avatars.githubusercontent.com/u/10408243?v=3","gravatar_id":"","url":"https://api.github.com/users/TrumanDev","html_url":"https://github.com/TrumanDev","followers_url":"https://api.github.com/users/TrumanDev/followers","following_url":"https://api.github.com/users/TrumanDev/following{/other_user}","gists_url":"https://api.github.com/users/TrumanDev/gists{/gist_id}","starred_url":"https://api.github.com/users/TrumanDev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TrumanDev/subscriptions","organizations_url":"https://api.github.com/users/TrumanDev/orgs","repos_url":"https://api.github.com/users/TrumanDev/repos","events_url":"https://api.github.com/users/TrumanDev/events{/privacy}","received_events_url":"https://api.github.com/users/TrumanDev/received_events","type":"Organization","site_admin":false},"repo":{"id":35828681,"name":"csv_query","full_name":"TrumanDev/csv_query","owner":{"login":"TrumanDev","id":10408243,"avatar_url":"https://avatars.githubusercontent.com/u/10408243?v=3","gravatar_id":"","url":"https://api.github.com/users/TrumanDev","html_url":"https://github.com/TrumanDev","followers_url":"https://api.github.com/users/TrumanDev/followers","following_url":"https://api.github.com/users/TrumanDev/following{/other_user}","gists_url":"https://api.github.com/users/TrumanDev/gists{/gist_id}","starred_url":"https://api.github.com/users/TrumanDev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TrumanDev/subscriptions","organizations_url":"https://api.github.com/users/TrumanDev/orgs","repos_url":"https://api.github.com/users/TrumanDev/repos","events_url":"https://api.github.com/users/TrumanDev/events{/privacy}","received_events_url":"https://api.github.com/users/TrumanDev/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/TrumanDev/csv_query","description":"Command line tool to query CSV data using SQL","fork":true,"url":"https://api.github.com/repos/TrumanDev/csv_query","forks_url":"https://api.github.com/repos/TrumanDev/csv_query/forks","keys_url":"https://api.github.com/repos/TrumanDev/csv_query/keys{/key_id}","collaborators_url":"https://api.github.com/repos/TrumanDev/csv_query/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/TrumanDev/csv_query/teams","hooks_url":"https://api.github.com/repos/TrumanDev/csv_query/hooks","issue_events_url":"https://api.github.com/repos/TrumanDev/csv_query/issues/events{/number}","events_url":"https://api.github.com/repos/TrumanDev/csv_query/events","assignees_url":"https://api.github.com/repos/TrumanDev/csv_query/assignees{/user}","branches_url":"https://api.github.com/repos/TrumanDev/csv_query/branches{/branch}","tags_url":"https://api.github.com/repos/TrumanDev/csv_query/tags","blobs_url":"https://api.github.com/repos/TrumanDev/csv_query/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/TrumanDev/csv_query/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/TrumanDev/csv_query/git/refs{/sha}","trees_url":"https://api.github.com/repos/TrumanDev/csv_query/git/trees{/sha}","statuses_url":"https://api.github.com/repos/TrumanDev/csv_query/statuses/{sha}","languages_url":"https://api.github.com/repos/TrumanDev/csv_query/languages","stargazers_url":"https://api.github.com/repos/TrumanDev/csv_query/stargazers","contributors_url":"https://api.github.com/repos/TrumanDev/csv_query/contributors","subscribers_url":"https://api.github.com/repos/TrumanDev/csv_query/subscribers","subscription_url":"https://api.github.com/repos/TrumanDev/csv_query/subscription","commits_url":"https://api.github.com/repos/TrumanDev/csv_query/commits{/sha}","git_commits_url":"https://api.github.com/repos/TrumanDev/csv_query/git/commits{/sha}","comments_url":"https://api.github.com/repos/TrumanDev/csv_query/comments{/number}","issue_comment_url":"https://api.github.com/repos/TrumanDev/csv_query/issues/comments{/number}","contents_url":"https://api.github.com/repos/TrumanDev/csv_query/contents/{+path}","compare_url":"https://api.github.com/repos/TrumanDev/csv_query/compare/{base}...{head}","merges_url":"https://api.github.com/repos/TrumanDev/csv_query/merges","archive_url":"https://api.github.com/repos/TrumanDev/csv_query/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/TrumanDev/csv_query/downloads","issues_url":"https://api.github.com/repos/TrumanDev/csv_query/issues{/number}","pulls_url":"https://api.github.com/repos/TrumanDev/csv_query/pulls{/number}","milestones_url":"https://api.github.com/repos/TrumanDev/csv_query/milestones{/number}","notifications_url":"https://api.github.com/repos/TrumanDev/csv_query/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/TrumanDev/csv_query/labels{/name}","releases_url":"https://api.github.com/repos/TrumanDev/csv_query/releases{/id}","created_at":"2015-05-18T16:17:45Z","updated_at":"2015-05-18T16:17:45Z","pushed_at":"2015-05-21T16:57:39Z","git_url":"git://github.com/TrumanDev/csv_query.git","ssh_url":"git@github.com:TrumanDev/csv_query.git","clone_url":"https://github.com/TrumanDev/csv_query.git","svn_url":"https://github.com/TrumanDev/csv_query","homepage":null,"size":112,"stargazers_count":0,"watchers_count":0,"language":"Ruby","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":0,"default_branch":"master"}},"base":{"label":"koppen:master","ref":"master","sha":"6fc44156e8888e6d16ceb04232f7bad96dd30911","user":{"login":"koppen","id":6480,"avatar_url":"https://avatars.githubusercontent.com/u/6480?v=3","gravatar_id":"","url":"https://api.github.com/users/koppen","html_url":"https://github.com/koppen","followers_url":"https://api.github.com/users/koppen/followers","following_url":"https://api.github.com/users/koppen/following{/other_user}","gists_url":"https://api.github.com/users/koppen/gists{/gist_id}","starred_url":"https://api.github.com/users/koppen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koppen/subscriptions","organizations_url":"https://api.github.com/users/koppen/orgs","repos_url":"https://api.github.com/users/koppen/repos","events_url":"https://api.github.com/users/koppen/events{/privacy}","received_events_url":"https://api.github.com/users/koppen/received_events","type":"User","site_admin":false},"repo":{"id":6541851,"name":"csv_query","full_name":"koppen/csv_query","owner":{"login":"koppen","id":6480,"avatar_url":"https://avatars.githubusercontent.com/u/6480?v=3","gravatar_id":"","url":"https://api.github.com/users/koppen","html_url":"https://github.com/koppen","followers_url":"https://api.github.com/users/koppen/followers","following_url":"https://api.github.com/users/koppen/following{/other_user}","gists_url":"https://api.github.com/users/koppen/gists{/gist_id}","starred_url":"https://api.github.com/users/koppen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koppen/subscriptions","organizations_url":"https://api.github.com/users/koppen/orgs","repos_url":"https://api.github.com/users/koppen/repos","events_url":"https://api.github.com/users/koppen/events{/privacy}","received_events_url":"https://api.github.com/users/koppen/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/koppen/csv_query","description":"Command line tool to query CSV data using SQL","fork":false,"url":"https://api.github.com/repos/koppen/csv_query","forks_url":"https://api.github.com/repos/koppen/csv_query/forks","keys_url":"https://api.github.com/repos/koppen/csv_query/keys{/key_id}","collaborators_url":"https://api.github.com/repos/koppen/csv_query/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/koppen/csv_query/teams","hooks_url":"https://api.github.com/repos/koppen/csv_query/hooks","issue_events_url":"https://api.github.com/repos/koppen/csv_query/issues/events{/number}","events_url":"https://api.github.com/repos/koppen/csv_query/events","assignees_url":"https://api.github.com/repos/koppen/csv_query/assignees{/user}","branches_url":"https://api.github.com/repos/koppen/csv_query/branches{/branch}","tags_url":"https://api.github.com/repos/koppen/csv_query/tags","blobs_url":"https://api.github.com/repos/koppen/csv_query/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/koppen/csv_query/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/koppen/csv_query/git/refs{/sha}","trees_url":"https://api.github.com/repos/koppen/csv_query/git/trees{/sha}","statuses_url":"https://api.github.com/repos/koppen/csv_query/statuses/{sha}","languages_url":"https://api.github.com/repos/koppen/csv_query/languages","stargazers_url":"https://api.github.com/repos/koppen/csv_query/stargazers","contributors_url":"https://api.github.com/repos/koppen/csv_query/contributors","subscribers_url":"https://api.github.com/repos/koppen/csv_query/subscribers","subscription_url":"https://api.github.com/repos/koppen/csv_query/subscription","commits_url":"https://api.github.com/repos/koppen/csv_query/commits{/sha}","git_commits_url":"https://api.github.com/repos/koppen/csv_query/git/commits{/sha}","comments_url":"https://api.github.com/repos/koppen/csv_query/comments{/number}","issue_comment_url":"https://api.github.com/repos/koppen/csv_query/issues/comments{/number}","contents_url":"https://api.github.com/repos/koppen/csv_query/contents/{+path}","compare_url":"https://api.github.com/repos/koppen/csv_query/compare/{base}...{head}","merges_url":"https://api.github.com/repos/koppen/csv_query/merges","archive_url":"https://api.github.com/repos/koppen/csv_query/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/koppen/csv_query/downloads","issues_url":"https://api.github.com/repos/koppen/csv_query/issues{/number}","pulls_url":"https://api.github.com/repos/koppen/csv_query/pulls{/number}","milestones_url":"https://api.github.com/repos/koppen/csv_query/milestones{/number}","notifications_url":"https://api.github.com/repos/koppen/csv_query/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/koppen/csv_query/labels{/name}","releases_url":"https://api.github.com/repos/koppen/csv_query/releases{/id}","created_at":"2012-11-05T09:25:22Z","updated_at":"2015-04-06T17:22:10Z","pushed_at":"2015-05-30T09:14:47Z","git_url":"git://github.com/koppen/csv_query.git","ssh_url":"git@github.com:koppen/csv_query.git","clone_url":"https://github.com/koppen/csv_query.git","svn_url":"https://github.com/koppen/csv_query","homepage":null,"size":233,"stargazers_count":13,"watchers_count":13,"language":"Ruby","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":13,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/koppen/csv_query/pulls/1"},"html":{"href":"https://github.com/koppen/csv_query/pull/1"},"issue":{"href":"https://api.github.com/repos/koppen/csv_query/issues/1"},"comments":{"href":"https://api.github.com/repos/koppen/csv_query/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/koppen/csv_query/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/koppen/csv_query/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/koppen/csv_query/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/koppen/csv_query/statuses/4cc6954d43d9f8c47a318b540a8fd5b4ecc6936a"}}}}