sqlite3-ruby 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sqlite3-ruby might be problematic. Click here for more details.

data/doc/faq/faq.html CHANGED
@@ -62,25 +62,25 @@
62
62
  <ul>
63
63
  <li>How do I do a database query?
64
64
  <ul>
65
- <li><a href='#3228052'>I just want an array of the rows&#8230;</a></li>
66
- <li><a href='#3227912'>I&#8217;d like to use a block to iterate through the rows&#8230;</a></li>
67
- <li><a href='#3227772'>I need to get the column names as well as the rows&#8230;</a></li>
68
- <li><a href='#3227652'>I just want the first row of the result set&#8230;</a></li>
69
- <li><a href='#3227532'>I just want the first value of the first row of the result set&#8230;</a></li>
65
+ <li><a href='#37380'>I just want an array of the rows&#8230;</a></li>
66
+ <li><a href='#36440'>I&#8217;d like to use a block to iterate through the rows&#8230;</a></li>
67
+ <li><a href='#32250'>I need to get the column names as well as the rows&#8230;</a></li>
68
+ <li><a href='#31680'>I just want the first row of the result set&#8230;</a></li>
69
+ <li><a href='#31240'>I just want the first value of the first row of the result set&#8230;</a></li>
70
70
  </ul>
71
71
  </li>
72
- <li><a href='#3227292'>How do I prepare a statement for repeated execution?</a></li>
73
- <li><a href='#3227192'>How do I use placeholders in an <span class="caps">SQL</span> statement?</a></li>
74
- <li><a href='#3227072'>How do I discover metadata about a query?</a></li>
75
- <li><a href='#3226852'>I&#8217;d like the rows to be indexible by column name.</a></li>
76
- <li><a href='#3226702'>I&#8217;d like the values from a query to be the correct types, instead of String.</a></li>
77
- <li><a href='#3226552'>How do insert binary data into the database?</a></li>
78
- <li><a href='#3226372'>How do I do a <span class="caps">DDL</span> (insert, update, delete) statement?</a></li>
79
- <li><a href='#3226232'>How do I execute multiple statements in a single string?</a></li>
80
- <li><a href='#3226102'>How do I begin/end a transaction?</a></li>
72
+ <li><a href='#30080'>How do I prepare a statement for repeated execution?</a></li>
73
+ <li><a href='#27350'>How do I use placeholders in an <span class="caps">SQL</span> statement?</a></li>
74
+ <li><a href='#26510'>How do I discover metadata about a query?</a></li>
75
+ <li><a href='#26320'>I&#8217;d like the rows to be indexible by column name.</a></li>
76
+ <li><a href='#26050'>I&#8217;d like the values from a query to be the correct types, instead of String.</a></li>
77
+ <li><a href='#25850'>How do insert binary data into the database?</a></li>
78
+ <li><a href='#25740'>How do I do a <span class="caps">DDL</span> (insert, update, delete) statement?</a></li>
79
+ <li><a href='#23610'>How do I execute multiple statements in a single string?</a></li>
80
+ <li><a href='#22110'>How do I begin/end a transaction?</a></li>
81
81
  </ul>
82
82
  </div>
83
- <a name='3228052'></a>
83
+ <a name='37380'></a>
84
84
  <div class='faq-title'>How do I do a database query? I just want an array of the rows&#8230;</div>
85
85
  <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method. If you don&#8217;t give it a block, it will return an array of all the rows:</p>
86
86
 
@@ -91,7 +91,7 @@
91
91
  db = SQLite3::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
92
92
  rows = db.execute( "select * from test" )
93
93
  </pre></div>
94
- <a name='3227912'></a>
94
+ <a name='36440'></a>
95
95
  <div class='faq-title'>How do I do a database query? I&#8217;d like to use a block to iterate through the rows&#8230;</div>
96
96
  <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method. If you give it a block, each row of the result will be yielded to the block:</p>
97
97
 
@@ -104,7 +104,7 @@
104
104
  ...
105
105
  end
106
106
  </pre></div>
107
- <a name='3227772'></a>
107
+ <a name='32250'></a>
108
108
  <div class='faq-title'>How do I do a database query? I need to get the column names as well as the rows&#8230;</div>
109
109
  <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a> method. This works just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>; if you don&#8217;t give it a block, it returns an array of rows; otherwise, it will yield each row to the block. <em>However</em>, the first row returned is always an array of the column names from the query:</p>
110
110
 
@@ -126,7 +126,7 @@
126
126
  end
127
127
  end
128
128
  </pre></div>
129
- <a name='3227652'></a>
129
+ <a name='31680'></a>
130
130
  <div class='faq-title'>How do I do a database query? I just want the first row of the result set&#8230;</div>
131
131
  <div class='faq-answer'><p>Easy. Just call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#get_first_row</a>:</p>
132
132
 
@@ -136,7 +136,7 @@
136
136
  </pre>
137
137
 
138
138
  <p>This also supports bind variables, just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> and friends.</p></div>
139
- <a name='3227532'></a>
139
+ <a name='31240'></a>
140
140
  <div class='faq-title'>How do I do a database query? I just want the first value of the first row of the result set&#8230;</div>
141
141
  <div class='faq-answer'><p>Also easy. Just call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#get_first_value</a>:</p>
142
142
 
@@ -146,7 +146,7 @@
146
146
  </pre>
147
147
 
148
148
  <p>This also supports bind variables, just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> and friends.</p></div>
149
- <a name='3227292'></a>
149
+ <a name='30080'></a>
150
150
  <div class='faq-title'>How do I prepare a statement for repeated execution?</div>
151
151
  <div class='faq-answer'><p>If the same statement is going to be executed repeatedly, you can speed things up a bit by <em>preparing</em> the statement. You do this via the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#prepare</a> method. It returns a <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a> object, and you can then invoke #execute on that to get the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/ResultSet.html'>ResultSet</a>:</p>
152
152
 
@@ -174,7 +174,7 @@
174
174
  </pre>
175
175
 
176
176
  <p>This is made more useful by the ability to bind variables to placeholders via the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_param</a> and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_params</a> methods. (See the next <span class="caps">FAQ</span> for details.)</p></div>
177
- <a name='3227192'></a>
177
+ <a name='27350'></a>
178
178
  <div class='faq-title'>How do I use placeholders in an <span class="caps">SQL</span> statement?</div>
179
179
  <div class='faq-answer'><p>Placeholders in an <span class="caps">SQL</span> statement take any of the following formats:</p>
180
180
 
@@ -249,7 +249,7 @@
249
249
 
250
250
  stmt.bind_params( "value", "name" =&gt; "bob" )
251
251
  </pre></div>
252
- <a name='3227072'></a>
252
+ <a name='26510'></a>
253
253
  <div class='faq-title'>How do I discover metadata about a query?</div>
254
254
  <div class='faq-answer'><p>If you ever want to know the names or types of the columns in a result set, you can do it in several ways.</p>
255
255
 
@@ -282,7 +282,7 @@
282
282
  p stmt.columns
283
283
  p stmt.types
284
284
  </pre></div>
285
- <a name='3226852'></a>
285
+ <a name='26320'></a>
286
286
  <div class='faq-title'>I&#8217;d like the rows to be indexible by column name.</div>
287
287
  <div class='faq-answer'><p>By default, each row from a query is returned as an Array of values. This means that you can only obtain values by their index. Sometimes, however, you would like to obtain values by their column name.</p>
288
288
 
@@ -310,7 +310,7 @@
310
310
  p row[1] == row['column2']
311
311
  end
312
312
  </pre></div>
313
- <a name='3226702'></a>
313
+ <a name='26050'></a>
314
314
  <div class='faq-title'>I&#8217;d like the values from a query to be the correct types, instead of String.</div>
315
315
  <div class='faq-answer'><p>You can turn on &#8220;type translation&#8221; by setting <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#type_translation</a> to true:</p>
316
316
 
@@ -348,7 +348,7 @@
348
348
  obj = db.get_first_value( "select thing from objects where name='bob'" )
349
349
  p obj == h
350
350
  </pre></div>
351
- <a name='3226552'></a>
351
+ <a name='25850'></a>
352
352
  <div class='faq-title'>How do insert binary data into the database?</div>
353
353
  <div class='faq-answer'><p>Use blobs. Blobs are new features of SQLite3. You have to use bind variables to make it work:</p>
354
354
 
@@ -360,7 +360,7 @@
360
360
  </pre>
361
361
 
362
362
  <p>The blob values must be indicated explicitly by binding each parameter to a value of type SQLite3::Blob.</p></div>
363
- <a name='3226372'></a>
363
+ <a name='25740'></a>
364
364
  <div class='faq-title'>How do I do a <span class="caps">DDL</span> (insert, update, delete) statement?</div>
365
365
  <div class='faq-answer'><p>You can actually do inserts, updates, and deletes in exactly the same way as selects, but in general the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method will be most convenient:</p>
366
366
 
@@ -368,7 +368,7 @@
368
368
  <pre>
369
369
  db.execute( "insert into table values ( ?, ? )", *bind_vars )
370
370
  </pre></div>
371
- <a name='3226232'></a>
371
+ <a name='23610'></a>
372
372
  <div class='faq-title'>How do I execute multiple statements in a single string?</div>
373
373
  <div class='faq-answer'><p>The standard query methods (<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a>, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a>, and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#execute</a>) will only execute the first statement in the string that is given to them. Thus, if you have a string with multiple <span class="caps">SQL</span> statements, each separated by a string, you can&#8217;t use those methods to execute them all at once.</p>
374
374
 
@@ -392,7 +392,7 @@
392
392
  </pre>
393
393
 
394
394
  <p>Unlike the other query methods, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute_batch</a> accepts no block. It will also only ever return <ins>nil</ins>. Thus, it is really only suitable for batch processing of <span class="caps">DDL</span> statements.</p></div>
395
- <a name='3226102'></a>
395
+ <a name='22110'></a>
396
396
  <div class='faq-title'>How do I begin/end a transaction?</div>
397
397
  <div class='faq-answer'><p>Use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#transaction</a> to start a transaction. If you give it a block, the block will be automatically committed at the end of the block, unless an exception was raised, in which case the transaction will be rolled back. (Never explicitly call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#commit</a> or <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#rollback</a> inside of a transaction block&#8212;you&#8217;ll get errors when the block terminates!)</p>
398
398
 
@@ -246,9 +246,56 @@ module SQLite3
246
246
  end
247
247
 
248
248
  def table_info( table, &block ) # :yields: row
249
- get_query_pragma "table_info", table, &block
249
+ columns, *rows = execute2("PRAGMA table_info(#{table})")
250
+
251
+ needs_tweak_default = version_compare(driver.libversion, "3.3.7") > 0
252
+
253
+ result = [] unless block_given?
254
+ rows.each do |row|
255
+ new_row = {}
256
+ columns.each_with_index do |name, index|
257
+ new_row[name] = row[index]
258
+ end
259
+
260
+ tweak_default(new_row) if needs_tweak_default
261
+
262
+ if block_given?
263
+ yield new_row
264
+ else
265
+ result << new_row
266
+ end
267
+ end
268
+
269
+ result
250
270
  end
251
-
271
+
272
+ private
273
+
274
+ # Compares two version strings
275
+ def version_compare(v1, v2)
276
+ v1 = v1.split(".").map { |i| i.to_i }
277
+ v2 = v2.split(".").map { |i| i.to_i }
278
+ parts = [v1.length, v2.length].max
279
+ v1.push 0 while v1.length < parts
280
+ v2.push 0 while v2.length < parts
281
+ v1.zip(v2).each do |a,b|
282
+ return -1 if a < b
283
+ return 1 if a > b
284
+ end
285
+ return 0
286
+ end
287
+
288
+ # Since SQLite 3.3.8, the table_info pragma has returned the default
289
+ # value of the row as a quoted SQL value. This method essentially
290
+ # unquotes those values.
291
+ def tweak_default(hash)
292
+ case hash["dflt_value"]
293
+ when /^null$/i then
294
+ hash["dflt_value"] = nil
295
+ when /^'(.*)'$/
296
+ hash["dflt_value"] = $1.gsub(/''/, "'")
297
+ end
298
+ end
252
299
  end
253
300
 
254
301
  end
@@ -36,9 +36,10 @@ module SQLite3
36
36
 
37
37
  MAJOR = 1
38
38
  MINOR = 2
39
- TINY = 0
39
+ TINY = 1
40
40
 
41
41
  STRING = [ MAJOR, MINOR, TINY ].join( "." )
42
+ #:beta-tag:
42
43
 
43
44
  end
44
45
 
@@ -82,6 +82,17 @@ module Integration
82
82
  assert_nothing_raised { @db.table_info("foo") }
83
83
  end
84
84
 
85
+ define_method( "test_table_info_with_defaults_for_version_3_3_8_and_higher" ) do
86
+ @db.transaction do
87
+ @db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
88
+ data = @db.table_info( "defaults_test" )
89
+ assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => "0", "cid" => "0", "pk" => "0"},
90
+ data[0])
91
+ assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => "0", "cid" => "1", "pk" => "0"},
92
+ data[1])
93
+ end
94
+ end
95
+
85
96
  define_method( "test_complete_fail" ) do
86
97
  assert !@db.complete?( "select * from foo" )
87
98
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: sqlite3-ruby
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.0
7
- date: 2007-01-13 00:00:00 -07:00
6
+ version: 1.2.1
7
+ date: 2007-02-03 00:00:00 -07:00
8
8
  summary: SQLite3/Ruby is a module to allow Ruby scripts to interface with a SQLite3 database.
9
9
  require_paths:
10
10
  - lib