jsonsql 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 96c7bbaa0d3e9df043848e47c4ba1d77d8ebba76
4
- data.tar.gz: 019089ab9c083a07822a0238d5fd196fa6867c9e
3
+ metadata.gz: 8903ce2ba94d38ef654e8f863739e1b332c4667d
4
+ data.tar.gz: c460087e79b014e09fe967ba8b77c8d034a2b036
5
5
  SHA512:
6
- metadata.gz: 5be271218b893fa326ba37948e739dad396e24da1a481b41926de5332de9aaf1ce9195c4ec7e893f9f0fe7ef18d74d6ca5f292f4d5a2b07238741c2a2e97f905
7
- data.tar.gz: 784fc08a97341ffdc1ab189613aea32c1e8b69119fa195df4a623e10e42e01c2f60c44dfae64cb465143d43ec7d2763b91566b633e6698855d8de4a5394ca281
6
+ metadata.gz: 02f61e1cb22c045b35f4690750c148ea64e8319f3be10d011351129e04523c265bff4192c4a489d9c5064be8e89d1c9bf9da2ab650d36693398ff72ac5492b28
7
+ data.tar.gz: e4ec9071902be1306d62351e5d654b27e7859520e1cd17848e3dd3168bd4a5d8796f9525c4d64c013128abb8137880e3b2a696e8518e4ae78e382f7214ef436b
data/README.md CHANGED
@@ -79,7 +79,7 @@ If you omit the ``--save-to`` option, the database will be discarded after the c
79
79
 
80
80
  ## Contributing
81
81
 
82
- 1. Fork it ( http://github.com/<my-github-username>/jsonsql/fork )
82
+ 1. Fork it ( https://github.com/siuying/jsonsql/fork )
83
83
  2. Create your feature branch (`git checkout -b my-new-feature`)
84
84
  3. Commit your changes (`git commit -am 'Add some feature'`)
85
85
  4. Push to the branch (`git push origin my-new-feature`)
@@ -23,6 +23,7 @@ module Jsonsql
23
23
  @jsons << argument
24
24
  end
25
25
 
26
+ @verbose = argv.flag?('verbose', false)
26
27
  @console = argv.flag?('console', true)
27
28
  @filename = argv.option('save-to') || ':memory:'
28
29
  @table_name = argv.option('table-name') || 'table'
@@ -38,7 +39,7 @@ module Jsonsql
38
39
 
39
40
  def run
40
41
  sequel = Sequel.sqlite(@filename)
41
- importer = Importer.new(database: sequel, table_name: @table_name)
42
+ importer = Importer.new(database: sequel, table_name: @table_name, verbose: @verbose)
42
43
  importer.import @jsons
43
44
 
44
45
  if @console
@@ -1,5 +1,6 @@
1
1
  require 'sequel'
2
2
  require 'json'
3
+ require 'logger'
3
4
 
4
5
  module Jsonsql
5
6
  class Importer
@@ -7,11 +8,12 @@ module Jsonsql
7
8
 
8
9
  attr_reader :database, :table_name, :transformer
9
10
 
10
- def initialize(database: Sequel.sqlite, table_name: "table", transformer: nil)
11
+ def initialize(database: Sequel.sqlite, table_name: "table", transformer: nil, verbose: false)
11
12
  @table_name = table_name
12
13
  @database = database
13
14
  @transformer = transformer
14
15
  @table_created = false
16
+ @database.loggers << Logger.new($stdout) if verbose
15
17
  end
16
18
 
17
19
  def table
@@ -50,20 +52,31 @@ module Jsonsql
50
52
  # filter to only supported fields
51
53
  row = row.select { |k, v| Jsonsql::Importer.class_supported?(v.class) }
52
54
 
53
- create_table_if_needed(row)
55
+ create_or_alter_table_if_needed(row)
54
56
  table.insert(row)
55
57
  end
56
58
 
57
- # create a table using row data, if it has not been created
58
- def create_table_if_needed(row)
59
+ def create_or_alter_table_if_needed(row)
59
60
  unless @table_created
60
- create_columns = columns_with_row(row)
61
+ columns = columns_with_row(row)
61
62
  database.create_table(table_name.to_sym) do
62
- create_columns.each do |name, clazz|
63
+ columns.each do |name, clazz|
63
64
  column name.to_sym, clazz
64
65
  end
65
66
  end
67
+ @columns = columns
66
68
  @table_created = true
69
+ else
70
+ updated_columns = columns_with_row(row)
71
+ added_keys = updated_columns.keys - @columns.keys
72
+ if added_keys.count > 0
73
+ database.alter_table(table_name.to_sym) do
74
+ added_keys.each do |key|
75
+ add_column key.to_sym, updated_columns[key], :default => nil
76
+ end
77
+ end
78
+ @columns = updated_columns
79
+ end
67
80
  end
68
81
  end
69
82
 
@@ -74,8 +87,6 @@ module Jsonsql
74
87
  clazz = value.class
75
88
  if self.class.class_supported?(clazz)
76
89
  columns[name] = value.class
77
- else
78
- $stderr.puts "Class #{clazz} not supported, skipped"
79
90
  end
80
91
  end
81
92
  columns
@@ -8,7 +8,7 @@ module Jsonsql
8
8
  self.instance_exec(row, &@transformation)
9
9
  end
10
10
 
11
- def self.transformer_with_block(string)
11
+ def self.transformer_with_string(string)
12
12
  block = eval(string)
13
13
  Transformer.new(&block)
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module Jsonsql
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -6,6 +6,27 @@ require 'pry'
6
6
  describe Jsonsql::Importer do
7
7
  subject { Jsonsql::Importer.new }
8
8
 
9
+ context "using transformer" do
10
+ it "import JSON with transformer" do
11
+ transformer = Jsonsql::Transformer.transformer_with_string("Proc.new { |row| row['created_at'] = Time.parse(row['created_at']) if row['created_at']; row } ")
12
+ expect(transformer).to_not be_nil
13
+
14
+ subject = Jsonsql::Importer.new(transformer: transformer)
15
+ subject.import_jsonfile("samples/mtr/437582112921640960.json")
16
+
17
+ expect(subject.table).to_not be_nil
18
+ expect(subject.table.count).to eq(1)
19
+ expected_row = {
20
+ :id => 437582112921640960,
21
+ :text => "@kaede19940908 圖片係港鐵官方的",
22
+ :created_at => Time.parse("2014-02-23 21:38:00 +0800"),
23
+ :lang => "zh",
24
+ :reply_to => "kaede19940908"
25
+ }
26
+ expect(subject.table.where(:id => 437582112921640960).first).to eq(expected_row)
27
+ end
28
+ end
29
+
9
30
  context "#import_jsonfile" do
10
31
  it "import one json Hash file to db" do
11
32
  subject.import_jsonfile("samples/mtr/437582112921640960.json")
@@ -15,9 +15,9 @@ describe Jsonsql::Transformer do
15
15
  end
16
16
  end
17
17
 
18
- context "::transformer_with_block" do
18
+ context "::transformer_with_string" do
19
19
  it "create a transformer with block" do
20
- transformer = Jsonsql::Transformer.transformer_with_block "
20
+ transformer = Jsonsql::Transformer.transformer_with_string "
21
21
  Proc.new { |row| row[:name] = row[:name].downcase if row[:name]; row }
22
22
  "
23
23
  data = transformer.transform({:name => "Peter", :id => 1})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonsql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francis Chong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-04 00:00:00.000000000 Z
11
+ date: 2014-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  requirements: []
171
171
  rubyforge_project:
172
- rubygems_version: 2.2.2
172
+ rubygems_version: 2.2.0
173
173
  signing_key:
174
174
  specification_version: 4
175
175
  summary: Execute SQL against set of JSON files.