/usr/lib/ruby/vendor_ruby/sequel/adapters/swift.rb is in ruby-sequel 4.15.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 149 150 | module Sequel
# 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, 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|
Sequel.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|
Sequel.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.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
# Create an instance of swift_class for the given options.
def connect(server)
opts = server_opts(server)
opts[:pass] = opts[:password]
setup_connection(swift_class.new(opts))
end
# Execute the given SQL, yielding a Swift::Result if a block is given.
def execute(sql, opts=OPTS)
synchronize(opts[:server]) do |conn|
begin
res = log_yield(sql){conn.execute(sql)}
yield res if block_given?
nil
rescue ::Swift::Error => 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=OPTS)
synchronize(opts[:server]) do |conn|
begin
log_yield(sql){conn.execute(sql).affected_rows}
rescue ::Swift::Error => 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=OPTS)
synchronize(opts[:server]) do |conn|
begin
log_yield(sql){conn.execute(sql).insert_id}
rescue ::Swift::Error => e
raise_error(e)
end
end
end
private
# 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 adapter_initialize
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
# Method to call on a statement object to execute SQL that does
# not return any rows.
def connection_execute_method
:execute
end
def database_error_classes
[::Swift::Error]
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)
execute(sql) do |res|
col_map = {}
@columns = res.fields.map do |c|
col_map[c] = output_identifier(c)
end
res.each do |r|
h = {}
r.each do |k, v|
h[col_map[k]] = v.is_a?(StringIO) ? SQL::Blob.new(v.read) : v
end
yield h
end
end
self
end
end
end
end
|