csv_query 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8cc10e4acf7a8a4c739e8c9c8aa3ad9f652567e6
4
+ data.tar.gz: 78abb583938228f9c3f69f8f9c35c08f31b607cb
5
+ SHA512:
6
+ metadata.gz: 85ef2a10afd195da6f9a2164d46ea45d6af53d5c5ced3f5f425e24cbea4f0879e4027513918b4a279c500532d1cadd6efc881c39fb7119633cc895d025a1e1e4
7
+ data.tar.gz: e950e74458b94be36b289465136a804c44e95b775a9cff04ec26805d2e68266ac4e658da703375797dcb4ac5dbe60956e4210920f1ae5380ae2306d44716da67
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.2.0
4
+ - 2.1.5
5
+ - 2.0.0
3
6
  - 1.9.3
4
- - rbx-19mode
@@ -0,0 +1,9 @@
1
+ # Change log
2
+
3
+ ## [1.0.4] - 2015-01-10
4
+
5
+ ### Fixed
6
+
7
+ - Actually take user supplied options like `delimiter` and `select` into account instead of always overriding them with the defaults.
8
+
9
+ [1.0.4]: https://github.com/koppen/csv_query/compare/v1.0.3...v1.0.4
data/Gemfile CHANGED
@@ -3,4 +3,5 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in csv_query.gemspec
4
4
  gemspec
5
5
 
6
+ gem "minitest"
6
7
  gem "rake"
data/bin/csvq CHANGED
File without changes
@@ -0,0 +1,37 @@
1
+ require 'sqlite3'
2
+
3
+ module CsvQuery
4
+ # Wraps a SQLite in-memory database with a single table named csv.
5
+ class Database
6
+ attr_reader :database
7
+
8
+ def initialize(csv)
9
+ @database = SQLite3::Database.new(':memory:')
10
+ @columns = csv.headers
11
+ create_table(@columns)
12
+ end
13
+
14
+ def import_data_from_csv(csv)
15
+ columns = csv.headers
16
+
17
+ sql = "INSERT INTO csv VALUES (#{(['?'] * columns.size).join(',')})"
18
+ statement = database.prepare(sql)
19
+
20
+ csv.each do |row|
21
+ statement.execute(row.fields)
22
+ end
23
+ end
24
+
25
+ # Returns the results of sql. First row of the resultset contains the column names
26
+ def query(sql)
27
+ database.execute2(sql)
28
+ end
29
+
30
+ private
31
+
32
+ def create_table(column_names)
33
+ column_definitions = column_names.collect { |name| "\"#{name}\" VARCHAR(255)" }
34
+ database.execute "CREATE TABLE csv (#{column_definitions.join(", ")})"
35
+ end
36
+ end
37
+ end
@@ -2,6 +2,8 @@ require 'csv'
2
2
  require 'optparse'
3
3
  require 'sqlite3'
4
4
 
5
+ require 'csv_query/database'
6
+
5
7
  module CsvQuery
6
8
  class Query
7
9
  attr_reader :csv_data, :options
@@ -14,7 +16,7 @@ module CsvQuery
14
16
  def initialize(csv_data, outputter, options = {})
15
17
  @csv_data = csv_data
16
18
  @outputter = outputter
17
- @options = options.merge(DEFAULT_OPTIONS)
19
+ @options = DEFAULT_OPTIONS.merge(options)
18
20
  end
19
21
 
20
22
  def run
@@ -41,18 +43,9 @@ module CsvQuery
41
43
  ].join(" ")
42
44
  end
43
45
 
44
- def create_database_and_table(csv)
45
- database = SQLite3::Database.new(':memory:')
46
-
47
- column_definitions = csv.headers.collect { |name| "\"#{name}\" VARCHAR(255)" }
48
- database.execute "CREATE TABLE csv (#{column_definitions.join(", ")})"
49
-
50
- database
51
- end
52
-
53
46
  def create_database_with_data_from_csv
54
- database = create_database_and_table(csv)
55
- import_csv_into_database(csv, database)
47
+ database = CsvQuery::Database.new(csv)
48
+ database.import_data_from_csv(csv)
56
49
  database
57
50
  end
58
51
 
@@ -68,15 +61,6 @@ module CsvQuery
68
61
  options[:headers] || :first_row
69
62
  end
70
63
 
71
- def import_csv_into_database(csv, database)
72
- sql = "INSERT INTO csv VALUES (#{(['?'] * csv.headers.size).join(',')})"
73
- statement = database.prepare(sql)
74
-
75
- csv.each do |row|
76
- statement.execute(row.fields)
77
- end
78
- end
79
-
80
64
  def output_results_table(results)
81
65
  @outputter.output(results)
82
66
  end
@@ -91,7 +75,7 @@ module CsvQuery
91
75
  end
92
76
 
93
77
  def run_query
94
- database.execute2(sql_query)
78
+ database.query(sql_query)
95
79
  end
96
80
 
97
81
  def sql_query
@@ -1,3 +1,3 @@
1
1
  module CsvQuery
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -0,0 +1,48 @@
1
+ require_relative '../test_helper'
2
+
3
+ require 'csv_query/database'
4
+
5
+ describe CsvQuery::Database do
6
+
7
+ def csv_data
8
+ @csv_data ||= CSV.parse("Bar", :headers => ["Foo"])
9
+ end
10
+
11
+ def build_database
12
+ CsvQuery::Database.new(csv_data)
13
+ end
14
+
15
+ describe ".new" do
16
+ it "creates SQLite database" do
17
+ database = build_database
18
+ database.database.class.must_equal(SQLite3::Database)
19
+ end
20
+
21
+ it "creates a database table" do
22
+ database = build_database
23
+
24
+ columns = database.database.table_info("csv")
25
+ columns.collect { |column| column["name"] }.must_equal(["Foo"])
26
+ end
27
+ end
28
+
29
+ describe "#import_csv" do
30
+ it "imports data from csv" do
31
+ database = build_database
32
+ database.import_data_from_csv(csv_data)
33
+
34
+ results = database.database.query("SELECT * FROM csv")
35
+ results.to_a.must_equal([["Bar"]])
36
+ end
37
+ end
38
+
39
+ describe "#query" do
40
+ it "returns headers and results as arrays" do
41
+ database = build_database
42
+ database.import_data_from_csv(csv_data)
43
+
44
+ results = database.query("SELECT * FROM csv")
45
+ results.to_a.must_equal([["Foo"], ["Bar"]])
46
+ end
47
+ end
48
+ end
@@ -11,12 +11,18 @@ describe CsvQuery::Query do
11
11
  query.csv_data.must_equal('foo')
12
12
  end
13
13
 
14
- it "merges options with default options" do
14
+ it "uses default options" do
15
15
  query = CsvQuery::Query.new('foo', CsvQuery::Outputter, {:bar => 'baz'})
16
16
  query.options[:delimiter].must_equal(',')
17
17
  query.options[:select].must_equal('*')
18
18
  end
19
19
 
20
+ it "merges options with default options" do
21
+ query = CsvQuery::Query.new('foo', CsvQuery::Outputter, {:delimiter => ";", :select => "stuff"})
22
+ query.options[:delimiter].must_equal(";")
23
+ query.options[:select].must_equal("stuff")
24
+ end
25
+
20
26
  it "preserves extra options" do
21
27
  query = CsvQuery::Query.new('foo', CsvQuery::Outputter, {:bar => 'baz'})
22
28
  query.options[:bar].must_equal('baz')
metadata CHANGED
@@ -1,30 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
5
- prerelease:
4
+ version: 1.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jakob Skjerning
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-12 00:00:00.000000000 Z
11
+ date: 2015-01-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: sqlite3
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  description: CSV Query allows you to run SQL queries against data stored in CSV files.
@@ -35,8 +32,9 @@ executables:
35
32
  extensions: []
36
33
  extra_rdoc_files: []
37
34
  files:
38
- - .gitignore
39
- - .travis.yml
35
+ - ".gitignore"
36
+ - ".travis.yml"
37
+ - CHANGELOG.md
40
38
  - Gemfile
41
39
  - LICENSE
42
40
  - README.md
@@ -45,41 +43,40 @@ files:
45
43
  - csv_query.gemspec
46
44
  - lib/csv_query.rb
47
45
  - lib/csv_query/command_line.rb
46
+ - lib/csv_query/database.rb
48
47
  - lib/csv_query/outputter.rb
49
48
  - lib/csv_query/query.rb
50
49
  - lib/csv_query/version.rb
50
+ - test/csv_query/database_test.rb
51
51
  - test/csv_query/outputter_test.rb
52
52
  - test/csv_query/query_test.rb
53
53
  - test/fixtures/simple.csv
54
54
  - test/test_helper.rb
55
55
  homepage: http://mentalized.net
56
56
  licenses: []
57
+ metadata: {}
57
58
  post_install_message:
58
59
  rdoc_options: []
59
60
  require_paths:
60
61
  - lib
61
62
  required_ruby_version: !ruby/object:Gem::Requirement
62
- none: false
63
63
  requirements:
64
- - - ! '>='
64
+ - - ">="
65
65
  - !ruby/object:Gem::Version
66
66
  version: '1.9'
67
67
  required_rubygems_version: !ruby/object:Gem::Requirement
68
- none: false
69
68
  requirements:
70
- - - ! '>='
69
+ - - ">="
71
70
  - !ruby/object:Gem::Version
72
71
  version: '0'
73
- segments:
74
- - 0
75
- hash: 4047582568054347755
76
72
  requirements: []
77
73
  rubyforge_project:
78
- rubygems_version: 1.8.24
74
+ rubygems_version: 2.4.5
79
75
  signing_key:
80
- specification_version: 3
76
+ specification_version: 4
81
77
  summary: Use SQL to query CSV data
82
78
  test_files:
79
+ - test/csv_query/database_test.rb
83
80
  - test/csv_query/outputter_test.rb
84
81
  - test/csv_query/query_test.rb
85
82
  - test/fixtures/simple.csv