Skip to content

Commit

Permalink
fixing a bug with tyep_translation=, results as hash, and calling exe…
Browse files Browse the repository at this point in the history
…cute()
  • Loading branch information
tenderlove committed Jul 10, 2010
1 parent 038a67e commit 685a7f1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rdoc
@@ -1,3 +1,12 @@
=== 1.3.2 / unreleased

* Bugfixes
* type_translation= works along with Database#execute and a block

* DEPRECATIONS
* SQLite3::Database#type_translation= will be deprecated in the future with
no replacement.

=== 1.3.1 / 2010-07-09

* Enhancements
Expand Down
36 changes: 22 additions & 14 deletions lib/sqlite3/database.rb
Expand Up @@ -54,9 +54,15 @@ def quote( string )
# as hashes or not. By default, rows are returned as arrays.
attr_accessor :results_as_hash

# A boolean indicating whether or not type translation is enabled for this
# database.
attr_accessor :type_translation
def type_translation= value # :nodoc:
warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling SQLite3::Database#type_translation=
SQLite3::Database#type_translation= is deprecated and will be removed
in the future.
eowarn
@type_translation = value
end
attr_reader :type_translation # :nodoc:

# Return the type translator employed by this database instance. Each
# database instance has its own type translator; this allows for different
Expand Down Expand Up @@ -126,26 +132,21 @@ def execute sql, bind_vars = [], *args, &block

prepare( sql ) do |stmt|
stmt.bind_params(bind_vars)
if type_translation
stmt = ResultSet.new(self, stmt).to_a
end
columns = stmt.columns
stmt = ResultSet.new(self, stmt).to_a if type_translation

if block_given?
stmt.each do |row|
if @results_as_hash
h = Hash[*stmt.columns.zip(row).flatten]
row.each_with_index { |r, i| h[i] = r }

yield h
yield type_translation ? row : ordered_map_for(columns, row)
else
yield row
end
end
else
if @results_as_hash
stmt.map { |row|
h = Hash[*stmt.columns.zip(row).flatten]
row.each_with_index { |r, i| h[i] = r }
h = type_translation ? row : ordered_map_for(columns, row)

# FIXME UGH TERRIBLE HACK!
h['unique'] = h['unique'].to_s if hack
Expand Down Expand Up @@ -273,8 +274,7 @@ def query( sql, bind_vars = [], *args )
#
# See also #get_first_value.
def get_first_row( sql, *bind_vars )
execute( sql, *bind_vars ) { |row| return row }
nil
execute( sql, *bind_vars ).first
end

# A convenience method for obtaining the first value of the first row of a
Expand Down Expand Up @@ -566,5 +566,13 @@ def []=( key, value )
@context[ key ] = value
end
end

private

def ordered_map_for columns, row
h = Hash[*columns.zip(row).flatten]
row.each_with_index { |r, i| h[i] = r }
h
end
end
end
19 changes: 19 additions & 0 deletions test/test_database.rb
Expand Up @@ -7,6 +7,25 @@ def setup
@db = SQLite3::Database.new(':memory:')
end

def test_get_first_row
assert_equal [1], @db.get_first_row('SELECT 1')
end

def test_get_first_row_with_type_translation_and_hash_results
@db.results_as_hash = true
@db.type_translation = true
assert_equal({0=>1, "1"=>1}, @db.get_first_row('SELECT 1'))
end

def test_execute_with_type_translation_and_hash
@db.results_as_hash = true
@db.type_translation = true
rows = []
@db.execute('SELECT 1') { |row| rows << row }

assert_equal({0=>1, "1"=>1}, rows.first)
end

def test_encoding
assert @db.encoding, 'database has encoding'
end
Expand Down

0 comments on commit 685a7f1

Please sign in to comment.