sequel 5.51.0 → 5.56.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +62 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -0
  5. data/doc/opening_databases.rdoc +4 -1
  6. data/doc/querying.rdoc +3 -1
  7. data/doc/release_notes/5.52.0.txt +87 -0
  8. data/doc/release_notes/5.53.0.txt +23 -0
  9. data/doc/release_notes/5.54.0.txt +27 -0
  10. data/doc/release_notes/5.55.0.txt +21 -0
  11. data/doc/release_notes/5.56.0.txt +51 -0
  12. data/doc/sql.rdoc +1 -1
  13. data/doc/testing.rdoc +3 -1
  14. data/lib/sequel/adapters/amalgalite.rb +3 -5
  15. data/lib/sequel/adapters/jdbc/h2.rb +55 -10
  16. data/lib/sequel/adapters/jdbc.rb +12 -14
  17. data/lib/sequel/adapters/mysql.rb +80 -67
  18. data/lib/sequel/adapters/mysql2.rb +53 -48
  19. data/lib/sequel/adapters/postgres.rb +17 -21
  20. data/lib/sequel/adapters/shared/mysql.rb +3 -2
  21. data/lib/sequel/adapters/shared/postgres.rb +2 -2
  22. data/lib/sequel/adapters/shared/sqlite.rb +6 -0
  23. data/lib/sequel/adapters/sqlite.rb +60 -18
  24. data/lib/sequel/adapters/utils/mysql_mysql2.rb +1 -1
  25. data/lib/sequel/connection_pool/sharded_single.rb +5 -7
  26. data/lib/sequel/connection_pool/single.rb +6 -8
  27. data/lib/sequel/core.rb +17 -18
  28. data/lib/sequel/database/query.rb +1 -1
  29. data/lib/sequel/database/schema_generator.rb +6 -5
  30. data/lib/sequel/database/schema_methods.rb +9 -0
  31. data/lib/sequel/dataset/sql.rb +3 -2
  32. data/lib/sequel/extensions/core_refinements.rb +36 -11
  33. data/lib/sequel/extensions/date_parse_input_handler.rb +67 -0
  34. data/lib/sequel/extensions/datetime_parse_to_time.rb +5 -1
  35. data/lib/sequel/extensions/pg_array_ops.rb +2 -2
  36. data/lib/sequel/extensions/pg_hstore_ops.rb +1 -1
  37. data/lib/sequel/extensions/pg_inet_ops.rb +1 -1
  38. data/lib/sequel/extensions/pg_interval.rb +1 -0
  39. data/lib/sequel/extensions/pg_json.rb +3 -5
  40. data/lib/sequel/extensions/pg_json_ops.rb +3 -2
  41. data/lib/sequel/extensions/pg_range_ops.rb +1 -1
  42. data/lib/sequel/extensions/pg_row_ops.rb +1 -1
  43. data/lib/sequel/extensions/s.rb +2 -1
  44. data/lib/sequel/extensions/schema_dumper.rb +2 -2
  45. data/lib/sequel/extensions/server_block.rb +8 -12
  46. data/lib/sequel/extensions/sql_comments.rb +110 -3
  47. data/lib/sequel/extensions/sqlite_json_ops.rb +255 -0
  48. data/lib/sequel/extensions/string_date_time.rb +19 -23
  49. data/lib/sequel/model/base.rb +8 -12
  50. data/lib/sequel/plugins/auto_restrict_eager_graph.rb +62 -0
  51. data/lib/sequel/plugins/column_encryption.rb +1 -1
  52. data/lib/sequel/plugins/enum.rb +124 -0
  53. data/lib/sequel/plugins/instance_specific_default.rb +1 -1
  54. data/lib/sequel/plugins/sql_comments.rb +189 -0
  55. data/lib/sequel/plugins/subclasses.rb +28 -11
  56. data/lib/sequel/plugins/unused_associations.rb +2 -2
  57. data/lib/sequel/timezones.rb +12 -14
  58. data/lib/sequel/version.rb +1 -1
  59. metadata +21 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ebee8a266be02f6ba3c1d5d340b606336dbb0fb74beb4b4333eb3c930cd59b5c
4
- data.tar.gz: f657a1aaedf3e0ef8f644097ee94760dfb04381730dd0c6f0a53c57e3f2bc742
3
+ metadata.gz: 3505371d31fd90d388e3d889117e0dc949e73d41ebd6bee01441be9d061ea33a
4
+ data.tar.gz: fed52ae3813a0799065e695e90ab95e9633195477d01d1df9de6e6dbddbcaa23
5
5
  SHA512:
6
- metadata.gz: 9a0aac448e73636a0c20e5e4d87d65ac648a56bb4267034c5b92fdf2cf297aeeaff408dc4e79c6fe05bdd113e22824a5d7bc7a5b236b44346cda6ed427a377c1
7
- data.tar.gz: d99a2b488a0735ba9012e4dfcaae30bec76fce1e7d3c34a14be9395241432a3d00ec5fa7a22df371810adf734e0c39230d212934f10a7d39c5839e58231d6625
6
+ metadata.gz: 6bdd85a8fbd1ba0c0fc871e2e318836e7463d73f9677c0fbba667b6bc86fbe0cb6f0fe09026a39b9b9c79aa783c60f211ffd993b22dff6f9e0d43d3e18754351
7
+ data.tar.gz: 6ce33f8f9593e7cf73d1322104b7dd362deca59eb6397e7d4ea2705c8bb17d4087453903edd59389181d9829818fd31dc2b990dc77b4c3f3a714b846c8a5db3b
data/CHANGELOG CHANGED
@@ -1,3 +1,65 @@
1
+ === 5.56.0 (2022-05-01)
2
+
3
+ * Make alter_table add_column/add_foreign_key methods support :index option to create an index on the column (jeremyevans)
4
+
5
+ * Support creation of STRICT tables on SQLite 3.37.0+ via create_table :strict option (jeremyevans)
6
+
7
+ * Add sqlite_json_ops extension for DSL support for JSON functions and operators added in SQLite 3.38.0 (jeremyevans)
8
+
9
+ * Recognize "INTEGER" type same as "integer" type in the schema dumper, helpful on SQLite 3.37.0+ (jeremyevans)
10
+
11
+ === 5.55.0 (2022-04-01)
12
+
13
+ * Support :setup_regexp_function Database option in the sqlite adapter to allow the use of regexps when querying (jeremyevans)
14
+
15
+ * Add auto_restrict_eager_graph plugin for automatically disallow eager_graph with associations needing but lacking graph options (jeremyevans)
16
+
17
+ * Fix placeholder literalizer optimization for dataset aggregate methods on a model dataset (belousovAV) (#1847, #1848)
18
+
19
+ === 5.54.0 (2022-03-01)
20
+
21
+ * Add enum plugin for treating columns as enums in a model (jeremyevans) (#1839)
22
+
23
+ === 5.53.0 (2022-02-01)
24
+
25
+ * Make Dataset#_sql_comment private when using the Database sql_comments extension (jeremyevans)
26
+
27
+ * Fix prepared statements in the mysql2 adapter to reuse native prepared statements (jeremyevans) (#1832)
28
+
29
+ * Support H2 version 2+ in the jdbc/h2 adapter (jeremyevans) (#1817)
30
+
31
+ * Work around active_support breaking subclasses plugin on Ruby <3.1 (jeremyevans) (#1816)
32
+
33
+ * Fix error handling if trying to setup column_encryption plugin without keys (jeremyevans) (#1815)
34
+
35
+ === 5.52.0 (2022-01-01)
36
+
37
+ * Use Class#subclasses if available in the subclasses plugin, instead of a custom Model.subclasses accessor (jeremyevans)
38
+
39
+ * Add Model.descendants and .freeze_descendants to subclasses plugin (jeremyevans)
40
+
41
+ * Avoid use of deprecated Refinement#include on Ruby 3.1+ (jeremyevans)
42
+
43
+ * Add date_parse_input_handler extension for custom handling of input to date parsing methods (jeremyevans)
44
+
45
+ * Make postgres adapter respect Database#default_string_column_size (jeremyevans)
46
+
47
+ * Make pg_interval extension work with ActiveSupport 7.0 (jeremyevans)
48
+
49
+ * Make :ruby_default schema entry for type: :datetime respect Sequel.datetime_class (jeremyevans)
50
+
51
+ * Make alter_table drop_constraint have an effect on MySQL 8.0.19+ (jeremyevans)
52
+
53
+ * Make mysql adapter support ruby-mysql 3 API (jeremyevans) (#1795)
54
+
55
+ * Make mysql adapter no longer use connection's server_version, since it isn't accurate when using the ruby-mysql driver (jeremyevans)
56
+
57
+ * Add sql_comments plugin for automatically including comments on queries generated by model class, instance, and dataset methods (jeremyevans)
58
+
59
+ * Make sql_comments Database extension support Database#with_comments, for automatically including comments for queries executed inside the block (jeremyevans)
60
+
61
+ * Fix sql_comments extension to not modify cached SQL for a dataset (jeremyevans)
62
+
1
63
  === 5.51.0 (2021-12-01)
2
64
 
3
65
  * 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
 
@@ -383,6 +383,9 @@ The following additional options are supported:
383
383
 
384
384
  :readonly :: open database in read-only mode
385
385
  :timeout :: the busy timeout to use in milliseconds (default: 5000).
386
+ :setup_regexp_function :: Whether to setup a REGEXP function in the underlying SQLite3::Database object. Doing so
387
+ allows you to use regexp support in dataset expressions. Note that this creates a new
388
+ Regexp object per call to the function, so it is not an efficient implementation.
386
389
 
387
390
  Note that SQLite memory databases are restricted to a single connection by
388
391
  default. This is because SQLite does not allow multiple connections to
data/doc/querying.rdoc CHANGED
@@ -357,7 +357,9 @@ For ranges, Sequel uses a pair of inequality statements:
357
357
  # SELECT * FROM artists WHERE ((id >= 1) AND (id < 5))
358
358
 
359
359
  Finally, for regexps, Sequel uses an SQL regular expression. Note that this
360
- is probably only supported on PostgreSQL and MySQL.
360
+ is only supported by default on PostgreSQL and MySQL. It can also be supported
361
+ on SQLite when using the sqlite adapter with the :setup_regexp_function
362
+ Database option.
361
363
 
362
364
  Artist.where(name: /JM$/)
363
365
  # SELECT * FROM artists WHERE (name ~ 'JM$')
@@ -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.
@@ -0,0 +1,21 @@
1
+ = New Features
2
+
3
+ * An auto_restrict_eager_graph plugin has been added for automatically
4
+ disallowing the use of eager_graph with associations using blocks but
5
+ lacking graph_* options. This can prevent potentionally invalid usage,
6
+ as the restrictions added by the block are not used by eager_graph.
7
+
8
+ * The sqlite adapter now supports the :setup_regexp_function
9
+ Database option. This option will define a REGEXP function in the
10
+ database that will allow regexp support in queries, such as:
11
+
12
+ DB[:table].where(column: /(some|pattern)/)
13
+
14
+ Note that this creates a Ruby Regexp object per column value tested,
15
+ so it isn't the most optimal approach.
16
+
17
+ = Other Improvements
18
+
19
+ * Calling dataset aggregate methods such as #max on a model dataset now
20
+ works correctly. Previously, it could fail if called enough times to
21
+ optimize using a placeholder literalizer.
@@ -0,0 +1,51 @@
1
+ = New Features
2
+
3
+ * On SQLite, Database#create_table now supports a :strict option to
4
+ use the STRICT keyword when creating the table. When this option
5
+ is used, SQLite will enforce the types for each column. When using
6
+ this option, you are limited to using the following column types:
7
+ int, integer, real, text, blob, and any (any allows for dynamic
8
+ types).
9
+
10
+ * An sqlite_json_ops extension has been added, providing DSL support
11
+ for JSON functions and operators supported in SQLite 3.38.0. Usage
12
+ is similar to the pg_json_ops extension. First, you create an
13
+ appropriate object:
14
+
15
+ j = Sequel.sqlite_json_op(:json_column)
16
+ # or:
17
+ j = Sequel[:json_column].sqlite_json_op
18
+
19
+ Then, you call methods on that object to create expressions for the
20
+ JSON functions and operators:
21
+
22
+ j[1] # (json_column ->> 1)
23
+ j.get_text(1) # (json_column -> 1)
24
+ j.extract('$.a') # json_extract(json_column, '$.a')
25
+
26
+ j.array_length # json_array_length(json_column)
27
+ j.type # json_type(json_column)
28
+ j.valid # json_valid(json_column)
29
+ j.json # json(json_column)
30
+
31
+ j.insert('$.a', 1) # json_insert(json_column, '$.a', 1)
32
+ j.set('$.a', 1) # json_set(json_column, '$.a', 1)
33
+ j.replace('$.a', 1) # json_replace(json_column, '$.a', 1)
34
+ j.remove('$.a') # json_remove(json_column, '$.a')
35
+ j.patch('{"a":2}') # json_patch(json_column, '{"a":2}')
36
+
37
+ j.each # json_each(json_column)
38
+ j.tree # json_tree(json_column)
39
+
40
+ = Other Improvements
41
+
42
+ * The alter_table add_column and add_foreign_key methods now support
43
+ the :index option to create an index on the added column, for
44
+ compatibility with the :index option on the create_table column and
45
+ foreign_key methods.
46
+
47
+ * The schema_dumper extension now treats the "INTEGER" type the same
48
+ as the "integer" type. This fixes some behavior when using SQLite
49
+ 3.37.0+.
50
+
51
+ * Sequel's website has a much improved visual design.
data/doc/sql.rdoc CHANGED
@@ -528,7 +528,7 @@ Inverting the LIKE operator works like other inversions:
528
528
 
529
529
  ~Sequel.like(:name, 'A%') # ("name" NOT LIKE 'A%' ESCAPE '\')
530
530
 
531
- Sequel also supports SQL regular expressions on MySQL and PostgreSQL. You can use these by passing a Ruby regular expression to +like+ or +ilike+, or by making the regular expression a hash value:
531
+ Sequel also supports SQL regular expressions on MySQL and PostgreSQL (and SQLite when using the sqlite adapter with the :setup_regexp_function Database option). You can use these by passing a Ruby regular expression to +like+ or +ilike+, or by making the regular expression a hash value:
532
532
 
533
533
  Sequel.like(:name, /^A/) # ("name" ~ '^A')
534
534
  ~Sequel.ilike(:name, /^A/) # ("name" !~* '^A')
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