sequel 5.51.0 → 5.54.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +44 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -0
  5. data/doc/opening_databases.rdoc +1 -1
  6. data/doc/release_notes/5.52.0.txt +87 -0
  7. data/doc/release_notes/5.53.0.txt +23 -0
  8. data/doc/release_notes/5.54.0.txt +27 -0
  9. data/doc/testing.rdoc +3 -1
  10. data/lib/sequel/adapters/amalgalite.rb +3 -5
  11. data/lib/sequel/adapters/jdbc/h2.rb +55 -10
  12. data/lib/sequel/adapters/jdbc.rb +12 -14
  13. data/lib/sequel/adapters/mysql.rb +80 -67
  14. data/lib/sequel/adapters/mysql2.rb +53 -48
  15. data/lib/sequel/adapters/postgres.rb +17 -21
  16. data/lib/sequel/adapters/shared/mysql.rb +3 -2
  17. data/lib/sequel/adapters/shared/postgres.rb +2 -2
  18. data/lib/sequel/adapters/sqlite.rb +16 -18
  19. data/lib/sequel/adapters/utils/mysql_mysql2.rb +1 -1
  20. data/lib/sequel/connection_pool/sharded_single.rb +5 -7
  21. data/lib/sequel/connection_pool/single.rb +6 -8
  22. data/lib/sequel/core.rb +17 -18
  23. data/lib/sequel/database/query.rb +1 -1
  24. data/lib/sequel/extensions/core_refinements.rb +36 -11
  25. data/lib/sequel/extensions/date_parse_input_handler.rb +67 -0
  26. data/lib/sequel/extensions/datetime_parse_to_time.rb +5 -1
  27. data/lib/sequel/extensions/pg_array_ops.rb +2 -2
  28. data/lib/sequel/extensions/pg_hstore_ops.rb +1 -1
  29. data/lib/sequel/extensions/pg_inet_ops.rb +1 -1
  30. data/lib/sequel/extensions/pg_interval.rb +1 -0
  31. data/lib/sequel/extensions/pg_json.rb +3 -5
  32. data/lib/sequel/extensions/pg_json_ops.rb +1 -1
  33. data/lib/sequel/extensions/pg_range_ops.rb +1 -1
  34. data/lib/sequel/extensions/pg_row_ops.rb +1 -1
  35. data/lib/sequel/extensions/s.rb +2 -1
  36. data/lib/sequel/extensions/server_block.rb +8 -12
  37. data/lib/sequel/extensions/sql_comments.rb +110 -3
  38. data/lib/sequel/extensions/string_date_time.rb +19 -23
  39. data/lib/sequel/model/base.rb +8 -12
  40. data/lib/sequel/plugins/column_encryption.rb +1 -1
  41. data/lib/sequel/plugins/enum.rb +124 -0
  42. data/lib/sequel/plugins/sql_comments.rb +189 -0
  43. data/lib/sequel/plugins/subclasses.rb +28 -11
  44. data/lib/sequel/plugins/unused_associations.rb +2 -2
  45. data/lib/sequel/timezones.rb +12 -14
  46. data/lib/sequel/version.rb +1 -1
  47. metadata +12 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ebee8a266be02f6ba3c1d5d340b606336dbb0fb74beb4b4333eb3c930cd59b5c
4
- data.tar.gz: f657a1aaedf3e0ef8f644097ee94760dfb04381730dd0c6f0a53c57e3f2bc742
3
+ metadata.gz: 3baaadc192f621e371699d654e998661db242d11385b328e5298ebfa7ce97d53
4
+ data.tar.gz: b6595670122d1ec5bbaccabef8d3af58a08f4d6edb6ffa782e34a1152d971143
5
5
  SHA512:
6
- metadata.gz: 9a0aac448e73636a0c20e5e4d87d65ac648a56bb4267034c5b92fdf2cf297aeeaff408dc4e79c6fe05bdd113e22824a5d7bc7a5b236b44346cda6ed427a377c1
7
- data.tar.gz: d99a2b488a0735ba9012e4dfcaae30bec76fce1e7d3c34a14be9395241432a3d00ec5fa7a22df371810adf734e0c39230d212934f10a7d39c5839e58231d6625
6
+ metadata.gz: ebf7ff5a43c42d769a1f35e7c90f32c24e7776cde36c17c1800a4603b8173066bb90864b0644dfb6402221e1ed37ea36a9b026e6ab07e3086ed208eb6fb58d67
7
+ data.tar.gz: 82b525b77248038a08e2b247ae8de8028a1d4f67f332b267bd81f9d4087ebd900fcb58b1a7b9b82b20e3b93a00dd3a665d03dbe583ba94866c43663993c7436b
data/CHANGELOG CHANGED
@@ -1,3 +1,47 @@
1
+ === 5.54.0 (2022-03-01)
2
+
3
+ * Add enum plugin for treating columns as enums in a model (jeremyevans) (#1839)
4
+
5
+ === 5.53.0 (2022-02-01)
6
+
7
+ * Make Dataset#_sql_comment private when using the Database sql_comments extension (jeremyevans)
8
+
9
+ * Fix prepared statements in the mysql2 adapter to reuse native prepared statements (jeremyevans) (#1832)
10
+
11
+ * Support H2 version 2+ in the jdbc/h2 adapter (jeremyevans) (#1817)
12
+
13
+ * Work around active_support breaking subclasses plugin on Ruby <3.1 (jeremyevans) (#1816)
14
+
15
+ * Fix error handling if trying to setup column_encryption plugin without keys (jeremyevans) (#1815)
16
+
17
+ === 5.52.0 (2022-01-01)
18
+
19
+ * Use Class#subclasses if available in the subclasses plugin, instead of a custom Model.subclasses accessor (jeremyevans)
20
+
21
+ * Add Model.descendants and .freeze_descendants to subclasses plugin (jeremyevans)
22
+
23
+ * Avoid use of deprecated Refinement#include on Ruby 3.1+ (jeremyevans)
24
+
25
+ * Add date_parse_input_handler extension for custom handling of input to date parsing methods (jeremyevans)
26
+
27
+ * Make postgres adapter respect Database#default_string_column_size (jeremyevans)
28
+
29
+ * Make pg_interval extension work with ActiveSupport 7.0 (jeremyevans)
30
+
31
+ * Make :ruby_default schema entry for type: :datetime respect Sequel.datetime_class (jeremyevans)
32
+
33
+ * Make alter_table drop_constraint have an effect on MySQL 8.0.19+ (jeremyevans)
34
+
35
+ * Make mysql adapter support ruby-mysql 3 API (jeremyevans) (#1795)
36
+
37
+ * Make mysql adapter no longer use connection's server_version, since it isn't accurate when using the ruby-mysql driver (jeremyevans)
38
+
39
+ * Add sql_comments plugin for automatically including comments on queries generated by model class, instance, and dataset methods (jeremyevans)
40
+
41
+ * Make sql_comments Database extension support Database#with_comments, for automatically including comments for queries executed inside the block (jeremyevans)
42
+
43
+ * Fix sql_comments extension to not modify cached SQL for a dataset (jeremyevans)
44
+
1
45
  === 5.51.0 (2021-12-01)
2
46
 
3
47
  * Make eager loading via tactical_eager_loading no longer modify objects who already have a cached value for the association (jeremyevans)
data/MIT-LICENSE CHANGED
@@ -1,5 +1,5 @@
1
1
  Copyright (c) 2007-2008 Sharon Rosner
2
- Copyright (c) 2008-2021 Jeremy Evans
2
+ Copyright (c) 2008-2022 Jeremy Evans
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to
data/README.rdoc CHANGED
@@ -880,6 +880,11 @@ raise an error by default:
880
880
  end
881
881
  end
882
882
 
883
+ == Testing Sequel
884
+
885
+ Please see the {testing guide}[rdoc-ref:doc/testing.rdoc] for recommendations on testing
886
+ applications that use Sequel, as well as the how to run the tests for Sequel itself.
887
+
883
888
  == Sequel Release Policy
884
889
 
885
890
  New major versions of Sequel do not have a defined release policy, but historically have
@@ -249,7 +249,7 @@ jdbc-mysql :: Depending on the configuration of the MySQL server, jdbc-mysql ver
249
249
 
250
250
  Requires: mysql
251
251
 
252
- The MySQL adapter does not support the pure-ruby mysql.rb driver, it requires the C-extension driver.
252
+ This should work with the mysql gem (C extension) and the ruby-mysql gem (pure ruby).
253
253
 
254
254
  The following additional options are supported:
255
255
 
@@ -0,0 +1,87 @@
1
+ = New Features
2
+
3
+ * When the sql_comments Database extension is used,
4
+ Database#with_comments is now added, which can be used for including
5
+ comments for all queries executed inside a given block. This can
6
+ be useful if you want to analyze database query logs, and want to
7
+ group all related queries:
8
+
9
+ DB.with_comments(model: Album, action: :all) do
10
+ DB[:albums].all
11
+ # SELECT * FROM albums -- model:Album,action:all
12
+ end
13
+
14
+ * An sql_comments plugin has been added, which will automatically
15
+ add SQL comments for all queries generated by model class, instance
16
+ and dataset methods:
17
+
18
+ Album.plugin :sql_comments
19
+
20
+ album = Album[1]
21
+ # SELECT * FROM albums WHERE (id = 1) LIMIT 1
22
+ # -- model:Album,method_type:class,method:[]
23
+
24
+ album.update(:name=>'A')
25
+ # UPDATE albums SET name = 'baz' WHERE (id = 1)
26
+ # -- model:Album,method_type:instance,method:update
27
+
28
+ Album.where(id: 1).delete
29
+ # DELETE FROM albums WHERE (id = 1)
30
+ # -- model:Album,method_type:dataset,method:delete
31
+
32
+ This plugin requires you have loaded the sql_comments Database
33
+ extension into the related Database before use.
34
+
35
+ * A date_parse_input_handler extension has been added to support
36
+ custom handling of input to date parsing methods. Among other
37
+ things, you can use this to limit the length of strings that
38
+ will be parsed, which can prevent ArgumentErrors in newer Ruby
39
+ versions:
40
+
41
+ Sequel.extension :date_parse_input_handler
42
+ Sequel.date_parse_input_handler do |string|
43
+ string.b[0, 128]
44
+ end
45
+
46
+ = Other Improvements
47
+
48
+ * On Ruby 3.1, the core_refinements extension now avoids the
49
+ deprecated Refinement#include, switching to
50
+ Refinement#import_methods.
51
+
52
+ * On Ruby 3.1, the subclasses plugin will use Ruby's native support
53
+ for Class#subclasses.
54
+
55
+ * The subclasses plugin has renamed descendents to descendants and
56
+ freeze_descendents to freeze_descendants. The previous method
57
+ names are still available as aliases.
58
+
59
+ * The :ruby_default schema entry for datetime/timestamp columns now
60
+ respects Sequel.datetime_class. Previously, the value for the
61
+ :ruby_default schema entry would always be a DateTime value for
62
+ such columns.
63
+
64
+ * The pg_interval extension now works with ActiveSupport 7.0.
65
+
66
+ * The shared postgres adapter now respects
67
+ Database#default_string_column_size for setting the size of string
68
+ columns that don't use text as the database type.
69
+
70
+ * Database#supports_check_constraints? now returns true on MySQL
71
+ 8.0.19+. This fixes drop_constraint in certain cases when combining
72
+ the constraint dropping with other changes in the same alter_table
73
+ block.
74
+
75
+ * The mysql adapter now supports the ruby-mysql 3 API (ruby-mysql
76
+ is a pure-ruby MySQL driver).
77
+
78
+ * The mysql adapter no longer uses the connection's server_version
79
+ method if it is defined, as the method does not return the
80
+ correct value when using the ruby-mysql driver with MariaDB.
81
+
82
+ * Comments added by the sql_comments extension no longer modify
83
+ cached SQL for a dataset.
84
+
85
+ = Other
86
+
87
+ * This is Sequel's 250th release!
@@ -0,0 +1,23 @@
1
+ = Improvements
2
+
3
+ * The jdbc/h2 subadapter now supports H2 version 2.0. It continues to
4
+ support H2 versions 1.3 and 1.4.
5
+
6
+ * The mysql2 adapter's prepared statement support now reuses existing
7
+ native prepared statements, instead of only binding variables on
8
+ newly prepared statements. This was the intended behavior
9
+ previously, and should result in increased performance in cases
10
+ where preparing a query takes significant time.
11
+
12
+ * The subclasses plugin now ignores an existing Class#subclasses
13
+ method if it is defined in Ruby. This fixes cases where usage of
14
+ ActiveSupport would break the subclasses plugin.
15
+
16
+ * Database#call_sproc in the jdbc adapter will now always close the
17
+ prepared call it creates. Before, if there was an exception raised
18
+ when setting the arguments for the prepared call, the prepared call
19
+ would not be closed.
20
+
21
+ * A more appropriate error is now issued if you try to use the
22
+ column_encryption plugin to encrypt a column without setting up an
23
+ encryption key.
@@ -0,0 +1,27 @@
1
+ = New Feature
2
+
3
+ * An enum plugin has been added. This plugin allows you to create
4
+ model-level enums, giving names to underlying values of a column.
5
+ For example:
6
+
7
+ Album.plugin :enum
8
+ Album.enum :status_id, good: 1, bad: 2
9
+
10
+ Adds Album#good! and Album#bad! for changing the status_id to 1 or
11
+ 2 respectively. It adds Album#good? and Album#bad? for checking
12
+ whether the status_id is 1 or 2 respectively. It overrides
13
+ Album#status_id to return :good or :bad instead of 1 or 2,
14
+ respectively, and overrides Album#status_id= to accept :good or
15
+ :bad instead of 1 or 2 respectively.
16
+
17
+ Additionally, it adds good and bad dataset methods for filtering
18
+ the model's dataset to records where status_id is 1 or 2
19
+ respectively. It also adds not_good and not_bad dataset methods
20
+ for filtering the model's dataset to records where status_id is not
21
+ 1 or not 2 respectively.
22
+
23
+ You can use :prefix and :suffix options when calling enum to
24
+ add a prefix or suffix to the method names created. You can
25
+ set the :override_accessors option to false to not override
26
+ the accessor methods for the column, and set the :dataset_methods
27
+ option to false to not add dataset methods.
data/doc/testing.rdoc CHANGED
@@ -113,7 +113,7 @@ The order in which you delete/truncate the tables is important if you are using
113
113
 
114
114
  = Testing Sequel Itself
115
115
 
116
- Sequel has multiple separate test suites. All test suites use minitest/spec, with the minitest-hooks and minitest-shared_description extensions.
116
+ Sequel has multiple separate test suites. All test suites use minitest/spec, with the minitest-hooks, minitest-global_expectations, and minitest-shared_description extensions. To install the dependencies necessary to test Sequel, run <tt>gem install --development sequel</tt>.
117
117
 
118
118
  == rake
119
119
 
@@ -145,6 +145,8 @@ The <tt>spec_<i>adapter</i></tt> specs run against a real database connection wi
145
145
 
146
146
  These specs are broken down into two parts. For each database, there are specific specs that only apply to that database, and these are called the adapter specs. There are also shared specs that apply to all (or almost all) databases, these are called the integration specs. For database types that don't have specific adapter tests, you can use <tt>rake spec_integration</tt> to just run the shared integration tests.
147
147
 
148
+ Each adapter needs a specific gem installed in order to run. Please see the {connecting to a database guide}[rdoc-ref:doc/opening_databases.rdoc] for which gem you need to install for the adapter you are testing.
149
+
148
150
  == Environment variables
149
151
 
150
152
  Sequel uses environment variables when testing to specify either the database to be tested or specify how testing should be done. You can also specify the databases to test by copying <tt>spec/spec_config.rb.example</tt> to <tt>spec/spec_config.rb</tt> and modifying it. See that file for details. It may be necessary to use +spec_config.rb+ as opposed to an environment variable if your database connection cannot be specified by a connection string.
@@ -118,11 +118,9 @@ module Sequel
118
118
  # Yield an available connection. Rescue
119
119
  # any Amalgalite::Errors and turn them into DatabaseErrors.
120
120
  def _execute(sql, opts)
121
- begin
122
- synchronize(opts[:server]){|conn| yield conn}
123
- rescue ::Amalgalite::Error, ::Amalgalite::SQLite3::Error => e
124
- raise_error(e)
125
- end
121
+ synchronize(opts[:server]){|conn| yield conn}
122
+ rescue ::Amalgalite::Error, ::Amalgalite::SQLite3::Error => e
123
+ raise_error(e)
126
124
  end
127
125
 
128
126
  # The Amagalite adapter does not need the pool to convert exceptions.
@@ -24,6 +24,7 @@ module Sequel
24
24
 
25
25
  def freeze
26
26
  h2_version
27
+ version2?
27
28
  super
28
29
  end
29
30
 
@@ -140,13 +141,36 @@ module Sequel
140
141
  DATABASE_ERROR_REGEXPS
141
142
  end
142
143
 
143
- # Use IDENTITY() to get the last inserted id.
144
+ def execute_statement_insert(stmt, sql)
145
+ stmt.executeUpdate(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS)
146
+ end
147
+
148
+ def prepare_jdbc_statement(conn, sql, opts)
149
+ opts[:type] == :insert ? conn.prepareStatement(sql, JavaSQL::Statement::RETURN_GENERATED_KEYS) : super
150
+ end
151
+
152
+ # Get the last inserted id using getGeneratedKeys, scope_identity, or identity.
144
153
  def last_insert_id(conn, opts=OPTS)
145
- statement(conn) do |stmt|
146
- sql = 'SELECT IDENTITY();'
147
- rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)}
148
- rs.next
149
- rs.getLong(1)
154
+ if stmt = opts[:stmt]
155
+ rs = stmt.getGeneratedKeys
156
+ begin
157
+ if rs.next
158
+ begin
159
+ rs.getLong(1)
160
+ rescue
161
+ rs.getObject(1) rescue nil
162
+ end
163
+ end
164
+ ensure
165
+ rs.close
166
+ end
167
+ elsif !version2?
168
+ statement(conn) do |stmt|
169
+ sql = 'SELECT IDENTITY()'
170
+ rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)}
171
+ rs.next
172
+ rs.getLong(1)
173
+ end
150
174
  end
151
175
  end
152
176
 
@@ -161,7 +185,12 @@ module Sequel
161
185
 
162
186
  # Use BIGINT IDENTITY for identity columns that use :Bignum type
163
187
  def type_literal_generic_bignum_symbol(column)
164
- column[:identity] ? 'BIGINT IDENTITY' : super
188
+ column[:identity] ? 'BIGINT AUTO_INCREMENT' : super
189
+ end
190
+
191
+ def version2?
192
+ return @version2 if defined?(@version2)
193
+ @version2 = h2_version.to_i >= 2
165
194
  end
166
195
  end
167
196
 
@@ -209,9 +238,21 @@ module Sequel
209
238
 
210
239
  # H2 expects hexadecimal strings for blob values
211
240
  def literal_blob_append(sql, v)
212
- sql << "'" << v.unpack("H*").first << "'"
241
+ if db.send(:version2?)
242
+ super
243
+ else
244
+ sql << "'" << v.unpack("H*").first << "'"
245
+ end
246
+ end
247
+
248
+ def literal_false
249
+ 'FALSE'
213
250
  end
214
251
 
252
+ def literal_true
253
+ 'TRUE'
254
+ end
255
+
215
256
  # H2 handles fractional seconds in timestamps, but not in times
216
257
  def literal_sqltime(v)
217
258
  v.strftime("'%H:%M:%S'")
@@ -223,8 +264,12 @@ module Sequel
223
264
  end
224
265
 
225
266
  def select_only_offset_sql(sql)
226
- sql << " LIMIT -1 OFFSET "
227
- literal_append(sql, @opts[:offset])
267
+ if db.send(:version2?)
268
+ super
269
+ else
270
+ sql << " LIMIT -1 OFFSET "
271
+ literal_append(sql, @opts[:offset])
272
+ end
228
273
  end
229
274
 
230
275
  # H2 supports quoted function names.
@@ -32,15 +32,13 @@ module Sequel
32
32
 
33
33
  # Allow loading the necessary JDBC support via a gem.
34
34
  def self.load_gem(name)
35
- begin
36
- require "jdbc/#{name.to_s.downcase}"
37
- rescue LoadError
38
- # jdbc gem not used, hopefully the user has the .jar in their CLASSPATH
39
- else
40
- if defined?(::Jdbc) && ( ::Jdbc.const_defined?(name) rescue nil )
41
- jdbc_module = ::Jdbc.const_get(name) # e.g. Jdbc::SQLite3
42
- jdbc_module.load_driver if jdbc_module.respond_to?(:load_driver)
43
- end
35
+ require "jdbc/#{name.to_s.downcase}"
36
+ rescue LoadError
37
+ # jdbc gem not used, hopefully the user has the .jar in their CLASSPATH
38
+ else
39
+ if defined?(::Jdbc) && ( ::Jdbc.const_defined?(name) rescue nil )
40
+ jdbc_module = ::Jdbc.const_get(name) # e.g. Jdbc::SQLite3
41
+ jdbc_module.load_driver if jdbc_module.respond_to?(:load_driver)
44
42
  end
45
43
  end
46
44
 
@@ -190,12 +188,12 @@ module Sequel
190
188
  args = opts[:args] || []
191
189
  sql = "{call #{name}(#{args.map{'?'}.join(',')})}"
192
190
  synchronize(opts[:server]) do |conn|
193
- cps = conn.prepareCall(sql)
191
+ begin
192
+ cps = conn.prepareCall(sql)
194
193
 
195
- i = 0
196
- args.each{|arg| set_ps_arg(cps, arg, i+=1)}
194
+ i = 0
195
+ args.each{|arg| set_ps_arg(cps, arg, i+=1)}
197
196
 
198
- begin
199
197
  if defined?(yield)
200
198
  yield log_connection_yield(sql, conn){cps.executeQuery}
201
199
  else
@@ -207,7 +205,7 @@ module Sequel
207
205
  rescue *DATABASE_ERROR_CLASSES => e
208
206
  raise_error(e)
209
207
  ensure
210
- cps.close
208
+ cps.close if cps
211
209
  end
212
210
  end
213
211
  end
@@ -1,7 +1,7 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  require 'mysql'
4
- raise(LoadError, "require 'mysql' did not define Mysql::CLIENT_MULTI_RESULTS!\n You are probably using the pure ruby mysql.rb driver,\n which Sequel does not support. You need to install\n the C based adapter, and make sure that the mysql.so\n file is loaded instead of the mysql.rb file.\n") unless defined?(Mysql::CLIENT_MULTI_RESULTS)
4
+ raise(LoadError, "require 'mysql' did not define Mysql::CLIENT_MULTI_RESULTS!, so it not supported. Please install the mysql or ruby-mysql gem.\n") unless defined?(Mysql::CLIENT_MULTI_RESULTS)
5
5
 
6
6
  require_relative 'utils/mysql_mysql2'
7
7
  require_relative 'utils/mysql_prepared_statements'
@@ -71,21 +71,43 @@ module Sequel
71
71
  # disconnect this connection (a.k.a @@wait_timeout).
72
72
  def connect(server)
73
73
  opts = server_opts(server)
74
- conn = Mysql.init
75
- conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client")
76
- conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile)
77
- conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey]
78
- if encoding = opts[:encoding] || opts[:charset]
79
- # Set encoding before connecting so that the mysql driver knows what
80
- # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP.
81
- conn.options(Mysql::SET_CHARSET_NAME, encoding)
82
- end
83
- if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT
84
- conn.options(Mysql::OPT_READ_TIMEOUT, read_timeout)
85
- end
86
- if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT
87
- conn.options(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout)
74
+
75
+ if Mysql.respond_to?(:init)
76
+ conn = Mysql.init
77
+ conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client")
78
+ conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile)
79
+ if encoding = opts[:encoding] || opts[:charset]
80
+ # Set encoding before connecting so that the mysql driver knows what
81
+ # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP.
82
+ conn.options(Mysql::SET_CHARSET_NAME, encoding)
83
+ end
84
+ if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT
85
+ conn.options(Mysql::OPT_READ_TIMEOUT, read_timeout)
86
+ end
87
+ if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT
88
+ conn.options(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout)
89
+ end
90
+ else
91
+ # ruby-mysql 3 API
92
+ conn = Mysql.new
93
+ # no support for default group
94
+ conn.local_infile = opts[:config_local_infile] if opts.has_key?(:config_local_infile)
95
+ if encoding = opts[:encoding] || opts[:charset]
96
+ conn.charset = encoding
97
+ end
98
+ if read_timeout = opts[:read_timeout]
99
+ conn.read_timeout = read_timeout
100
+ end
101
+ if connect_timeout = opts[:connect_timeout]
102
+ conn.connect_timeout = connect_timeout
103
+ end
104
+ conn.singleton_class.class_eval do
105
+ alias real_connect connect
106
+ alias use_result store_result
107
+ end
88
108
  end
109
+
110
+ conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey]
89
111
  conn.real_connect(
90
112
  opts[:host] || 'localhost',
91
113
  opts[:user],
@@ -152,56 +174,49 @@ module Sequel
152
174
  super
153
175
  end
154
176
 
155
- # Return the version of the MySQL server to which we are connecting.
156
- def server_version(server=nil)
157
- @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super)
158
- end
159
-
160
177
  private
161
178
 
162
179
  # Execute the given SQL on the given connection. If the :type
163
180
  # option is :select, yield the result of the query, otherwise
164
181
  # yield the connection if a block is given.
165
182
  def _execute(conn, sql, opts)
166
- begin
167
- r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn){conn.query(sql)}
168
- if opts[:type] == :select
169
- yield r if r
170
- elsif defined?(yield)
171
- yield conn
172
- end
173
- if conn.respond_to?(:more_results?)
174
- while conn.more_results? do
175
- if r
176
- r.free
177
- r = nil
178
- end
179
- begin
180
- conn.next_result
181
- r = conn.use_result
182
- rescue Mysql::Error => e
183
- raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
184
- break
185
- end
186
- yield r if opts[:type] == :select
183
+ r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn){conn.query(sql)}
184
+ if opts[:type] == :select
185
+ yield r if r
186
+ elsif defined?(yield)
187
+ yield conn
188
+ end
189
+ if conn.respond_to?(:more_results?)
190
+ while conn.more_results? do
191
+ if r
192
+ r.free
193
+ r = nil
194
+ end
195
+ begin
196
+ conn.next_result
197
+ r = conn.use_result
198
+ rescue Mysql::Error => e
199
+ raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
200
+ break
187
201
  end
202
+ yield r if opts[:type] == :select
188
203
  end
189
- rescue Mysql::Error => e
190
- raise_error(e)
191
- ensure
192
- r.free if r
193
- # Use up all results to avoid a commands out of sync message.
194
- if conn.respond_to?(:more_results?)
195
- while conn.more_results? do
196
- begin
197
- conn.next_result
198
- r = conn.use_result
199
- rescue Mysql::Error => e
200
- raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
201
- break
202
- end
203
- r.free if r
204
+ end
205
+ rescue Mysql::Error => e
206
+ raise_error(e)
207
+ ensure
208
+ r.free if r
209
+ # Use up all results to avoid a commands out of sync message.
210
+ if conn.respond_to?(:more_results?)
211
+ while conn.more_results? do
212
+ begin
213
+ conn.next_result
214
+ r = conn.use_result
215
+ rescue Mysql::Error => e
216
+ raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
217
+ break
204
218
  end
219
+ r.free if r
205
220
  end
206
221
  end
207
222
  end
@@ -233,17 +248,15 @@ module Sequel
233
248
  # the conversion raises an InvalidValue exception, return v
234
249
  # if :string and nil otherwise.
235
250
  def convert_date_time(v)
236
- begin
237
- yield v
238
- rescue InvalidValue
239
- case @convert_invalid_date_time
240
- when nil, :nil
241
- nil
242
- when :string
243
- v
244
- else
245
- raise
246
- end
251
+ yield v
252
+ rescue InvalidValue
253
+ case @convert_invalid_date_time
254
+ when nil, :nil
255
+ nil
256
+ when :string
257
+ v
258
+ else
259
+ raise
247
260
  end
248
261
  end
249
262