/usr/lib/ruby/vendor_ruby/sequel/adapters/swift.rb is in ruby-sequel 3.33.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 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++.
#
# The Swift adapter currently supports PostgreSQL and MySQL:
#
# 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|
Sequel.ts_require 'adapters/swift/postgres'
db.extend(Sequel::Swift::Postgres::DatabaseMethods)
db.dataset_class = Sequel::Swift::Postgres::Dataset
db.swift_class = ::Swift::DB::Postgres
end,
:mysql=>proc do |db|
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|
Sequel.ts_require 'adapters/swift/sqlite'
db.extend(Sequel::Swift::SQLite::DatabaseMethods)
db.dataset_class = Sequel::Swift::SQLite::Dataset
db.swift_class = ::Swift::DB::Sqlite3
db.set_integer_booleans
end,
}
class Database < Sequel::Database
set_adapter_scheme :swift
# The Swift adapter class being used by this database. Connections
# in this database's connection pool will be instances of this class.
attr_accessor :swift_class
# Call the DATABASE_SETUP proc directly after initialization,
# so the object always uses sub adapter specific code. Also,
# raise an error immediately if the connection doesn't have a
# db_type specified, since one is required to include the correct
# subadapter.
def initialize(opts)
super
if db_type = opts[:db_type] and !db_type.to_s.empty?
if prok = DATABASE_SETUP[db_type.to_s.to_sym]
prok.call(self)
else
raise(Error, "No :db_type option specified")
end
else
raise(Error, ":db_type option not valid, should be postgres, mysql, or sqlite")
end
end
# Create an instance of swift_class for the given options.
def connect(server)
setup_connection(swift_class.new(server_opts(server)))
end
# Execute the given SQL, yielding a Swift::Result if a block is given.
def execute(sql, opts={})
synchronize(opts[:server]) do |conn|
begin
res = log_yield(sql){conn.execute(sql)}
yield res if block_given?
nil
rescue SwiftError => e
raise_error(e)
end
end
end
# Execute the SQL on the this database, returning the number of affected
# rows.
def execute_dui(sql, opts={})
synchronize(opts[:server]) do |conn|
begin
log_yield(sql){conn.execute(sql).rows}
rescue SwiftError => e
raise_error(e)
end
end
end
# Execute the SQL on this database, returning the primary key of the
# table being inserted to.
def execute_insert(sql, opts={})
synchronize(opts[:server]) do |conn|
begin
log_yield(sql){conn.execute(sql).insert_id}
rescue SwiftError => e
raise_error(e)
end
end
end
private
# Method to call on a statement object to execute SQL that does
# not return any rows.
def connection_execute_method
:execute
end
# Close the given database connection.
def disconnect_connection(c)
end
# Execute SQL on the connection
def log_connection_execute(conn, sql)
log_yield(sql){conn.execute(sql)}
end
# Set the :db entry to the same as the :database entry, since
# Swift uses :db.
def server_opts(o)
o = super
o[:db] ||= o[:database]
o
end
# Allow extending the given connection when it is first created.
# By default, just returns the connection.
def setup_connection(conn)
conn
end
end
class Dataset < Sequel::Dataset
Database::DatasetClass = self
# Set the columns and yield the hashes to the block.
def fetch_rows(sql, &block)
execute(sql) do |res|
@columns = res.fields
res.each(&block)
end
self
end
end
end
end
|