colincasey-sequel 2.10.0 → 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/doc/advanced_associations.rdoc +614 -0
- data/doc/cheat_sheet.rdoc +223 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/schema.rdoc +29 -0
- data/doc/sharding.rdoc +113 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel_core/adapters/ado.rb +89 -0
- data/lib/sequel_core/adapters/db2.rb +143 -0
- data/lib/sequel_core/adapters/dbi.rb +112 -0
- data/lib/sequel_core/adapters/do/mysql.rb +38 -0
- data/lib/sequel_core/adapters/do/postgres.rb +92 -0
- data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
- data/lib/sequel_core/adapters/do.rb +205 -0
- data/lib/sequel_core/adapters/firebird.rb +298 -0
- data/lib/sequel_core/adapters/informix.rb +85 -0
- data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel_core/adapters/jdbc.rb +491 -0
- data/lib/sequel_core/adapters/mysql.rb +369 -0
- data/lib/sequel_core/adapters/odbc.rb +174 -0
- data/lib/sequel_core/adapters/openbase.rb +68 -0
- data/lib/sequel_core/adapters/oracle.rb +107 -0
- data/lib/sequel_core/adapters/postgres.rb +456 -0
- data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
- data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
- data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
- data/lib/sequel_core/adapters/shared/progress.rb +31 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
- data/lib/sequel_core/adapters/sqlite.rb +248 -0
- data/lib/sequel_core/connection_pool.rb +258 -0
- data/lib/sequel_core/core_ext.rb +217 -0
- data/lib/sequel_core/core_sql.rb +202 -0
- data/lib/sequel_core/database/schema.rb +164 -0
- data/lib/sequel_core/database.rb +691 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +237 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +1010 -0
- data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
- data/lib/sequel_core/dataset/unsupported.rb +43 -0
- data/lib/sequel_core/dataset.rb +511 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +44 -0
- data/lib/sequel_core/migration.rb +212 -0
- data/lib/sequel_core/object_graph.rb +230 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema/generator.rb +320 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/sql.rb +887 -0
- data/lib/sequel_core/version.rb +11 -0
- data/lib/sequel_core.rb +172 -0
- data/lib/sequel_model/association_reflection.rb +267 -0
- data/lib/sequel_model/associations.rb +499 -0
- data/lib/sequel_model/base.rb +523 -0
- data/lib/sequel_model/caching.rb +82 -0
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +370 -0
- data/lib/sequel_model/exceptions.rb +7 -0
- data/lib/sequel_model/hooks.rb +101 -0
- data/lib/sequel_model/inflector.rb +281 -0
- data/lib/sequel_model/plugins.rb +62 -0
- data/lib/sequel_model/record.rb +568 -0
- data/lib/sequel_model/schema.rb +49 -0
- data/lib/sequel_model/validations.rb +429 -0
- data/lib/sequel_model.rb +91 -0
- data/spec/adapters/ado_spec.rb +46 -0
- data/spec/adapters/firebird_spec.rb +376 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +881 -0
- data/spec/adapters/oracle_spec.rb +244 -0
- data/spec/adapters/postgres_spec.rb +687 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +555 -0
- data/spec/integration/dataset_test.rb +134 -0
- data/spec/integration/eager_loader_test.rb +696 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +180 -0
- data/spec/integration/spec_helper.rb +58 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/rcov.opts +6 -0
- data/spec/sequel_core/connection_pool_spec.rb +526 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +522 -0
- data/spec/sequel_core/database_spec.rb +1188 -0
- data/spec/sequel_core/dataset_spec.rb +3481 -0
- data/spec/sequel_core/expression_filters_spec.rb +363 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +272 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +167 -0
- data/spec/sequel_core/schema_spec.rb +780 -0
- data/spec/sequel_core/spec_helper.rb +55 -0
- data/spec/sequel_core/version_spec.rb +7 -0
- data/spec/sequel_model/association_reflection_spec.rb +93 -0
- data/spec/sequel_model/associations_spec.rb +1767 -0
- data/spec/sequel_model/base_spec.rb +419 -0
- data/spec/sequel_model/caching_spec.rb +215 -0
- data/spec/sequel_model/dataset_methods_spec.rb +78 -0
- data/spec/sequel_model/eager_loading_spec.rb +1165 -0
- data/spec/sequel_model/hooks_spec.rb +485 -0
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/sequel_model/model_spec.rb +588 -0
- data/spec/sequel_model/plugins_spec.rb +80 -0
- data/spec/sequel_model/record_spec.rb +1184 -0
- data/spec/sequel_model/schema_spec.rb +90 -0
- data/spec/sequel_model/spec_helper.rb +78 -0
- data/spec/sequel_model/validations_spec.rb +1067 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +177 -3
@@ -0,0 +1,205 @@
|
|
1
|
+
require 'data_objects'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
# Module holding the DataObjects support for Sequel. DataObjects is a
|
5
|
+
# ruby library with a standard API for accessing databases.
|
6
|
+
#
|
7
|
+
# The DataObjects adapter currently supports PostgreSQL, MySQL, and
|
8
|
+
# SQLite:
|
9
|
+
#
|
10
|
+
# * Sequel.connect('do:sqlite3::memory:')
|
11
|
+
# * Sequel.connect('do:postgres://user:password@host/database')
|
12
|
+
# * Sequel.connect('do:mysql://user:password@host/database')
|
13
|
+
module DataObjects
|
14
|
+
# Contains procs keyed on sub adapter type that extend the
|
15
|
+
# given database object so it supports the correct database type.
|
16
|
+
DATABASE_SETUP = {:postgres=>proc do |db|
|
17
|
+
require 'do_postgres'
|
18
|
+
require 'sequel_core/adapters/do/postgres'
|
19
|
+
db.converted_exceptions << PostgresError
|
20
|
+
db.extend(Sequel::DataObjects::Postgres::DatabaseMethods)
|
21
|
+
end,
|
22
|
+
:mysql=>proc do |db|
|
23
|
+
require 'do_mysql'
|
24
|
+
require 'sequel_core/adapters/do/mysql'
|
25
|
+
db.converted_exceptions << MysqlError
|
26
|
+
db.extend(Sequel::DataObjects::MySQL::DatabaseMethods)
|
27
|
+
end,
|
28
|
+
:sqlite3=>proc do |db|
|
29
|
+
require 'do_sqlite3'
|
30
|
+
require 'sequel_core/adapters/do/sqlite'
|
31
|
+
db.converted_exceptions << Sqlite3Error
|
32
|
+
db.extend(Sequel::DataObjects::SQLite::DatabaseMethods)
|
33
|
+
end
|
34
|
+
}
|
35
|
+
|
36
|
+
# DataObjects uses it's own internal connection pooling in addition to the
|
37
|
+
# pooling that Sequel uses. You should make sure that you don't set
|
38
|
+
# the connection pool size to more than 8 for a
|
39
|
+
# Sequel::DataObjects::Database object, or hack DataObjects (or Extlib) to
|
40
|
+
# use a pool size at least as large as the pool size being used by Sequel.
|
41
|
+
class Database < Sequel::Database
|
42
|
+
set_adapter_scheme :do
|
43
|
+
|
44
|
+
# Convert the given exceptions to Sequel:Errors, necessary
|
45
|
+
# because DO raises errors specific to database types in
|
46
|
+
# certain cases.
|
47
|
+
attr_accessor :converted_exceptions
|
48
|
+
|
49
|
+
# Call the DATABASE_SETUP proc directly after initialization,
|
50
|
+
# so the object always uses sub adapter specific code. Also,
|
51
|
+
# raise an error immediately if the connection doesn't have a
|
52
|
+
# uri, since DataObjects requires one.
|
53
|
+
def initialize(opts)
|
54
|
+
@opts = opts
|
55
|
+
@converted_exceptions = []
|
56
|
+
raise(Error, "No connection string specified") unless uri
|
57
|
+
if prok = DATABASE_SETUP[subadapter.to_sym]
|
58
|
+
prok.call(self)
|
59
|
+
end
|
60
|
+
super(opts)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Setup a DataObjects::Connection to the database.
|
64
|
+
def connect(server)
|
65
|
+
setup_connection(::DataObjects::Connection.new(uri(server_opts(server))))
|
66
|
+
end
|
67
|
+
|
68
|
+
# Return a Sequel::DataObjects::Dataset object for this database.
|
69
|
+
def dataset(opts = nil)
|
70
|
+
DataObjects::Dataset.new(self, opts)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Execute the given SQL. If a block is given, the DataObjects::Reader
|
74
|
+
# created is yielded to it. A block should not be provided unless a
|
75
|
+
# a SELECT statement is being used (or something else that returns rows).
|
76
|
+
# Otherwise, the return value is the insert id if opts[:type] is :insert,
|
77
|
+
# or the number of affected rows, otherwise.
|
78
|
+
def execute(sql, opts={})
|
79
|
+
log_info(sql)
|
80
|
+
synchronize(opts[:server]) do |conn|
|
81
|
+
begin
|
82
|
+
command = conn.create_command(sql)
|
83
|
+
res = block_given? ? command.execute_reader : command.execute_non_query
|
84
|
+
rescue Exception => e
|
85
|
+
raise_error(e, :classes=>@converted_exceptions)
|
86
|
+
end
|
87
|
+
if block_given?
|
88
|
+
begin
|
89
|
+
yield(res)
|
90
|
+
ensure
|
91
|
+
res.close if res
|
92
|
+
end
|
93
|
+
elsif opts[:type] == :insert
|
94
|
+
res.insert_id
|
95
|
+
else
|
96
|
+
res.affected_rows
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Execute the SQL on the this database, returning the number of affected
|
102
|
+
# rows.
|
103
|
+
def execute_dui(sql, opts={})
|
104
|
+
execute(sql, opts)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Execute the SQL on this database, returning the primary key of the
|
108
|
+
# table being inserted to.
|
109
|
+
def execute_insert(sql, opts={})
|
110
|
+
execute(sql, opts.merge(:type=>:insert))
|
111
|
+
end
|
112
|
+
|
113
|
+
# Return the subadapter type for this database, i.e. sqlite3 for
|
114
|
+
# do:sqlite3::memory:.
|
115
|
+
def subadapter
|
116
|
+
uri.split(":").first
|
117
|
+
end
|
118
|
+
|
119
|
+
# Use DataObject's transaction support for transactions. This
|
120
|
+
# only supports single level transactions, and it always prepares
|
121
|
+
# transactions and commits them immediately after. It's wasteful,
|
122
|
+
# but required by DataObject's API.
|
123
|
+
def transaction(server=nil)
|
124
|
+
th = Thread.current
|
125
|
+
synchronize(server) do |conn|
|
126
|
+
return yield(conn) if @transactions.include?(th)
|
127
|
+
t = ::DataObjects::Transaction.create_for_uri(uri)
|
128
|
+
t.instance_variable_get(:@connection).close
|
129
|
+
t.instance_variable_set(:@connection, conn)
|
130
|
+
begin
|
131
|
+
log_info("Transaction.begin")
|
132
|
+
t.begin
|
133
|
+
@transactions << th
|
134
|
+
yield(conn)
|
135
|
+
rescue Exception => e
|
136
|
+
log_info("Transaction.rollback")
|
137
|
+
t.rollback
|
138
|
+
transaction_error(e)
|
139
|
+
ensure
|
140
|
+
unless e
|
141
|
+
log_info("Transaction.commit")
|
142
|
+
t.prepare
|
143
|
+
t.commit
|
144
|
+
end
|
145
|
+
@transactions.delete(th)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# Return the DataObjects URI for the Sequel URI, removing the do:
|
151
|
+
# prefix.
|
152
|
+
def uri(opts={})
|
153
|
+
opts = @opts.merge(opts)
|
154
|
+
(opts[:uri] || opts[:url]).sub(/\Ado:/, '')
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
# Close the given database connection.
|
160
|
+
def disconnect_connection(c)
|
161
|
+
c.close
|
162
|
+
end
|
163
|
+
|
164
|
+
# Allow extending the given connection when it is first created.
|
165
|
+
# By default, just returns the connection.
|
166
|
+
def setup_connection(conn)
|
167
|
+
conn
|
168
|
+
end
|
169
|
+
|
170
|
+
# The DataObjects adapter should convert exceptions by default.
|
171
|
+
def connection_pool_default_options
|
172
|
+
super.merge(:pool_convert_exceptions=>false)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
# Dataset class for Sequel::DataObjects::Database objects.
|
177
|
+
class Dataset < Sequel::Dataset
|
178
|
+
# Handle the usual time class overrides.
|
179
|
+
def literal(v)
|
180
|
+
case v
|
181
|
+
when Time
|
182
|
+
literal(v.iso8601)
|
183
|
+
when Date, DateTime
|
184
|
+
literal(v.to_s)
|
185
|
+
else
|
186
|
+
super
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# Execute the SQL on the database and yield the rows as hashes
|
191
|
+
# with symbol keys.
|
192
|
+
def fetch_rows(sql)
|
193
|
+
execute(sql) do |reader|
|
194
|
+
cols = @columns = reader.fields.map{|f| output_identifier(f)}
|
195
|
+
while(reader.next!) do
|
196
|
+
h = {}
|
197
|
+
cols.zip(reader.values).each{|k, v| h[k] = v}
|
198
|
+
yield h
|
199
|
+
end
|
200
|
+
end
|
201
|
+
self
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
@@ -0,0 +1,298 @@
|
|
1
|
+
require 'fb'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
# The Sequel Firebird adapter requires the ruby fb driver located at
|
5
|
+
# http://github.com/wishdev/fb.
|
6
|
+
module Firebird
|
7
|
+
class Database < Sequel::Database
|
8
|
+
set_adapter_scheme :firebird
|
9
|
+
|
10
|
+
AUTO_INCREMENT = ''.freeze
|
11
|
+
|
12
|
+
# Add the primary_keys and primary_key_sequences instance variables,
|
13
|
+
# so we can get the correct return values for inserted rows.
|
14
|
+
def initialize(*args)
|
15
|
+
super
|
16
|
+
@primary_keys = {}
|
17
|
+
@primary_key_sequences = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
# Use Firebird specific syntax for add column
|
21
|
+
def alter_table_sql(table, op)
|
22
|
+
case op[:op]
|
23
|
+
when :add_column
|
24
|
+
"ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op)}"
|
25
|
+
when :drop_column
|
26
|
+
"ALTER TABLE #{quote_schema_table(table)} DROP #{column_definition_sql(op)}"
|
27
|
+
when :rename_column
|
28
|
+
"ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}"
|
29
|
+
when :set_column_type
|
30
|
+
"ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} TYPE #{type_literal(op)}"
|
31
|
+
else
|
32
|
+
super(table, op)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def auto_increment_sql()
|
37
|
+
AUTO_INCREMENT
|
38
|
+
end
|
39
|
+
|
40
|
+
def connect(server)
|
41
|
+
opts = server_opts(server)
|
42
|
+
|
43
|
+
db = Fb::Database.new(
|
44
|
+
:database => "#{opts[:host]}:#{opts[:database]}",
|
45
|
+
:username => opts[:user],
|
46
|
+
:password => opts[:password])
|
47
|
+
conn = db.connect
|
48
|
+
conn.downcase_names = true
|
49
|
+
conn
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_sequence_sql(name, opts={})
|
53
|
+
"CREATE SEQUENCE #{quote_identifier(name)}"
|
54
|
+
end
|
55
|
+
|
56
|
+
# Creates a table with the columns given in the provided block:
|
57
|
+
#
|
58
|
+
# DB.create_table :posts do
|
59
|
+
# primary_key :id, :serial
|
60
|
+
# column :title, :text
|
61
|
+
# column :content, :text
|
62
|
+
# index :title
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# See Schema::Generator.
|
66
|
+
# Firebird gets an override because of the mess of creating a
|
67
|
+
# generator for auto-incrementing primary keys.
|
68
|
+
def create_table(name, options={}, &block)
|
69
|
+
options = {:generator=>options} if options.is_a?(Schema::Generator)
|
70
|
+
statements = create_table_sql_list(name, *((options[:generator] ||= Schema::Generator.new(self, &block)).create_info << options))
|
71
|
+
begin
|
72
|
+
execute_ddl(statements[1])
|
73
|
+
rescue
|
74
|
+
nil
|
75
|
+
end if statements[1]
|
76
|
+
statements[0].flatten.each {|sql| execute_ddl(sql)}
|
77
|
+
end
|
78
|
+
|
79
|
+
def create_table_sql_list(name, columns, indexes = nil, options={})
|
80
|
+
statements = super
|
81
|
+
drop_seq_statement = nil
|
82
|
+
columns.each do |c|
|
83
|
+
if c[:auto_increment]
|
84
|
+
c[:sequence_name] ||= "seq_#{name}_#{c[:name]}"
|
85
|
+
unless c[:create_sequence] == false
|
86
|
+
drop_seq_statement = drop_sequence_sql(c[:sequence_name])
|
87
|
+
statements << create_sequence_sql(c[:sequence_name])
|
88
|
+
statements << restart_sequence_sql(c[:sequence_name], {:restart_position => c[:sequence_start_position]}) if c[:sequence_start_position]
|
89
|
+
end
|
90
|
+
unless c[:create_trigger] == false
|
91
|
+
c[:trigger_name] ||= "BI_#{name}_#{c[:name]}"
|
92
|
+
c[:quoted_name] = quote_identifier(c[:name])
|
93
|
+
trigger_definition = <<-END
|
94
|
+
begin
|
95
|
+
if ((new.#{c[:quoted_name]} is null) or (new.#{c[:quoted_name]} = 0)) then
|
96
|
+
begin
|
97
|
+
new.#{c[:quoted_name]} = next value for #{c[:sequence_name]};
|
98
|
+
end
|
99
|
+
end
|
100
|
+
END
|
101
|
+
statements << create_trigger_sql(name, c[:trigger_name], trigger_definition, {:events => [:insert]})
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
[statements, drop_seq_statement]
|
106
|
+
end
|
107
|
+
|
108
|
+
def create_trigger(*args)
|
109
|
+
self << create_trigger_sql(*args)
|
110
|
+
end
|
111
|
+
|
112
|
+
def create_trigger_sql(table, name, definition, opts={})
|
113
|
+
events = opts[:events] ? Array(opts[:events]) : [:insert, :update, :delete]
|
114
|
+
whence = opts[:after] ? 'AFTER' : 'BEFORE'
|
115
|
+
inactive = opts[:inactive] ? 'INACTIVE' : 'ACTIVE'
|
116
|
+
position = opts[:position] ? opts[:position] : 0
|
117
|
+
sql = <<-end_sql
|
118
|
+
CREATE TRIGGER #{quote_identifier(name)} for #{quote_identifier(table)}
|
119
|
+
#{inactive} #{whence} #{events.map{|e| e.to_s.upcase}.join(' OR ')} position #{position}
|
120
|
+
as #{definition}
|
121
|
+
end_sql
|
122
|
+
sql
|
123
|
+
end
|
124
|
+
|
125
|
+
def dataset(opts = nil)
|
126
|
+
Firebird::Dataset.new(self, opts)
|
127
|
+
end
|
128
|
+
|
129
|
+
def drop_sequence(name)
|
130
|
+
self << drop_sequence_sql(name)
|
131
|
+
end
|
132
|
+
|
133
|
+
def drop_sequence_sql(name)
|
134
|
+
"DROP SEQUENCE #{quote_identifier(name)}"
|
135
|
+
end
|
136
|
+
|
137
|
+
def execute(sql, opts={})
|
138
|
+
log_info(sql)
|
139
|
+
begin
|
140
|
+
synchronize(opts[:server]) do |conn|
|
141
|
+
r = conn.execute(sql)
|
142
|
+
yield(r) if block_given?
|
143
|
+
r
|
144
|
+
end
|
145
|
+
rescue => e
|
146
|
+
log_info(e.message)
|
147
|
+
raise_error(e, :classes=>[Fb::Error])
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Return primary key for the given table.
|
152
|
+
def primary_key(table, server=nil)
|
153
|
+
synchronize(server){|conn| primary_key_for_table(conn, table)}
|
154
|
+
end
|
155
|
+
|
156
|
+
# Returns primary key for the given table. This information is
|
157
|
+
# cached, and if the primary key for a table is changed, the
|
158
|
+
# @primary_keys instance variable should be reset manually.
|
159
|
+
def primary_key_for_table(conn, table)
|
160
|
+
@primary_keys[quote_identifier(table)] ||= conn.table_primary_key(quote_identifier(table))
|
161
|
+
end
|
162
|
+
|
163
|
+
def restart_sequence(*args)
|
164
|
+
self << restart_sequence_sql(*args)
|
165
|
+
end
|
166
|
+
|
167
|
+
def restart_sequence_sql(name, opts={})
|
168
|
+
seq_name = quote_identifier(name)
|
169
|
+
"ALTER SEQUENCE #{seq_name} RESTART WITH #{opts[:restart_position]}"
|
170
|
+
end
|
171
|
+
|
172
|
+
def sequences(opts={})
|
173
|
+
ds = self[:"rdb$generators"].server(opts[:server]).filter(:"rdb$system_flag" => 0).select(:"rdb$generator_name")
|
174
|
+
block_given? ? yield(ds) : ds.map{|r| ds.send(:output_identifier, r[:"rdb$generator_name"])}
|
175
|
+
end
|
176
|
+
|
177
|
+
def tables(opts={})
|
178
|
+
ds = self[:"rdb$relations"].server(opts[:server]).filter(:"rdb$view_blr" => nil, Sequel::SQL::Function.new(:COALESCE, :"rdb$system_flag", 0) => 0).select(:"rdb$relation_name")
|
179
|
+
block_given? ? yield(ds) : ds.map{|r| ds.send(:output_identifier, r[:"rdb$relation_name"])}
|
180
|
+
end
|
181
|
+
|
182
|
+
def transaction(server=nil)
|
183
|
+
synchronize(server) do |conn|
|
184
|
+
return yield(conn) if @transactions.include?(Thread.current)
|
185
|
+
log_info("Transaction.begin")
|
186
|
+
conn.transaction
|
187
|
+
begin
|
188
|
+
@transactions << Thread.current
|
189
|
+
yield(conn)
|
190
|
+
rescue ::Exception => e
|
191
|
+
log_info("Transaction.rollback")
|
192
|
+
conn.rollback
|
193
|
+
transaction_error(e, Fb::Error)
|
194
|
+
ensure
|
195
|
+
unless e
|
196
|
+
log_info("Transaction.commit")
|
197
|
+
conn.commit
|
198
|
+
end
|
199
|
+
@transactions.delete(Thread.current)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
private
|
205
|
+
|
206
|
+
def disconnect_connection(c)
|
207
|
+
c.close
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Dataset class for Firebird datasets
|
212
|
+
class Dataset < Sequel::Dataset
|
213
|
+
include UnsupportedIntersectExcept
|
214
|
+
|
215
|
+
BOOL_TRUE = '1'.freeze
|
216
|
+
BOOL_FALSE = '0'.freeze
|
217
|
+
COMMA_SEPARATOR = ', '.freeze
|
218
|
+
FIREBIRD_TIMESTAMP_FORMAT = "TIMESTAMP '%Y-%m-%d %H:%M:%S".freeze
|
219
|
+
SELECT_CLAUSE_ORDER = %w'distinct limit columns from join where group having compounds order'.freeze
|
220
|
+
|
221
|
+
# Yield all rows returned by executing the given SQL and converting
|
222
|
+
# the types.
|
223
|
+
def fetch_rows(sql, &block)
|
224
|
+
execute(sql) do |s|
|
225
|
+
begin
|
226
|
+
@columns = s.fields.map{|c| output_identifier(c.name)}
|
227
|
+
s.fetchall(:symbols_hash).each do |r|
|
228
|
+
h = {}
|
229
|
+
r.each{|k,v| h[output_identifier(k)] = v}
|
230
|
+
yield h
|
231
|
+
end
|
232
|
+
ensure
|
233
|
+
s.close
|
234
|
+
end
|
235
|
+
end
|
236
|
+
self
|
237
|
+
end
|
238
|
+
|
239
|
+
# Insert given values into the database.
|
240
|
+
def insert(*values)
|
241
|
+
if !@opts[:sql]
|
242
|
+
single_value(default_server_opts(:sql=>insert_returning_pk_sql(*values)))
|
243
|
+
else
|
244
|
+
execute_insert(insert_sql(*values), :table=>opts[:from].first,
|
245
|
+
:values=>values.size == 1 ? values.first : values)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
# Use the RETURNING clause to return the primary key of the inserted record, if it exists
|
250
|
+
def insert_returning_pk_sql(*values)
|
251
|
+
pk = db.primary_key(opts[:from].first)
|
252
|
+
insert_returning_sql(pk ? Sequel::SQL::Identifier.new(pk) : 'NULL'.lit, *values)
|
253
|
+
end
|
254
|
+
|
255
|
+
# Use the RETURNING clause to return the columns listed in returning.
|
256
|
+
def insert_returning_sql(returning, *values)
|
257
|
+
"#{insert_sql(*values)} RETURNING #{column_list(Array(returning))}"
|
258
|
+
end
|
259
|
+
|
260
|
+
# Insert a record returning the record inserted
|
261
|
+
def insert_select(*values)
|
262
|
+
single_record(default_server_opts(:naked=>true, :sql=>insert_returning_sql(nil, *values)))
|
263
|
+
end
|
264
|
+
|
265
|
+
def literal(v)
|
266
|
+
case v
|
267
|
+
when Time, DateTime
|
268
|
+
"#{v.strftime(FIREBIRD_TIMESTAMP_FORMAT)}.#{sprintf("%04d",v.usec / 100)}'"
|
269
|
+
when TrueClass
|
270
|
+
BOOL_TRUE
|
271
|
+
when FalseClass
|
272
|
+
BOOL_FALSE
|
273
|
+
else
|
274
|
+
super
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
# The order of clauses in the SELECT SQL statement
|
279
|
+
def select_clause_order
|
280
|
+
SELECT_CLAUSE_ORDER
|
281
|
+
end
|
282
|
+
|
283
|
+
def select_limit_sql(sql, opts)
|
284
|
+
sql << " FIRST #{opts[:limit]}" if opts[:limit]
|
285
|
+
sql << " SKIP #{opts[:offset]}" if opts[:offset]
|
286
|
+
end
|
287
|
+
|
288
|
+
private
|
289
|
+
|
290
|
+
def hash_row(stmt, row)
|
291
|
+
@columns.inject({}) do |m, c|
|
292
|
+
m[c] = row.shift
|
293
|
+
m
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'informix'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Informix
|
5
|
+
class Database < Sequel::Database
|
6
|
+
set_adapter_scheme :informix
|
7
|
+
|
8
|
+
def connect(server)
|
9
|
+
opts = server_opts(server)
|
10
|
+
::Informix.connect(opts[:database], opts[:user], opts[:password])
|
11
|
+
end
|
12
|
+
|
13
|
+
def dataset(opts = nil)
|
14
|
+
Sequel::Informix::Dataset.new(self, opts)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns number of rows affected
|
18
|
+
def execute_dui(sql, opts={})
|
19
|
+
log_info(sql)
|
20
|
+
synchronize(opts[:server]){|c| c.immediate(sql)}
|
21
|
+
end
|
22
|
+
alias_method :do, :execute_dui
|
23
|
+
|
24
|
+
def execute(sql, opts={})
|
25
|
+
log_info(sql)
|
26
|
+
synchronize(opts[:server]){|c| yield c.cursor(sql)}
|
27
|
+
end
|
28
|
+
alias_method :query, :execute
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def disconnect_connection(c)
|
33
|
+
c.close
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Dataset < Sequel::Dataset
|
38
|
+
include UnsupportedIntersectExcept
|
39
|
+
|
40
|
+
SELECT_CLAUSE_ORDER = %w'limit distinct columns from join where having group compounds order'.freeze
|
41
|
+
|
42
|
+
def literal(v)
|
43
|
+
case v
|
44
|
+
when Time
|
45
|
+
literal(v.iso8601)
|
46
|
+
when Date, DateTime
|
47
|
+
literal(v.to_s)
|
48
|
+
else
|
49
|
+
super
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def fetch_rows(sql, &block)
|
54
|
+
execute(sql) do |cursor|
|
55
|
+
begin
|
56
|
+
col_map = nil
|
57
|
+
cursor.open.each_hash do |h|
|
58
|
+
unless col_map
|
59
|
+
col_map = {}
|
60
|
+
@columns = h.keys.map{|k| col_map[k] = output_identifier(k)}
|
61
|
+
end
|
62
|
+
h2 = {}
|
63
|
+
h.each{|k,v| h2[col_map[k]||k] = v}
|
64
|
+
yield h2
|
65
|
+
end
|
66
|
+
ensure
|
67
|
+
cursor.drop
|
68
|
+
end
|
69
|
+
end
|
70
|
+
self
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def select_clause_order
|
76
|
+
SELECT_CLAUSE_ORDER
|
77
|
+
end
|
78
|
+
|
79
|
+
def select_limit_sql(sql, opts)
|
80
|
+
sql << " SKIP #{opts[:offset]}" if opts[:offset]
|
81
|
+
sql << " FIRST #{opts[:limit]}" if opts[:limit]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Sequel
|
2
|
+
module JDBC
|
3
|
+
# Database and Dataset support for H2 databases accessed via JDBC.
|
4
|
+
module H2
|
5
|
+
# Instance methods for H2 Database objects accessed via JDBC.
|
6
|
+
module DatabaseMethods
|
7
|
+
# H2 needs to add a primary key column as a constraint
|
8
|
+
def alter_table_sql(table, op)
|
9
|
+
case op[:op]
|
10
|
+
when :add_column
|
11
|
+
if op.delete(:primary_key)
|
12
|
+
sql = super(table, op)
|
13
|
+
[sql, "ALTER TABLE #{quote_schema_table(table)} ADD PRIMARY KEY (#{quote_identifier(op[:name])})"]
|
14
|
+
else
|
15
|
+
super(table, op)
|
16
|
+
end
|
17
|
+
else
|
18
|
+
super(table, op)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return Sequel::JDBC::H2::Dataset object with the given opts.
|
23
|
+
def dataset(opts=nil)
|
24
|
+
Sequel::JDBC::H2::Dataset.new(self, opts)
|
25
|
+
end
|
26
|
+
|
27
|
+
# H2 uses an IDENTITY type
|
28
|
+
def serial_primary_key_options
|
29
|
+
{:primary_key => true, :type => :identity}
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
# Use IDENTITY() to get the last inserted id.
|
35
|
+
def last_insert_id(conn, opts={})
|
36
|
+
stmt = conn.createStatement
|
37
|
+
begin
|
38
|
+
rs = stmt.executeQuery('SELECT IDENTITY();')
|
39
|
+
rs.next
|
40
|
+
rs.getInt(1)
|
41
|
+
ensure
|
42
|
+
stmt.close
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Default to a single connection for a memory database.
|
47
|
+
def connection_pool_default_options
|
48
|
+
o = super
|
49
|
+
uri == 'jdbc:h2:mem:' ? o.merge(:max_connections=>1) : o
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Dataset class for H2 datasets accessed via JDBC.
|
54
|
+
class Dataset < JDBC::Dataset
|
55
|
+
# Use H2 syntax for Date, DateTime, and Time types.
|
56
|
+
def literal(v)
|
57
|
+
case v
|
58
|
+
when Date
|
59
|
+
v.strftime("DATE '%Y-%m-%d'")
|
60
|
+
when DateTime, Time
|
61
|
+
v.strftime("TIMESTAMP '%Y-%m-%d %H:%M:%S'")
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|