amalgalite 0.12.0-x86-mswin32 → 0.15.0-x86-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,8 +14,6 @@
14
14
  ** an SQLite instance. Shared libraries that intend to be loaded
15
15
  ** as extensions by SQLite should #include this file instead of
16
16
  ** sqlite3.h.
17
- **
18
- ** @(#) $Id: sqlite3ext.h,v 1.25 2008/10/12 00:27:54 shane Exp $
19
17
  */
20
18
  #ifndef _SQLITE3EXT_H_
21
19
  #define _SQLITE3EXT_H_
data/gemspec.rb CHANGED
@@ -21,6 +21,7 @@ Amalgalite::GEM_SPEC = Gem::Specification.new do |spec|
21
21
 
22
22
  # add dependencies here
23
23
  spec.add_dependency("arrayfields", "~> 4.7.0")
24
+ spec.add_dependency("fastercsv", "~> 1.5.3")
24
25
 
25
26
  spec.add_development_dependency("rake", "~> 0.8.4")
26
27
  spec.add_development_dependency("configuration", "~> 0.0.5")
data/lib/amalgalite.rb CHANGED
@@ -29,6 +29,7 @@ require 'amalgalite/column'
29
29
  require 'amalgalite/database'
30
30
  require 'amalgalite/function'
31
31
  require 'amalgalite/index'
32
+ require 'amalgalite/memory_database'
32
33
  require 'amalgalite/paths'
33
34
  require 'amalgalite/profile_tap'
34
35
  require 'amalgalite/progress_handler'
Binary file
Binary file
@@ -0,0 +1,69 @@
1
+ require 'fastercsv'
2
+ module Amalgalite
3
+ ##
4
+ # A class to deal with importing CSV data into a single table in the
5
+ # database.
6
+ #
7
+ class CSVTableImporter
8
+ def initialize( csv_path, database, table_name, options = {} )
9
+ @csv_path = File.expand_path( csv_path )
10
+ @database = database
11
+ @table_name = table_name
12
+ @table = @database.schema.tables[@table_name]
13
+ @options = options
14
+ validate
15
+ end
16
+
17
+ def run
18
+ @database.transaction do |db|
19
+ db.prepare( insert_sql ) do |stmt|
20
+ ::FasterCSV.foreach( @csv_path, @options ) do |row|
21
+ stmt.execute( row )
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ ##
28
+ # The column names of the import table in definiation order
29
+ #
30
+ def column_names
31
+ @table.columns_in_order.collect { |c| c.name }
32
+ end
33
+
34
+ ##
35
+ # The columns used for the insertion. This is either #column_names
36
+ # or the value out of @options[:headers] if that value is an Array
37
+ #
38
+ def insert_column_list
39
+ column_list = self.column_names
40
+ if Array === @options[:headers] then
41
+ column_list = @options[:headers]
42
+ end
43
+ return column_list
44
+ end
45
+
46
+ ##
47
+ # The prepared statement SQL that is used for the import
48
+ #
49
+ def insert_sql
50
+ column_sql = insert_column_list.join(",")
51
+ vars = insert_column_list.collect { |x| "?" }.join(",")
52
+ return "INSERT INTO #{@table_name}(#{column_sql}) VALUES (#{vars})"
53
+ end
54
+
55
+ def table_list
56
+ @database.schema.tables.keys
57
+ end
58
+
59
+ ##
60
+ # validate that the arguments for initialization are valid and that the #run
61
+ # method will probably execute
62
+ #
63
+ def validate
64
+ raise ArgumentError, "CSV file #{@csv_path} does not exist" unless File.exist?( @csv_path )
65
+ raise ArgumentError, "CSV file #{@csv_path} is not readable" unless File.readable?( @csv_path )
66
+ raise ArgumentError, "The table '#{@table_name} is not found in the database. The known tables are #{table_list.sort.join(", ")}" unless @table
67
+ end
68
+ end
69
+ end
@@ -10,6 +10,7 @@ require 'amalgalite/function'
10
10
  require 'amalgalite/aggregate'
11
11
  require 'amalgalite/busy_timeout'
12
12
  require 'amalgalite/progress_handler'
13
+ require 'amalgalite/csv_table_importer'
13
14
 
14
15
  module Amalgalite
15
16
  #
@@ -30,7 +31,7 @@ module Amalgalite
30
31
  #
31
32
  # Open an in-memory database:
32
33
  #
33
- # db = Amalgalite::Database.new( ":memory:" )
34
+ # db = Amalgalite::MemoryDatabase.new
34
35
  #
35
36
  class Database
36
37
 
@@ -946,6 +947,30 @@ module Amalgalite
946
947
  @api.replicate_to( to_db.api )
947
948
  return to_db
948
949
  end
950
+
951
+ ##
952
+ # call-seq:
953
+ # db.import_csv_to_table( "/some/location/data.csv", "my_table" )
954
+ # db.import_csv_to_table( "countries.csv", "countries", :col_sep => "|", :headers => %w[ name two_letter id ] )
955
+ #
956
+ #
957
+ # import_csv_to_table() takes 2 required arguments, and a hash of options. The
958
+ # first argument is the path to a CSV, the second is the table in which
959
+ # to load the data. The options has is a subset of those used by FasterCSV
960
+ #
961
+ # * :col_sep - the string placed between each field. Default is ","
962
+ # * :row_sep - the String appended to the end of each row. Default is :auto
963
+ # * :quote_char - The character used to quote fields. Default '"'
964
+ # * :headers - set to true or :first_row if there are headers in this CSV. Default is false.
965
+ # This may also be an Array. If that is the case then the
966
+ # array is used as the fields in the CSV and the fields in the
967
+ # table in which to insert. If this is set to an Array, it is
968
+ # assumed that all rows in the csv will be inserted.
969
+ #
970
+ def import_csv_to_table( csv_path, table_name, options = {} )
971
+ importer = CSVTableImporter.new( csv_path, self, table_name, options )
972
+ importer.run
973
+ end
949
974
  end
950
975
  end
951
976
 
@@ -0,0 +1,15 @@
1
+ require 'amalgalite/database'
2
+ module Amalgalite
3
+ #
4
+ # The encapsulation of a connection to an SQLite3 in-memory database.
5
+ #
6
+ # Open an in-memory database:
7
+ #
8
+ # db = Amalgalite::MemoryDatabase.new
9
+ #
10
+ class MemoryDatabase < Database
11
+ def initialize
12
+ super( ":memory:" )
13
+ end
14
+ end
15
+ end
@@ -68,8 +68,10 @@ module Amalgalite
68
68
  amalgalite/type_maps/default_map.rb
69
69
  amalgalite/function.rb
70
70
  amalgalite/progress_handler.rb
71
+ amalgalite/csv_table_importer.rb
71
72
  amalgalite/database.rb
72
73
  amalgalite/index.rb
74
+ amalgalite/memory_database.rb
73
75
  amalgalite/paths.rb
74
76
  amalgalite/table.rb
75
77
  amalgalite/view.rb
@@ -8,7 +8,7 @@ module Amalgalite
8
8
  module Version
9
9
 
10
10
  MAJOR = 0
11
- MINOR = 12
11
+ MINOR = 15
12
12
  BUILD = 0
13
13
 
14
14
  #
@@ -1,11 +1,5 @@
1
- require 'rubygems'
2
- require 'spec'
3
-
4
1
  require File.expand_path( File.join( File.dirname( __FILE__ ), "spec_helper.rb" ) )
5
2
 
6
- require 'amalgalite'
7
- require 'amalgalite/database'
8
-
9
3
  class AggregateTest1 < ::Amalgalite::Aggregate
10
4
  def initialize
11
5
  @name = 'atest1'
@@ -23,19 +17,6 @@ end
23
17
 
24
18
  describe "Aggregate SQL Functions" do
25
19
 
26
- before(:each) do
27
- @schema = IO.read( SpecInfo.test_schema_file )
28
- @iso_db_file = SpecInfo.make_iso_db
29
- @iso_db = Amalgalite::Database.new( SpecInfo.make_iso_db )
30
- end
31
-
32
- after(:each) do
33
- File.unlink SpecInfo.test_db if File.exist?( SpecInfo.test_db )
34
- @iso_db.close
35
- File.unlink @iso_db_file if File.exist?( @iso_db_file )
36
- end
37
-
38
-
39
20
  it "must have a finalize method implemented" do
40
21
  ag = ::Amalgalite::Aggregate.new
41
22
  lambda { ag.finalize }.should raise_error( NotImplementedError, /Aggregate#finalize must be implemented/ )
data/spec/blob_spec.rb CHANGED
@@ -1,11 +1,7 @@
1
- require 'rubygems'
2
- require 'spec'
3
-
4
- $: << File.expand_path(File.join(File.dirname(__FILE__),"..","lib"))
5
- require 'amalgalite'
1
+ require File.expand_path( File.join( File.dirname(__FILE__), 'spec_helper'))
6
2
 
7
3
  describe Amalgalite::Blob do
8
- DATA_FILE = File.expand_path( File.join( File.dirname(__FILE__), "iso-3166-country.txt" ) )
4
+
9
5
  before(:each) do
10
6
  @blob_db_name = File.join(File.dirname( __FILE__ ), "blob.db")
11
7
  File.unlink @blob_db_name if File.exist?( @blob_db_name )
@@ -17,6 +13,7 @@ describe Amalgalite::Blob do
17
13
  data TEXT );
18
14
  SQL
19
15
  @db.execute( @schema_sql )
16
+ @country_data_file = Amalgalite::Iso3166Database.country_data_file
20
17
  @junk_file = File.join( File.dirname(__FILE__), "test_output")
21
18
  end
22
19
 
@@ -26,14 +23,14 @@ describe Amalgalite::Blob do
26
23
  File.unlink @junk_file if File.exist?( @junk_file )
27
24
  end
28
25
 
29
- { :file => DATA_FILE,
30
- :string => IO.read( DATA_FILE ),
31
- :io => StringIO.new( IO.read( DATA_FILE ) ) }.each_pair do |style, data |
26
+ { :file => Amalgalite::Iso3166Database.country_data_file,
27
+ :string => IO.read( Amalgalite::Iso3166Database.country_data_file),
28
+ :io => StringIO.new( IO.read( Amalgalite::Iso3166Database.country_data_file) ) }.each_pair do |style, data |
32
29
  describe "inserts a blob from a #{style}" do
33
30
  before(:each) do
34
31
  column = @db.schema.tables['blobs'].columns['data']
35
32
  @db.execute("INSERT INTO blobs(name, data) VALUES ($name, $data)",
36
- { "$name" => DATA_FILE,
33
+ { "$name" => @country_data_file,
37
34
  "$data" => Amalgalite::Blob.new( style => data,
38
35
  :column => column ) } )
39
36
  @db.execute("VACUUM")
@@ -47,26 +44,26 @@ describe Amalgalite::Blob do
47
44
  it "and retrieves the data as a single value" do
48
45
  all_rows = @db.execute("SELECT name,data FROM blobs")
49
46
  all_rows.size.should eql(1)
50
- all_rows.first['name'].should eql(DATA_FILE)
47
+ all_rows.first['name'].should eql(@country_data_file)
51
48
  all_rows.first['data'].should_not be_incremental
52
- all_rows.first['data'].to_string_io.string.should eql(IO.read( DATA_FILE ))
49
+ all_rows.first['data'].to_string_io.string.should eql(IO.read( @country_data_file ))
53
50
  end
54
51
 
55
52
  it "and retrieves the data using incremental IO" do
56
53
  all_rows = @db.execute("SELECT * FROM blobs")
57
54
  all_rows.size.should eql(1)
58
- all_rows.first['name'].should eql(DATA_FILE)
55
+ all_rows.first['name'].should eql(@country_data_file)
59
56
  all_rows.first['data'].should be_incremental
60
- all_rows.first['data'].to_string_io.string.should eql(IO.read( DATA_FILE ))
57
+ all_rows.first['data'].to_string_io.string.should eql(IO.read( @country_data_file ))
61
58
  end
62
59
 
63
60
  it "writes the data to a file " do
64
61
  all_rows = @db.execute("SELECT * FROM blobs")
65
62
  all_rows.size.should eql(1)
66
- all_rows.first['name'].should eql(DATA_FILE)
63
+ all_rows.first['name'].should eql(@country_data_file)
67
64
  all_rows.first['data'].should be_incremental
68
65
  all_rows.first['data'].write_to_file( @junk_file )
69
- IO.read( @junk_file).should eql(IO.read( DATA_FILE ))
66
+ IO.read( @junk_file).should eql(IO.read( @country_data_file ))
70
67
  end
71
68
  end
72
69
  end
data/spec/busy_handler.rb CHANGED
@@ -1,11 +1,5 @@
1
- require 'rubygems'
2
- require 'spec'
3
-
4
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper.rb' ) )
5
2
 
6
- require 'amalgalite'
7
- require 'amalgalite/database'
8
-
9
3
  class BusyHandlerTest < Amalgalite::BusyHandler
10
4
  attr_accessor :call_count
11
5
  def initialize( max = 5 )
@@ -24,15 +18,13 @@ end
24
18
 
25
19
  describe "Busy Handlers" do
26
20
  before(:each) do
27
- @db_name = SpecInfo.make_iso_db
28
- @read_db = Amalgalite::Database.new( @db_name )
29
- @write_db = Amalgalite::Database.new( @db_name )
21
+ @read_db = Amalgalite::Database.new( @iso_db_path )
22
+ @write_db = Amalgalite::Database.new( @iso_db_path )
30
23
  end
31
24
 
32
25
  after(:each) do
33
26
  @write_db.close
34
27
  @read_db.close
35
- File.unlink @db_name if File.exist?( @db_name )
36
28
  end
37
29
 
38
30
  it "raises NotImplemented if #call is not overwritten" do
@@ -0,0 +1,242 @@
1
+ Afghanistan|AF|4
2
+ Albania|AL|8
3
+ Algeria|DZ|12
4
+ American Samoa|AS|16
5
+ Andorra|AD|20
6
+ Angola|AO|24
7
+ Anguilla|AI|660
8
+ Antarctica|AQ|10
9
+ Antigua and Barbuda|AG|28
10
+ Argentina|AR|32
11
+ Armenia|AM|51
12
+ Aruba|AW|533
13
+ Australia|AU|36
14
+ Austria|AT|40
15
+ Azerbaijan|AZ|31
16
+ Bahamas|BS|44
17
+ Bahrain|BH|48
18
+ Bangladesh|BD|50
19
+ Barbados|BB|52
20
+ Belarus|BY|112
21
+ Belgium|BE|56
22
+ Belize|BZ|84
23
+ Benin|BJ|204
24
+ Bermuda|BM|60
25
+ Bhutan|BT|64
26
+ Bolivia|BO|68
27
+ Bosnia and Herzegowina|BA|70
28
+ Botswana|BW|72
29
+ Bouvet Island|BV|74
30
+ Brazil|BR|76
31
+ British Indian Ocean Territory|IO|86
32
+ Brunei Darussalam|BN|96
33
+ Bulgaria|BG|100
34
+ Burkina Faso|BF|854
35
+ Burundi|BI|108
36
+ Cambodia|KH|116
37
+ Cameroon|CM|120
38
+ Cape Verde|CV|132
39
+ Cayman Islands|KY|136
40
+ Central African Republic|CF|140
41
+ Chad|TD|148
42
+ Chile|CL|152
43
+ China|CN|156
44
+ Christmas Island|CX|162
45
+ Cocos (Keeling) Islands|CC|166
46
+ Colombia|CO|170
47
+ Comoros|KM|174
48
+ Congo|CG|178
49
+ Congo, The Democratic Republic of the|CD|180
50
+ Cook Islands|CK|184
51
+ Costa Rica|CR|188
52
+ Cote D'Ivoire|CI|384
53
+ Croatia (local name: Hrvatska)|HR|191
54
+ Cuba|CU|192
55
+ Cyprus|CY|196
56
+ Czech Republic|CZ|203
57
+ Denmark|DK|208
58
+ Djibouti|DJ|262
59
+ Dominica|DM|212
60
+ Dominican Republic|DO|214
61
+ East Timor|TP|626
62
+ Ecuador|EC|218
63
+ Egypt|EG|818
64
+ El Salvador|SV|222
65
+ Equatorial Guinea|GQ|226
66
+ Eritrea|ER|232
67
+ Estonia|EE|233
68
+ Ethiopia|ET|231
69
+ Falkland Islands (Malvinas)|FK|238
70
+ Faroe Islands|FO|234
71
+ Fiji|FJ|242
72
+ Finland|FI|246
73
+ France|FR|250
74
+ France, Metropolitan|FX|249
75
+ French Guiana|GF|254
76
+ French Polynesia|PF|258
77
+ French Southern Territories|TF|260
78
+ Gabon|GA|266
79
+ Gambia|GM|270
80
+ Georgia|GE|268
81
+ Germany|DE|276
82
+ Ghana|GH|288
83
+ Gibraltar|GI|292
84
+ Greece|GR|300
85
+ Greenland|GL|304
86
+ Grenada|GD|308
87
+ Guadeloupe|GP|312
88
+ Guam|GU|316
89
+ Guatemala|GT|320
90
+ Guinea|GN|324
91
+ Guinea-Bissau|GW|624
92
+ Guyana|GY|328
93
+ Haiti|HT|332
94
+ Heard and McDonald Islands|HM|334
95
+ Holy See (Vatican City State)|VA|336
96
+ Honduras|HN|340
97
+ Hong Kong|HK|344
98
+ Hungary|HU|348
99
+ Iceland|IS|352
100
+ India|IN|356
101
+ Indonesia|ID|360
102
+ Iran (Islamic Republic of)|IR|364
103
+ Iraq|IQ|368
104
+ Ireland|IE|372
105
+ Israel|IL|376
106
+ Italy|IT|380
107
+ Jamaica|JM|388
108
+ Japan|JP|392
109
+ Jordan|JO|400
110
+ Kazakhstan|KZ|398
111
+ Kenya|KE|404
112
+ Kiribati|KI|296
113
+ Korea, Democratic People's Republic of|KP|408
114
+ Korea, Republic of|KR|410
115
+ Kuwait|KW|414
116
+ Kyrgyzstan|KG|417
117
+ Lao People's Democratic Republic|LA|418
118
+ Latvia|LV|428
119
+ Lebanon|LB|422
120
+ Lesotho|LS|426
121
+ Liberia|LR|430
122
+ Libyan Arab Jamahiriya|LY|434
123
+ Liechtenstein|LI|438
124
+ Lithuania|LT|440
125
+ Luxembourg|LU|442
126
+ Macau|MO|446
127
+ Macedonia, the Former Yugoslav Republic of|MK|807
128
+ Madagascar|MG|450
129
+ Malawi|MW|454
130
+ Malaysia|MY|458
131
+ Maldives|MV|462
132
+ Mali|ML|466
133
+ Malta|MT|470
134
+ Marshall Islands|MH|584
135
+ Martinique|MQ|474
136
+ Mauritania|MR|478
137
+ Mauritius|MU|480
138
+ Mayotte|YT|175
139
+ Micronesia, Federated States of|FM|583
140
+ Moldova, Republic of|MD|498
141
+ Monaco|MC|492
142
+ Mongolia|MN|496
143
+ Montserrat|MS|500
144
+ Morocco|MA|504
145
+ Mozambique|MZ|508
146
+ Myanmar|MM|104
147
+ Namibia|NA|516
148
+ Nauru|NR|520
149
+ Nepal|NP|524
150
+ Netherlands|NL|528
151
+ Netherlands Antilles|AN|530
152
+ New Caledonia|NC|540
153
+ New Zealand|NZ|554
154
+ Nicaragua|NI|558
155
+ Niger|NE|562
156
+ Nigeria|NG|566
157
+ Niue|NU|570
158
+ Norfolk Island|NF|574
159
+ Northern Mariana Islands|MP|580
160
+ Norway|NO|578
161
+ Oman|OM|512
162
+ Pakistan|PK|586
163
+ Palau|PW|585
164
+ Palestinian Territory, Occupied|PS|275
165
+ Panama|PA|591
166
+ Papua New Guinea|PG|598
167
+ Paraguay|PY|600
168
+ Peru|PE|604
169
+ Philippines|PH|608
170
+ Pitcairn|PN|612
171
+ Poland|PL|616
172
+ Portugal|PT|620
173
+ Puerto Rico|PR|630
174
+ Qatar|QA|634
175
+ Reunion|RE|638
176
+ Romania|RO|642
177
+ Russian Federation|RU|643
178
+ Rwanda|RW|646
179
+ Saint Kitts and Nevis|KN|659
180
+ Saint Lucia|LC|662
181
+ Saint Vincent and the Grenadines|VC|670
182
+ Samoa|WS|882
183
+ San Marino|SM|674
184
+ Sao Tome and Principe|ST|678
185
+ Saudi Arabia|SA|682
186
+ Senegal|SN|686
187
+ Serbia and Montenegro|CS|891
188
+ Seychelles|SC|690
189
+ Sierra Leone|SL|694
190
+ Singapore|SG|702
191
+ Slovakia (Slovak Republic)|SK|703
192
+ Slovenia|SI|705
193
+ Solomon Islands|SB|90
194
+ Somalia|SO|706
195
+ South Africa|ZA|710
196
+ South Georgia and the South Sandwich Islands|GS|239
197
+ Spain|ES|724
198
+ Sri Lanka|LK|144
199
+ St. Helena|SH|654
200
+ St. Pierre and Miquelon|PM|666
201
+ Sudan|SD|736
202
+ Suriname|SR|740
203
+ Svalbard and Jan Mayen Islands|SJ|744
204
+ Swaziland|SZ|748
205
+ Sweden|SE|752
206
+ Switzerland|CH|756
207
+ Syrian Arab Republic|SY|760
208
+ Taiwan, Province of China|TW|158
209
+ Tajikistan|TJ|762
210
+ Tanzania, United Republic of|TZ|834
211
+ Thailand|TH|764
212
+ Timor-Leste|TL|626
213
+ Togo|TG|768
214
+ Tokelau|TK|772
215
+ Tonga|TO|776
216
+ Trinidad and Tobago|TT|780
217
+ Tunisia|TN|788
218
+ Turkey|TR|792
219
+ Turkmenistan|TM|795
220
+ Turks and Caicos Islands|TC|796
221
+ Tuvalu|TV|798
222
+ Uganda|UG|800
223
+ Ukraine|UA|804
224
+ United Arab Emirates|AE|784
225
+ United States Minor Outlying Islands|UM|581
226
+ Uruguay|UY|858
227
+ Uzbekistan|UZ|860
228
+ Vanuatu|VU|548
229
+ Venezuela|VE|862
230
+ Viet Nam|VN|704
231
+ Virgin Islands (British)|VG|92
232
+ Virgin Islands (U.S.)|VI|850
233
+ Wallis and Futuna Islands|WF|876
234
+ Western Sahara|EH|732
235
+ Yemen|YE|887
236
+ Yugoslavia|YU|891
237
+ Zambia|ZM|894
238
+ Zimbabwe|ZW|716
239
+ Canada|CA|124
240
+ Mexico|MX|484
241
+ United Kingdom|GB|826
242
+ United States|US|840