Skip to content

Commit

Permalink
Support creating VIRTUAL tables on SQLite via the create_table :using…
Browse files Browse the repository at this point in the history
… option
  • Loading branch information
jeremyevans committed Aug 22, 2024
1 parent 743bafb commit 5c0e37b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Support creating VIRTUAL tables on SQLite via the create_table :using option (jeremyevans)

* Support json_{exists,value,query} on PostgreSQL 17+ in the pg_json_ops extension (jeremyevans)

* Remove documentation from the gem to reduce gem size by 25% (jeremyevans)
Expand Down
4 changes: 3 additions & 1 deletion lib/sequel/adapters/shared/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,16 @@ def connection_pragmas
ps
end

# Support creating STRICT AND/OR WITHOUT ROWID tables via :strict and :without_rowid options
# Support creating STRICT AND/OR WITHOUT ROWID tables via :strict and :without_rowid options, and VIRTUAL tables with :using option.
def create_table_sql(name, generator, options)
if options[:strict] && options[:without_rowid]
"#{super} STRICT, WITHOUT ROWID"
elsif options[:strict]
"#{super} STRICT"
elsif options[:without_rowid]
"#{super} WITHOUT ROWID"
elsif options[:using]
"CREATE VIRTUAL TABLE#{' IF NOT EXISTS' if options[:if_not_exists]} #{create_table_table_name_sql(name, options)} USING #{options[:using]}"
else
super
end
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/database/schema_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ def create_join_table?(hash, options=OPTS)
# The +any+ type is treated like a SQLite column in a non-strict table,
# allowing any type of data to be stored. This option is supported on
# SQLite 3.37.0+.
# :using :: Create a VIRTUAL table with the given USING clause. The value should be
# a string, as it is used directly in the SQL query.
# :without_rowid :: Create a WITHOUT ROWID table. Every row in SQLite has a special
# 'rowid' column, that uniquely identifies that row within the table.
# If this option is used, the 'rowid' column is omitted, which can
Expand Down
13 changes: 13 additions & 0 deletions spec/adapters/sqlite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@
@db.sqlite_version.must_be_kind_of(Integer)
end

it "should support creating virtual tables via the create_table :using option" do
if @db['PRAGMA compile_options'].map(:compile_options).include?('ENABLE_FTS5')
@db.create_table(:fk, using: 'fts5(c)')
@db[:fk].insert(:c=>'foo bar')
@db[:fk].where(fk: 'foo').get(:c).must_equal 'foo bar'
@db[:fk].where(fk: 'bar').get(:c).must_equal 'foo bar'
@db[:fk].where(fk: 'baz').must_be_empty

@db.create_table(:fk, using: 'fts5(c)', if_not_exists: true)
@db[:fk].where(fk: 'foo').get(:c).must_equal 'foo bar'
end
end

it "should support dropping noncomposite unique constraint" do
@db.create_table(:fk) do
primary_key :id
Expand Down

0 comments on commit 5c0e37b

Please sign in to comment.