summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2007-01-30 00:07:47 +0000
committerJamis Buck <jamis@37signals.com>2007-01-30 00:07:47 +0000
commita1472f5339fb163b0ca5a330c8c599e51f0468f0 (patch)
tree8bd8363035d2adbc459d4752c7debd178a3bf134
parent5f4b8ddb1e2f9a36d5d1408c4630a63df49e8aa5 (diff)
downloadthird_party-sqlite3-ruby-a1472f5339fb163b0ca5a330c8c599e51f0468f0.tar.gz
third_party-sqlite3-ruby-a1472f5339fb163b0ca5a330c8c599e51f0468f0.tar.xz
third_party-sqlite3-ruby-a1472f5339fb163b0ca5a330c8c599e51f0468f0.zip
make the table_info method adjust the returned default value for the rows so that the sqlite3 change in 3.3.8 and greater can be handled transparently
-rw-r--r--Rakefile2
-rw-r--r--lib/sqlite3/pragmas.rb51
-rw-r--r--test/tc_integration.rb11
3 files changed, 61 insertions, 3 deletions
diff --git a/Rakefile b/Rakefile
index 9e53abd..43ddfc2 100644
--- a/Rakefile
+++ b/Rakefile
@@ -195,5 +195,5 @@ task :beta do
system "rake gem"
- #system "svn revert lib/sqlite3/version.rb"
+ system "svn revert lib/sqlite3/version.rb"
end
diff --git a/lib/sqlite3/pragmas.rb b/lib/sqlite3/pragmas.rb
index 7247387..20b3180 100644
--- a/lib/sqlite3/pragmas.rb
+++ b/lib/sqlite3/pragmas.rb
@@ -246,9 +246,56 @@ module SQLite3
end
def table_info( table, &block ) # :yields: row
- get_query_pragma "table_info", table, &block
+ columns, *rows = execute2("PRAGMA table_info(#{table})")
+
+ needs_tweak_default = version_compare(driver.libversion, "3.3.7") > 0
+
+ result = [] unless block_given?
+ rows.each do |row|
+ new_row = {}
+ columns.each_with_index do |name, index|
+ new_row[name] = row[index]
+ end
+
+ tweak_default(new_row) if needs_tweak_default
+
+ if block_given?
+ yield new_row
+ else
+ result << new_row
+ end
+ end
+
+ result
end
-
+
+ private
+
+ # Compares two version strings
+ def version_compare(v1, v2)
+ v1 = v1.split(".").map { |i| i.to_i }
+ v2 = v2.split(".").map { |i| i.to_i }
+ parts = [v1.length, v2.length].max
+ v1.push 0 while v1.length < parts
+ v2.push 0 while v2.length < parts
+ v1.zip(v2).each do |a,b|
+ return -1 if a < b
+ return 1 if a > b
+ end
+ return 0
+ end
+
+ # Since SQLite 3.3.8, the table_info pragma has returned the default
+ # value of the row as a quoted SQL value. This method essentially
+ # unquotes those values.
+ def tweak_default(hash)
+ case hash["dflt_value"]
+ when /^null$/i then
+ hash["dflt_value"] = nil
+ when /^'(.*)'$/
+ hash["dflt_value"] = $1.gsub(/''/, "'")
+ end
+ end
end
end
diff --git a/test/tc_integration.rb b/test/tc_integration.rb
index 8bb4c4a..9d78313 100644
--- a/test/tc_integration.rb
+++ b/test/tc_integration.rb
@@ -82,6 +82,17 @@ module Integration
assert_nothing_raised { @db.table_info("foo") }
end
+ define_method( "test_table_info_with_defaults_for_version_3_3_8_and_higher" ) do
+ @db.transaction do
+ @db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
+ data = @db.table_info( "defaults_test" )
+ assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => "0", "cid" => "0", "pk" => "0"},
+ data[0])
+ assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => "0", "cid" => "1", "pk" => "0"},
+ data[1])
+ end
+ end
+
define_method( "test_complete_fail" ) do
assert !@db.complete?( "select * from foo" )
end