/
database.rb
1079 lines (951 loc) · 35.9 KB
/
database.rb
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Sequel
# Hash of adapters that have been used. The key is the adapter scheme
# symbol, and the value is the Database subclass.
ADAPTER_MAP = {}
# Array of all databases to which Sequel has connected. If you are
# developing an application that can connect to an arbitrary number of
# databases, delete the database objects from this or they will not get
# garbage collected.
DATABASES = []
# A Database object represents a virtual connection to a database.
# The Database class is meant to be subclassed by database adapters in order
# to provide the functionality needed for executing queries.
class Database
extend Metaprogramming
include Metaprogramming
# Array of supported database adapters
ADAPTERS = %w'ado amalgalite db2 dbi do firebird informix jdbc mysql odbc openbase oracle postgres sqlite'.collect{|x| x.to_sym}
SQL_BEGIN = 'BEGIN'.freeze
SQL_COMMIT = 'COMMIT'.freeze
SQL_RELEASE_SAVEPOINT = 'RELEASE SAVEPOINT autopoint_%d'.freeze
SQL_ROLLBACK = 'ROLLBACK'.freeze
SQL_ROLLBACK_TO_SAVEPOINT = 'ROLLBACK TO SAVEPOINT autopoint_%d'.freeze
SQL_SAVEPOINT = 'SAVEPOINT autopoint_%d'.freeze
TRANSACTION_BEGIN = 'Transaction.begin'.freeze
TRANSACTION_COMMIT = 'Transaction.commit'.freeze
TRANSACTION_ROLLBACK = 'Transaction.rollback'.freeze
POSTGRES_DEFAULT_RE = /\A(?:B?('.*')::[^']+|\((-?\d+(?:\.\d+)?)\))\z/
MSSQL_DEFAULT_RE = /\A(?:\(N?('.*')\)|\(\((-?\d+(?:\.\d+)?)\)\))\z/
MYSQL_TIMESTAMP_RE = /\ACURRENT_(?:DATE|TIMESTAMP)?\z/
STRING_DEFAULT_RE = /\A'(.*)'\z/
# The identifier input method to use by default
@@identifier_input_method = nil
# The identifier output method to use by default
@@identifier_output_method = nil
# Whether to use the single threaded connection pool by default
@@single_threaded = false
# Whether to quote identifiers (columns and tables) by default
@@quote_identifiers = nil
# The default schema to use, generally should be nil.
attr_accessor :default_schema
# Numeric specifying the duration beyond which queries are logged at warn
# level instead of info level.
attr_accessor :log_warn_duration
# Array of SQL loggers to use for this database
attr_accessor :loggers
# The options for this database
attr_reader :opts
# The connection pool for this database
attr_reader :pool
# The prepared statement objects for this database, keyed by name
attr_reader :prepared_statements
# Constructs a new instance of a database connection with the specified
# options hash.
#
# Sequel::Database is an abstract class that is not useful by itself.
#
# Takes the following options:
# * :default_schema : The default schema to use, should generally be nil
# * :disconnection_proc: A proc used to disconnect the connection.
# * :identifier_input_method: A string method symbol to call on identifiers going into the database
# * :identifier_output_method: A string method symbol to call on identifiers coming from the database
# * :loggers : An array of loggers to use.
# * :quote_identifiers : Whether to quote identifiers
# * :single_threaded : Whether to use a single-threaded connection pool
#
# All options given are also passed to the ConnectionPool. If a block
# is given, it is used as the connection_proc for the ConnectionPool.
def initialize(opts = {}, &block)
@opts ||= opts
@opts = connection_pool_default_options.merge(@opts)
@loggers = Array(@opts[:logger]) + Array(@opts[:loggers])
self.log_warn_duration = @opts[:log_warn_duration]
@opts[:disconnection_proc] ||= proc{|conn| disconnect_connection(conn)}
block ||= proc{|server| connect(server)}
@opts[:servers] = {} if @opts[:servers].is_a?(String)
@opts[:single_threaded] = @single_threaded = typecast_value_boolean(@opts.fetch(:single_threaded, @@single_threaded))
@schemas = {}
@default_schema = @opts.fetch(:default_schema, default_schema_default)
@prepared_statements = {}
@transactions = []
@identifier_input_method = nil
@identifier_output_method = nil
@quote_identifiers = nil
@pool = ConnectionPool.get_pool(@opts, &block)
::Sequel::DATABASES.push(self)
end
### Class Methods ###
# The Database subclass for the given adapter scheme.
# Raises Sequel::AdapterNotFound if the adapter
# could not be loaded.
def self.adapter_class(scheme)
scheme = scheme.to_s.gsub('-', '_').to_sym
unless klass = ADAPTER_MAP[scheme]
# attempt to load the adapter file
begin
Sequel.tsk_require "sequel/adapters/#{scheme}"
rescue LoadError => e
raise Sequel.convert_exception_class(e, AdapterNotFound)
end
# make sure we actually loaded the adapter
unless klass = ADAPTER_MAP[scheme]
raise AdapterNotFound, "Could not load #{scheme} adapter"
end
end
klass
end
# Returns the scheme for the Database class.
def self.adapter_scheme
@scheme
end
# Connects to a database. See Sequel.connect.
def self.connect(conn_string, opts = {})
case conn_string
when String
if match = /\A(jdbc|do):/o.match(conn_string)
c = adapter_class(match[1].to_sym)
opts = {:uri=>conn_string}.merge(opts)
else
uri = URI.parse(conn_string)
scheme = uri.scheme
scheme = :dbi if scheme =~ /\Adbi-/
c = adapter_class(scheme)
uri_options = c.send(:uri_to_options, uri)
uri.query.split('&').collect{|s| s.split('=')}.each{|k,v| uri_options[k.to_sym] = v if k && !k.empty?} unless uri.query.to_s.strip.empty?
uri_options.entries.each{|k,v| uri_options[k] = URI.unescape(v) if v.is_a?(String)}
opts = uri_options.merge(opts)
end
when Hash
opts = conn_string.merge(opts)
c = adapter_class(opts[:adapter] || opts['adapter'])
else
raise Error, "Sequel::Database.connect takes either a Hash or a String, given: #{conn_string.inspect}"
end
# process opts a bit
opts = opts.inject({}) do |m, kv| k, v = *kv
k = :user if k.to_s == 'username'
m[k.to_sym] = v
m
end
begin
db = c.new(opts)
db.test_connection if opts[:test] && db.send(:typecast_value_boolean, opts[:test])
result = yield(db) if block_given?
ensure
if block_given?
db.disconnect if db
::Sequel::DATABASES.delete(db)
end
end
block_given? ? result : db
end
# The method to call on identifiers going into the database
def self.identifier_input_method
@@identifier_input_method
end
# Set the method to call on identifiers going into the database
# See Sequel.identifier_input_method=.
def self.identifier_input_method=(v)
@@identifier_input_method = v || ""
end
# The method to call on identifiers coming from the database
def self.identifier_output_method
@@identifier_output_method
end
# Set the method to call on identifiers coming from the database
# See Sequel.identifier_output_method=.
def self.identifier_output_method=(v)
@@identifier_output_method = v || ""
end
# Sets the default quote_identifiers mode for new databases.
# See Sequel.quote_identifiers=.
def self.quote_identifiers=(value)
@@quote_identifiers = value
end
# Sets the default single_threaded mode for new databases.
# See Sequel.single_threaded=.
def self.single_threaded=(value)
@@single_threaded = value
end
### Private Class Methods ###
# Sets the adapter scheme for the Database class. Call this method in
# descendants of Database to allow connection using a URL. For example the
# following:
#
# class Sequel::MyDB::Database < Sequel::Database
# set_adapter_scheme :mydb
# ...
# end
#
# would allow connection using:
#
# Sequel.connect('mydb://user:password@dbserver/mydb')
def self.set_adapter_scheme(scheme) # :nodoc:
@scheme = scheme
ADAPTER_MAP[scheme.to_sym] = self
end
# Converts a uri to an options hash. These options are then passed
# to a newly created database object.
def self.uri_to_options(uri) # :nodoc:
{ :user => uri.user,
:password => uri.password,
:host => uri.host,
:port => uri.port,
:database => (m = /\/(.*)/.match(uri.path)) && (m[1]) }
end
private_class_method :set_adapter_scheme, :uri_to_options
### Instance Methods ###
# Runs the supplied SQL statement string on the database server.
# Alias for run.
def <<(sql)
run(sql)
end
# Returns a dataset from the database. If the first argument is a string,
# the method acts as an alias for Database#fetch, returning a dataset for
# arbitrary SQL:
#
# DB['SELECT * FROM items WHERE name = ?', my_name].all
#
# Otherwise, acts as an alias for Database#from, setting the primary
# table for the dataset:
#
# DB[:items].sql #=> "SELECT * FROM items"
def [](*args)
(String === args.first) ? fetch(*args) : from(*args)
end
# Dynamically add new servers or modify server options at runtime. Also adds new
# servers to the connection pool. Intended for use with master/slave or shard
# configurations where it is useful to add new server hosts at runtime.
#
# servers argument should be a hash with server name symbol keys and hash or
# proc values. If a servers key is already in use, it's value is overridden
# with the value provided.
#
# DB.add_servers(:f=>{:host=>"hash_host_f"})
def add_servers(servers)
@opts[:servers] = @opts[:servers] ? @opts[:servers].merge(servers) : servers
@pool.add_servers(servers.keys)
end
# Call the prepared statement with the given name with the given hash
# of arguments.
def call(ps_name, hash={})
prepared_statements[ps_name].call(hash)
end
# Cast the given type to a literal type
def cast_type_literal(type)
type_literal(:type=>type)
end
# Connects to the database. This method should be overridden by descendants.
def connect(server)
raise NotImplementedError, "#connect should be overridden by adapters"
end
# The database type for this database object, the same as the adapter scheme
# by default. Should be overridden in adapters (especially shared adapters)
# to be the correct type, so that even if two separate Database objects are
# using different adapters you can tell that they are using the same database
# type. Even better, you can tell that two Database objects that are using
# the same adapter are connecting to different database types (think JDBC or
# DataObjects).
def database_type
self.class.adapter_scheme
end
# Returns a blank dataset for this database
def dataset
ds = Sequel::Dataset.new(self)
end
# Disconnects all available connections from the connection pool. Any
# connections currently in use will not be disconnected. Options:
# * :servers - Should be a symbol specifing the server to disconnect from,
# or an array of symbols to specify multiple servers.
def disconnect(opts = {})
pool.disconnect(opts)
end
# Yield a new database object for every server in the connection pool.
# Intended for use in sharded environments where there is a need to make schema
# modifications (DDL queries) on each shard.
#
# DB.each_server{|db| db.create_table(:users){primary_key :id; String :name}}
def each_server(&block)
servers.each{|s| self.class.connect(server_opts(s), &block)}
end
# Executes the given SQL on the database. This method should be overridden in descendants.
# This method should not be called directly by user code.
def execute(sql, opts={})
raise NotImplementedError, "#execute should be overridden by adapters"
end
# Method that should be used when submitting any DDL (Data Definition
# Language) SQL. By default, calls execute_dui.
# This method should not be called directly by user code.
def execute_ddl(sql, opts={}, &block)
execute_dui(sql, opts, &block)
end
# Method that should be used when issuing a DELETE, UPDATE, or INSERT
# statement. By default, calls execute.
# This method should not be called directly by user code.
def execute_dui(sql, opts={}, &block)
execute(sql, opts, &block)
end
# Method that should be used when issuing a INSERT
# statement. By default, calls execute_dui.
# This method should not be called directly by user code.
def execute_insert(sql, opts={}, &block)
execute_dui(sql, opts, &block)
end
# Fetches records for an arbitrary SQL statement. If a block is given,
# it is used to iterate over the records:
#
# DB.fetch('SELECT * FROM items'){|r| p r}
#
# The method returns a dataset instance:
#
# DB.fetch('SELECT * FROM items').all
#
# Fetch can also perform parameterized queries for protection against SQL
# injection:
#
# DB.fetch('SELECT * FROM items WHERE name = ?', my_name).all
def fetch(sql, *args, &block)
ds = dataset.with_sql(sql, *args)
ds.each(&block) if block
ds
end
# Returns a new dataset with the from method invoked. If a block is given,
# it is used as a filter on the dataset.
def from(*args, &block)
ds = dataset.from(*args)
block ? ds.filter(&block) : ds
end
# Returns a single value from the database, e.g.:
#
# # SELECT 1
# DB.get(1) #=> 1
#
# # SELECT version()
# DB.get(:version.sql_function) #=> ...
def get(*args, &block)
dataset.get(*args, &block)
end
# The method to call on identifiers going into the database
def identifier_input_method
case @identifier_input_method
when nil
@identifier_input_method = @opts.fetch(:identifier_input_method, (@@identifier_input_method.nil? ? identifier_input_method_default : @@identifier_input_method))
@identifier_input_method == "" ? nil : @identifier_input_method
when ""
nil
else
@identifier_input_method
end
end
# Set the method to call on identifiers going into the database
def identifier_input_method=(v)
reset_schema_utility_dataset
@identifier_input_method = v || ""
end
# The method to call on identifiers coming from the database
def identifier_output_method
case @identifier_output_method
when nil
@identifier_output_method = @opts.fetch(:identifier_output_method, (@@identifier_output_method.nil? ? identifier_output_method_default : @@identifier_output_method))
@identifier_output_method == "" ? nil : @identifier_output_method
when ""
nil
else
@identifier_output_method
end
end
# Set the method to call on identifiers coming from the database
def identifier_output_method=(v)
reset_schema_utility_dataset
@identifier_output_method = v || ""
end
# Returns a string representation of the database object including the
# class name and the connection URI (or the opts if the URI
# cannot be constructed).
def inspect
"#<#{self.class}: #{(uri rescue opts).inspect}>"
end
# Proxy the literal call to the dataset, used for default values.
def literal(v)
schema_utility_dataset.literal(v)
end
# Log a message at level info to all loggers.
def log_info(message, args=nil)
log_each(:info, args ? "#{message}; #{args.inspect}" : message)
end
# Yield to the block, logging any errors at error level to all loggers,
# and all other queries with the duration at warn or info level.
def log_yield(sql, args=nil)
return yield if @loggers.empty?
sql = "#{sql}; #{args.inspect}" if args
start = Time.now
begin
yield
rescue => e
log_each(:error, "#{e.class}: #{e.message.strip}: #{sql}")
raise
ensure
log_duration(Time.now - start, sql) unless e
end
end
# Remove any existing loggers and just use the given logger.
def logger=(logger)
@loggers = Array(logger)
end
# Whether to quote identifiers (columns and tables) for this database
def quote_identifiers=(v)
reset_schema_utility_dataset
@quote_identifiers = v
end
# Returns true if the database quotes identifiers.
def quote_identifiers?
return @quote_identifiers unless @quote_identifiers.nil?
@quote_identifiers = @opts.fetch(:quote_identifiers, (@@quote_identifiers.nil? ? quote_identifiers_default : @@quote_identifiers))
end
# Dynamically remove existing servers from the connection pool. Intended for
# use with master/slave or shard configurations where it is useful to remove
# existing server hosts at runtime.
#
# servers should be symbols or arrays of symbols. If a nonexistent server
# is specified, it is ignored. If no servers have been specified for
# this database, no changes are made. If you attempt to remove the :default server,
# an error will be raised.
#
# DB.remove_servers(:f1, :f2)
def remove_servers(*servers)
if @opts[:servers] && !@opts[:servers].empty?
servs = @opts[:servers].dup
servers.flatten!
servers.each{|s| servs.delete(s)}
@opts[:servers] = servs
@pool.remove_servers(servers)
end
end
# Runs the supplied SQL statement string on the database server. Returns nil.
# Options:
# * :server - The server to run the SQL on.
def run(sql, opts={})
execute_ddl(sql, opts)
nil
end
# Parse the schema from the database.
# Returns the schema for the given table as an array with all members being arrays of length 2,
# the first member being the column name, and the second member being a hash of column information.
# Available options are:
#
# * :reload - Get fresh information from the database, instead of using
# cached information. If table_name is blank, :reload should be used
# unless you are sure that schema has not been called before with a
# table_name, otherwise you may only getting the schemas for tables
# that have been requested explicitly.
# * :schema - An explicit schema to use. It may also be implicitly provided
# via the table name.
def schema(table, opts={})
raise(Error, 'schema parsing is not implemented on this database') unless respond_to?(:schema_parse_table, true)
sch, table_name = schema_and_table(table)
quoted_name = quote_schema_table(table)
opts = opts.merge(:schema=>sch) if sch && !opts.include?(:schema)
@schemas.delete(quoted_name) if opts[:reload]
return @schemas[quoted_name] if @schemas[quoted_name]
cols = schema_parse_table(table_name, opts)
raise(Error, 'schema parsing returned no columns, table probably doesn\'t exist') if cols.nil? || cols.empty?
cols.each{|_,c| c[:ruby_default] = column_schema_to_ruby_default(c[:default], c[:type])}
@schemas[quoted_name] = cols
end
# Returns a new dataset with the select method invoked.
def select(*args, &block)
dataset.select(*args, &block)
end
# An array of servers/shards for this Database object.
def servers
pool.servers
end
# Returns true if the database is using a single-threaded connection pool.
def single_threaded?
@single_threaded
end
# Acquires a database connection, yielding it to the passed block.
def synchronize(server=nil, &block)
@pool.hold(server || :default, &block)
end
# Whether the database and adapter support savepoints, false by default
def supports_savepoints?
false
end
# Returns true if a table with the given name exists. This requires a query
# to the database.
def table_exists?(name)
begin
from(name).first
true
rescue
false
end
end
# Attempts to acquire a database connection. Returns true if successful.
# Will probably raise an error if unsuccessful.
def test_connection(server=nil)
synchronize(server){|conn|}
true
end
# Starts a database transaction. When a database transaction is used,
# either all statements are successful or none of the statements are
# successful. Note that MySQL MyISAM tabels do not support transactions.
#
# The following options are respected:
#
# * :server - The server to use for the transaction
# * :savepoint - Whether to create a new savepoint for this transaction,
# only respected if the database adapter supports savepoints. By
# default Sequel will reuse an existing transaction, so if you want to
# use a savepoint you must use this option.
def transaction(opts={}, &block)
synchronize(opts[:server]) do |conn|
return yield(conn) if already_in_transaction?(conn, opts)
_transaction(conn, &block)
end
end
# Typecast the value to the given column_type. Calls
# typecast_value_#{column_type} if the method exists,
# otherwise returns the value.
# This method should raise Sequel::InvalidValue if assigned value
# is invalid.
def typecast_value(column_type, value)
return nil if value.nil?
meth = "typecast_value_#{column_type}"
begin
respond_to?(meth, true) ? send(meth, value) : value
rescue ArgumentError, TypeError => e
raise Sequel.convert_exception_class(e, InvalidValue)
end
end
# Returns the URI identifying the database.
# This method can raise an error if the database used options
# instead of a connection string.
def uri
uri = URI::Generic.new(
self.class.adapter_scheme.to_s,
nil,
@opts[:host],
@opts[:port],
nil,
"/#{@opts[:database]}",
nil,
nil,
nil
)
uri.user = @opts[:user]
uri.password = @opts[:password] if uri.user
uri.to_s
end
# Explicit alias of uri for easier subclassing.
def url
uri
end
private
# Internal generic transaction method. Any exception raised by the given
# block will cause the transaction to be rolled back. If the exception is
# not Sequel::Rollback, the error will be reraised. If no exception occurs
# inside the block, the transaction is commited.
def _transaction(conn)
begin
add_transaction
t = begin_transaction(conn)
yield(conn)
rescue Exception => e
rollback_transaction(t) if t
transaction_error(e)
ensure
begin
commit_transaction(t) unless e
rescue Exception => e
raise_error(e, :classes=>database_error_classes)
ensure
remove_transaction(t)
end
end
end
# Add the current thread to the list of active transactions
def add_transaction
th = Thread.current
if supports_savepoints?
unless @transactions.include?(th)
th[:sequel_transaction_depth] = 0
@transactions << th
end
else
@transactions << th
end
end
# Whether the current thread/connection is already inside a transaction
def already_in_transaction?(conn, opts)
@transactions.include?(Thread.current) && (!supports_savepoints? || !opts[:savepoint])
end
# SQL to start a new savepoint
def begin_savepoint_sql(depth)
SQL_SAVEPOINT % depth
end
# Start a new database transaction on the given connection.
def begin_transaction(conn)
if supports_savepoints?
th = Thread.current
depth = th[:sequel_transaction_depth]
conn = transaction_statement_object(conn) if respond_to?(:transaction_statement_object, true)
log_connection_execute(conn, depth > 0 ? begin_savepoint_sql(depth) : begin_transaction_sql)
th[:sequel_transaction_depth] += 1
else
log_connection_execute(conn, begin_transaction_sql)
end
conn
end
# SQL to BEGIN a transaction.
def begin_transaction_sql
SQL_BEGIN
end
# Returns true when the object is considered blank.
# The only objects that are blank are nil, false,
# strings with all whitespace, and ones that respond
# true to empty?
def blank_object?(obj)
return obj.blank? if obj.respond_to?(:blank?)
case obj
when NilClass, FalseClass
true
when Numeric, TrueClass
false
when String
obj.strip.empty?
else
obj.respond_to?(:empty?) ? obj.empty? : false
end
end
# Convert the given default, which should be a database specific string, into
# a ruby object.
def column_schema_to_ruby_default(default, type)
return if default.nil?
orig_default = default
if database_type == :postgres and m = POSTGRES_DEFAULT_RE.match(default)
default = m[1] || m[2]
end
if database_type == :mssql and m = MSSQL_DEFAULT_RE.match(default)
default = m[1] || m[2]
end
if [:string, :blob, :date, :datetime, :time, :enum].include?(type)
if database_type == :mysql
return if [:date, :datetime, :time].include?(type) && MYSQL_TIMESTAMP_RE.match(default)
orig_default = default = "'#{default.gsub("'", "''").gsub('\\', '\\\\')}'"
end
return unless m = STRING_DEFAULT_RE.match(default)
default = m[1].gsub("''", "'")
end
res = begin
case type
when :boolean
case default
when /[f0]/i
false
when /[t1]/i
true
end
when :string, :enum
default
when :blob
Sequel::SQL::Blob.new(default)
when :integer
Integer(default)
when :float
Float(default)
when :date
Sequel.string_to_date(default)
when :datetime
DateTime.parse(default)
when :time
Sequel.string_to_time(default)
when :decimal
BigDecimal.new(default)
end
rescue
nil
end
end
# SQL to commit a savepoint
def commit_savepoint_sql(depth)
SQL_RELEASE_SAVEPOINT % depth
end
# Commit the active transaction on the connection
def commit_transaction(conn)
if supports_savepoints?
depth = Thread.current[:sequel_transaction_depth]
log_connection_execute(conn, depth > 1 ? commit_savepoint_sql(depth-1) : commit_transaction_sql)
else
log_connection_execute(conn, commit_transaction_sql)
end
end
# SQL to COMMIT a transaction.
def commit_transaction_sql
SQL_COMMIT
end
# Method called on the connection object to execute SQL on the database,
# used by the transaction code.
def connection_execute_method
:execute
end
# The default options for the connection pool.
def connection_pool_default_options
{}
end
# Which transaction errors to translate, blank by default.
def database_error_classes
[]
end
# The default value for default_schema.
def default_schema_default
nil
end
# The method to apply to identifiers going into the database by default.
# Should be overridden in subclasses for databases that fold unquoted
# identifiers to lower case instead of uppercase, such as
# MySQL, PostgreSQL, and SQLite.
def identifier_input_method_default
:upcase
end
# The method to apply to identifiers coming the database by default.
# Should be overridden in subclasses for databases that fold unquoted
# identifiers to lower case instead of uppercase, such as
# MySQL, PostgreSQL, and SQLite.
def identifier_output_method_default
:downcase
end
# Return a Method object for the dataset's output_identifier_method.
# Used in metadata parsing to make sure the returned information is in the
# correct format.
def input_identifier_meth
dataset.method(:input_identifier)
end
# Log the given SQL and then execute it on the connection, used by
# the transaction code.
def log_connection_execute(conn, sql)
log_yield(sql){conn.send(connection_execute_method, sql)}
end
# Log message with message prefixed by duration at info level, or
# warn level if duration is greater than log_warn_duration.
def log_duration(duration, message)
log_each((lwd = log_warn_duration and duration >= lwd) ? :warn : :info, "(#{sprintf('%0.6fs', duration)}) #{message}")
end
# Log message at level (which should be :error, :warn, or :info)
# to all loggers.
def log_each(level, message)
@loggers.each{|logger| logger.send(level, message)}
end
# Return a dataset that uses the default identifier input and output methods
# for this database. Used when parsing metadata so that column symbols are
# returned as expected.
def metadata_dataset
return @metadata_dataset if @metadata_dataset
ds = dataset
ds.identifier_input_method = identifier_input_method_default
ds.identifier_output_method = identifier_output_method_default
@metadata_dataset = ds
end
# Return a Method object for the dataset's output_identifier_method.
# Used in metadata parsing to make sure the returned information is in the
# correct format.
def output_identifier_meth
dataset.method(:output_identifier)
end
# Whether to quote identifiers by default for this database, true
# by default.
def quote_identifiers_default
true
end
# SQL to ROLLBACK a transaction.
def rollback_transaction_sql
SQL_ROLLBACK
end
# Convert the given exception to a DatabaseError, keeping message
# and traceback.
def raise_error(exception, opts={})
if !opts[:classes] || Array(opts[:classes]).any?{|c| exception.is_a?(c)}
raise Sequel.convert_exception_class(exception, opts[:disconnect] ? DatabaseDisconnectError : DatabaseError)
else
raise exception
end
end
# Remove the cached schema for the given schema name
def remove_cached_schema(table)
@schemas.delete(quote_schema_table(table)) if @schemas
end
# Remove the current thread from the list of active transactions
def remove_transaction(conn)
th = Thread.current
@transactions.delete(th) if !supports_savepoints? || ((th[:sequel_transaction_depth] -= 1) <= 0)
end
# Remove the cached schema_utility_dataset, because the identifier
# quoting has changed.
def reset_schema_utility_dataset
@schema_utility_dataset = nil
end
# SQL to rollback to a savepoint
def rollback_savepoint_sql(depth)
SQL_ROLLBACK_TO_SAVEPOINT % depth
end
# Rollback the active transaction on the connection
def rollback_transaction(conn)
if supports_savepoints?
depth = Thread.current[:sequel_transaction_depth]
log_connection_execute(conn, depth > 1 ? rollback_savepoint_sql(depth-1) : rollback_transaction_sql)
else
log_connection_execute(conn, rollback_transaction_sql)
end
end
# Split the schema information from the table
def schema_and_table(table_name)
schema_utility_dataset.schema_and_table(table_name)
end
# Return true if the given column schema represents an autoincrementing primary key.
def schema_autoincrementing_primary_key?(schema)
!!schema[:primary_key]
end
# Match the database's column type to a ruby type via a
# regular expression. The following ruby types are supported:
# integer, string, date, datetime, boolean, and float.
def schema_column_type(db_type)
case db_type
when /\Ainterval\z/io
:interval
when /\A(character( varying)?|n?(var)?char|n?text)/io
:string
when /\A(int(eger)?|(big|small|tiny)int)/io
:integer
when /\Adate\z/io
:date
when /\A((small)?datetime|timestamp( with(out)? time zone)?)\z/io
:datetime
when /\Atime( with(out)? time zone)?\z/io
:time
when /\A(boolean|bit)\z/io
:boolean
when /\A(real|float|double( precision)?)\z/io
:float
when /\A(?:(?:(?:num(?:ber|eric)?|decimal)(?:\(\d+,\s*(\d+)\))?)|(?:small)?money)\z/io
$1 && $1 == '0' ? :integer : :decimal
when /bytea|blob|image|(var)?binary/io
:blob
when /\Aenum/io
:enum
end
end
# The dataset to use for proxying certain schema methods.
def schema_utility_dataset
@schema_utility_dataset ||= dataset
end
# Return the options for the given server by merging the generic
# options for all server with the specific options for the given
# server specified in the :servers option.
def server_opts(server)
opts = if @opts[:servers] && server_options = @opts[:servers][server]
case server_options
when Hash
@opts.merge(server_options)
when Proc
@opts.merge(server_options.call(self))
else
raise Error, 'Server opts should be a hash or proc'
end
else
@opts.dup
end
opts.delete(:servers)
opts
end
# Raise a database error unless the exception is an Rollback.
def transaction_error(e)
raise_error(e, :classes=>database_error_classes) unless e.is_a?(Rollback)
end
# Typecast the value to an SQL::Blob
def typecast_value_blob(value)
value.is_a?(Sequel::SQL::Blob) ? value : Sequel::SQL::Blob.new(value)
end
# Typecast the value to true, false, or nil