sequel 2.11.0 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/CHANGELOG +168 -0
  2. data/README.rdoc +77 -95
  3. data/Rakefile +100 -80
  4. data/bin/sequel +2 -1
  5. data/doc/advanced_associations.rdoc +23 -32
  6. data/doc/cheat_sheet.rdoc +23 -40
  7. data/doc/dataset_filtering.rdoc +6 -6
  8. data/doc/prepared_statements.rdoc +22 -22
  9. data/doc/release_notes/2.12.0.txt +534 -0
  10. data/doc/schema.rdoc +3 -1
  11. data/doc/sharding.rdoc +8 -8
  12. data/doc/virtual_rows.rdoc +65 -0
  13. data/lib/sequel.rb +1 -1
  14. data/lib/{sequel_core → sequel}/adapters/ado.rb +3 -3
  15. data/lib/{sequel_core → sequel}/adapters/db2.rb +0 -0
  16. data/lib/{sequel_core → sequel}/adapters/dbi.rb +1 -1
  17. data/lib/{sequel_core → sequel}/adapters/do.rb +9 -5
  18. data/lib/{sequel_core → sequel}/adapters/do/mysql.rb +1 -1
  19. data/lib/{sequel_core → sequel}/adapters/do/postgres.rb +1 -1
  20. data/lib/{sequel_core → sequel}/adapters/do/sqlite.rb +1 -1
  21. data/lib/{sequel_core → sequel}/adapters/firebird.rb +84 -80
  22. data/lib/{sequel_core → sequel}/adapters/informix.rb +1 -1
  23. data/lib/{sequel_core → sequel}/adapters/jdbc.rb +21 -14
  24. data/lib/{sequel_core → sequel}/adapters/jdbc/h2.rb +14 -13
  25. data/lib/{sequel_core → sequel}/adapters/jdbc/mysql.rb +1 -1
  26. data/lib/{sequel_core → sequel}/adapters/jdbc/oracle.rb +1 -1
  27. data/lib/{sequel_core → sequel}/adapters/jdbc/postgresql.rb +1 -1
  28. data/lib/{sequel_core → sequel}/adapters/jdbc/sqlite.rb +1 -1
  29. data/lib/{sequel_core → sequel}/adapters/mysql.rb +60 -39
  30. data/lib/{sequel_core → sequel}/adapters/odbc.rb +8 -4
  31. data/lib/{sequel_core → sequel}/adapters/openbase.rb +0 -0
  32. data/lib/{sequel_core → sequel}/adapters/oracle.rb +38 -7
  33. data/lib/{sequel_core → sequel}/adapters/postgres.rb +24 -24
  34. data/lib/{sequel_core → sequel}/adapters/shared/mssql.rb +5 -5
  35. data/lib/{sequel_core → sequel}/adapters/shared/mysql.rb +126 -71
  36. data/lib/{sequel_core → sequel}/adapters/shared/oracle.rb +7 -10
  37. data/lib/{sequel_core → sequel}/adapters/shared/postgres.rb +159 -125
  38. data/lib/{sequel_core → sequel}/adapters/shared/progress.rb +1 -2
  39. data/lib/{sequel_core → sequel}/adapters/shared/sqlite.rb +72 -67
  40. data/lib/{sequel_core → sequel}/adapters/sqlite.rb +11 -7
  41. data/lib/{sequel_core → sequel}/adapters/utils/date_format.rb +0 -0
  42. data/lib/{sequel_core → sequel}/adapters/utils/stored_procedures.rb +0 -0
  43. data/lib/{sequel_core → sequel}/adapters/utils/unsupported.rb +19 -0
  44. data/lib/{sequel_core → sequel}/connection_pool.rb +7 -5
  45. data/lib/sequel/core.rb +221 -0
  46. data/lib/{sequel_core → sequel}/core_sql.rb +91 -49
  47. data/lib/{sequel_core → sequel}/database.rb +264 -149
  48. data/lib/{sequel_core/schema/generator.rb → sequel/database/schema_generator.rb} +6 -2
  49. data/lib/{sequel_core/database/schema.rb → sequel/database/schema_methods.rb} +12 -12
  50. data/lib/sequel/database/schema_sql.rb +224 -0
  51. data/lib/{sequel_core → sequel}/dataset.rb +78 -236
  52. data/lib/{sequel_core → sequel}/dataset/convenience.rb +99 -61
  53. data/lib/{sequel_core/object_graph.rb → sequel/dataset/graph.rb} +16 -14
  54. data/lib/{sequel_core → sequel}/dataset/prepared_statements.rb +1 -1
  55. data/lib/{sequel_core → sequel}/dataset/sql.rb +150 -99
  56. data/lib/sequel/deprecated.rb +593 -0
  57. data/lib/sequel/deprecated_migration.rb +91 -0
  58. data/lib/sequel/exceptions.rb +48 -0
  59. data/lib/sequel/extensions/blank.rb +42 -0
  60. data/lib/{sequel_model → sequel/extensions}/inflector.rb +8 -1
  61. data/lib/{sequel_core → sequel/extensions}/migration.rb +1 -1
  62. data/lib/{sequel_core/dataset → sequel/extensions}/pagination.rb +0 -0
  63. data/lib/{sequel_core → sequel/extensions}/pretty_table.rb +7 -0
  64. data/lib/{sequel_core/dataset → sequel/extensions}/query.rb +7 -0
  65. data/lib/sequel/extensions/string_date_time.rb +47 -0
  66. data/lib/sequel/metaprogramming.rb +43 -0
  67. data/lib/sequel/model.rb +110 -0
  68. data/lib/sequel/model/associations.rb +1300 -0
  69. data/lib/sequel/model/base.rb +937 -0
  70. data/lib/sequel/model/deprecated.rb +204 -0
  71. data/lib/sequel/model/deprecated_hooks.rb +103 -0
  72. data/lib/sequel/model/deprecated_inflector.rb +335 -0
  73. data/lib/sequel/model/deprecated_validations.rb +388 -0
  74. data/lib/sequel/model/errors.rb +39 -0
  75. data/lib/{sequel_model → sequel/model}/exceptions.rb +4 -4
  76. data/lib/sequel/model/inflections.rb +208 -0
  77. data/lib/sequel/model/plugins.rb +76 -0
  78. data/lib/sequel/plugins/caching.rb +122 -0
  79. data/lib/sequel/plugins/hook_class_methods.rb +122 -0
  80. data/lib/sequel/plugins/schema.rb +53 -0
  81. data/lib/sequel/plugins/serialization.rb +117 -0
  82. data/lib/sequel/plugins/single_table_inheritance.rb +63 -0
  83. data/lib/sequel/plugins/validation_class_methods.rb +384 -0
  84. data/lib/sequel/plugins/validation_helpers.rb +150 -0
  85. data/lib/{sequel_core → sequel}/sql.rb +125 -190
  86. data/lib/{sequel_core → sequel}/version.rb +2 -1
  87. data/lib/sequel_core.rb +1 -172
  88. data/lib/sequel_model.rb +1 -91
  89. data/spec/adapters/firebird_spec.rb +5 -5
  90. data/spec/adapters/informix_spec.rb +1 -1
  91. data/spec/adapters/mysql_spec.rb +128 -42
  92. data/spec/adapters/oracle_spec.rb +47 -19
  93. data/spec/adapters/postgres_spec.rb +64 -52
  94. data/spec/adapters/spec_helper.rb +1 -1
  95. data/spec/adapters/sqlite_spec.rb +12 -17
  96. data/spec/{sequel_core → core}/connection_pool_spec.rb +10 -10
  97. data/spec/{sequel_core → core}/core_ext_spec.rb +19 -19
  98. data/spec/{sequel_core → core}/core_sql_spec.rb +68 -71
  99. data/spec/{sequel_core → core}/database_spec.rb +135 -99
  100. data/spec/{sequel_core → core}/dataset_spec.rb +398 -242
  101. data/spec/{sequel_core → core}/expression_filters_spec.rb +13 -13
  102. data/spec/core/migration_spec.rb +263 -0
  103. data/spec/{sequel_core → core}/object_graph_spec.rb +10 -10
  104. data/spec/{sequel_core → core}/pretty_table_spec.rb +2 -2
  105. data/spec/{sequel_core → core}/schema_generator_spec.rb +0 -0
  106. data/spec/{sequel_core → core}/schema_spec.rb +8 -10
  107. data/spec/{sequel_core → core}/spec_helper.rb +29 -2
  108. data/spec/{sequel_core → core}/version_spec.rb +0 -0
  109. data/spec/extensions/blank_spec.rb +67 -0
  110. data/spec/extensions/caching_spec.rb +201 -0
  111. data/spec/{sequel_model/hooks_spec.rb → extensions/hook_class_methods_spec.rb} +8 -23
  112. data/spec/{sequel_model → extensions}/inflector_spec.rb +3 -0
  113. data/spec/{sequel_core → extensions}/migration_spec.rb +4 -4
  114. data/spec/extensions/pagination_spec.rb +99 -0
  115. data/spec/extensions/pretty_table_spec.rb +91 -0
  116. data/spec/extensions/query_spec.rb +85 -0
  117. data/spec/{sequel_model → extensions}/schema_spec.rb +22 -1
  118. data/spec/extensions/serialization_spec.rb +109 -0
  119. data/spec/extensions/single_table_inheritance_spec.rb +53 -0
  120. data/spec/{sequel_model → extensions}/spec_helper.rb +13 -4
  121. data/spec/extensions/string_date_time_spec.rb +93 -0
  122. data/spec/{sequel_model/validations_spec.rb → extensions/validation_class_methods_spec.rb} +15 -103
  123. data/spec/extensions/validation_helpers_spec.rb +291 -0
  124. data/spec/integration/dataset_test.rb +31 -0
  125. data/spec/integration/eager_loader_test.rb +17 -30
  126. data/spec/integration/schema_test.rb +8 -5
  127. data/spec/integration/spec_helper.rb +17 -0
  128. data/spec/integration/transaction_test.rb +68 -0
  129. data/spec/{sequel_model → model}/association_reflection_spec.rb +0 -0
  130. data/spec/{sequel_model → model}/associations_spec.rb +23 -10
  131. data/spec/{sequel_model → model}/base_spec.rb +29 -20
  132. data/spec/{sequel_model → model}/caching_spec.rb +16 -14
  133. data/spec/{sequel_model → model}/dataset_methods_spec.rb +0 -0
  134. data/spec/{sequel_model → model}/eager_loading_spec.rb +8 -8
  135. data/spec/model/hooks_spec.rb +472 -0
  136. data/spec/model/inflector_spec.rb +126 -0
  137. data/spec/{sequel_model → model}/model_spec.rb +25 -20
  138. data/spec/model/plugins_spec.rb +142 -0
  139. data/spec/{sequel_model → model}/record_spec.rb +121 -62
  140. data/spec/model/schema_spec.rb +92 -0
  141. data/spec/model/spec_helper.rb +124 -0
  142. data/spec/model/validations_spec.rb +1080 -0
  143. metadata +136 -107
  144. data/lib/sequel_core/core_ext.rb +0 -217
  145. data/lib/sequel_core/dataset/callback.rb +0 -13
  146. data/lib/sequel_core/dataset/schema.rb +0 -15
  147. data/lib/sequel_core/deprecated.rb +0 -26
  148. data/lib/sequel_core/exceptions.rb +0 -44
  149. data/lib/sequel_core/schema.rb +0 -2
  150. data/lib/sequel_core/schema/sql.rb +0 -325
  151. data/lib/sequel_model/association_reflection.rb +0 -267
  152. data/lib/sequel_model/associations.rb +0 -499
  153. data/lib/sequel_model/base.rb +0 -539
  154. data/lib/sequel_model/caching.rb +0 -82
  155. data/lib/sequel_model/dataset_methods.rb +0 -26
  156. data/lib/sequel_model/eager_loading.rb +0 -370
  157. data/lib/sequel_model/hooks.rb +0 -101
  158. data/lib/sequel_model/plugins.rb +0 -62
  159. data/lib/sequel_model/record.rb +0 -568
  160. data/lib/sequel_model/schema.rb +0 -49
  161. data/lib/sequel_model/validations.rb +0 -429
  162. data/spec/sequel_model/plugins_spec.rb +0 -80
@@ -1,4 +1,4 @@
1
- require 'sequel_core/adapters/utils/unsupported'
1
+ Sequel.require 'adapters/utils/unsupported'
2
2
  require 'informix'
3
3
 
4
4
  module Sequel
@@ -1,5 +1,5 @@
1
1
  require 'java'
2
- require 'sequel_core/adapters/utils/stored_procedures'
2
+ Sequel.require 'adapters/utils/stored_procedures'
3
3
 
4
4
  module Sequel
5
5
  # Houses Sequel's JDBC support when running on JRuby.
@@ -35,35 +35,35 @@ module Sequel
35
35
  # Contains procs keyed on sub adapter type that extend the
36
36
  # given database object so it supports the correct database type.
37
37
  DATABASE_SETUP = {:postgresql=>proc do |db|
38
- require 'sequel_core/adapters/jdbc/postgresql'
38
+ Sequel.require 'adapters/jdbc/postgresql'
39
39
  db.extend(Sequel::JDBC::Postgres::DatabaseMethods)
40
40
  JDBC.load_gem('postgres')
41
41
  org.postgresql.Driver
42
42
  end,
43
43
  :mysql=>proc do |db|
44
- require 'sequel_core/adapters/jdbc/mysql'
44
+ Sequel.require 'adapters/jdbc/mysql'
45
45
  db.extend(Sequel::JDBC::MySQL::DatabaseMethods)
46
46
  JDBC.load_gem('mysql')
47
47
  com.mysql.jdbc.Driver
48
48
  end,
49
49
  :sqlite=>proc do |db|
50
- require 'sequel_core/adapters/jdbc/sqlite'
50
+ Sequel.require 'adapters/jdbc/sqlite'
51
51
  db.extend(Sequel::JDBC::SQLite::DatabaseMethods)
52
52
  JDBC.load_gem('sqlite3')
53
53
  org.sqlite.JDBC
54
54
  end,
55
55
  :oracle=>proc do |db|
56
- require 'sequel_core/adapters/jdbc/oracle'
56
+ Sequel.require 'adapters/jdbc/oracle'
57
57
  db.extend(Sequel::JDBC::Oracle::DatabaseMethods)
58
58
  Java::oracle.jdbc.driver.OracleDriver
59
59
  end,
60
60
  :sqlserver=>proc do |db|
61
- require 'sequel_core/adapters/shared/mssql'
61
+ Sequel.require 'adapters/shared/mssql'
62
62
  db.extend(Sequel::MSSQL::DatabaseMethods)
63
63
  com.microsoft.sqlserver.jdbc.SQLServerDriver
64
64
  end,
65
65
  :h2=>proc do |db|
66
- require 'sequel_core/adapters/jdbc/h2'
66
+ Sequel.require 'adapters/jdbc/h2'
67
67
  db.extend(Sequel::JDBC::H2::DatabaseMethods)
68
68
  JDBC.load_gem('h2')
69
69
  org.h2.Driver
@@ -146,7 +146,7 @@ module Sequel
146
146
  # statement or something else that returns rows.
147
147
  def execute(sql, opts={}, &block)
148
148
  return call_sproc(sql, opts, &block) if opts[:sproc]
149
- return execute_prepared_statement(sql, opts, &block) if sql.is_one_of?(Symbol, Dataset)
149
+ return execute_prepared_statement(sql, opts, &block) if [Symbol, Dataset].any?{|c| sql.is_a?(c)}
150
150
  log_info(sql)
151
151
  synchronize(opts[:server]) do |conn|
152
152
  stmt = conn.createStatement
@@ -196,8 +196,12 @@ module Sequel
196
196
  # Default transaction method that should work on most JDBC
197
197
  # databases. Does not use the JDBC transaction methods, uses
198
198
  # SQL BEGIN/ROLLBACK/COMMIT statements instead.
199
- def transaction(server=nil)
200
- synchronize(server) do |conn|
199
+ def transaction(opts={})
200
+ unless opts.is_a?(Hash)
201
+ Deprecation.deprecate('Passing an argument other than a Hash to Database#transaction', "Use DB.transaction(:server=>#{opts.inspect})")
202
+ opts = {:server=>opts}
203
+ end
204
+ synchronize(opts[:server]) do |conn|
201
205
  return yield(conn) if @transactions.include?(Thread.current)
202
206
  stmt = conn.createStatement
203
207
  begin
@@ -334,7 +338,9 @@ module Sequel
334
338
  # and set it to an empty hash. This is used to store
335
339
  # adapter specific prepared statements.
336
340
  def setup_connection(conn)
337
- conn.meta_eval{attr_accessor :prepared_statements}
341
+ class << conn
342
+ attr_accessor :prepared_statements
343
+ end
338
344
  conn.prepared_statements = {}
339
345
  conn
340
346
  end
@@ -432,9 +438,9 @@ module Sequel
432
438
  def convert_type(v)
433
439
  case v
434
440
  when Java::JavaSQL::Timestamp, Java::JavaSQL::Time
435
- v.to_string.to_sequel_time
441
+ Sequel.string_to_datetime(v.to_string)
436
442
  when Java::JavaSQL::Date
437
- v.to_string.to_date
443
+ Sequel.string_to_date(v.to_string)
438
444
  when Java::JavaIo::BufferedReader
439
445
  lines = []
440
446
  while(line = v.read_line) do lines << line end
@@ -458,7 +464,7 @@ module Sequel
458
464
  meta = result.getMetaData
459
465
  column_count = meta.getColumnCount
460
466
  @columns = []
461
- column_count.times{|i| @columns << output_identifier(meta.getColumnName(i+1))}
467
+ column_count.times{|i| @columns << output_identifier(meta.getColumnLabel(i+1))}
462
468
 
463
469
  # get rows
464
470
  while result.next
@@ -474,6 +480,7 @@ end
474
480
  class Java::JavaSQL::Timestamp
475
481
  # Add a usec method in order to emulate Time values.
476
482
  def usec
483
+ Deprecation.deprecate('Java::JavaSQL::Timestamp#usec', 'Use timestamp.getNanos/1000')
477
484
  getNanos/1000
478
485
  end
479
486
  end
@@ -1,4 +1,4 @@
1
- require 'sequel_core/adapters/utils/date_format'
1
+ Sequel.require %w'date_format unsupported', 'adapters/utils'
2
2
 
3
3
  module Sequel
4
4
  module JDBC
@@ -6,6 +6,18 @@ module Sequel
6
6
  module H2
7
7
  # Instance methods for H2 Database objects accessed via JDBC.
8
8
  module DatabaseMethods
9
+ # Return Sequel::JDBC::H2::Dataset object with the given opts.
10
+ def dataset(opts=nil)
11
+ Sequel::JDBC::H2::Dataset.new(self, opts)
12
+ end
13
+
14
+ # H2 uses an IDENTITY type
15
+ def serial_primary_key_options
16
+ {:primary_key => true, :type => :identity}
17
+ end
18
+
19
+ private
20
+
9
21
  # H2 needs to add a primary key column as a constraint
10
22
  def alter_table_sql(table, op)
11
23
  case op[:op]
@@ -21,18 +33,6 @@ module Sequel
21
33
  end
22
34
  end
23
35
 
24
- # Return Sequel::JDBC::H2::Dataset object with the given opts.
25
- def dataset(opts=nil)
26
- Sequel::JDBC::H2::Dataset.new(self, opts)
27
- end
28
-
29
- # H2 uses an IDENTITY type
30
- def serial_primary_key_options
31
- {:primary_key => true, :type => :identity}
32
- end
33
-
34
- private
35
-
36
36
  # Use IDENTITY() to get the last inserted id.
37
37
  def last_insert_id(conn, opts={})
38
38
  stmt = conn.createStatement
@@ -55,6 +55,7 @@ module Sequel
55
55
  # Dataset class for H2 datasets accessed via JDBC.
56
56
  class Dataset < JDBC::Dataset
57
57
  include Dataset::SQLStandardDateFormat
58
+ include Dataset::UnsupportedIsTrue
58
59
  end
59
60
  end
60
61
  end
@@ -1,4 +1,4 @@
1
- require 'sequel_core/adapters/shared/mysql'
1
+ Sequel.require 'adapters/shared/mysql'
2
2
 
3
3
  module Sequel
4
4
  module JDBC
@@ -1,4 +1,4 @@
1
- require 'sequel_core/adapters/shared/oracle'
1
+ Sequel.require 'adapters/shared/oracle'
2
2
 
3
3
  module Sequel
4
4
  module JDBC
@@ -1,4 +1,4 @@
1
- require 'sequel_core/adapters/shared/postgres'
1
+ Sequel.require 'adapters/shared/postgres'
2
2
 
3
3
  module Sequel
4
4
  Postgres::CONVERTED_EXCEPTIONS << NativeException
@@ -1,4 +1,4 @@
1
- require 'sequel_core/adapters/shared/sqlite'
1
+ Sequel.require 'adapters/shared/sqlite'
2
2
 
3
3
  module Sequel
4
4
  module JDBC
@@ -1,40 +1,55 @@
1
1
  require 'mysql'
2
- require 'sequel_core/adapters/shared/mysql'
3
- require 'sequel_core/adapters/utils/stored_procedures'
2
+ Sequel.require %w'shared/mysql utils/stored_procedures', 'adapters'
4
3
 
5
4
  module Sequel
6
5
  # Module for holding all MySQL-related classes and modules for Sequel.
6
+ #
7
+ # A class level convert_invalid_date_time accessor exists if
8
+ # the native adapter is used. If set to nil or :nil, the adapter treats dates
9
+ # like 0000-00-00 and times like 838:00:00 as nil values. If set to :string,
10
+ # it returns the strings as is. If is false by default, which means that
11
+ # invalid dates and times will raise errors.
7
12
  module MySQL
8
- # Mapping of type numbers to conversion methods.
9
- MYSQL_TYPES = {
10
- 0 => :to_d, # MYSQL_TYPE_DECIMAL
11
- 1 => :to_i, # MYSQL_TYPE_TINY
12
- 2 => :to_i, # MYSQL_TYPE_SHORT
13
- 3 => :to_i, # MYSQL_TYPE_LONG
14
- 4 => :to_f, # MYSQL_TYPE_FLOAT
15
- 5 => :to_f, # MYSQL_TYPE_DOUBLE
16
- # 6 => ??, # MYSQL_TYPE_NULL
17
- 7 => :to_sequel_time, # MYSQL_TYPE_TIMESTAMP
18
- 8 => :to_i, # MYSQL_TYPE_LONGLONG
19
- 9 => :to_i, # MYSQL_TYPE_INT24
20
- 10 => :to_date, # MYSQL_TYPE_DATE
21
- 11 => :to_time, # MYSQL_TYPE_TIME
22
- 12 => :to_sequel_time, # MYSQL_TYPE_DATETIME
23
- 13 => :to_i, # MYSQL_TYPE_YEAR
24
- 14 => :to_date, # MYSQL_TYPE_NEWDATE
25
- # 15 => :to_s # MYSQL_TYPE_VARCHAR
26
- # 16 => :to_s, # MYSQL_TYPE_BIT
27
- 246 => :to_d, # MYSQL_TYPE_NEWDECIMAL
28
- 247 => :to_i, # MYSQL_TYPE_ENUM
29
- 248 => :to_i, # MYSQL_TYPE_SET
30
- 249 => :to_sequel_blob, # MYSQL_TYPE_TINY_BLOB
31
- 250 => :to_sequel_blob, # MYSQL_TYPE_MEDIUM_BLOB
32
- 251 => :to_sequel_blob, # MYSQL_TYPE_LONG_BLOB
33
- 252 => :to_sequel_blob, # MYSQL_TYPE_BLOB
34
- # 253 => :to_s, # MYSQL_TYPE_VAR_STRING
35
- # 254 => :to_s, # MYSQL_TYPE_STRING
36
- # 255 => :to_s # MYSQL_TYPE_GEOMETRY
13
+ # Mapping of type numbers to conversion procs
14
+ MYSQL_TYPES = {}
15
+
16
+ # Use only a single proc for each type to save on memory
17
+ MYSQL_TYPE_PROCS = {
18
+ [0, 246] => lambda{|v| BigDecimal.new(v)}, # decimal
19
+ [1, 2, 3, 8, 9, 13, 247, 248] => lambda{|v| v.to_i}, # integer
20
+ [4, 5] => lambda{|v| v.to_f}, # float
21
+ [10, 14] => lambda{|v| convert_date_time(:string_to_date, v)}, # date
22
+ [7, 12] => lambda{|v| convert_date_time(:string_to_datetime, v)}, # datetime
23
+ [11] => lambda{|v| convert_date_time(:string_to_time, v)}, # time
24
+ [249, 250, 251, 252] => lambda{|v| Sequel::SQL::Blob.new(v)} # blob
37
25
  }
26
+ MYSQL_TYPE_PROCS.each do |k,v|
27
+ k.each{|n| MYSQL_TYPES[n] = v}
28
+ end
29
+
30
+ @convert_invalid_date_time = false
31
+
32
+ class << self
33
+ attr_accessor :convert_invalid_date_time
34
+ end
35
+
36
+ # If convert_invalid_date_time is nil, :nil, or :string and
37
+ # the conversion raises an InvalidValue exception, return v
38
+ # if :string and nil otherwise.
39
+ def self.convert_date_time(meth, v)
40
+ begin
41
+ Sequel.send(meth, v)
42
+ rescue InvalidValue
43
+ case @convert_invalid_date_time
44
+ when nil, :nil
45
+ nil
46
+ when :string
47
+ v
48
+ else
49
+ raise
50
+ end
51
+ end
52
+ end
38
53
 
39
54
  # Database class for MySQL databases used with Sequel.
40
55
  class Database < Sequel::Database
@@ -88,7 +103,9 @@ module Sequel
88
103
  conn.query("set SQL_AUTO_IS_NULL=0") unless opts[:auto_is_null]
89
104
 
90
105
  conn.query_with_result = false
91
- conn.meta_eval{attr_accessor :prepared_statements}
106
+ class << conn
107
+ attr_accessor :prepared_statements
108
+ end
92
109
  conn.prepared_statements = {}
93
110
  conn.reconnect = true
94
111
  conn
@@ -117,8 +134,12 @@ module Sequel
117
134
  end
118
135
 
119
136
  # Support single level transactions on MySQL.
120
- def transaction(server=nil)
121
- synchronize(server) do |conn|
137
+ def transaction(opts={})
138
+ unless opts.is_a?(Hash)
139
+ Deprecation.deprecate('Passing an argument other than a Hash to Database#transaction', "Use DB.transaction(:server=>#{opts.inspect})")
140
+ opts = {:server=>opts}
141
+ end
142
+ synchronize(opts[:server]) do |conn|
122
143
  return yield(conn) if @transactions.include?(Thread.current)
123
144
  log_info(begin_transaction_sql)
124
145
  conn.query(begin_transaction_sql)
@@ -276,8 +297,8 @@ module Sequel
276
297
  end
277
298
 
278
299
  # Delete rows matching this dataset
279
- def delete(opts = nil)
280
- execute_dui(delete_sql(opts)){|c| c.affected_rows}
300
+ def delete(opts = (defarg=true;nil))
301
+ execute_dui(defarg ? delete_sql : delete_sql(opts)){|c| c.affected_rows}
281
302
  end
282
303
 
283
304
  # Yield all rows matching this dataset
@@ -317,8 +338,8 @@ module Sequel
317
338
  end
318
339
 
319
340
  # Update the matching rows.
320
- def update(*args)
321
- execute_dui(update_sql(*args)){|c| c.affected_rows}
341
+ def update(values={}, opts=(defarg=true;nil))
342
+ execute_dui(defarg ? update_sql(values) : update_sql(values, opts)){|c| c.affected_rows}
322
343
  end
323
344
 
324
345
  private
@@ -331,7 +352,7 @@ module Sequel
331
352
  # a method to an ancestor of Fixnum
332
353
  v.to_i == 0 ? false : true
333
354
  else
334
- (t = MYSQL_TYPES[type]) ? v.send(t) : v
355
+ (b = MYSQL_TYPES[type]) ? b.call(v) : v
335
356
  end
336
357
  else
337
358
  nil
@@ -12,10 +12,10 @@ module Sequel
12
12
  super(opts)
13
13
  case opts[:db_type]
14
14
  when 'mssql'
15
- require 'sequel_core/adapters/shared/mssql'
15
+ Sequel.require 'adapters/shared/mssql'
16
16
  extend Sequel::MSSQL::DatabaseMethods
17
17
  when 'progress'
18
- require 'sequel_core/adapters/shared/progress'
18
+ Sequel.require 'adapters/shared/progress'
19
19
  extend Sequel::Progress::DatabaseMethods
20
20
  end
21
21
  end
@@ -64,8 +64,12 @@ module Sequel
64
64
  alias_method :do, :execute_dui
65
65
 
66
66
  # Support single level transactions on ODBC
67
- def transaction(server=nil)
68
- synchronize(server) do |conn|
67
+ def transaction(opts={})
68
+ unless opts.is_a?(Hash)
69
+ Deprecation.deprecate('Passing an argument other than a Hash to Database#transaction', "Use DB.transaction(:server=>#{opts.inspect})")
70
+ opts = {:server=>opts}
71
+ end
72
+ synchronize(opts[:server]) do |conn|
69
73
  return yield(conn) if @transactions.include?(Thread.current)
70
74
  log_info(begin_transaction_sql)
71
75
  conn.do(begin_transaction_sql)
File without changes
@@ -1,5 +1,5 @@
1
1
  require 'oci8'
2
- require 'sequel_core/adapters/shared/oracle'
2
+ Sequel.require 'adapters/shared/oracle'
3
3
 
4
4
  module Sequel
5
5
  module Oracle
@@ -30,7 +30,35 @@ module Sequel
30
30
  def dataset(opts = nil)
31
31
  Oracle::Dataset.new(self, opts)
32
32
  end
33
-
33
+
34
+ def schema_parse_table(table, opts={})
35
+ ds = dataset
36
+ ds.identifier_output_method = :downcase
37
+ schema, table = schema_and_table(table)
38
+ table_schema = []
39
+ metadata = transaction(opts){|conn| conn.describe_table(table.to_s)}
40
+ metadata.columns.each do |column|
41
+ table_schema << [
42
+ column.name.downcase.to_sym,
43
+ {
44
+ :type => column.data_type,
45
+ :db_type => column.type_string.split(' ')[0],
46
+ :type_string => column.type_string,
47
+ :charset_form => column.charset_form,
48
+ :char_used => column.char_used?,
49
+ :char_size => column.char_size,
50
+ :data_size => column.data_size,
51
+ :precision => column.precision,
52
+ :scale => column.scale,
53
+ :fsprecision => column.fsprecision,
54
+ :lfprecision => column.lfprecision,
55
+ :allow_null => column.nullable?
56
+ }
57
+ ]
58
+ end
59
+ table_schema
60
+ end
61
+
34
62
  def execute(sql, opts={})
35
63
  log_info(sql)
36
64
  synchronize(opts[:server]) do |conn|
@@ -47,19 +75,22 @@ module Sequel
47
75
  end
48
76
  end
49
77
  end
50
- alias_method :do, :execute
78
+ alias do execute
51
79
 
52
- def transaction(server=nil)
53
- synchronize(server) do |conn|
80
+ def transaction(opts={})
81
+ unless opts.is_a?(Hash)
82
+ Deprecation.deprecate('Passing an argument other than a Hash to Database#transaction', "Use DB.transaction(:server=>#{opts.inspect})")
83
+ opts = {:server=>opts}
84
+ end
85
+ synchronize(opts[:server]) do |conn|
54
86
  return yield(conn) if @transactions.include?(Thread.current)
55
-
56
87
  conn.autocommit = false
57
88
  begin
58
89
  @transactions << Thread.current
59
90
  yield(conn)
60
91
  rescue => e
61
92
  conn.rollback
62
- raise e unless Error::Rollback === e
93
+ raise e unless Rollback === e
63
94
  ensure
64
95
  conn.commit unless e
65
96
  conn.autocommit = true
@@ -1,4 +1,4 @@
1
- require 'sequel_core/adapters/shared/postgres'
1
+ Sequel.require 'adapters/shared/postgres'
2
2
 
3
3
  begin
4
4
  require 'pg'
@@ -41,7 +41,7 @@ rescue LoadError => e
41
41
  def escape_bytea(obj)
42
42
  self.class.encode_bytea(obj)
43
43
  end
44
- metaalias :unescape_bytea, :decode_bytea
44
+ instance_eval{alias unescape_bytea decode_bytea}
45
45
  rescue
46
46
  # If no valid bytea escaping method can be found, create one that
47
47
  # raises an error
@@ -83,31 +83,31 @@ module Sequel
83
83
  CONVERTED_EXCEPTIONS << PGError
84
84
 
85
85
  # Hash with integer keys and proc values for converting PostgreSQL types.
86
- PG_TYPES = {
87
- 16 => lambda{ |s| s == 't' }, # boolean
88
- 17 => lambda{ |s| ::Sequel::SQL::Blob.new(Adapter.unescape_bytea(s)) }, # bytea
89
- 20 => lambda{ |s| s.to_i }, # int8
90
- 21 => lambda{ |s| s.to_i }, # int2
91
- 22 => lambda{ |s| s.to_i }, # int2vector
92
- 23 => lambda{ |s| s.to_i }, # int4
93
- 26 => lambda{ |s| s.to_i }, # oid
94
- 700 => lambda{ |s| s.to_f }, # float4
95
- 701 => lambda{ |s| s.to_f }, # float8
96
- 790 => lambda{ |s| s.to_d }, # money
97
- 1082 => lambda{ |s| @use_iso_date_format ? Date.new(*s.split("-").map{|x| x.to_i}) : s.to_date }, # date
98
- 1083 => lambda{ |s| s.to_time }, # time without time zone
99
- 1114 => lambda{ |s| s.to_sequel_time }, # timestamp without time zone
100
- 1184 => lambda{ |s| s.to_sequel_time }, # timestamp with time zone
101
- 1266 => lambda{ |s| s.to_time }, # time with time zone
102
- 1700 => lambda{ |s| s.to_d }, # numeric
86
+ PG_TYPES = {}
87
+
88
+ # Use a single proc for each type to conserve memory
89
+ PG_TYPE_PROCS = {
90
+ [16] => lambda{|s| s == 't'}, # boolean
91
+ [17] => lambda{|s| ::Sequel::SQL::Blob.new(Adapter.unescape_bytea(s))}, # bytea
92
+ [20, 21, 22, 23, 26] => lambda{|s| s.to_i}, # integer
93
+ [700, 701] => lambda{|s| s.to_f}, # float
94
+ [790, 1700] => lambda{|s| BigDecimal.new(s)}, # numeric
95
+ [1082] => lambda{|s| @use_iso_date_format ? Date.new(*s.split("-").map{|x| x.to_i}) : Sequel.string_to_date(s)}, # date
96
+ [1083, 1266] => lambda{|s| Sequel.string_to_time(s)}, # time
97
+ [1114, 1184] => lambda{|s| Sequel.string_to_datetime(s)}, # timestamp
103
98
  }
99
+ PG_TYPE_PROCS.each do |k,v|
100
+ k.each{|n| PG_TYPES[n] = v}
101
+ end
104
102
 
105
103
  @use_iso_date_format = true
106
104
 
107
- # As an optimization, Sequel sets the date style to ISO, so that PostgreSQL provides
108
- # the date in a known format that Sequel can parse faster. This can be turned off
109
- # if you require a date style other than ISO.
110
- metaattr_accessor :use_iso_date_format
105
+ class << self
106
+ # As an optimization, Sequel sets the date style to ISO, so that PostgreSQL provides
107
+ # the date in a known format that Sequel can parse faster. This can be turned off
108
+ # if you require a date style other than ISO.
109
+ attr_accessor :use_iso_date_format
110
+ end
111
111
 
112
112
  # PGconn subclass for connection specific methods used with the
113
113
  # pg, postgres, or postgres-pr driver.
@@ -195,7 +195,7 @@ module Sequel
195
195
  def connect(server)
196
196
  opts = server_opts(server)
197
197
  conn = Adapter.connect(
198
- (opts[:host] unless opts[:host].blank?),
198
+ (opts[:host] unless blank_object?(opts[:host])),
199
199
  opts[:port] || 5432,
200
200
  nil, '',
201
201
  opts[:database],