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 +4 -4
- data/README.md +1 -1
- data/lib/jsonsql/command.rb +2 -1
- data/lib/jsonsql/importer.rb +19 -8
- data/lib/jsonsql/transformer.rb +1 -1
- data/lib/jsonsql/version.rb +1 -1
- data/spec/importer_spec.rb +21 -0
- data/spec/transformer_spec.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8903ce2ba94d38ef654e8f863739e1b332c4667d
|
4
|
+
data.tar.gz: c460087e79b014e09fe967ba8b77c8d034a2b036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 (
|
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`)
|
data/lib/jsonsql/command.rb
CHANGED
@@ -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
|
data/lib/jsonsql/importer.rb
CHANGED
@@ -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
|
-
|
55
|
+
create_or_alter_table_if_needed(row)
|
54
56
|
table.insert(row)
|
55
57
|
end
|
56
58
|
|
57
|
-
|
58
|
-
def create_table_if_needed(row)
|
59
|
+
def create_or_alter_table_if_needed(row)
|
59
60
|
unless @table_created
|
60
|
-
|
61
|
+
columns = columns_with_row(row)
|
61
62
|
database.create_table(table_name.to_sym) do
|
62
|
-
|
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
|
data/lib/jsonsql/transformer.rb
CHANGED
data/lib/jsonsql/version.rb
CHANGED
data/spec/importer_spec.rb
CHANGED
@@ -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")
|
data/spec/transformer_spec.rb
CHANGED
@@ -15,9 +15,9 @@ describe Jsonsql::Transformer do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
context "::
|
18
|
+
context "::transformer_with_string" do
|
19
19
|
it "create a transformer with block" do
|
20
|
-
transformer = Jsonsql::Transformer.
|
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.
|
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-
|
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.
|
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.
|