sequel 3.21.0 → 3.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +1131 -3
- data/MIT-LICENSE +1 -1
- data/README.rdoc +106 -65
- data/Rakefile +59 -27
- data/bin/sequel +50 -26
- data/doc/active_record.rdoc +68 -52
- data/doc/advanced_associations.rdoc +201 -75
- data/doc/association_basics.rdoc +327 -79
- data/doc/bin_sequel.rdoc +144 -0
- data/doc/cheat_sheet.rdoc +21 -21
- data/doc/core_extensions.rdoc +374 -0
- data/doc/dataset_basics.rdoc +10 -10
- data/doc/dataset_filtering.rdoc +54 -42
- data/doc/mass_assignment.rdoc +56 -0
- data/doc/migration.rdoc +89 -557
- data/doc/model_hooks.rdoc +80 -29
- data/doc/object_model.rdoc +576 -0
- data/doc/opening_databases.rdoc +121 -53
- data/doc/prepared_statements.rdoc +41 -15
- data/doc/querying.rdoc +137 -56
- data/doc/reflection.rdoc +36 -10
- data/doc/release_notes/3.22.0.txt +39 -0
- data/doc/release_notes/3.23.0.txt +172 -0
- data/doc/release_notes/3.24.0.txt +420 -0
- data/doc/release_notes/3.25.0.txt +88 -0
- data/doc/release_notes/3.26.0.txt +88 -0
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/release_notes/3.28.0.txt +304 -0
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/release_notes/3.30.0.txt +135 -0
- data/doc/release_notes/3.31.0.txt +146 -0
- data/doc/release_notes/3.32.0.txt +202 -0
- data/doc/release_notes/3.33.0.txt +157 -0
- data/doc/release_notes/3.34.0.txt +671 -0
- data/doc/release_notes/3.35.0.txt +144 -0
- data/doc/release_notes/3.36.0.txt +245 -0
- data/doc/release_notes/3.37.0.txt +338 -0
- data/doc/release_notes/3.38.0.txt +234 -0
- data/doc/release_notes/3.39.0.txt +237 -0
- data/doc/release_notes/3.40.0.txt +73 -0
- data/doc/release_notes/3.41.0.txt +155 -0
- data/doc/schema_modification.rdoc +585 -0
- data/doc/sharding.rdoc +99 -8
- data/doc/sql.rdoc +154 -112
- data/doc/testing.rdoc +169 -0
- data/doc/thread_safety.rdoc +17 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/validations.rdoc +1 -1
- data/doc/virtual_rows.rdoc +78 -43
- data/lib/sequel/adapters/ado/access.rb +334 -0
- data/lib/sequel/adapters/ado/mssql.rb +18 -12
- data/lib/sequel/adapters/ado.rb +69 -21
- data/lib/sequel/adapters/amalgalite.rb +17 -14
- data/lib/sequel/adapters/cubrid.rb +143 -0
- data/lib/sequel/adapters/db2.rb +178 -85
- data/lib/sequel/adapters/dbi.rb +19 -21
- data/lib/sequel/adapters/do/mysql.rb +17 -11
- data/lib/sequel/adapters/do/postgres.rb +2 -61
- data/lib/sequel/adapters/do/sqlite.rb +0 -10
- data/lib/sequel/adapters/do.rb +20 -39
- data/lib/sequel/adapters/firebird.rb +33 -210
- data/lib/sequel/adapters/ibmdb.rb +452 -0
- data/lib/sequel/adapters/informix.rb +6 -29
- data/lib/sequel/adapters/jdbc/as400.rb +16 -34
- data/lib/sequel/adapters/jdbc/cubrid.rb +52 -0
- data/lib/sequel/adapters/jdbc/db2.rb +78 -0
- data/lib/sequel/adapters/jdbc/derby.rb +325 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
- data/lib/sequel/adapters/jdbc/h2.rb +68 -31
- data/lib/sequel/adapters/jdbc/hsqldb.rb +189 -0
- data/lib/sequel/adapters/jdbc/informix.rb +21 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +40 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
- data/lib/sequel/adapters/jdbc/mysql.rb +16 -28
- data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +77 -52
- data/lib/sequel/adapters/jdbc/progress.rb +21 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +20 -10
- data/lib/sequel/adapters/jdbc/sqlserver.rb +72 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +308 -114
- data/lib/sequel/adapters/mock.rb +372 -0
- data/lib/sequel/adapters/mysql.rb +130 -230
- data/lib/sequel/adapters/mysql2.rb +64 -63
- data/lib/sequel/adapters/odbc/mssql.rb +17 -9
- data/lib/sequel/adapters/odbc.rb +29 -10
- data/lib/sequel/adapters/openbase.rb +7 -11
- data/lib/sequel/adapters/oracle.rb +364 -57
- data/lib/sequel/adapters/postgres.rb +379 -145
- data/lib/sequel/adapters/shared/access.rb +236 -11
- data/lib/sequel/adapters/shared/cubrid.rb +216 -0
- data/lib/sequel/adapters/shared/db2.rb +353 -0
- data/lib/sequel/adapters/shared/firebird.rb +231 -0
- data/lib/sequel/adapters/shared/informix.rb +53 -0
- data/lib/sequel/adapters/shared/mssql.rb +412 -116
- data/lib/sequel/adapters/shared/mysql.rb +396 -89
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +166 -0
- data/lib/sequel/adapters/shared/oracle.rb +262 -45
- data/lib/sequel/adapters/shared/postgres.rb +688 -279
- data/lib/sequel/adapters/shared/progress.rb +5 -8
- data/lib/sequel/adapters/shared/sqlite.rb +317 -96
- data/lib/sequel/adapters/sqlite.rb +109 -46
- data/lib/sequel/adapters/swift/mysql.rb +10 -12
- data/lib/sequel/adapters/swift/postgres.rb +13 -79
- data/lib/sequel/adapters/swift/sqlite.rb +9 -6
- data/lib/sequel/adapters/swift.rb +31 -27
- data/lib/sequel/adapters/tinytds.rb +177 -25
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +77 -0
- data/lib/sequel/adapters/utils/pg_types.rb +81 -0
- data/lib/sequel/adapters/utils/split_alter_table.rb +36 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
- data/lib/sequel/ast_transformer.rb +194 -0
- data/lib/sequel/connection_pool/sharded_single.rb +23 -13
- data/lib/sequel/connection_pool/sharded_threaded.rb +64 -13
- data/lib/sequel/connection_pool/single.rb +11 -3
- data/lib/sequel/connection_pool/threaded.rb +69 -20
- data/lib/sequel/connection_pool.rb +20 -18
- data/lib/sequel/core.rb +143 -11
- data/lib/sequel/database/connecting.rb +66 -29
- data/lib/sequel/database/dataset.rb +4 -4
- data/lib/sequel/database/dataset_defaults.rb +63 -2
- data/lib/sequel/database/logging.rb +7 -2
- data/lib/sequel/database/misc.rb +192 -51
- data/lib/sequel/database/query.rb +299 -114
- data/lib/sequel/database/schema_generator.rb +103 -36
- data/lib/sequel/database/schema_methods.rb +310 -69
- data/lib/sequel/dataset/actions.rb +344 -90
- data/lib/sequel/dataset/features.rb +101 -8
- data/lib/sequel/dataset/graph.rb +42 -15
- data/lib/sequel/dataset/misc.rb +52 -25
- data/lib/sequel/dataset/mutation.rb +12 -7
- data/lib/sequel/dataset/prepared_statements.rb +68 -30
- data/lib/sequel/dataset/query.rb +322 -68
- data/lib/sequel/dataset/sql.rb +716 -293
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/_pretty_table.rb +83 -0
- data/lib/sequel/extensions/arbitrary_servers.rb +109 -0
- data/lib/sequel/extensions/blank.rb +4 -0
- data/lib/sequel/extensions/columns_introspection.rb +72 -0
- data/lib/sequel/extensions/connection_validator.rb +109 -0
- data/lib/sequel/extensions/constraint_validations.rb +451 -0
- data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +30 -46
- data/lib/sequel/extensions/eval_inspect.rb +173 -0
- data/lib/sequel/extensions/inflector.rb +4 -0
- data/lib/sequel/extensions/looser_typecasting.rb +5 -4
- data/lib/sequel/extensions/migration.rb +100 -11
- data/lib/sequel/extensions/named_timezones.rb +9 -0
- data/lib/sequel/extensions/null_dataset.rb +94 -0
- data/lib/sequel/extensions/pagination.rb +4 -0
- data/lib/sequel/extensions/pg_array.rb +539 -0
- data/lib/sequel/extensions/pg_array_ops.rb +263 -0
- data/lib/sequel/extensions/pg_auto_parameterize.rb +175 -0
- data/lib/sequel/extensions/pg_hstore.rb +334 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +293 -0
- data/lib/sequel/extensions/pg_inet.rb +113 -0
- data/lib/sequel/extensions/pg_interval.rb +191 -0
- data/lib/sequel/extensions/pg_json.rb +212 -0
- data/lib/sequel/extensions/pg_range.rb +513 -0
- data/lib/sequel/extensions/pg_range_ops.rb +150 -0
- data/lib/sequel/extensions/pg_row.rb +576 -0
- data/lib/sequel/extensions/pg_row_ops.rb +182 -0
- data/lib/sequel/extensions/pg_statement_cache.rb +317 -0
- data/lib/sequel/extensions/pretty_table.rb +17 -72
- data/lib/sequel/extensions/query.rb +8 -4
- data/lib/sequel/extensions/query_literals.rb +79 -0
- data/lib/sequel/extensions/schema_caching.rb +76 -0
- data/lib/sequel/extensions/schema_dumper.rb +282 -76
- data/lib/sequel/extensions/select_remove.rb +39 -0
- data/lib/sequel/extensions/server_block.rb +140 -0
- data/lib/sequel/extensions/split_array_nil.rb +65 -0
- data/lib/sequel/extensions/sql_expr.rb +8 -110
- data/lib/sequel/extensions/string_date_time.rb +4 -0
- data/lib/sequel/extensions/thread_local_timezones.rb +10 -4
- data/lib/sequel/extensions/to_dot.rb +99 -83
- data/lib/sequel/model/associations.rb +1263 -409
- data/lib/sequel/model/base.rb +624 -171
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +24 -2
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/model.rb +30 -11
- data/lib/sequel/no_core_ext.rb +2 -0
- data/lib/sequel/plugins/active_model.rb +13 -1
- data/lib/sequel/plugins/association_pks.rb +137 -16
- data/lib/sequel/plugins/caching.rb +25 -18
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/composition.rb +44 -12
- data/lib/sequel/plugins/constraint_validations.rb +198 -0
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +72 -0
- data/lib/sequel/plugins/dirty.rb +214 -0
- data/lib/sequel/plugins/eager_each.rb +59 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/hook_class_methods.rb +1 -1
- data/lib/sequel/plugins/identity_map.rb +134 -15
- data/lib/sequel/plugins/instance_filters.rb +10 -0
- data/lib/sequel/plugins/instance_hooks.rb +1 -1
- data/lib/sequel/plugins/json_serializer.rb +77 -6
- data/lib/sequel/plugins/list.rb +13 -2
- data/lib/sequel/plugins/many_through_many.rb +103 -51
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
- data/lib/sequel/plugins/nested_attributes.rb +150 -66
- data/lib/sequel/plugins/optimistic_locking.rb +8 -0
- data/lib/sequel/plugins/pg_row.rb +121 -0
- data/lib/sequel/plugins/pg_typecast_on_load.rb +65 -0
- data/lib/sequel/plugins/prepared_statements.rb +168 -0
- data/lib/sequel/plugins/prepared_statements_associations.rb +87 -0
- data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
- data/lib/sequel/plugins/rcte_tree.rb +31 -17
- data/lib/sequel/plugins/schema.rb +8 -3
- data/lib/sequel/plugins/serialization.rb +98 -49
- data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
- data/lib/sequel/plugins/sharding.rb +21 -54
- data/lib/sequel/plugins/single_table_inheritance.rb +57 -12
- data/lib/sequel/plugins/static_cache.rb +99 -0
- data/lib/sequel/plugins/subclasses.rb +29 -3
- data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/touch.rb +18 -6
- data/lib/sequel/plugins/tree.rb +3 -3
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +2 -2
- data/lib/sequel/plugins/validation_class_methods.rb +2 -1
- data/lib/sequel/plugins/validation_helpers.rb +57 -4
- data/lib/sequel/plugins/xml_serializer.rb +15 -4
- data/lib/sequel/sql.rb +721 -141
- data/lib/sequel/timezones.rb +67 -40
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/firebird_spec.rb +53 -39
- data/spec/adapters/mssql_spec.rb +261 -66
- data/spec/adapters/mysql_spec.rb +597 -352
- data/spec/adapters/oracle_spec.rb +92 -117
- data/spec/adapters/postgres_spec.rb +2103 -341
- data/spec/adapters/spec_helper.rb +16 -6
- data/spec/adapters/sqlite_spec.rb +237 -76
- data/spec/core/connection_pool_spec.rb +270 -141
- data/spec/core/database_spec.rb +980 -524
- data/spec/core/dataset_spec.rb +1658 -1243
- data/spec/core/expression_filters_spec.rb +741 -334
- data/spec/core/mock_adapter_spec.rb +453 -0
- data/spec/core/object_graph_spec.rb +88 -129
- data/spec/core/schema_generator_spec.rb +4 -4
- data/spec/core/schema_spec.rb +556 -53
- data/spec/core/spec_helper.rb +7 -48
- data/spec/core_extensions_spec.rb +626 -0
- data/spec/extensions/active_model_spec.rb +13 -0
- data/spec/extensions/arbitrary_servers_spec.rb +110 -0
- data/spec/extensions/association_autoreloading_spec.rb +18 -10
- data/spec/extensions/association_dependencies_spec.rb +15 -25
- data/spec/extensions/association_pks_spec.rb +227 -33
- data/spec/extensions/association_proxies_spec.rb +4 -4
- data/spec/extensions/boolean_readers_spec.rb +25 -25
- data/spec/extensions/caching_spec.rb +47 -51
- data/spec/extensions/class_table_inheritance_spec.rb +31 -83
- data/spec/extensions/columns_introspection_spec.rb +91 -0
- data/spec/extensions/composition_spec.rb +18 -13
- data/spec/extensions/connection_validator_spec.rb +118 -0
- data/spec/extensions/constraint_validations_plugin_spec.rb +196 -0
- data/spec/extensions/constraint_validations_spec.rb +316 -0
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +88 -0
- data/spec/extensions/dirty_spec.rb +155 -0
- data/spec/extensions/eager_each_spec.rb +34 -0
- data/spec/extensions/eval_inspect_spec.rb +67 -0
- data/spec/extensions/force_encoding_spec.rb +4 -2
- data/spec/extensions/hook_class_methods_spec.rb +97 -128
- data/spec/extensions/identity_map_spec.rb +142 -24
- data/spec/extensions/inflector_spec.rb +0 -4
- data/spec/extensions/instance_filters_spec.rb +11 -21
- data/spec/extensions/instance_hooks_spec.rb +72 -0
- data/spec/extensions/json_serializer_spec.rb +49 -12
- data/spec/extensions/lazy_attributes_spec.rb +16 -20
- data/spec/extensions/list_spec.rb +49 -40
- data/spec/extensions/looser_typecasting_spec.rb +7 -7
- data/spec/extensions/many_through_many_spec.rb +368 -254
- data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
- data/spec/extensions/migration_spec.rb +140 -35
- data/spec/extensions/named_timezones_spec.rb +29 -11
- data/spec/extensions/nested_attributes_spec.rb +268 -89
- data/spec/extensions/null_dataset_spec.rb +85 -0
- data/spec/extensions/optimistic_locking_spec.rb +24 -21
- data/spec/extensions/pg_array_ops_spec.rb +112 -0
- data/spec/extensions/pg_array_spec.rb +324 -0
- data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
- data/spec/extensions/pg_hstore_ops_spec.rb +140 -0
- data/spec/extensions/pg_hstore_spec.rb +200 -0
- data/spec/extensions/pg_inet_spec.rb +47 -0
- data/spec/extensions/pg_interval_spec.rb +72 -0
- data/spec/extensions/pg_json_spec.rb +99 -0
- data/spec/extensions/pg_range_ops_spec.rb +56 -0
- data/spec/extensions/pg_range_spec.rb +395 -0
- data/spec/extensions/pg_row_ops_spec.rb +58 -0
- data/spec/extensions/pg_row_plugin_spec.rb +49 -0
- data/spec/extensions/pg_row_spec.rb +340 -0
- data/spec/extensions/pg_statement_cache_spec.rb +208 -0
- data/spec/extensions/pg_typecast_on_load_spec.rb +58 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
- data/spec/extensions/prepared_statements_spec.rb +87 -0
- data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
- data/spec/extensions/pretty_table_spec.rb +7 -6
- data/spec/extensions/query_literals_spec.rb +167 -0
- data/spec/extensions/query_spec.rb +9 -3
- data/spec/extensions/rcte_tree_spec.rb +50 -43
- data/spec/extensions/schema_caching_spec.rb +41 -0
- data/spec/extensions/schema_dumper_spec.rb +434 -49
- data/spec/extensions/schema_spec.rb +30 -49
- data/spec/extensions/select_remove_spec.rb +38 -0
- data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
- data/spec/extensions/serialization_spec.rb +86 -48
- data/spec/extensions/server_block_spec.rb +90 -0
- data/spec/extensions/sharding_spec.rb +69 -143
- data/spec/extensions/single_table_inheritance_spec.rb +65 -29
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +34 -67
- data/spec/extensions/split_array_nil_spec.rb +24 -0
- data/spec/extensions/sql_expr_spec.rb +29 -60
- data/spec/extensions/static_cache_spec.rb +145 -0
- data/spec/extensions/subclasses_spec.rb +14 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +17 -19
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/timestamps_spec.rb +6 -6
- data/spec/extensions/to_dot_spec.rb +8 -11
- data/spec/extensions/touch_spec.rb +53 -14
- data/spec/extensions/tree_spec.rb +11 -26
- data/spec/extensions/typecast_on_load_spec.rb +9 -6
- data/spec/extensions/update_primary_key_spec.rb +30 -24
- data/spec/extensions/validation_class_methods_spec.rb +77 -68
- data/spec/extensions/validation_helpers_spec.rb +48 -37
- data/spec/extensions/xml_serializer_spec.rb +29 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +4 -0
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +3 -0
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +3 -0
- data/spec/integration/associations_test.rb +659 -21
- data/spec/integration/database_test.rb +25 -5
- data/spec/integration/dataset_test.rb +729 -141
- data/spec/integration/eager_loader_test.rb +38 -54
- data/spec/integration/migrator_test.rb +2 -3
- data/spec/integration/model_test.rb +81 -2
- data/spec/integration/plugin_test.rb +871 -160
- data/spec/integration/prepared_statement_test.rb +272 -128
- data/spec/integration/schema_test.rb +377 -57
- data/spec/integration/spec_helper.rb +49 -32
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +183 -8
- data/spec/integration/type_test.rb +40 -8
- data/spec/model/association_reflection_spec.rb +207 -8
- data/spec/model/associations_spec.rb +1206 -693
- data/spec/model/base_spec.rb +334 -100
- data/spec/model/dataset_methods_spec.rb +45 -27
- data/spec/model/eager_loading_spec.rb +828 -692
- data/spec/model/hooks_spec.rb +355 -67
- data/spec/model/model_spec.rb +251 -179
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +506 -232
- data/spec/model/spec_helper.rb +16 -77
- data/spec/model/validations_spec.rb +38 -2
- data/spec/rcov.opts +2 -0
- metadata +223 -81
- data/spec/core/core_sql_spec.rb +0 -451
data/doc/bin_sequel.rdoc
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
= bin/sequel
|
|
2
|
+
|
|
3
|
+
bin/sequel is the name used to refer to the "sequel" command line tool that ships with the sequel gem. By default, bin/sequel provides an IRB shell with the +DB+ constant set to a Sequel::Database object created using the database connection string provided on the command line. For example, to connect to a new in-memory SQLite database using the sqlite adapter, you can use the following:
|
|
4
|
+
|
|
5
|
+
sequel sqlite:/
|
|
6
|
+
|
|
7
|
+
This is very useful for quick testing of ideas, and does not affect the environment, since the in-memory SQLite database is destroyed when the program exits.
|
|
8
|
+
|
|
9
|
+
== Running from a git checkout
|
|
10
|
+
|
|
11
|
+
If you've installed the sequel gem, then just running "sequel" should load the program, since rubygems should place the sequel binary in your load path. However, if you want to run bin/sequel from the root of a repository checkout, you should probably do:
|
|
12
|
+
|
|
13
|
+
ruby -I lib bin/sequel
|
|
14
|
+
|
|
15
|
+
The -I lib makes sure that you are using the repository checkout's code.
|
|
16
|
+
|
|
17
|
+
== Choosing the Database to Connect to
|
|
18
|
+
|
|
19
|
+
=== Connection String
|
|
20
|
+
|
|
21
|
+
In general, you probably want to provide a connection string argument to bin/sequel, indicating the adapter and database connection information you want to use. For example:
|
|
22
|
+
|
|
23
|
+
sequel sqlite:/
|
|
24
|
+
sequel postgres://user:pass@host/database_name
|
|
25
|
+
sequel mysql2://user:pass@host/database_name
|
|
26
|
+
|
|
27
|
+
See the {Connecting to a database guide}[link:files/doc/opening_databases_rdoc.html] for more details about and examples of connection strings.
|
|
28
|
+
|
|
29
|
+
=== YAML Connection File
|
|
30
|
+
|
|
31
|
+
Instead of specifying the database connection using a connection string, you can provide the path to a YAML configuration file containing the connection information. This YAML file can contain a single options hash, or it can contain a nested hash, where the top-level hash uses environment keys with hash values for
|
|
32
|
+
each environment. Using the -e option with a yaml connection file, you can choose which environment to use if using a nested hash.
|
|
33
|
+
|
|
34
|
+
sequel -e production config/database.yml
|
|
35
|
+
|
|
36
|
+
Note that bin/sequel does not directly support ActiveRecord YAML configuration files, as they use different names for some options.
|
|
37
|
+
|
|
38
|
+
=== Mock Connection
|
|
39
|
+
|
|
40
|
+
If you don't provide a connection string or YAML connection file, Sequel will start with a mock database. The mock database allows you to play around with Sequel without any database at all, and can be useful if you just want to test things out and generate SQL without actually getting results from a database.
|
|
41
|
+
|
|
42
|
+
sequel
|
|
43
|
+
|
|
44
|
+
Sequel also has the ability to use the mock adapter with database-specific syntax, allowing you to pretend you are connecting to a specific type of database without actually connecting to one. To do that, you need to use a connection string:
|
|
45
|
+
|
|
46
|
+
sequel mock://postgres
|
|
47
|
+
|
|
48
|
+
== Not Just an IRB shell
|
|
49
|
+
|
|
50
|
+
bin/sequel is not just an IRB shell, it can also do far more.
|
|
51
|
+
|
|
52
|
+
=== Execute Code
|
|
53
|
+
|
|
54
|
+
bin/sequel can also be used to execute other ruby files with +DB+ preset to the database given on the command line:
|
|
55
|
+
|
|
56
|
+
sequel postgres://host/database_name path/to/some_file.rb
|
|
57
|
+
|
|
58
|
+
On modern versions of Linux, this means that you can use bin/sequel in a shebang line:
|
|
59
|
+
|
|
60
|
+
#!/path/to/bin/sequel postgres://host/database_name
|
|
61
|
+
|
|
62
|
+
If you want to quickly execute a small piece of ruby code, you can use the -c option:
|
|
63
|
+
|
|
64
|
+
sequel -c "p DB.tables" postgres://host/database_name
|
|
65
|
+
|
|
66
|
+
Similarly, if data is piped into bin/sequel, it will be executed:
|
|
67
|
+
|
|
68
|
+
echo "p DB.tables" | sequel postgres://host/database_name
|
|
69
|
+
|
|
70
|
+
=== Migrate Databases
|
|
71
|
+
|
|
72
|
+
With -m option, Sequel will migrate the database given using the migration directory provided by -m:
|
|
73
|
+
|
|
74
|
+
sequel -m /path/to/migrations/dir postgres://host/database
|
|
75
|
+
|
|
76
|
+
You can use the -M attribute to set the version to migrate to:
|
|
77
|
+
|
|
78
|
+
sequel -m /path/to/migrations/dir -M 3 postgres://host/database
|
|
79
|
+
|
|
80
|
+
See the {migration guide}[link:files/doc/migration_rdoc.html] for more details about migrations.
|
|
81
|
+
|
|
82
|
+
=== Dump Schemas
|
|
83
|
+
|
|
84
|
+
Using the -d or -D options, Sequel will dump the database's schema in Sequel migration format to the standard output:
|
|
85
|
+
|
|
86
|
+
sequel -d postgres://host/database
|
|
87
|
+
|
|
88
|
+
To save this information to a file, use a standard shell redirection:
|
|
89
|
+
|
|
90
|
+
sequel -d postgres://host/database > /path/to/migrations/dir/001_base_schema.rb
|
|
91
|
+
|
|
92
|
+
The -d option dumps the migration in database-independent format, the -D option dumps it in database-specific format.
|
|
93
|
+
|
|
94
|
+
The -S option dumps the schema cache for all tables in the database, which can speed up the usage of Sequel with models when using the schema_caching extension. You should provide this option with the path to which to dump the schema:
|
|
95
|
+
|
|
96
|
+
sequel -S /path/to/schema_cache.db postgres://host/database
|
|
97
|
+
|
|
98
|
+
=== Copy Databases
|
|
99
|
+
|
|
100
|
+
Using the -C option, Sequel can copy the contents of one database to another, even between different database types. Using this option, you provide two connection strings on the command line:
|
|
101
|
+
|
|
102
|
+
sequel -C mysql://host1/database postgres://host2/database2
|
|
103
|
+
|
|
104
|
+
This copies the table structure, table data, indexes, and foreign keys from the MySQL database to the PostgreSQL database.
|
|
105
|
+
|
|
106
|
+
== Other Options
|
|
107
|
+
|
|
108
|
+
Other options not mentioned above are explained briefly here.
|
|
109
|
+
|
|
110
|
+
=== -E
|
|
111
|
+
|
|
112
|
+
-E logs all SQL queries to the standard output, so you can see all SQL that Sequel is sending the database.
|
|
113
|
+
|
|
114
|
+
=== -I include_directory
|
|
115
|
+
|
|
116
|
+
-I is similar to ruby -I, and specifies an additional $LOAD_PATH directory.
|
|
117
|
+
|
|
118
|
+
=== -l log_file
|
|
119
|
+
|
|
120
|
+
-l is similar to -E, but logs all SQL queries to the given file.
|
|
121
|
+
|
|
122
|
+
=== -L load_directory
|
|
123
|
+
|
|
124
|
+
-L loads all *.rb files under the given directory. This is usually used to load Sequel::Model classes into bin/sequel.
|
|
125
|
+
|
|
126
|
+
=== -N
|
|
127
|
+
|
|
128
|
+
-N skips testing the connection when creating the Database object. This is rarely needed.
|
|
129
|
+
|
|
130
|
+
=== -r require_lib
|
|
131
|
+
|
|
132
|
+
-r is similar to ruby -r, requiring the given library.
|
|
133
|
+
|
|
134
|
+
=== -t
|
|
135
|
+
|
|
136
|
+
-t tells bin/sequel to output full backtraces in the case of an error, which can aid in debugging.
|
|
137
|
+
|
|
138
|
+
=== -h
|
|
139
|
+
|
|
140
|
+
-h prints the usage information for bin/sequel.
|
|
141
|
+
|
|
142
|
+
=== -v
|
|
143
|
+
|
|
144
|
+
-v prints the Sequel version in use.
|
data/doc/cheat_sheet.rdoc
CHANGED
|
@@ -53,8 +53,8 @@ Without a filename argument, the sqlite adapter will setup a new sqlite database
|
|
|
53
53
|
|
|
54
54
|
== Update/Delete rows
|
|
55
55
|
|
|
56
|
-
dataset.
|
|
57
|
-
dataset.
|
|
56
|
+
dataset.exclude(:active).delete
|
|
57
|
+
dataset.where('price < ?', 100).update(:active => true)
|
|
58
58
|
|
|
59
59
|
== Datasets are Enumerable
|
|
60
60
|
|
|
@@ -64,21 +64,21 @@ Without a filename argument, the sqlite adapter will setup a new sqlite database
|
|
|
64
64
|
dataset.inject(0){|sum, r| sum + r[:value]}
|
|
65
65
|
dataset.sum(:value) # same as above
|
|
66
66
|
|
|
67
|
-
== Filtering (see also doc/
|
|
67
|
+
== Filtering (see also {Dataset Filtering}[link:files/doc/dataset_filtering_rdoc.html])
|
|
68
68
|
|
|
69
69
|
=== Equality
|
|
70
70
|
|
|
71
|
-
dataset.
|
|
72
|
-
dataset.
|
|
71
|
+
dataset.where(:name => 'abc')
|
|
72
|
+
dataset.where('name = ?', 'abc')
|
|
73
73
|
|
|
74
74
|
=== Inequality
|
|
75
75
|
|
|
76
|
-
dataset.
|
|
76
|
+
dataset.where{value > 100}
|
|
77
77
|
dataset.exclude{value <= 100}
|
|
78
78
|
|
|
79
79
|
=== Inclusion
|
|
80
80
|
|
|
81
|
-
dataset.
|
|
81
|
+
dataset.where(:value => 50..100)
|
|
82
82
|
dataset.where{(value >= 50) & (value <= 100)}
|
|
83
83
|
|
|
84
84
|
dataset.where('value IN ?', [50,75,100])
|
|
@@ -89,34 +89,34 @@ Without a filename argument, the sqlite adapter will setup a new sqlite database
|
|
|
89
89
|
=== Subselects as scalar values
|
|
90
90
|
|
|
91
91
|
dataset.where('price > (SELECT avg(price) + 100 FROM table)')
|
|
92
|
-
dataset.
|
|
92
|
+
dataset.where{price > dataset.select(avg(price) + 100)}
|
|
93
93
|
|
|
94
94
|
=== LIKE/Regexp
|
|
95
95
|
|
|
96
|
-
DB[:items].
|
|
97
|
-
DB[:items].
|
|
96
|
+
DB[:items].where(Sequel.like(:name, 'AL%'))
|
|
97
|
+
DB[:items].where(:name => /^AL/)
|
|
98
98
|
|
|
99
99
|
=== AND/OR/NOT
|
|
100
100
|
|
|
101
|
-
DB[:items].
|
|
101
|
+
DB[:items].where{(x > 5) & (y > 10)}.sql
|
|
102
102
|
# SELECT * FROM items WHERE ((x > 5) AND (y > 10))
|
|
103
103
|
|
|
104
|
-
DB[:items].
|
|
104
|
+
DB[:items].where({:x => 1, :y => 2}.sql_or & Sequel.~(:z => 3)).sql
|
|
105
105
|
# SELECT * FROM items WHERE (((x = 1) OR (y = 2)) AND (z != 3))
|
|
106
106
|
|
|
107
107
|
=== Mathematical operators
|
|
108
108
|
|
|
109
|
-
DB[:items].
|
|
109
|
+
DB[:items].where{x + y > z}.sql
|
|
110
110
|
# SELECT * FROM items WHERE ((x + y) > z)
|
|
111
111
|
|
|
112
|
-
DB[:items].
|
|
112
|
+
DB[:items].where{price - 100 < avg(price)}.sql
|
|
113
113
|
# SELECT * FROM items WHERE ((price - 100) < avg(price))
|
|
114
114
|
|
|
115
115
|
== Ordering
|
|
116
116
|
|
|
117
117
|
dataset.order(:kind)
|
|
118
118
|
dataset.reverse_order(:kind)
|
|
119
|
-
dataset.order(:kind
|
|
119
|
+
dataset.order(Sequel.desc(:kind), :name)
|
|
120
120
|
|
|
121
121
|
== Limit/Offset
|
|
122
122
|
|
|
@@ -140,15 +140,15 @@ Without a filename argument, the sqlite adapter will setup a new sqlite database
|
|
|
140
140
|
dataset.sum(:stock)
|
|
141
141
|
|
|
142
142
|
dataset.group_and_count(:category)
|
|
143
|
-
dataset.group(:category).select(:category,
|
|
143
|
+
dataset.group(:category).select(:category, Sequel.function(:AVG, :price))
|
|
144
144
|
|
|
145
145
|
== SQL Functions / Literals
|
|
146
146
|
|
|
147
|
-
dataset.update(:updated_at => :NOW
|
|
148
|
-
dataset.update(:updated_at => 'NOW()'
|
|
147
|
+
dataset.update(:updated_at => Sequel.function(:NOW))
|
|
148
|
+
dataset.update(:updated_at => Sequel.lit('NOW()'))
|
|
149
149
|
|
|
150
|
-
dataset.update(:updated_at => "DateValue('1/1/2001')"
|
|
151
|
-
dataset.update(:updated_at => :DateValue
|
|
150
|
+
dataset.update(:updated_at => Sequel.lit("DateValue('1/1/2001')")
|
|
151
|
+
dataset.update(:updated_at => Sequel.function(:DateValue, '1/1/2001'))
|
|
152
152
|
|
|
153
153
|
== Schema Manipulation
|
|
154
154
|
|
|
@@ -171,7 +171,7 @@ Without a filename argument, the sqlite adapter will setup a new sqlite database
|
|
|
171
171
|
|
|
172
172
|
== Aliasing
|
|
173
173
|
|
|
174
|
-
DB[:items].select(
|
|
174
|
+
DB[:items].select(Sequel.as(:name, :item_name))
|
|
175
175
|
DB[:items].select(:name___item_name)
|
|
176
176
|
DB[:items___items_table].select(:items_table__name___item_name)
|
|
177
177
|
# SELECT items_table.name AS item_name FROM items AS items_table
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
= Sequel's Core Extensions
|
|
2
|
+
|
|
3
|
+
== Background
|
|
4
|
+
|
|
5
|
+
Historically, Sequel added methods to many of the core classes, and usage of those methods was the primary and recommended way to use Sequel. For example:
|
|
6
|
+
|
|
7
|
+
DB[:table].select(:column.cast(Integer)). # Symbol#cast
|
|
8
|
+
where(:column.like('A%')). # Symbol#like
|
|
9
|
+
order({1=>2}.case(0, :a)) # Hash#case
|
|
10
|
+
|
|
11
|
+
While Sequel never override any methods defined by ruby, it is possible that other libraries could define the same methods that Sequel defines, which could cause problems. Also, some rubyists do not like using libraries that add methods to the core classes.
|
|
12
|
+
|
|
13
|
+
Alternatives for the core extension methods where added to Sequel, so the query above could be written as:
|
|
14
|
+
|
|
15
|
+
DB[:table].select(Sequel.cast(:column, Integer)).
|
|
16
|
+
where(Sequel.like(:column, 'A%')).
|
|
17
|
+
order(Sequel.case({1=>2}, 0, :a))
|
|
18
|
+
|
|
19
|
+
Almost all of the core extension methods have a replacement on the Sequel module. So it is now up to the user which style to use. Using the methods on the Sequel module results in slightly more verbose code, but allows the code to work without modifications to the core classes.
|
|
20
|
+
|
|
21
|
+
== Issues
|
|
22
|
+
|
|
23
|
+
There is no recommendation on whether the core_extensions should be used or not. It is very rare that any of the methods added by core_extensions actually causes a problem, but some of them can make it more difficult to find other problems. For example, if you type:
|
|
24
|
+
|
|
25
|
+
do_somehting if value | other_value
|
|
26
|
+
|
|
27
|
+
while meaning to type:
|
|
28
|
+
|
|
29
|
+
do_something if value || other_value
|
|
30
|
+
|
|
31
|
+
and value is a Symbol, instead of a NoMethodError being raised because Symbol#| is not implemented by default, <tt>value | other_value</tt> will return a Sequel expression object, which if will evaluate as true, and do_something will be called.
|
|
32
|
+
|
|
33
|
+
== Usage
|
|
34
|
+
|
|
35
|
+
All of Sequel's extensions to the core classes are stored in Sequel's core_extensions extension, which you can load via:
|
|
36
|
+
|
|
37
|
+
Sequel.extension :core_extensions
|
|
38
|
+
|
|
39
|
+
For backwards compatibility, the core_extensions are loaded by default in Sequel 3. Starting in Sequel 4, the core extensions will no longer be loaded by default, so you will have to load the core_extensions extension manually using the above code. If you plan to use the core extensions, it's recommended that you manually load the extension, even in Sequel 3. If you do not plan to use the core extensions, you can change from:
|
|
40
|
+
|
|
41
|
+
require 'sequel'
|
|
42
|
+
|
|
43
|
+
to:
|
|
44
|
+
|
|
45
|
+
require 'sequel/no_core_ext'
|
|
46
|
+
|
|
47
|
+
which will load Sequel without the core extensions.
|
|
48
|
+
|
|
49
|
+
== No Internal Dependency
|
|
50
|
+
|
|
51
|
+
Sequel has no internal dependency on the core extensions. This includes Sequel's core, Sequel::Model, and all plugins and extensions that ship with Sequel. However, it is possible that external plugins and extensions will depend on the core extensions. Such plugins and extensions should be updated so that they no longer depend on the core extensions.
|
|
52
|
+
|
|
53
|
+
== Core Extension Methods
|
|
54
|
+
|
|
55
|
+
This section will briefly describe all of the methods added to the core classes, and what the alternative method is that doesn't require the core extensions.
|
|
56
|
+
|
|
57
|
+
=== Symbol & String
|
|
58
|
+
|
|
59
|
+
==== as
|
|
60
|
+
|
|
61
|
+
Symbol#as and String#as return Sequel aliased expressions using the provided alias:
|
|
62
|
+
|
|
63
|
+
:a.as(:b) # SQL: a AS b
|
|
64
|
+
'a'.as(:b) # SQL: 'a' AS b
|
|
65
|
+
|
|
66
|
+
Alternative: Sequel.as:
|
|
67
|
+
|
|
68
|
+
Sequel.as(:a, :b)
|
|
69
|
+
|
|
70
|
+
==== cast
|
|
71
|
+
|
|
72
|
+
Symbol#cast and String#cast return Sequel cast expressions for typecasting in the database:
|
|
73
|
+
|
|
74
|
+
:a.cast(Integer) # SQL: CAST(a AS integer)
|
|
75
|
+
'a'.cast(Integer) # SQL: CAST('a' AS integer)
|
|
76
|
+
|
|
77
|
+
Alternative: Sequel.cast:
|
|
78
|
+
|
|
79
|
+
Sequel.cast(:a, Integer)
|
|
80
|
+
|
|
81
|
+
==== cast_numeric
|
|
82
|
+
|
|
83
|
+
Symbol#cast_numeric and String#cast_numeric return Sequel cast expressions for typecasting in the database, defaulting to integers, where the returned expression is treated as an numeric value:
|
|
84
|
+
|
|
85
|
+
:a.cast_numeric # SQL: CAST(a AS integer)
|
|
86
|
+
'a'.cast_numeric(Float) # SQL: CAST('a' AS double precision)
|
|
87
|
+
|
|
88
|
+
Alternative: Sequel.cast_numeric:
|
|
89
|
+
|
|
90
|
+
Sequel.cast_numeric(:a)
|
|
91
|
+
|
|
92
|
+
==== cast_string
|
|
93
|
+
|
|
94
|
+
Symbol#cast_string and String#cast_string return Sequel cast expressions for typecasting in the database, defaulting to strings, where the returned expression is treated as a string value:
|
|
95
|
+
|
|
96
|
+
:a.cast_string # SQL: CAST(a AS varchar(255))
|
|
97
|
+
'a'.cast_string(:text) # SQL: CAST('a' AS text)
|
|
98
|
+
|
|
99
|
+
Alternative: Sequel.cast_string:
|
|
100
|
+
|
|
101
|
+
Sequel.cast_string(:a)
|
|
102
|
+
|
|
103
|
+
=== Symbol
|
|
104
|
+
|
|
105
|
+
==== identifier
|
|
106
|
+
|
|
107
|
+
Symbol#identifier wraps the symbol in a single identifier that will not be split. By default, Sequel will split symbols with double or triple underscores to do qualifying and aliasing.
|
|
108
|
+
|
|
109
|
+
:table__column.identifier # SQL: table__column
|
|
110
|
+
|
|
111
|
+
Alternative: Sequel.identifier:
|
|
112
|
+
|
|
113
|
+
Sequel.identifier(:table__column)
|
|
114
|
+
|
|
115
|
+
==== asc
|
|
116
|
+
|
|
117
|
+
Symbol#asc is used to define an ascending order on a column. It exists mostly for consistency with #desc, since ascending is the default order:
|
|
118
|
+
|
|
119
|
+
:a.asc # SQL: a ASC
|
|
120
|
+
|
|
121
|
+
Alternative: Sequel.asc:
|
|
122
|
+
|
|
123
|
+
Sequel.asc(:a)
|
|
124
|
+
|
|
125
|
+
==== desc
|
|
126
|
+
|
|
127
|
+
Symbol#desc is used to defined a descending order on a column. The returned value is usually passed to one of the dataset order methods.
|
|
128
|
+
|
|
129
|
+
:a.desc # SQL: a DESC
|
|
130
|
+
|
|
131
|
+
Alternative: Sequel.desc:
|
|
132
|
+
|
|
133
|
+
Sequel.desc(:a)
|
|
134
|
+
|
|
135
|
+
==== +, -, *, /
|
|
136
|
+
|
|
137
|
+
The standard mathematical operators are defined on Symbol, and return a Sequel numeric expression object representing the operation:
|
|
138
|
+
|
|
139
|
+
:a + :b # SQL: a + b
|
|
140
|
+
:a - :b # SQL: a - b
|
|
141
|
+
:a * :b # SQL: a * b
|
|
142
|
+
:a / :b # SQL: a / b
|
|
143
|
+
|
|
144
|
+
Alternatives:
|
|
145
|
+
|
|
146
|
+
Sequel.+(:a, :b)
|
|
147
|
+
Sequel.-(:a, :b)
|
|
148
|
+
Sequel.*(:a, :b)
|
|
149
|
+
Sequel./(:a, :b)
|
|
150
|
+
|
|
151
|
+
==== *
|
|
152
|
+
|
|
153
|
+
The * operator is overloaded on Symbol such that if it is called with no arguments, it represents a selection of all columns in the table:
|
|
154
|
+
|
|
155
|
+
:a.* # SQL: a.*
|
|
156
|
+
|
|
157
|
+
Alternative: Sequel.expr.*:
|
|
158
|
+
|
|
159
|
+
Sequel.expr(:a).*
|
|
160
|
+
|
|
161
|
+
==== qualify
|
|
162
|
+
|
|
163
|
+
Symbol#qualify qualifies the identifier (e.g. a column) with a another identifier (e.g. a table):
|
|
164
|
+
|
|
165
|
+
:column.qualify(:table) # SQL: table.column
|
|
166
|
+
|
|
167
|
+
Alternative: Sequel.qualify:
|
|
168
|
+
|
|
169
|
+
Sequel.qualify(:table, :column)
|
|
170
|
+
|
|
171
|
+
Note the reversed order of the arguments. For the Symbol#qualify method, the argument is the qualifier, while for Sequel.qualify, the qualifier is the first argument.
|
|
172
|
+
|
|
173
|
+
==== like
|
|
174
|
+
|
|
175
|
+
Symbol#like returns a case sensitive LIKE expression between the identifier and the given argument:
|
|
176
|
+
|
|
177
|
+
:a.like('b%') # SQL: a LIKE 'b%'
|
|
178
|
+
|
|
179
|
+
Alternative: Sequel.like:
|
|
180
|
+
|
|
181
|
+
Sequel.like(:a, 'b%')
|
|
182
|
+
|
|
183
|
+
==== like
|
|
184
|
+
|
|
185
|
+
Symbol#ilike returns a case insensitive LIKE expression between the identifier and the given argument:
|
|
186
|
+
|
|
187
|
+
:a.ilike('b%') # SQL: a ILIKE 'b%'
|
|
188
|
+
|
|
189
|
+
Alternative: Sequel.ilike:
|
|
190
|
+
|
|
191
|
+
Sequel.ilike(:a, 'b%')
|
|
192
|
+
|
|
193
|
+
==== sql_subscript
|
|
194
|
+
|
|
195
|
+
Symbol#sql_subscript returns a Sequel expression representing an SQL array access:
|
|
196
|
+
|
|
197
|
+
:a.sql_subscript(1) # SQL: a[1]
|
|
198
|
+
|
|
199
|
+
Alternative: Sequel.subscript:
|
|
200
|
+
|
|
201
|
+
Sequel.subscript(:a, 1)
|
|
202
|
+
|
|
203
|
+
==== extract
|
|
204
|
+
|
|
205
|
+
Symbol#extract does a datetime part extraction from the receiver:
|
|
206
|
+
|
|
207
|
+
:a.extract(:year) # SQL: extract(year FROM a)
|
|
208
|
+
|
|
209
|
+
Alternative: Sequel.extract:
|
|
210
|
+
|
|
211
|
+
Sequel.extract(:year, :a)
|
|
212
|
+
|
|
213
|
+
Note the reversed order of the arguments. In Symbol#extract, the datetime part is the argument, while in Sequel.extract, the datetime part is the first argument.
|
|
214
|
+
|
|
215
|
+
==== sql_boolean, sql_number, sql_string
|
|
216
|
+
|
|
217
|
+
These Symbol methods are used to force the treating of the object as a specific SQL type, instead of as a general SQL type. For example:
|
|
218
|
+
|
|
219
|
+
:a.sql_boolean + 1 # NoMethodError
|
|
220
|
+
:a.sql_number << 1 # SQL: a << 1
|
|
221
|
+
:a.sql_string + 'a' # SQL: a || 'a'
|
|
222
|
+
|
|
223
|
+
Alternative: Sequel.expr:
|
|
224
|
+
|
|
225
|
+
Sequel.expr(:a).sql_boolean
|
|
226
|
+
Sequel.expr(:a).sql_number
|
|
227
|
+
Sequel.expr(:a).sql_string
|
|
228
|
+
|
|
229
|
+
==== sql_function
|
|
230
|
+
|
|
231
|
+
Symbol#sql_function returns an SQL function call expression object:
|
|
232
|
+
|
|
233
|
+
:now.sql_function # SQL: now()
|
|
234
|
+
:sum.sql_function(:a) # SQL: sum(a)
|
|
235
|
+
:concat.sql_function(:a, :b) # SQL: concat(a, b)
|
|
236
|
+
|
|
237
|
+
Alternative: Sequel.function:
|
|
238
|
+
|
|
239
|
+
Sequel.function(:now, :a)
|
|
240
|
+
|
|
241
|
+
=== String
|
|
242
|
+
|
|
243
|
+
==== lit
|
|
244
|
+
|
|
245
|
+
String#lit creates a literal string, using placeholders if any arguments are given. Literal strings are not escaped, they are treated as SQL code, not as an SQL string:
|
|
246
|
+
|
|
247
|
+
'a'.lit # SQL: a
|
|
248
|
+
'"a" = ?'.lit(1) # SQL: "a" = 1
|
|
249
|
+
|
|
250
|
+
Alternative: Sequel.lit:
|
|
251
|
+
|
|
252
|
+
Sequel.lit('a')
|
|
253
|
+
|
|
254
|
+
==== to_sequel_blob
|
|
255
|
+
|
|
256
|
+
String#to_sequel_blob returns the string wrapper in Sequel blob object. Often blobs need to be handled differently than regular strings by the database adapters.
|
|
257
|
+
|
|
258
|
+
"a\0".to_sequel_blob # SQL: X'6100'
|
|
259
|
+
|
|
260
|
+
Alternative: Sequel.blob:
|
|
261
|
+
|
|
262
|
+
Sequel.blob("a\0")
|
|
263
|
+
|
|
264
|
+
=== Hash, Array, & Symbol
|
|
265
|
+
|
|
266
|
+
==== ~
|
|
267
|
+
|
|
268
|
+
Array#~, Hash#~, and Symbol#~ treat the receiver as a conditions specifier, not matching all of the conditions:
|
|
269
|
+
|
|
270
|
+
~{:a=>1, :b=>[2, 3]} # SQL: a != 1 OR b NOT IN (2, 3)
|
|
271
|
+
~[[:a, 1], [:b, [1, 2]]] # SQL: a != 1 OR b NOT IN (1, 2)
|
|
272
|
+
|
|
273
|
+
Alternative: Sequel.~:
|
|
274
|
+
|
|
275
|
+
Sequel.~(:a=>1, :b=>[2, 3])
|
|
276
|
+
|
|
277
|
+
=== Hash & Array
|
|
278
|
+
|
|
279
|
+
==== case
|
|
280
|
+
|
|
281
|
+
Array#case and Hash#case return an SQL CASE expression, where the keys are conditions and the values are results:
|
|
282
|
+
|
|
283
|
+
{{:a=>[2,3]}=>1}.case(0) # SQL: CASE WHEN a IN (2, 3) THEN 1 ELSE 0 END
|
|
284
|
+
[[{:a=>[2,3]}, 1]].case(0) # SQL: CASE WHEN a IN (2, 3) THEN 1 ELSE 0 END
|
|
285
|
+
|
|
286
|
+
Alternative: Sequel.case:
|
|
287
|
+
|
|
288
|
+
Sequel.case({:a=>[2,3]}=>1}, 0)
|
|
289
|
+
|
|
290
|
+
==== sql_expr
|
|
291
|
+
|
|
292
|
+
Array#sql_expr and Hash#sql_expr treat the receiver as a conditions specifier, matching all of the conditions in the array.
|
|
293
|
+
|
|
294
|
+
{:a=>1, :b=>[2, 3]}.sql_expr # SQL: a = 1 AND b IN (2, 3)
|
|
295
|
+
[[:a, 1], [:b, [2, 3]]].sql_expr # SQL: a = 1 AND b IN (2, 3)
|
|
296
|
+
|
|
297
|
+
Alternative: Sequel.expr:
|
|
298
|
+
|
|
299
|
+
Sequel.expr(:a=>1, :b=>[2, 3])
|
|
300
|
+
|
|
301
|
+
==== sql_negate
|
|
302
|
+
|
|
303
|
+
Array#sql_negate and Hash#sql_negate treat the receiver as a conditions specifier, matching none of the conditions in the array:
|
|
304
|
+
|
|
305
|
+
{:a=>1, :b=>[2, 3]}.sql_negate # SQL: a != 1 AND b NOT IN (2, 3)
|
|
306
|
+
[[:a, 1], [:b, [2, 3]]].sql_negate # SQL: a != 1 AND b NOT IN (2, 3)
|
|
307
|
+
|
|
308
|
+
Alternative: Sequel.negate:
|
|
309
|
+
|
|
310
|
+
Sequel.negate(:a=>1, :b=>[2, 3])
|
|
311
|
+
|
|
312
|
+
==== sql_or
|
|
313
|
+
|
|
314
|
+
Array#sql_or nd Hash#sql_or treat the receiver as a conditions specifier, matching any of the conditions in the array:
|
|
315
|
+
|
|
316
|
+
{:a=>1, :b=>[2, 3]}.sql_or # SQL: a = 1 OR b IN (2, 3)
|
|
317
|
+
[[:a, 1], [:b, [2, 3]]].sql_or # SQL: a = 1 OR b IN (2, 3)
|
|
318
|
+
|
|
319
|
+
Alternative: Sequel.or:
|
|
320
|
+
|
|
321
|
+
Sequel.or(:a=>1, :b=>[2, 3])
|
|
322
|
+
|
|
323
|
+
=== Array
|
|
324
|
+
|
|
325
|
+
==== sql_value_list
|
|
326
|
+
|
|
327
|
+
Array#sql_value_list wraps the array in an array subclass, which Sequel will always treat as a value list and not a conditions specifier. By default, Sequel treats arrays of two element arrays as a conditions specifier.
|
|
328
|
+
|
|
329
|
+
DB[:a].filter('(a, b) IN ?', [[1, 2], [3, 4]]) # SQL: (a, b) IN ((1 = 2) AND (3 = 4))
|
|
330
|
+
DB[:a].filter('(a, b) IN ?', [[1, 2], [3, 4]].sql_value_list) # SQL: (a, b) IN ((1, 2), (3, 4))
|
|
331
|
+
|
|
332
|
+
Alternative: Sequel.value_list:
|
|
333
|
+
|
|
334
|
+
Sequel.value_list([[1, 2], [3, 4]])
|
|
335
|
+
|
|
336
|
+
==== sql_string_join
|
|
337
|
+
|
|
338
|
+
Array#sql_string_join joins all of the elements in the array in an SQL string concatentation expression:
|
|
339
|
+
|
|
340
|
+
[:a].sql_string_join # SQL: a
|
|
341
|
+
[:a, :b].sql_string_join # SQL: a || b
|
|
342
|
+
[:a, 'b'].sql_string_join # SQL: a || 'b'
|
|
343
|
+
['a', :b].sql_string_join(' ') # SQL: 'a' || ' ' || b
|
|
344
|
+
|
|
345
|
+
Alternative: Sequel.join:
|
|
346
|
+
|
|
347
|
+
Sequel.join(['a', :b], ' ')
|
|
348
|
+
|
|
349
|
+
=== Hash & Symbol
|
|
350
|
+
|
|
351
|
+
==== &
|
|
352
|
+
|
|
353
|
+
Hash#& and Symbol#& return a Sequel boolean expression, matching the condition specified by the receiver and the condition specified by the given argument:
|
|
354
|
+
|
|
355
|
+
:a & :b # SQL: a AND b
|
|
356
|
+
{:a=>1} & :b # SQL: a = 1 AND b
|
|
357
|
+
{:a=>true} & :b # SQL: a IS TRUE AND b
|
|
358
|
+
|
|
359
|
+
Alternative: Sequel.&:
|
|
360
|
+
|
|
361
|
+
Sequel.&({:a=>1}, :b)
|
|
362
|
+
|
|
363
|
+
==== |
|
|
364
|
+
|
|
365
|
+
Hash#| returns a Sequel boolean expression, matching the condition specified by the receiver or the condition specified by the given argument:
|
|
366
|
+
|
|
367
|
+
:a | :b # SQL: a OR b
|
|
368
|
+
{:a=>1} | :b # SQL: a = 1 OR b
|
|
369
|
+
{:a=>true} | :b # SQL: a IS TRUE OR b
|
|
370
|
+
|
|
371
|
+
Alternative: Sequel.|:
|
|
372
|
+
|
|
373
|
+
Sequel.|({:a=>1}, :b)
|
|
374
|
+
|
data/doc/dataset_basics.rdoc
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
== Introduction
|
|
4
4
|
|
|
5
|
-
Datasets are
|
|
5
|
+
Datasets are the primary way Sequel uses to access the database. While most database libraries have specific support for updating all records or only a single record, Sequel's ability to represent SQL queries themselves as objects is what gives Sequel most of its power. However, if you haven't been exposed to the dataset concept before, it can be a little disorienting. This document aims to give a basic introduction to datasets and how to use them.
|
|
6
6
|
|
|
7
7
|
== What a Dataset Represents
|
|
8
8
|
|
|
@@ -24,23 +24,23 @@ Here, DB represents your Sequel::Database object, and ds is your dataset, with t
|
|
|
24
24
|
|
|
25
25
|
One of the core dataset ideas that should be understood is that datasets use a functional style of modification, in which methods called on the dataset return modified copies of the dataset, they don't modify the dataset themselves:
|
|
26
26
|
|
|
27
|
-
ds2 = ds.
|
|
27
|
+
ds2 = ds.where(:id=>1)
|
|
28
28
|
ds2
|
|
29
29
|
# SELECT * FROM posts WHERE id = 1
|
|
30
30
|
ds
|
|
31
31
|
# SELECT * FROM posts
|
|
32
32
|
|
|
33
|
-
Note how ds itself is not modified. This is because ds.
|
|
33
|
+
Note how ds itself is not modified. This is because ds.where returns a modified copy of ds, instead of modifying ds itself. This makes using datasets both thread safe and easy to chain:
|
|
34
34
|
|
|
35
35
|
# Thread safe:
|
|
36
36
|
100.times do |i|
|
|
37
37
|
Thread.new do
|
|
38
|
-
ds.
|
|
38
|
+
ds.where(:id=>i).first
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# Easy to chain:
|
|
43
|
-
ds3 = ds.select(:id, :name).order(:name).
|
|
43
|
+
ds3 = ds.select(:id, :name).order(:name).where{id < 100}
|
|
44
44
|
# SELECT id, name FROM posts WHERE id < 100 ORDER BY name
|
|
45
45
|
|
|
46
46
|
Thread safety you don't really need to worry about, but chainability is core to how Sequel is generally used. Almost all dataset methods that affect the SQL produced return modified copies of the receiving dataset.
|
|
@@ -74,12 +74,12 @@ Most Dataset methods that users will use can be broken down into two types:
|
|
|
74
74
|
|
|
75
75
|
Most dataset methods fall into this category, which can be further broken down by the clause they affect:
|
|
76
76
|
|
|
77
|
-
SELECT:: select, select_all, select_append, select_more
|
|
77
|
+
SELECT:: select, select_all, select_append, select_group, select_more
|
|
78
78
|
FROM:: from, from_self
|
|
79
79
|
JOIN:: join, left_join, right_join, full_join, natural_join, natural_left_join, natural_right_join, natural_full_join, cross_join, inner_join, left_outer_join, right_outer_join, full_outer_join, join_table
|
|
80
|
-
WHERE:: where, filter, exclude, and, or, grep, invert, unfiltered
|
|
81
|
-
GROUP:: group, group_by, group_and_count, ungrouped
|
|
82
|
-
HAVING:: having, filter, exclude, and, or, grep, invert, unfiltered
|
|
80
|
+
WHERE:: where, filter, exclude, exclude_where, and, or, grep, invert, unfiltered
|
|
81
|
+
GROUP:: group, group_by, group_and_count, select_group, ungrouped
|
|
82
|
+
HAVING:: having, filter, exclude, exclude_having, and, or, grep, invert, unfiltered
|
|
83
83
|
ORDER:: order, order_by, order_append, order_prepend, order_more, reverse, reverse_order, unordered
|
|
84
84
|
LIMIT:: limit, unlimited
|
|
85
85
|
compounds:: union, intersect, except
|
|
@@ -93,7 +93,7 @@ other:: clone, distinct, naked, server, with_sql
|
|
|
93
93
|
|
|
94
94
|
Most other dataset methods commonly used will execute the dataset's SQL on the database:
|
|
95
95
|
|
|
96
|
-
SELECT (All Records):: all, each, map, to_hash, select_map, select_order_map, select_hash, to_csv
|
|
96
|
+
SELECT (All Records):: all, each, map, to_hash, to_hash_groups, select_map, select_order_map, select_hash, select_hash_groups, to_csv
|
|
97
97
|
SELECT (First Record):: first, last, get, []
|
|
98
98
|
SELECT (Aggregates):: count, avg, max, min, sum, range, interval
|
|
99
99
|
INSERT:: insert, <<, import, multi_insert, insert_multiple
|