viking-sequel 3.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3134 -0
- data/COPYING +19 -0
- data/README.rdoc +723 -0
- data/Rakefile +193 -0
- data/bin/sequel +196 -0
- data/doc/advanced_associations.rdoc +644 -0
- data/doc/cheat_sheet.rdoc +218 -0
- data/doc/dataset_basics.rdoc +106 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/opening_databases.rdoc +296 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/reflection.rdoc +84 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.11.0.txt +215 -0
- data/doc/release_notes/2.12.0.txt +534 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/release_notes/3.0.0.txt +221 -0
- data/doc/release_notes/3.1.0.txt +406 -0
- data/doc/release_notes/3.10.0.txt +286 -0
- data/doc/release_notes/3.2.0.txt +268 -0
- data/doc/release_notes/3.3.0.txt +192 -0
- data/doc/release_notes/3.4.0.txt +325 -0
- data/doc/release_notes/3.5.0.txt +510 -0
- data/doc/release_notes/3.6.0.txt +366 -0
- data/doc/release_notes/3.7.0.txt +179 -0
- data/doc/release_notes/3.8.0.txt +151 -0
- data/doc/release_notes/3.9.0.txt +233 -0
- data/doc/schema.rdoc +36 -0
- data/doc/sharding.rdoc +113 -0
- data/doc/virtual_rows.rdoc +205 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel/adapters/ado.rb +90 -0
- data/lib/sequel/adapters/ado/mssql.rb +30 -0
- data/lib/sequel/adapters/amalgalite.rb +176 -0
- data/lib/sequel/adapters/db2.rb +139 -0
- data/lib/sequel/adapters/dbi.rb +113 -0
- data/lib/sequel/adapters/do.rb +188 -0
- data/lib/sequel/adapters/do/mysql.rb +49 -0
- data/lib/sequel/adapters/do/postgres.rb +91 -0
- data/lib/sequel/adapters/do/sqlite.rb +40 -0
- data/lib/sequel/adapters/firebird.rb +283 -0
- data/lib/sequel/adapters/informix.rb +77 -0
- data/lib/sequel/adapters/jdbc.rb +587 -0
- data/lib/sequel/adapters/jdbc/as400.rb +58 -0
- data/lib/sequel/adapters/jdbc/h2.rb +133 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +57 -0
- data/lib/sequel/adapters/jdbc/mysql.rb +78 -0
- data/lib/sequel/adapters/jdbc/oracle.rb +50 -0
- data/lib/sequel/adapters/jdbc/postgresql.rb +108 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +55 -0
- data/lib/sequel/adapters/mysql.rb +421 -0
- data/lib/sequel/adapters/odbc.rb +143 -0
- data/lib/sequel/adapters/odbc/mssql.rb +42 -0
- data/lib/sequel/adapters/openbase.rb +64 -0
- data/lib/sequel/adapters/oracle.rb +131 -0
- data/lib/sequel/adapters/postgres.rb +504 -0
- data/lib/sequel/adapters/shared/mssql.rb +490 -0
- data/lib/sequel/adapters/shared/mysql.rb +498 -0
- data/lib/sequel/adapters/shared/oracle.rb +195 -0
- data/lib/sequel/adapters/shared/postgres.rb +830 -0
- data/lib/sequel/adapters/shared/progress.rb +44 -0
- data/lib/sequel/adapters/shared/sqlite.rb +389 -0
- data/lib/sequel/adapters/sqlite.rb +224 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +84 -0
- data/lib/sequel/connection_pool.rb +99 -0
- data/lib/sequel/connection_pool/sharded_single.rb +84 -0
- data/lib/sequel/connection_pool/sharded_threaded.rb +211 -0
- data/lib/sequel/connection_pool/single.rb +29 -0
- data/lib/sequel/connection_pool/threaded.rb +150 -0
- data/lib/sequel/core.rb +293 -0
- data/lib/sequel/core_sql.rb +241 -0
- data/lib/sequel/database.rb +1079 -0
- data/lib/sequel/database/schema_generator.rb +327 -0
- data/lib/sequel/database/schema_methods.rb +203 -0
- data/lib/sequel/database/schema_sql.rb +320 -0
- data/lib/sequel/dataset.rb +32 -0
- data/lib/sequel/dataset/actions.rb +441 -0
- data/lib/sequel/dataset/features.rb +86 -0
- data/lib/sequel/dataset/graph.rb +254 -0
- data/lib/sequel/dataset/misc.rb +119 -0
- data/lib/sequel/dataset/mutation.rb +64 -0
- data/lib/sequel/dataset/prepared_statements.rb +227 -0
- data/lib/sequel/dataset/query.rb +709 -0
- data/lib/sequel/dataset/sql.rb +996 -0
- data/lib/sequel/exceptions.rb +51 -0
- data/lib/sequel/extensions/blank.rb +43 -0
- data/lib/sequel/extensions/inflector.rb +242 -0
- data/lib/sequel/extensions/looser_typecasting.rb +21 -0
- data/lib/sequel/extensions/migration.rb +239 -0
- data/lib/sequel/extensions/named_timezones.rb +61 -0
- data/lib/sequel/extensions/pagination.rb +100 -0
- data/lib/sequel/extensions/pretty_table.rb +82 -0
- data/lib/sequel/extensions/query.rb +52 -0
- data/lib/sequel/extensions/schema_dumper.rb +271 -0
- data/lib/sequel/extensions/sql_expr.rb +122 -0
- data/lib/sequel/extensions/string_date_time.rb +46 -0
- data/lib/sequel/extensions/thread_local_timezones.rb +48 -0
- data/lib/sequel/metaprogramming.rb +9 -0
- data/lib/sequel/model.rb +120 -0
- data/lib/sequel/model/associations.rb +1514 -0
- data/lib/sequel/model/base.rb +1069 -0
- data/lib/sequel/model/default_inflections.rb +45 -0
- data/lib/sequel/model/errors.rb +39 -0
- data/lib/sequel/model/exceptions.rb +21 -0
- data/lib/sequel/model/inflections.rb +162 -0
- data/lib/sequel/model/plugins.rb +70 -0
- data/lib/sequel/plugins/active_model.rb +59 -0
- data/lib/sequel/plugins/association_dependencies.rb +103 -0
- data/lib/sequel/plugins/association_proxies.rb +41 -0
- data/lib/sequel/plugins/boolean_readers.rb +53 -0
- data/lib/sequel/plugins/caching.rb +141 -0
- data/lib/sequel/plugins/class_table_inheritance.rb +214 -0
- data/lib/sequel/plugins/composition.rb +138 -0
- data/lib/sequel/plugins/force_encoding.rb +72 -0
- data/lib/sequel/plugins/hook_class_methods.rb +126 -0
- data/lib/sequel/plugins/identity_map.rb +116 -0
- data/lib/sequel/plugins/instance_filters.rb +98 -0
- data/lib/sequel/plugins/instance_hooks.rb +57 -0
- data/lib/sequel/plugins/lazy_attributes.rb +77 -0
- data/lib/sequel/plugins/many_through_many.rb +208 -0
- data/lib/sequel/plugins/nested_attributes.rb +206 -0
- data/lib/sequel/plugins/optimistic_locking.rb +81 -0
- data/lib/sequel/plugins/rcte_tree.rb +281 -0
- data/lib/sequel/plugins/schema.rb +66 -0
- data/lib/sequel/plugins/serialization.rb +166 -0
- data/lib/sequel/plugins/single_table_inheritance.rb +74 -0
- data/lib/sequel/plugins/subclasses.rb +45 -0
- data/lib/sequel/plugins/tactical_eager_loading.rb +61 -0
- data/lib/sequel/plugins/timestamps.rb +87 -0
- data/lib/sequel/plugins/touch.rb +118 -0
- data/lib/sequel/plugins/typecast_on_load.rb +72 -0
- data/lib/sequel/plugins/validation_class_methods.rb +405 -0
- data/lib/sequel/plugins/validation_helpers.rb +223 -0
- data/lib/sequel/sql.rb +1020 -0
- data/lib/sequel/timezones.rb +161 -0
- data/lib/sequel/version.rb +12 -0
- data/lib/sequel_core.rb +1 -0
- data/lib/sequel_model.rb +1 -0
- data/spec/adapters/firebird_spec.rb +407 -0
- data/spec/adapters/informix_spec.rb +97 -0
- data/spec/adapters/mssql_spec.rb +403 -0
- data/spec/adapters/mysql_spec.rb +1019 -0
- data/spec/adapters/oracle_spec.rb +286 -0
- data/spec/adapters/postgres_spec.rb +969 -0
- data/spec/adapters/spec_helper.rb +51 -0
- data/spec/adapters/sqlite_spec.rb +432 -0
- data/spec/core/connection_pool_spec.rb +808 -0
- data/spec/core/core_sql_spec.rb +417 -0
- data/spec/core/database_spec.rb +1662 -0
- data/spec/core/dataset_spec.rb +3827 -0
- data/spec/core/expression_filters_spec.rb +595 -0
- data/spec/core/object_graph_spec.rb +296 -0
- data/spec/core/schema_generator_spec.rb +159 -0
- data/spec/core/schema_spec.rb +830 -0
- data/spec/core/spec_helper.rb +56 -0
- data/spec/core/version_spec.rb +7 -0
- data/spec/extensions/active_model_spec.rb +76 -0
- data/spec/extensions/association_dependencies_spec.rb +127 -0
- data/spec/extensions/association_proxies_spec.rb +50 -0
- data/spec/extensions/blank_spec.rb +67 -0
- data/spec/extensions/boolean_readers_spec.rb +92 -0
- data/spec/extensions/caching_spec.rb +250 -0
- data/spec/extensions/class_table_inheritance_spec.rb +252 -0
- data/spec/extensions/composition_spec.rb +194 -0
- data/spec/extensions/force_encoding_spec.rb +117 -0
- data/spec/extensions/hook_class_methods_spec.rb +470 -0
- data/spec/extensions/identity_map_spec.rb +202 -0
- data/spec/extensions/inflector_spec.rb +181 -0
- data/spec/extensions/instance_filters_spec.rb +55 -0
- data/spec/extensions/instance_hooks_spec.rb +133 -0
- data/spec/extensions/lazy_attributes_spec.rb +153 -0
- data/spec/extensions/looser_typecasting_spec.rb +39 -0
- data/spec/extensions/many_through_many_spec.rb +884 -0
- data/spec/extensions/migration_spec.rb +332 -0
- data/spec/extensions/named_timezones_spec.rb +72 -0
- data/spec/extensions/nested_attributes_spec.rb +396 -0
- data/spec/extensions/optimistic_locking_spec.rb +100 -0
- data/spec/extensions/pagination_spec.rb +99 -0
- data/spec/extensions/pretty_table_spec.rb +91 -0
- data/spec/extensions/query_spec.rb +85 -0
- data/spec/extensions/rcte_tree_spec.rb +205 -0
- data/spec/extensions/schema_dumper_spec.rb +357 -0
- data/spec/extensions/schema_spec.rb +127 -0
- data/spec/extensions/serialization_spec.rb +209 -0
- data/spec/extensions/single_table_inheritance_spec.rb +96 -0
- data/spec/extensions/spec_helper.rb +91 -0
- data/spec/extensions/sql_expr_spec.rb +89 -0
- data/spec/extensions/string_date_time_spec.rb +93 -0
- data/spec/extensions/subclasses_spec.rb +52 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +65 -0
- data/spec/extensions/thread_local_timezones_spec.rb +45 -0
- data/spec/extensions/timestamps_spec.rb +150 -0
- data/spec/extensions/touch_spec.rb +155 -0
- data/spec/extensions/typecast_on_load_spec.rb +69 -0
- data/spec/extensions/validation_class_methods_spec.rb +984 -0
- data/spec/extensions/validation_helpers_spec.rb +438 -0
- data/spec/integration/associations_test.rb +281 -0
- data/spec/integration/database_test.rb +26 -0
- data/spec/integration/dataset_test.rb +963 -0
- data/spec/integration/eager_loader_test.rb +734 -0
- data/spec/integration/model_test.rb +130 -0
- data/spec/integration/plugin_test.rb +814 -0
- data/spec/integration/prepared_statement_test.rb +213 -0
- data/spec/integration/schema_test.rb +361 -0
- data/spec/integration/spec_helper.rb +73 -0
- data/spec/integration/timezone_test.rb +55 -0
- data/spec/integration/transaction_test.rb +122 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/model/association_reflection_spec.rb +175 -0
- data/spec/model/associations_spec.rb +2633 -0
- data/spec/model/base_spec.rb +418 -0
- data/spec/model/dataset_methods_spec.rb +78 -0
- data/spec/model/eager_loading_spec.rb +1391 -0
- data/spec/model/hooks_spec.rb +240 -0
- data/spec/model/inflector_spec.rb +26 -0
- data/spec/model/model_spec.rb +593 -0
- data/spec/model/plugins_spec.rb +236 -0
- data/spec/model/record_spec.rb +1500 -0
- data/spec/model/spec_helper.rb +97 -0
- data/spec/model/validations_spec.rb +153 -0
- data/spec/rcov.opts +6 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +346 -0
@@ -0,0 +1,296 @@
|
|
1
|
+
= Connecting to a database
|
2
|
+
|
3
|
+
All Sequel activity begins with connecting to a database, which creates a
|
4
|
+
Sequel::Database object. The Database object is used to create datasets and execute
|
5
|
+
queries. Sequel provides a powerful and flexible mechanism for connecting to
|
6
|
+
databases. There are two main ways to establish database connections:
|
7
|
+
|
8
|
+
1. Using the Sequel.connect method
|
9
|
+
2. Using the specialized adapter method (Sequel.sqlite, Sequel.postgres, etc.)
|
10
|
+
|
11
|
+
The connection options needed depend on the adapter being used, though most adapters
|
12
|
+
share the same basic connection options.
|
13
|
+
|
14
|
+
If you are only connecting to a single database, it is recommended that you store the
|
15
|
+
database object in a constant named DB. This should never be required, but it is the
|
16
|
+
convention that most Sequel code uses.
|
17
|
+
|
18
|
+
== Using the Sequel.connect method
|
19
|
+
|
20
|
+
The connect method usually takes a well-formed URI, which is parsed into connection options needed to open
|
21
|
+
the database connection. The scheme/protocol part of the URI is used to determine the adapter to use:
|
22
|
+
|
23
|
+
DB = Sequel.connect('postgres://user:password@localhost/blog') # Uses the postgres adapter
|
24
|
+
|
25
|
+
You can use URI query parameters to specify options:
|
26
|
+
|
27
|
+
DB = Sequel.connect('postgres://localhost/blog?user=user&password=password')
|
28
|
+
|
29
|
+
You can also pass an additional option hash with the connection string:
|
30
|
+
|
31
|
+
DB = Sequel.connect('postgres://localhost/blog' :user=>'user', :password=>'password')
|
32
|
+
|
33
|
+
You can also just use an options hash without a connection string. If you do this, you must
|
34
|
+
provide the adapter to use:
|
35
|
+
|
36
|
+
DB = Sequel.connect(:adapter=>'postgres', :host=>'localhost', :database=>'blog', :user=>'user', :password=>'password')
|
37
|
+
|
38
|
+
All of the above statements are equivalent.
|
39
|
+
|
40
|
+
== Using the specialized adapter method
|
41
|
+
|
42
|
+
The specialized adapter method is similar to Sequel.connect with an options hash, except that it
|
43
|
+
automatically populates the :adapter option and assumes the first argument is the :database option,
|
44
|
+
unless the first argument is a hash. So the following statements are equivalent to the previous statements.
|
45
|
+
|
46
|
+
DB = Sequel.postgres('blog', :host=>'localhost', :user=>'user', :password=>'password')
|
47
|
+
DB = Sequel.postgres(:host=>'localhost', :user=>'user', :password=>'password', :database=>'blog')
|
48
|
+
|
49
|
+
== Passing a block to either method
|
50
|
+
|
51
|
+
Both the Sequel.connect method and the specialized adapter methods take a block. If you
|
52
|
+
provide a block to the method, Sequel will create a Database object and pass it as an argument
|
53
|
+
to the block. When the block returns, Sequel will disconnect the database connection.
|
54
|
+
For example:
|
55
|
+
|
56
|
+
Sequel.connect('sqlite://blog.db'){|db| puts db[:users].count}
|
57
|
+
|
58
|
+
== General connection options
|
59
|
+
|
60
|
+
These options are shared by all adapters unless otherwise noted.
|
61
|
+
|
62
|
+
* :adapter - The adapter to use
|
63
|
+
* :database - The name of the database to which to connect
|
64
|
+
* :default_schema - The database schema to use by default.
|
65
|
+
* :host - The hostname of the database server to which to connect
|
66
|
+
* :logger - An array of SQL loggers to log to
|
67
|
+
* :loggers - An array of SQL loggers to log to
|
68
|
+
* :password - The password for the user account
|
69
|
+
* :servers - A hash with symbol keys and hash or proc values, used with master/slave/partitioned database configurations
|
70
|
+
* :single_threaded - Whether to use a single-threaded (non-thread safe) connection pool
|
71
|
+
* :test - Whether to test that a valid database connection can be made (false by default)
|
72
|
+
* :user - The user account name to use logging in
|
73
|
+
|
74
|
+
The following options can be specified and are passed to the the database's internal connection pool.
|
75
|
+
|
76
|
+
* :after_connect - A proc called after a new connection is made, with the connection object (default: nil)
|
77
|
+
* :max_connections - The maximum size of the connection pool (default: 4 connections on most databases)
|
78
|
+
* :pool_sleep_time - The number of seconds to sleep before trying to acquire a connection again (default: 0.001 seconds)
|
79
|
+
* :pool_timeout - The number of seconds to wait if a connection cannot be acquired before raising an error (default: 5 seconds)
|
80
|
+
|
81
|
+
== Adapter specific connection options
|
82
|
+
|
83
|
+
The following sections explain the options and behavior specific to each adapter.
|
84
|
+
If the library the adapter requires is different from the name of the adapter
|
85
|
+
scheme, it is listed specifically, otherwise you can assume that is requires the
|
86
|
+
library with the same name.
|
87
|
+
|
88
|
+
=== ado
|
89
|
+
|
90
|
+
Requires: win32ole
|
91
|
+
|
92
|
+
The ADO adapter provides connectivity to ADO databases in Windows. It relies
|
93
|
+
on WIN32OLE library, so it isn't usable on other operating systems (except
|
94
|
+
possibly through WINE, but that's fairly unlikely).
|
95
|
+
|
96
|
+
The following options are supported:
|
97
|
+
* :driver - The driver to use. The default if not specified is 'SQL Server'.
|
98
|
+
* :command_timeout - Sets the time in seconds to wait while attempting
|
99
|
+
to execute a command before cancelling the attempt and generating
|
100
|
+
an error. Specifically, it sets the ADO CommandTimeout property.
|
101
|
+
If this property is not set, the default of 30 seconds is used.
|
102
|
+
* :conn_string - The full ADO connection string. If this is provided,
|
103
|
+
the general connection options are ignored.
|
104
|
+
* :provider - Sets the Provider of this ADO connection (for example, "SQLOLEDB")
|
105
|
+
|
106
|
+
=== amalgalite
|
107
|
+
|
108
|
+
Amalgalite is an ruby extension that provides self contained access to SQLite,
|
109
|
+
so you don't need to install SQLite separately. As amalgalite is a file backed
|
110
|
+
database, the :host, :user, and :password options are not used.
|
111
|
+
|
112
|
+
* :database - The name of the database file
|
113
|
+
* :timeout - The busy timeout period given in milliseconds
|
114
|
+
|
115
|
+
Without a database argument, assumes a memory database, so you can do:
|
116
|
+
|
117
|
+
Sequel.amalgalite
|
118
|
+
|
119
|
+
Handles paths in the connection string similar to the SQLite adapter, so see
|
120
|
+
the sqlite section below for details.
|
121
|
+
|
122
|
+
=== db2
|
123
|
+
|
124
|
+
Requires: db2/db2cli
|
125
|
+
|
126
|
+
I'm not even sure exactly how this works, or if it works at all (I've never heard from
|
127
|
+
anyone who attempted to use it). It uses the SQL_HANDLE_DBC constant to
|
128
|
+
get a handle, and respects the :database, :user, and :password options. It doesn't
|
129
|
+
appear to respect the :host or :port options.
|
130
|
+
|
131
|
+
=== dbi
|
132
|
+
|
133
|
+
Allows access to a multitude of databases via ruby-dbi. Additional options:
|
134
|
+
|
135
|
+
* :db_type - Specifying 'mssql' allows Microsoft SQL Server specific syntax to
|
136
|
+
be used. Otherwise has no effect.
|
137
|
+
|
138
|
+
DBI connection strings are a preprocessed a bit, and are specified with a dbi-
|
139
|
+
in front of the protocol. Examples:
|
140
|
+
|
141
|
+
dbi-ado://...
|
142
|
+
dbi-db2://...
|
143
|
+
dbi-frontbase://...
|
144
|
+
dbi-interbase://...
|
145
|
+
dbi-msql://...
|
146
|
+
dbi-mysql://...
|
147
|
+
dbi-odbc://...
|
148
|
+
dbi-oracle://...
|
149
|
+
dbi-pg://...
|
150
|
+
dbi-proxy://...
|
151
|
+
dbi-sqlite://...
|
152
|
+
dbi-sqlrelay://...
|
153
|
+
|
154
|
+
While the DBI adapter does work, it is recommended that you use another adapter
|
155
|
+
if your database supports it.
|
156
|
+
|
157
|
+
=== do
|
158
|
+
|
159
|
+
Requires: data_objects
|
160
|
+
|
161
|
+
The DataObjects adapter supports PostgreSQL, MySQL, and SQLite. One possible
|
162
|
+
advantage of using DataObjects is that it does the typecasting in C, which may
|
163
|
+
be faster than the other adapters.
|
164
|
+
|
165
|
+
Similar to the JDBC adapter, the DO adapter only cares about connection strings,
|
166
|
+
which can either be the String argument given to Sequel.connect directly or contained
|
167
|
+
in a :uri or :url option. The DO adapter passes through the connection string
|
168
|
+
directly to DataObjects, it does no processing of it.
|
169
|
+
|
170
|
+
Connection string examples:
|
171
|
+
|
172
|
+
do:sqlite3::memory:
|
173
|
+
do:postgres://user:password@host/database
|
174
|
+
do:mysql://user:password@host/database
|
175
|
+
|
176
|
+
=== firebird
|
177
|
+
|
178
|
+
Requires: fb (using code at http://github.com/wishdev/fb)
|
179
|
+
|
180
|
+
Does not support the :port option.
|
181
|
+
|
182
|
+
=== informix
|
183
|
+
|
184
|
+
Does not support the :host or :port options.
|
185
|
+
|
186
|
+
=== jdbc
|
187
|
+
|
188
|
+
Requires: java
|
189
|
+
|
190
|
+
Houses Sequel's JDBC support when running on JRuby.
|
191
|
+
Support for individual database types is done using sub adapters.
|
192
|
+
There are currently subadapters for PostgreSQL, MySQL, SQLite, H2,
|
193
|
+
Oracle, MSSQL, and AS400. All except Oracle, MSSQL, and AS400 can load the
|
194
|
+
JDBC gem, for those you need to have the .jar in your CLASSPATH
|
195
|
+
or load the Java class manually.
|
196
|
+
|
197
|
+
You just use the JDBC connection string directly, which can be specified
|
198
|
+
via the string given to Sequel.connect or via the :uri, :url, or :database options.
|
199
|
+
Sequel does no preprocessing of the string, it passes it directly to JDBC.
|
200
|
+
So if you have problems getting a connection string to work, look up the JDBC
|
201
|
+
documentation.
|
202
|
+
|
203
|
+
Example connections strings:
|
204
|
+
|
205
|
+
jdbc:sqlite::memory:
|
206
|
+
jdbc:postgresql://localhost/database?user=username
|
207
|
+
jdbc:mysql://localhost/test?user=root&password=root
|
208
|
+
jdbc:h2:mem:
|
209
|
+
|
210
|
+
You can also use JNDI connection strings:
|
211
|
+
|
212
|
+
jdbc:jndi:java:comp/env/jndi_resource_name
|
213
|
+
|
214
|
+
The following additional options are supported:
|
215
|
+
|
216
|
+
* :convert_types - If set to false, does not attempt to convert some Java types to ruby types.
|
217
|
+
Setting to false roughly doubles performance when selecting large numbers of rows.
|
218
|
+
Note that you can't provide this option inside the connection string (as that is passed
|
219
|
+
directly to JDBC), you have to pass it as a separate option.
|
220
|
+
|
221
|
+
=== mysql
|
222
|
+
|
223
|
+
The MySQL adapter does not support the pure-ruby MySQL adapter that used to ship with
|
224
|
+
ActiveRecord, it requires the native adapter.
|
225
|
+
|
226
|
+
The following additional options are supported:
|
227
|
+
|
228
|
+
* :auto_is_null - If set to true, makes "WHERE primary_key IS NULL" select the last inserted id.
|
229
|
+
* :charset - Same as :encoding, :encoding takes precedence.
|
230
|
+
* :compress - Whether to compress data sent/received via the socket connection.
|
231
|
+
* :config_default_group - The default group to read from the in the MySQL config file.
|
232
|
+
* :config_local_infile - If provided, sets the Mysql::OPT_LOCAL_INFILE option on the connection with the given value.
|
233
|
+
* :encoding - Specify the encoding/character set to use for the connection.
|
234
|
+
* :socket - Can be used to specify a Unix socket file to connect to instead of a TCP host and port.
|
235
|
+
* :timeout - Sets the wait_timeout for the connection, defaults to 1 month.
|
236
|
+
|
237
|
+
=== odbc
|
238
|
+
|
239
|
+
The ODBC adapter allows you to connect to any database with the appropriate ODBC drivers installed.
|
240
|
+
The :database option given ODBC database should be the DSN (Descriptive Service Name) from the ODBC configuration.
|
241
|
+
|
242
|
+
Sequel.odbc('mydb', :user => "user", :password => "password")
|
243
|
+
|
244
|
+
The :host and :port options are not respected. The following additional options are supported:
|
245
|
+
|
246
|
+
* :db_type - Can be specified as 'mssql' or 'progress' to use SQL syntax specific to those databases.
|
247
|
+
* :driver - The name of the ODBC driver to utilize.
|
248
|
+
|
249
|
+
=== openbase
|
250
|
+
|
251
|
+
The :port option is ignored.
|
252
|
+
|
253
|
+
=== oracle
|
254
|
+
|
255
|
+
Requires: oci8
|
256
|
+
|
257
|
+
The following additional options are supported:
|
258
|
+
|
259
|
+
* :privilege - The Oracle privilege level.
|
260
|
+
|
261
|
+
=== postgres
|
262
|
+
|
263
|
+
Requires: pg (or postgres if pg is not available)
|
264
|
+
|
265
|
+
The Sequel postgres adapter works with the pg, postgres, and postgres-pr ruby libraries.
|
266
|
+
The pg library is the best supported, as it supports real bound variables and prepared statements.
|
267
|
+
|
268
|
+
The following additional options are supported:
|
269
|
+
|
270
|
+
* :charset - Same as :encoding, :encoding takes precedence
|
271
|
+
* :encoding - Set the client_encoding to the given string
|
272
|
+
|
273
|
+
=== sqlite
|
274
|
+
|
275
|
+
As SQLite is a file-based database, the :host and :port options are ignored, and
|
276
|
+
the :database option should be a path to the file.
|
277
|
+
|
278
|
+
Examples:
|
279
|
+
|
280
|
+
# In Memory databases:
|
281
|
+
Sequel.sqlite
|
282
|
+
Sequel.connect('sqlite:/')
|
283
|
+
Sequel.sqlite(':memory:')
|
284
|
+
|
285
|
+
# Relative Path
|
286
|
+
Sequel.sqlite('blog.db')
|
287
|
+
Sequel.sqlite('./blog.db')
|
288
|
+
Sequel.connect('sqlite://blog.db')
|
289
|
+
|
290
|
+
# Absolute Path
|
291
|
+
Sequel.sqlite('/var/sqlite/blog.db')
|
292
|
+
Sequel.connect('sqlite:///var/sqlite/blog.db')
|
293
|
+
|
294
|
+
The following additional options are supported:
|
295
|
+
|
296
|
+
* :timeout - the busy timeout to use in milliseconds (default: 5000).
|
@@ -0,0 +1,104 @@
|
|
1
|
+
= Prepared Statements and Bound Variables
|
2
|
+
|
3
|
+
Sequel has support for prepared statements and bound variables. No matter which
|
4
|
+
database you are using, the Sequel prepared statement/bound variable API remains
|
5
|
+
the same. There is native support for prepared statements/bound variables on
|
6
|
+
the following databases:
|
7
|
+
|
8
|
+
* PostgreSQL (using the pg driver, requires type specifiers)
|
9
|
+
* MySQL (prepared statements only, as the ruby mysql driver doesn't support
|
10
|
+
bound variables)
|
11
|
+
* SQLite (a new native prepared statement is used for each call, though)
|
12
|
+
* JDBC
|
13
|
+
|
14
|
+
Support on other databases is emulated via string interpolation.
|
15
|
+
|
16
|
+
== Placeholders
|
17
|
+
|
18
|
+
Generally, when using prepared statements (and certainly when using bound
|
19
|
+
variables), you need to put placeholders in your SQL to indicate where you
|
20
|
+
want your bound arguments to appear. Database support and syntax vary
|
21
|
+
significantly for placeholders (e.g. :name, $1, ?). Sequel abstracts all of
|
22
|
+
that and allows you to specify placeholders by using the :$name format for
|
23
|
+
placeholders, e.g.:
|
24
|
+
|
25
|
+
ds = DB[:items].filter(:name=>:$n)
|
26
|
+
|
27
|
+
== Bound Variables
|
28
|
+
|
29
|
+
Using bound variables for this query is simple:
|
30
|
+
|
31
|
+
ds.call(:select, :n=>'Jim')
|
32
|
+
|
33
|
+
This will do the equivalent of selecting records that have the name 'Jim'. It
|
34
|
+
returns all records, and can take a block that is passed to Dataset#all.
|
35
|
+
|
36
|
+
Deleting or returning the first record works similarly:
|
37
|
+
|
38
|
+
ds.call(:first, :n=>'Jim') # First record with name 'Jim'
|
39
|
+
ds.call(:delete, :n=>'Jim') # Delete records with name 'Jim'
|
40
|
+
|
41
|
+
For inserting/updating records, you should also specify a value hash, which
|
42
|
+
may itself contain placeholders:
|
43
|
+
|
44
|
+
# Insert record with 'Jim', note that the previous filter is ignored
|
45
|
+
ds.call(:insert, {:n=>'Jim'}, :name=>:$n)
|
46
|
+
# Change name to 'Bob' for all records with name of 'Jim'
|
47
|
+
ds.call(:update, {:n=>'Jim', :new_n=>'Bob'}, :name=>$:new_n)
|
48
|
+
|
49
|
+
== Prepared Statements
|
50
|
+
|
51
|
+
Prepared statement support is similar to bound variable support, but you
|
52
|
+
use Dataset#prepare with a name, and Dataset#call or Database#call later with the values:
|
53
|
+
|
54
|
+
ds = DB[:items].filter(:name=>:$n)
|
55
|
+
ps = ds.prepare(:select, :select_by_name)
|
56
|
+
ps.call(:n=>'Jim')
|
57
|
+
DB.call(:select_by_name, :n=>'Jim') # same as above
|
58
|
+
|
59
|
+
The Dataset#prepare method returns a prepared statement, and also stores a
|
60
|
+
copy of the prepared statement in the database for later use. For insert
|
61
|
+
and update queries, the hash to insert/update is passed to prepare:
|
62
|
+
|
63
|
+
ps1 = DB[:items].prepare(:insert, :insert_with_name, :name=>:$n)
|
64
|
+
ps1.call(:n=>'Jim')
|
65
|
+
DB.call(:insert_with_name, :n=>'Jim') # same as above
|
66
|
+
ds = DB[:items].filter(:name=>:$n)
|
67
|
+
ps2 = ds.prepare(:update, :update_name, :name=>:$new_n)
|
68
|
+
ps2.call(:n=>'Jim', :new_n=>'Bob')
|
69
|
+
DB.call(:update_name, :n=>'Jim', :new_n=>'Bob') # same as above
|
70
|
+
|
71
|
+
== Database support
|
72
|
+
|
73
|
+
=== PostgreSQL
|
74
|
+
|
75
|
+
If you are using the ruby-postgres or postgres-pr driver, PostgreSQL uses the
|
76
|
+
default emulated support. If you are using ruby-pg, there is native support,
|
77
|
+
but it requires type specifiers most of the time. This is easy if you have
|
78
|
+
direct control over the SQL string, but since Sequel abstracts that, the types
|
79
|
+
have to be specified another way. This is done by adding a __* suffix to the
|
80
|
+
placeholder symbol (e.g. :$name__text, which will be compiled to "$1::text"
|
81
|
+
in the SQL). Prepared statements are always server side.
|
82
|
+
|
83
|
+
=== SQLite
|
84
|
+
|
85
|
+
SQLite supports bound variables and prepared statements exactly the same, since
|
86
|
+
a new native prepared statement is created and executed for each call.
|
87
|
+
|
88
|
+
=== MySQL
|
89
|
+
|
90
|
+
The MySQL ruby driver does not support bound variables, so the the bound
|
91
|
+
variable methods fall back to string interpolation. It uses server side
|
92
|
+
prepared statements.
|
93
|
+
|
94
|
+
=== JDBC
|
95
|
+
|
96
|
+
JDBC supports both prepared statements and bound variables. Whether these
|
97
|
+
are server side or client side depends on the JDBC driver. For PostgreSQL
|
98
|
+
over JDBC, you can add the prepareThreshold=N parameter to the connection
|
99
|
+
string, which will use a server side prepared statement after N calls to
|
100
|
+
the prepared statement.
|
101
|
+
|
102
|
+
=== All Others
|
103
|
+
|
104
|
+
Support is emulated using interpolation.
|
data/doc/reflection.rdoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
= Reflection
|
2
|
+
|
3
|
+
Sequel supports reflection information in multiple ways.
|
4
|
+
|
5
|
+
== Adapter in Use
|
6
|
+
|
7
|
+
You can get the adapter in use using Database.adapter_scheme. As this is a class method, you generally need to do DB.class.adapter_scheme:
|
8
|
+
|
9
|
+
DB.class.adapter_scheme # e.g. :postgres, :jdbc, :odbc
|
10
|
+
|
11
|
+
== Database Connected To
|
12
|
+
|
13
|
+
In some cases, the adapter scheme will be the same as the database to which you are connecting. However, many adapters support multiple databases. You can use the Database#database_type method to get the type of database to which you are connecting:
|
14
|
+
|
15
|
+
DB.database_type # :postgres, :h2, :mssql
|
16
|
+
|
17
|
+
== Tables in the Database
|
18
|
+
|
19
|
+
On many database types/adapters, Database#tables exists and gives an array of table name symbols:
|
20
|
+
|
21
|
+
DB.tables # [:table1, :table2, :table3, ...]
|
22
|
+
|
23
|
+
== Indexes on a table
|
24
|
+
|
25
|
+
On a few database types/adapters, Database#indexes takes a table name gives a hash of index information. Keys are index names, values are subhashes with the keys :columns and :unique :
|
26
|
+
|
27
|
+
DB.indexes(:table1) # {:index1=>{:columns=>[:column1], :unique=>false}, :index2=>{:columns=>[:column2, :column3], :unique=>true}}
|
28
|
+
|
29
|
+
Index information generally does not include partial indexes, functional indexes, or indexes on the primary key of the table.
|
30
|
+
|
31
|
+
== Column Information for a Table
|
32
|
+
|
33
|
+
Database#schema takes a table symbol and returns column information in an array with each element being an array with two elements. The first elements of the subarray is a column symbol, and the second element is a hash of information about that column. The hash should include the following keys:
|
34
|
+
|
35
|
+
* :allow_null - Whether NULL/nil is an allowed value for this column. Used by the Sequel::Model typecasting code.
|
36
|
+
* :db_type - The type of column the database provided, as a string. Used by the schema_dumper plugin for a more specific type translation.
|
37
|
+
* :default - The default value of the column, as either a string or nil. Uses a database specific format. Used by the schema_dumper plugin for converting to a ruby value.
|
38
|
+
* :primary_key - Whether this column is one of the primary key columns for the table. Used by the Sequel::Model code to determine primary key columns.
|
39
|
+
* :ruby_default - The default value of the column as a ruby object, or nil if there is no default or the default could not be successfully parsed into a ruby object.
|
40
|
+
* :type - The type of column, as a symbol (e.g. :string). Used by the Sequel::Model typecasting code.
|
41
|
+
|
42
|
+
Example:
|
43
|
+
|
44
|
+
DB.schema(:table) # [[:column1, {:allow_null=>true, :db_type=>'varchar(255)', :default=>'blah', :primary_key=>false, :type=>:string}], ...]
|
45
|
+
|
46
|
+
== Column Information for a Model
|
47
|
+
|
48
|
+
Model#db_schema returns pretty much the same information, except it returns it as a hash with column keys instead of an array of two element arrays.
|
49
|
+
|
50
|
+
Model.db_schema # {:column1=>{:allow_null=>true, :db_type=>'varchar(255)', :default=>'blah', :primary_key=>false, :type=>:string}, ...}
|
51
|
+
|
52
|
+
== Columns used by a dataset/model
|
53
|
+
|
54
|
+
Dataset#columns returns the columns of the current dataset as an array of symbols:
|
55
|
+
|
56
|
+
DB[:table].columns # [:column1, :column2, :column3, ...]
|
57
|
+
|
58
|
+
Dataset#columns! does the same thing, except it ignores any cached value. In general, the cached value should never be incorrect, unless the database schema is changed after the dataset is created.
|
59
|
+
|
60
|
+
DB[:table].columns! # [:column1, :column2, :column3, ...]
|
61
|
+
|
62
|
+
Model.columns does the same thing as Dataset#columns, using the model's dataset:
|
63
|
+
|
64
|
+
Model.columns # [:column1, :column2, :column3, ...]
|
65
|
+
|
66
|
+
== Associations Defined
|
67
|
+
|
68
|
+
Sequel::Model offers complete introspection capability for all associations.
|
69
|
+
|
70
|
+
You can get an array of association symbols with Model.associations:
|
71
|
+
|
72
|
+
Model.associations # [:association1, :association2, ...]
|
73
|
+
|
74
|
+
You can get the association reflection for a single association via the Model.association_reflection. Association reflections are subclasses of hash, for ease of use and introspection (and backwards compatibility):
|
75
|
+
|
76
|
+
Model.association_reflection(:association1) # {:name=>:association1, :type=>:many_to_one, :model=>Model, :associated_class=>OtherModel, ...}
|
77
|
+
|
78
|
+
You can get an array of all association reflections via Model.all_association_reflections:
|
79
|
+
|
80
|
+
Model.all_association_reflections # [{:name=>:association1, :type=>:many_to_one, :model=>Model, :associated_class=>OtherModel, ...}, ...]
|
81
|
+
|
82
|
+
Finally, you can get a hash of association reflections via Model.association_reflections:
|
83
|
+
|
84
|
+
Model.association_reflections # {:association1=>{:name=>:association1, :type=>:many_to_one, :model=>Model, :associated_class=>OtherModel, ...}, ...}
|