Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift db drivers #526

Closed
wants to merge 12 commits into from
Closed
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@
*.rbc
*.swp
*.lock
.rvmrc
6 changes: 6 additions & 0 deletions doc/opening_databases.rdoc
Expand Up @@ -387,6 +387,12 @@ swift is a ruby 1.9 only library, so you'll need to be running ruby 1.9. It
can connect to SQLite, MySQL, and PostgreSQL, and you must specify which
database using the db_type option.

You need to install one of the swift db adapters

* swift-db-sqlite3
* swift-db-mysql
* swift-db-postgres

Examples:

swift:///?database=:memory:&db_type=sqlite
Expand Down
20 changes: 10 additions & 10 deletions lib/sequel/adapters/swift.rb
@@ -1,30 +1,30 @@
require 'swift'

module Sequel
# Module holding the Swift support for Sequel. Swift is a
# ruby front-end for dbic++, a fast database access library
# written in C++.
# Module holding the Swift DB support for Sequel. Swift DB is a
# collection of drivers used in Swift ORM.
#
# The Swift adapter currently supports PostgreSQL and MySQL:
# The Swift adapter currently supports PostgreSQL, MySQL and SQLite3
#
# Sequel.connect('swift://user:password@host/database?db_type=postgres')
# Sequel.connect('swift://user:password@host/database?db_type=mysql')
module Swift
# Contains procs keyed on sub adapter type that extend the
# given database object so it supports the correct database type.
DATABASE_SETUP = {:postgres=>proc do |db|
require 'swift/db/postgres'
Sequel.ts_require 'adapters/swift/postgres'
db.extend(Sequel::Swift::Postgres::DatabaseMethods)
db.extend_datasets Sequel::Postgres::DatasetMethods
db.swift_class = ::Swift::DB::Postgres
end,
:mysql=>proc do |db|
require 'swift/db/mysql'
Sequel.ts_require 'adapters/swift/mysql'
db.extend(Sequel::Swift::MySQL::DatabaseMethods)
db.dataset_class = Sequel::Swift::MySQL::Dataset
db.swift_class = ::Swift::DB::Mysql
end,
:sqlite=>proc do |db|
require 'swift/db/sqlite3'
Sequel.ts_require 'adapters/swift/sqlite'
db.extend(Sequel::Swift::SQLite::DatabaseMethods)
db.dataset_class = Sequel::Swift::SQLite::Dataset
Expand Down Expand Up @@ -70,7 +70,7 @@ def execute(sql, opts={})
res = log_yield(sql){conn.execute(sql)}
yield res if block_given?
nil
rescue SwiftError => e
rescue ::Swift::Error => e
raise_error(e)
end
end
Expand All @@ -81,8 +81,8 @@ def execute(sql, opts={})
def execute_dui(sql, opts={})
synchronize(opts[:server]) do |conn|
begin
log_yield(sql){conn.execute(sql).rows}
rescue SwiftError => e
log_yield(sql){conn.execute(sql).affected_rows}
rescue ::Swift::Error => e
raise_error(e)
end
end
Expand All @@ -94,7 +94,7 @@ def execute_insert(sql, opts={})
synchronize(opts[:server]) do |conn|
begin
log_yield(sql){conn.execute(sql).insert_id}
rescue SwiftError => e
rescue ::Swift::Error => e
raise_error(e)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/adapters/swift/postgres.rb
@@ -1,9 +1,9 @@
Sequel.require 'adapters/shared/postgres'

module Sequel
Postgres::CONVERTED_EXCEPTIONS << ::SwiftError

module Swift
Postgres::CONVERTED_EXCEPTIONS << ::Swift::Error
# Adapter, Database, and Dataset support for accessing a PostgreSQL
# database via Swift.
module Postgres
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/adapters/swift/sqlite.rb
Expand Up @@ -25,7 +25,7 @@ class Dataset < Swift::Dataset

# Use Swift's escape method for quoting.
def literal_string_append(sql, s)
sql << db.synchronize{|c| c.escape(s)}
sql << APOS << db.synchronize{|c| c.escape(s)} << APOS
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/adapters/mysql_spec.rb
Expand Up @@ -81,7 +81,7 @@ def logger.method_missing(m, msg)
@db.schema(:dolls).map{|k, v| v[:auto_increment]}.should == [nil, nil, true]
end

cspecify "should support collate with various other column options", :swift do
cspecify "should support collate with various other column options" do
@db.create_table!(:dolls){ String :name, :size=>128, :collate=>:utf8_bin, :default=>'foo', :null=>false, :unique=>true}
@db[:dolls].insert
@db[:dolls].select_map(:name).should == ["foo"]
Expand Down