Skip to content

Commit

Permalink
Fix bug with virtual tables in statemnts
Browse files Browse the repository at this point in the history
Virtual tables can exist on a single statement and do not show up in the
db schema, so skip them when testing for rowid columns.
  • Loading branch information
copiousfreetime committed Dec 19, 2017
1 parent 75b28d4 commit 6eed277
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/amalgalite/statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def result_meta
# only check for rowid if we have a table name and it is not one of the
# sqlite_master tables. We could get recursion in those cases.
if not using_rowid_column? and tbl_name and
not %w[ sqlite_master sqlite_temp_master].include?( tbl_name ) and is_column_rowid?( tbl_name, col_name ) then
not %w[ sqlite_master sqlite_temp_master ].include?( tbl_name ) and is_column_rowid?( tbl_name, col_name ) then
@rowid_index = idx
end

Expand All @@ -363,7 +363,10 @@ def result_meta
# is the column indicated by the Column a 'rowid' column
#
def is_column_rowid?( table_name, column_name )
column_schema = @db.schema.tables[table_name].columns[column_name]
table_schema = @db.schema.tables[table_name]
return false unless table_schema

column_schema = table_schema.columns[column_name]
if column_schema then
if column_schema.primary_key? and column_schema.declared_data_type and column_schema.declared_data_type.upcase == "INTEGER" then
return true
Expand Down
15 changes: 15 additions & 0 deletions spec/json_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'
require 'amalgalite/sqlite3'
require 'rbconfig'

describe "Amalgalite handles the JSON extension" do
it "can parse a `json_each` call" do
db = Amalgalite::Database.new( ":memory:" )
values = %w[ a b c d e f g ]
db.execute("CREATE TABLE jtest(id, json)")
db.execute("INSERT INTO jtest(id, json) values (1, json($json))", { "$json" => values })
rows = db.execute("SELECT jtest.id as i, value as v FROM jtest, json_each(jtest.json)")

rows.size.should eq(values.size)
end
end

0 comments on commit 6eed277

Please sign in to comment.