scraperwiki 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/scraperwiki.rb +7 -3
- data/lib/scraperwiki/sqlite_save_info.rb +15 -9
- metadata +1 -1
data/lib/scraperwiki.rb
CHANGED
@@ -79,18 +79,22 @@ module ScraperWiki
|
|
79
79
|
SQLiteMagic._do_save_sqlite(unique_keys, rjdata, table_name)
|
80
80
|
end
|
81
81
|
|
82
|
+
def ScraperWiki.close_sqlite()
|
83
|
+
SQLiteMagic.close
|
84
|
+
end
|
85
|
+
|
82
86
|
# Internal function to check a row of data, convert to right format
|
83
87
|
def ScraperWiki._convdata(unique_keys, scraper_data)
|
84
88
|
if unique_keys
|
85
89
|
for key in unique_keys
|
86
90
|
if !key.kind_of?(String) and !key.kind_of?(Symbol)
|
87
|
-
|
91
|
+
raise 'unique_keys must each be a string or a symbol, this one is not: ' + key
|
88
92
|
end
|
89
93
|
if !scraper_data.include?(key) and !scraper_data.include?(key.to_sym)
|
90
|
-
|
94
|
+
raise 'unique_keys must be a subset of data, this one is not: ' + key
|
91
95
|
end
|
92
96
|
if scraper_data[key] == nil and scraper_data[key.to_sym] == nil
|
93
|
-
|
97
|
+
raise 'unique_key value should not be nil, this one is nil: ' + key
|
94
98
|
end
|
95
99
|
end
|
96
100
|
end
|
@@ -11,10 +11,14 @@ require 'set'
|
|
11
11
|
require 'sqlite3'
|
12
12
|
|
13
13
|
module SQLiteMagic
|
14
|
-
@db =
|
14
|
+
@db = nil
|
15
15
|
@sqlitesaveinfo = {}
|
16
16
|
|
17
17
|
def SQLiteMagic._do_save_sqlite(unique_keys, data, swdatatblname)
|
18
|
+
if @db.nil?
|
19
|
+
@db = SQLite3::Database.new("scraperwiki.sqlite")
|
20
|
+
end
|
21
|
+
|
18
22
|
res = { }
|
19
23
|
if data.class == Hash
|
20
24
|
data = [data]
|
@@ -58,9 +62,6 @@ module SQLiteMagic
|
|
58
62
|
end
|
59
63
|
|
60
64
|
lres = ssinfo.insertdata(ldata)
|
61
|
-
if lres.include?('error')
|
62
|
-
return lres
|
63
|
-
end
|
64
65
|
nrecords += 1
|
65
66
|
end
|
66
67
|
|
@@ -69,13 +70,19 @@ module SQLiteMagic
|
|
69
70
|
return res
|
70
71
|
end
|
71
72
|
|
73
|
+
def SQLiteMagic.close()
|
74
|
+
@db.close
|
75
|
+
@db = nil
|
76
|
+
@sqlitesaveinfo = {}
|
77
|
+
end
|
78
|
+
|
72
79
|
|
73
80
|
class SqliteSaveInfo
|
74
81
|
def initialize(swdatatblname, db)
|
75
82
|
@swdatatblname = swdatatblname
|
76
83
|
@swdatakeys = [ ]
|
77
84
|
@swdatatypes = [ ]
|
78
|
-
@sqdatatemplate = ""
|
85
|
+
@sqdatatemplate = ""
|
79
86
|
@db = db
|
80
87
|
end
|
81
88
|
|
@@ -87,8 +94,6 @@ module SQLiteMagic
|
|
87
94
|
|
88
95
|
tblinfo = @db.execute("PRAGMA main.table_info(`%s`)" % @swdatatblname)
|
89
96
|
# puts "tblinfo="+ tblinfo.to_s
|
90
|
-
# there's a bug: PRAGMA main.table_info(swdata) returns the schema for otherdatabase.swdata
|
91
|
-
# following an attach otherdatabase where otherdatabase has a swdata and main does not
|
92
97
|
|
93
98
|
@swdatakeys = tblinfo.map { |a| a[1] }
|
94
99
|
@swdatatypes = tblinfo.map { |a| a[2] }
|
@@ -194,8 +199,9 @@ module SQLiteMagic
|
|
194
199
|
end
|
195
200
|
|
196
201
|
def insertdata(data)
|
197
|
-
values = @swdatakeys.map { |k| data[k] }
|
198
|
-
|
202
|
+
values = @swdatakeys.map { |k| data[k] }
|
203
|
+
res = @db.query(@sqdatatemplate, values)
|
204
|
+
res.close
|
199
205
|
end
|
200
206
|
end
|
201
207
|
|