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 +28 -28
- data/lib/sqlite3/pragmas.rb +49 -2
- data/lib/sqlite3/version.rb +2 -1
- data/test/tc_integration.rb +11 -0
- metadata +2 -2
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='#
|
66
|
-
<li><a href='#
|
67
|
-
<li><a href='#
|
68
|
-
<li><a href='#
|
69
|
-
<li><a href='#
|
65
|
+
<li><a href='#37380'>I just want an array of the rows…</a></li>
|
66
|
+
<li><a href='#36440'>I’d like to use a block to iterate through the rows…</a></li>
|
67
|
+
<li><a href='#32250'>I need to get the column names as well as the rows…</a></li>
|
68
|
+
<li><a href='#31680'>I just want the first row of the result set…</a></li>
|
69
|
+
<li><a href='#31240'>I just want the first value of the first row of the result set…</a></li>
|
70
70
|
</ul>
|
71
71
|
</li>
|
72
|
-
<li><a href='#
|
73
|
-
<li><a href='#
|
74
|
-
<li><a href='#
|
75
|
-
<li><a href='#
|
76
|
-
<li><a href='#
|
77
|
-
<li><a href='#
|
78
|
-
<li><a href='#
|
79
|
-
<li><a href='#
|
80
|
-
<li><a href='#
|
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’d like the rows to be indexible by column name.</a></li>
|
76
|
+
<li><a href='#26050'>I’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='
|
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…</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’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='
|
94
|
+
<a name='36440'></a>
|
95
95
|
<div class='faq-title'>How do I do a database query? I’d like to use a block to iterate through the rows…</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='
|
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…</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’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='
|
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…</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='
|
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…</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='
|
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='
|
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" => "bob" )
|
251
251
|
</pre></div>
|
252
|
-
<a name='
|
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='
|
285
|
+
<a name='26320'></a>
|
286
286
|
<div class='faq-title'>I’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='
|
313
|
+
<a name='26050'></a>
|
314
314
|
<div class='faq-title'>I’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 “type translation” 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='
|
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='
|
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='
|
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’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='
|
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—you’ll get errors when the block terminates!)</p>
|
398
398
|
|
data/lib/sqlite3/pragmas.rb
CHANGED
@@ -246,9 +246,56 @@ module SQLite3
|
|
246
246
|
end
|
247
247
|
|
248
248
|
def table_info( table, &block ) # :yields: row
|
249
|
-
|
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
|
data/lib/sqlite3/version.rb
CHANGED
data/test/tc_integration.rb
CHANGED
@@ -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.
|
7
|
-
date: 2007-
|
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
|