viking-sequel 3.10.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.
Files changed (237) hide show
  1. data/CHANGELOG +3134 -0
  2. data/COPYING +19 -0
  3. data/README.rdoc +723 -0
  4. data/Rakefile +193 -0
  5. data/bin/sequel +196 -0
  6. data/doc/advanced_associations.rdoc +644 -0
  7. data/doc/cheat_sheet.rdoc +218 -0
  8. data/doc/dataset_basics.rdoc +106 -0
  9. data/doc/dataset_filtering.rdoc +158 -0
  10. data/doc/opening_databases.rdoc +296 -0
  11. data/doc/prepared_statements.rdoc +104 -0
  12. data/doc/reflection.rdoc +84 -0
  13. data/doc/release_notes/1.0.txt +38 -0
  14. data/doc/release_notes/1.1.txt +143 -0
  15. data/doc/release_notes/1.3.txt +101 -0
  16. data/doc/release_notes/1.4.0.txt +53 -0
  17. data/doc/release_notes/1.5.0.txt +155 -0
  18. data/doc/release_notes/2.0.0.txt +298 -0
  19. data/doc/release_notes/2.1.0.txt +271 -0
  20. data/doc/release_notes/2.10.0.txt +328 -0
  21. data/doc/release_notes/2.11.0.txt +215 -0
  22. data/doc/release_notes/2.12.0.txt +534 -0
  23. data/doc/release_notes/2.2.0.txt +253 -0
  24. data/doc/release_notes/2.3.0.txt +88 -0
  25. data/doc/release_notes/2.4.0.txt +106 -0
  26. data/doc/release_notes/2.5.0.txt +137 -0
  27. data/doc/release_notes/2.6.0.txt +157 -0
  28. data/doc/release_notes/2.7.0.txt +166 -0
  29. data/doc/release_notes/2.8.0.txt +171 -0
  30. data/doc/release_notes/2.9.0.txt +97 -0
  31. data/doc/release_notes/3.0.0.txt +221 -0
  32. data/doc/release_notes/3.1.0.txt +406 -0
  33. data/doc/release_notes/3.10.0.txt +286 -0
  34. data/doc/release_notes/3.2.0.txt +268 -0
  35. data/doc/release_notes/3.3.0.txt +192 -0
  36. data/doc/release_notes/3.4.0.txt +325 -0
  37. data/doc/release_notes/3.5.0.txt +510 -0
  38. data/doc/release_notes/3.6.0.txt +366 -0
  39. data/doc/release_notes/3.7.0.txt +179 -0
  40. data/doc/release_notes/3.8.0.txt +151 -0
  41. data/doc/release_notes/3.9.0.txt +233 -0
  42. data/doc/schema.rdoc +36 -0
  43. data/doc/sharding.rdoc +113 -0
  44. data/doc/virtual_rows.rdoc +205 -0
  45. data/lib/sequel.rb +1 -0
  46. data/lib/sequel/adapters/ado.rb +90 -0
  47. data/lib/sequel/adapters/ado/mssql.rb +30 -0
  48. data/lib/sequel/adapters/amalgalite.rb +176 -0
  49. data/lib/sequel/adapters/db2.rb +139 -0
  50. data/lib/sequel/adapters/dbi.rb +113 -0
  51. data/lib/sequel/adapters/do.rb +188 -0
  52. data/lib/sequel/adapters/do/mysql.rb +49 -0
  53. data/lib/sequel/adapters/do/postgres.rb +91 -0
  54. data/lib/sequel/adapters/do/sqlite.rb +40 -0
  55. data/lib/sequel/adapters/firebird.rb +283 -0
  56. data/lib/sequel/adapters/informix.rb +77 -0
  57. data/lib/sequel/adapters/jdbc.rb +587 -0
  58. data/lib/sequel/adapters/jdbc/as400.rb +58 -0
  59. data/lib/sequel/adapters/jdbc/h2.rb +133 -0
  60. data/lib/sequel/adapters/jdbc/mssql.rb +57 -0
  61. data/lib/sequel/adapters/jdbc/mysql.rb +78 -0
  62. data/lib/sequel/adapters/jdbc/oracle.rb +50 -0
  63. data/lib/sequel/adapters/jdbc/postgresql.rb +108 -0
  64. data/lib/sequel/adapters/jdbc/sqlite.rb +55 -0
  65. data/lib/sequel/adapters/mysql.rb +421 -0
  66. data/lib/sequel/adapters/odbc.rb +143 -0
  67. data/lib/sequel/adapters/odbc/mssql.rb +42 -0
  68. data/lib/sequel/adapters/openbase.rb +64 -0
  69. data/lib/sequel/adapters/oracle.rb +131 -0
  70. data/lib/sequel/adapters/postgres.rb +504 -0
  71. data/lib/sequel/adapters/shared/mssql.rb +490 -0
  72. data/lib/sequel/adapters/shared/mysql.rb +498 -0
  73. data/lib/sequel/adapters/shared/oracle.rb +195 -0
  74. data/lib/sequel/adapters/shared/postgres.rb +830 -0
  75. data/lib/sequel/adapters/shared/progress.rb +44 -0
  76. data/lib/sequel/adapters/shared/sqlite.rb +389 -0
  77. data/lib/sequel/adapters/sqlite.rb +224 -0
  78. data/lib/sequel/adapters/utils/stored_procedures.rb +84 -0
  79. data/lib/sequel/connection_pool.rb +99 -0
  80. data/lib/sequel/connection_pool/sharded_single.rb +84 -0
  81. data/lib/sequel/connection_pool/sharded_threaded.rb +211 -0
  82. data/lib/sequel/connection_pool/single.rb +29 -0
  83. data/lib/sequel/connection_pool/threaded.rb +150 -0
  84. data/lib/sequel/core.rb +293 -0
  85. data/lib/sequel/core_sql.rb +241 -0
  86. data/lib/sequel/database.rb +1079 -0
  87. data/lib/sequel/database/schema_generator.rb +327 -0
  88. data/lib/sequel/database/schema_methods.rb +203 -0
  89. data/lib/sequel/database/schema_sql.rb +320 -0
  90. data/lib/sequel/dataset.rb +32 -0
  91. data/lib/sequel/dataset/actions.rb +441 -0
  92. data/lib/sequel/dataset/features.rb +86 -0
  93. data/lib/sequel/dataset/graph.rb +254 -0
  94. data/lib/sequel/dataset/misc.rb +119 -0
  95. data/lib/sequel/dataset/mutation.rb +64 -0
  96. data/lib/sequel/dataset/prepared_statements.rb +227 -0
  97. data/lib/sequel/dataset/query.rb +709 -0
  98. data/lib/sequel/dataset/sql.rb +996 -0
  99. data/lib/sequel/exceptions.rb +51 -0
  100. data/lib/sequel/extensions/blank.rb +43 -0
  101. data/lib/sequel/extensions/inflector.rb +242 -0
  102. data/lib/sequel/extensions/looser_typecasting.rb +21 -0
  103. data/lib/sequel/extensions/migration.rb +239 -0
  104. data/lib/sequel/extensions/named_timezones.rb +61 -0
  105. data/lib/sequel/extensions/pagination.rb +100 -0
  106. data/lib/sequel/extensions/pretty_table.rb +82 -0
  107. data/lib/sequel/extensions/query.rb +52 -0
  108. data/lib/sequel/extensions/schema_dumper.rb +271 -0
  109. data/lib/sequel/extensions/sql_expr.rb +122 -0
  110. data/lib/sequel/extensions/string_date_time.rb +46 -0
  111. data/lib/sequel/extensions/thread_local_timezones.rb +48 -0
  112. data/lib/sequel/metaprogramming.rb +9 -0
  113. data/lib/sequel/model.rb +120 -0
  114. data/lib/sequel/model/associations.rb +1514 -0
  115. data/lib/sequel/model/base.rb +1069 -0
  116. data/lib/sequel/model/default_inflections.rb +45 -0
  117. data/lib/sequel/model/errors.rb +39 -0
  118. data/lib/sequel/model/exceptions.rb +21 -0
  119. data/lib/sequel/model/inflections.rb +162 -0
  120. data/lib/sequel/model/plugins.rb +70 -0
  121. data/lib/sequel/plugins/active_model.rb +59 -0
  122. data/lib/sequel/plugins/association_dependencies.rb +103 -0
  123. data/lib/sequel/plugins/association_proxies.rb +41 -0
  124. data/lib/sequel/plugins/boolean_readers.rb +53 -0
  125. data/lib/sequel/plugins/caching.rb +141 -0
  126. data/lib/sequel/plugins/class_table_inheritance.rb +214 -0
  127. data/lib/sequel/plugins/composition.rb +138 -0
  128. data/lib/sequel/plugins/force_encoding.rb +72 -0
  129. data/lib/sequel/plugins/hook_class_methods.rb +126 -0
  130. data/lib/sequel/plugins/identity_map.rb +116 -0
  131. data/lib/sequel/plugins/instance_filters.rb +98 -0
  132. data/lib/sequel/plugins/instance_hooks.rb +57 -0
  133. data/lib/sequel/plugins/lazy_attributes.rb +77 -0
  134. data/lib/sequel/plugins/many_through_many.rb +208 -0
  135. data/lib/sequel/plugins/nested_attributes.rb +206 -0
  136. data/lib/sequel/plugins/optimistic_locking.rb +81 -0
  137. data/lib/sequel/plugins/rcte_tree.rb +281 -0
  138. data/lib/sequel/plugins/schema.rb +66 -0
  139. data/lib/sequel/plugins/serialization.rb +166 -0
  140. data/lib/sequel/plugins/single_table_inheritance.rb +74 -0
  141. data/lib/sequel/plugins/subclasses.rb +45 -0
  142. data/lib/sequel/plugins/tactical_eager_loading.rb +61 -0
  143. data/lib/sequel/plugins/timestamps.rb +87 -0
  144. data/lib/sequel/plugins/touch.rb +118 -0
  145. data/lib/sequel/plugins/typecast_on_load.rb +72 -0
  146. data/lib/sequel/plugins/validation_class_methods.rb +405 -0
  147. data/lib/sequel/plugins/validation_helpers.rb +223 -0
  148. data/lib/sequel/sql.rb +1020 -0
  149. data/lib/sequel/timezones.rb +161 -0
  150. data/lib/sequel/version.rb +12 -0
  151. data/lib/sequel_core.rb +1 -0
  152. data/lib/sequel_model.rb +1 -0
  153. data/spec/adapters/firebird_spec.rb +407 -0
  154. data/spec/adapters/informix_spec.rb +97 -0
  155. data/spec/adapters/mssql_spec.rb +403 -0
  156. data/spec/adapters/mysql_spec.rb +1019 -0
  157. data/spec/adapters/oracle_spec.rb +286 -0
  158. data/spec/adapters/postgres_spec.rb +969 -0
  159. data/spec/adapters/spec_helper.rb +51 -0
  160. data/spec/adapters/sqlite_spec.rb +432 -0
  161. data/spec/core/connection_pool_spec.rb +808 -0
  162. data/spec/core/core_sql_spec.rb +417 -0
  163. data/spec/core/database_spec.rb +1662 -0
  164. data/spec/core/dataset_spec.rb +3827 -0
  165. data/spec/core/expression_filters_spec.rb +595 -0
  166. data/spec/core/object_graph_spec.rb +296 -0
  167. data/spec/core/schema_generator_spec.rb +159 -0
  168. data/spec/core/schema_spec.rb +830 -0
  169. data/spec/core/spec_helper.rb +56 -0
  170. data/spec/core/version_spec.rb +7 -0
  171. data/spec/extensions/active_model_spec.rb +76 -0
  172. data/spec/extensions/association_dependencies_spec.rb +127 -0
  173. data/spec/extensions/association_proxies_spec.rb +50 -0
  174. data/spec/extensions/blank_spec.rb +67 -0
  175. data/spec/extensions/boolean_readers_spec.rb +92 -0
  176. data/spec/extensions/caching_spec.rb +250 -0
  177. data/spec/extensions/class_table_inheritance_spec.rb +252 -0
  178. data/spec/extensions/composition_spec.rb +194 -0
  179. data/spec/extensions/force_encoding_spec.rb +117 -0
  180. data/spec/extensions/hook_class_methods_spec.rb +470 -0
  181. data/spec/extensions/identity_map_spec.rb +202 -0
  182. data/spec/extensions/inflector_spec.rb +181 -0
  183. data/spec/extensions/instance_filters_spec.rb +55 -0
  184. data/spec/extensions/instance_hooks_spec.rb +133 -0
  185. data/spec/extensions/lazy_attributes_spec.rb +153 -0
  186. data/spec/extensions/looser_typecasting_spec.rb +39 -0
  187. data/spec/extensions/many_through_many_spec.rb +884 -0
  188. data/spec/extensions/migration_spec.rb +332 -0
  189. data/spec/extensions/named_timezones_spec.rb +72 -0
  190. data/spec/extensions/nested_attributes_spec.rb +396 -0
  191. data/spec/extensions/optimistic_locking_spec.rb +100 -0
  192. data/spec/extensions/pagination_spec.rb +99 -0
  193. data/spec/extensions/pretty_table_spec.rb +91 -0
  194. data/spec/extensions/query_spec.rb +85 -0
  195. data/spec/extensions/rcte_tree_spec.rb +205 -0
  196. data/spec/extensions/schema_dumper_spec.rb +357 -0
  197. data/spec/extensions/schema_spec.rb +127 -0
  198. data/spec/extensions/serialization_spec.rb +209 -0
  199. data/spec/extensions/single_table_inheritance_spec.rb +96 -0
  200. data/spec/extensions/spec_helper.rb +91 -0
  201. data/spec/extensions/sql_expr_spec.rb +89 -0
  202. data/spec/extensions/string_date_time_spec.rb +93 -0
  203. data/spec/extensions/subclasses_spec.rb +52 -0
  204. data/spec/extensions/tactical_eager_loading_spec.rb +65 -0
  205. data/spec/extensions/thread_local_timezones_spec.rb +45 -0
  206. data/spec/extensions/timestamps_spec.rb +150 -0
  207. data/spec/extensions/touch_spec.rb +155 -0
  208. data/spec/extensions/typecast_on_load_spec.rb +69 -0
  209. data/spec/extensions/validation_class_methods_spec.rb +984 -0
  210. data/spec/extensions/validation_helpers_spec.rb +438 -0
  211. data/spec/integration/associations_test.rb +281 -0
  212. data/spec/integration/database_test.rb +26 -0
  213. data/spec/integration/dataset_test.rb +963 -0
  214. data/spec/integration/eager_loader_test.rb +734 -0
  215. data/spec/integration/model_test.rb +130 -0
  216. data/spec/integration/plugin_test.rb +814 -0
  217. data/spec/integration/prepared_statement_test.rb +213 -0
  218. data/spec/integration/schema_test.rb +361 -0
  219. data/spec/integration/spec_helper.rb +73 -0
  220. data/spec/integration/timezone_test.rb +55 -0
  221. data/spec/integration/transaction_test.rb +122 -0
  222. data/spec/integration/type_test.rb +96 -0
  223. data/spec/model/association_reflection_spec.rb +175 -0
  224. data/spec/model/associations_spec.rb +2633 -0
  225. data/spec/model/base_spec.rb +418 -0
  226. data/spec/model/dataset_methods_spec.rb +78 -0
  227. data/spec/model/eager_loading_spec.rb +1391 -0
  228. data/spec/model/hooks_spec.rb +240 -0
  229. data/spec/model/inflector_spec.rb +26 -0
  230. data/spec/model/model_spec.rb +593 -0
  231. data/spec/model/plugins_spec.rb +236 -0
  232. data/spec/model/record_spec.rb +1500 -0
  233. data/spec/model/spec_helper.rb +97 -0
  234. data/spec/model/validations_spec.rb +153 -0
  235. data/spec/rcov.opts +6 -0
  236. data/spec/spec_config.rb.example +10 -0
  237. metadata +346 -0
@@ -0,0 +1,29 @@
1
+ # This is the fastest connection pool, since it isn't a connection pool at all.
2
+ # It is just a wrapper around a single connection that uses the connection pool
3
+ # API.
4
+ class Sequel::SingleConnectionPool < Sequel::ConnectionPool
5
+ # The SingleConnectionPool always has a size of 1 if connected
6
+ # and 0 if not.
7
+ def size
8
+ @conn ? 1 : 0
9
+ end
10
+
11
+ # Disconnect the connection from the database.
12
+ def disconnect(opts=nil, &block)
13
+ block ||= @disconnection_proc
14
+ block.call(@conn) if block
15
+ @conn = nil
16
+ end
17
+
18
+ # Yield the connection to the block.
19
+ def hold(server=nil)
20
+ begin
21
+ yield(@conn ||= make_new(DEFAULT_SERVER))
22
+ rescue Sequel::DatabaseDisconnectError
23
+ disconnect
24
+ raise
25
+ end
26
+ end
27
+
28
+ CONNECTION_POOL_MAP[[true, false]] = self
29
+ end
@@ -0,0 +1,150 @@
1
+ # A connection pool allowing multi-threaded access to a pool of connections.
2
+ class Sequel::ThreadedConnectionPool < Sequel::ConnectionPool
3
+ # The maximum number of connections this pool will create (per shard/server
4
+ # if sharding).
5
+ attr_reader :max_size
6
+
7
+ # An array of connections that are available for use by the pool.
8
+ attr_reader :available_connections
9
+
10
+ # A hash with thread keys and connection values for currently allocated
11
+ # connections.
12
+ attr_reader :allocated
13
+
14
+ # The following additional options are respected:
15
+ # * :max_connections - The maximum number of connections the connection pool
16
+ # will open (default 4)
17
+ # * :pool_sleep_time - The amount of time to sleep before attempting to acquire
18
+ # a connection again (default 0.001)
19
+ # * :pool_timeout - The amount of seconds to wait to acquire a connection
20
+ # before raising a PoolTimeoutError (default 5)
21
+ def initialize(opts = {}, &block)
22
+ super
23
+ @max_size = Integer(opts[:max_connections] || 4)
24
+ raise(Sequel::Error, ':max_connections must be positive') if @max_size < 1
25
+ @mutex = Mutex.new
26
+ @available_connections = []
27
+ @allocated = {}
28
+ @timeout = Integer(opts[:pool_timeout] || 5)
29
+ @sleep_time = Float(opts[:pool_sleep_time] || 0.001)
30
+ end
31
+
32
+ # The total number of connections opened for the given server, should
33
+ # be equal to available_connections.length + allocated.length.
34
+ def size
35
+ @allocated.length + @available_connections.length
36
+ end
37
+
38
+ # Removes all connection currently available on all servers, optionally
39
+ # yielding each connection to the given block. This method has the effect of
40
+ # disconnecting from the database, assuming that no connections are currently
41
+ # being used. If connections are being used, they are scheduled to be
42
+ # disconnected as soon as they are returned to the pool.
43
+ #
44
+ # Once a connection is requested using #hold, the connection pool
45
+ # creates new connections to the database.
46
+ def disconnect(opts={}, &block)
47
+ block ||= @disconnection_proc
48
+ sync do
49
+ @available_connections.each{|conn| block.call(conn)} if block
50
+ @available_connections.clear
51
+ end
52
+ end
53
+
54
+ # Chooses the first available connection to the given server, or if none are
55
+ # available, creates a new connection. Passes the connection to the supplied
56
+ # block:
57
+ #
58
+ # pool.hold {|conn| conn.execute('DROP TABLE posts')}
59
+ #
60
+ # Pool#hold is re-entrant, meaning it can be called recursively in
61
+ # the same thread without blocking.
62
+ #
63
+ # If no connection is immediately available and the pool is already using the maximum
64
+ # number of connections, Pool#hold will block until a connection
65
+ # is available or the timeout expires. If the timeout expires before a
66
+ # connection can be acquired, a Sequel::PoolTimeout is
67
+ # raised.
68
+ def hold(server=nil)
69
+ t = Thread.current
70
+ if conn = owned_connection(t)
71
+ return yield(conn)
72
+ end
73
+ begin
74
+ unless conn = acquire(t)
75
+ time = Time.now
76
+ timeout = time + @timeout
77
+ sleep_time = @sleep_time
78
+ sleep sleep_time
79
+ until conn = acquire(t)
80
+ raise(::Sequel::PoolTimeout) if Time.now > timeout
81
+ sleep sleep_time
82
+ end
83
+ end
84
+ yield conn
85
+ rescue Sequel::DatabaseDisconnectError
86
+ @disconnection_proc.call(conn) if @disconnection_proc && conn
87
+ @allocated.delete(t)
88
+ conn = nil
89
+ raise
90
+ ensure
91
+ sync{release(t)} if conn
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ # Assigns a connection to the supplied thread for the given server, if one
98
+ # is available. The calling code should NOT already have the mutex when
99
+ # calling this.
100
+ def acquire(thread)
101
+ sync do
102
+ if conn = available
103
+ @allocated[thread] = conn
104
+ end
105
+ end
106
+ end
107
+
108
+ # Returns an available connection to the given server. If no connection is
109
+ # available, tries to create a new connection. The calling code should already
110
+ # have the mutex before calling this.
111
+ def available
112
+ @available_connections.pop || make_new(DEFAULT_SERVER)
113
+ end
114
+
115
+ # Alias the default make_new method, so subclasses can call it directly.
116
+ alias default_make_new make_new
117
+
118
+ # Creates a new connection to the given server if the size of the pool for
119
+ # the server is less than the maximum size of the pool. The calling code
120
+ # should already have the mutex before calling this.
121
+ def make_new(server)
122
+ if (n = size) >= @max_size
123
+ @allocated.keys.each{|t| release(t) unless t.alive?}
124
+ n = nil
125
+ end
126
+ super if (n || size) < @max_size
127
+ end
128
+
129
+ # Returns the connection owned by the supplied thread for the given server,
130
+ # if any. The calling code should NOT already have the mutex before calling this.
131
+ def owned_connection(thread)
132
+ sync{@allocated[thread]}
133
+ end
134
+
135
+ # Releases the connection assigned to the supplied thread and server. If the
136
+ # server or connection given is scheduled for disconnection, remove the
137
+ # connection instead of releasing it back to the pool.
138
+ # The calling code should already have the mutex before calling this.
139
+ def release(thread)
140
+ @available_connections << @allocated.delete(thread)
141
+ end
142
+
143
+ # Yield to the block while inside the mutex. The calling code should NOT
144
+ # already have the mutex before calling this.
145
+ def sync
146
+ @mutex.synchronize{yield}
147
+ end
148
+
149
+ CONNECTION_POOL_MAP[[false, false]] = self
150
+ end
@@ -0,0 +1,293 @@
1
+ %w'bigdecimal date thread time uri'.each{|f| require f}
2
+
3
+ # Top level module for Sequel
4
+ #
5
+ # There are some class methods that are added via metaprogramming, one for
6
+ # each supported adapter. For example:
7
+ #
8
+ # DB = Sequel.sqlite # Memory database
9
+ # DB = Sequel.sqlite('blog.db')
10
+ # DB = Sequel.postgres('database_name', :user=>'user',
11
+ # :password=>'password', :host=>'host', :port=>5432,
12
+ # :max_connections=>10)
13
+ #
14
+ # If a block is given to these methods, it is passed the opened Database
15
+ # object, which is closed (disconnected) when the block exits, just
16
+ # like a block passed to connect. For example:
17
+ #
18
+ # Sequel.sqlite('blog.db'){|db| puts db[:users].count}
19
+ #
20
+ # Sequel doesn't pay much attention to timezones by default, but you can set it
21
+ # handle timezones if you want. There are three separate timezone settings:
22
+ #
23
+ # * application_timezone - The timezone you want the application to use. This is
24
+ # the timezone that incoming times from the database and typecasting are converted
25
+ # to.
26
+ # * database_timezone - The timezone for storage in the database. This is the
27
+ # timezone to which Sequel will convert timestamps before literalizing them
28
+ # for storage in the database. It is also the timezone that Sequel will assume
29
+ # database timestamp values are already in (if they don't include an offset).
30
+ # * typecast_timezone - The timezone that incoming data that Sequel needs to typecast
31
+ # is assumed to be already in (if they don't include an offset).
32
+ #
33
+ # You can set also set all three timezones to the same value at once via
34
+ # Sequel.default_timezone=.
35
+ #
36
+ # Sequel.application_timezone = :utc # or :local or nil
37
+ # Sequel.database_timezone = :utc # or :local or nil
38
+ # Sequel.typecast_timezone = :utc # or :local or nil
39
+ # Sequel.default_timezone = :utc # or :local or nil
40
+ #
41
+ # The only timezone values that are supported by default are :utc (convert to UTC),
42
+ # :local (convert to local time), and nil (don't convert). If you need to
43
+ # convert to a specific timezone, or need the timezones being used to change based
44
+ # on the environment (e.g. current user), you need to use an extension (and use
45
+ # DateTime as the datetime_class).
46
+ #
47
+ # You can set the SEQUEL_NO_CORE_EXTENSIONS constant or environment variable to have
48
+ # Sequel not extend the core classes.
49
+ #
50
+ # For a more expanded introduction, see the {README}[link:files/README_rdoc.html].
51
+ # For a quicker introduction, see the {cheat sheet}[link:files/doc/cheat_sheet_rdoc.html].
52
+ module Sequel
53
+ @convert_two_digit_years = true
54
+ @datetime_class = Time
55
+ @virtual_row_instance_eval = true
56
+ @require_thread = nil
57
+
58
+ # Mutex used to protect file loading
59
+ @require_mutex = Mutex.new
60
+
61
+ class << self
62
+ # Sequel converts two digit years in Dates and DateTimes by default,
63
+ # so 01/02/03 is interpreted at January 2nd, 2003, and 12/13/99 is interpreted
64
+ # as December 13, 1999. You can override this to treat those dates as
65
+ # January 2nd, 0003 and December 13, 0099, respectively, by setting this to false.
66
+ attr_accessor :convert_two_digit_years
67
+
68
+ # Sequel can use either Time or DateTime for times returned from the
69
+ # database. It defaults to Time. To change it to DateTime, set this to DateTime.
70
+ attr_accessor :datetime_class
71
+
72
+ attr_accessor :virtual_row_instance_eval
73
+
74
+ # Alias to the standard version of require
75
+ alias k_require require
76
+
77
+ private
78
+
79
+ # Make thread safe requiring reentrant to prevent deadlocks.
80
+ def check_requiring_thread
81
+ t = Thread.current
82
+ return(yield) if @require_thread == t
83
+ @require_mutex.synchronize do
84
+ begin
85
+ @require_thread = t
86
+ yield
87
+ ensure
88
+ @require_thread = nil
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ # Returns true if the passed object could be a specifier of conditions, false otherwise.
95
+ # Currently, Sequel considers hashes and arrays of all two pairs as
96
+ # condition specifiers.
97
+ def self.condition_specifier?(obj)
98
+ case obj
99
+ when Hash
100
+ true
101
+ when Array
102
+ !obj.empty? && obj.all?{|i| (Array === i) && (i.length == 2)}
103
+ else
104
+ false
105
+ end
106
+ end
107
+
108
+ # Creates a new database object based on the supplied connection string
109
+ # and optional arguments. The specified scheme determines the database
110
+ # class used, and the rest of the string specifies the connection options.
111
+ # For example:
112
+ #
113
+ # DB = Sequel.connect('sqlite:/') # Memory database
114
+ # DB = Sequel.connect('sqlite://blog.db') # ./blog.db
115
+ # DB = Sequel.connect('sqlite:///blog.db') # /blog.db
116
+ # DB = Sequel.connect('postgres://user:password@host:port/database_name')
117
+ # DB = Sequel.connect('sqlite:///blog.db', :max_connections=>10)
118
+ #
119
+ # If a block is given, it is passed the opened Database object, which is
120
+ # closed when the block exits. For example:
121
+ #
122
+ # Sequel.connect('sqlite://blog.db'){|db| puts db[:users].count}
123
+ #
124
+ # For details, see the {"Connecting to a Database" guide}[link:files/doc/opening_databases_rdoc.html].
125
+ # To set up a master/slave or sharded database connection, see the {"Master/Slave Databases and Sharding" guide}[link:files/doc/sharding_rdoc.html].
126
+ def self.connect(*args, &block)
127
+ Database.connect(*args, &block)
128
+ end
129
+
130
+ # Convert the exception to the given class. The given class should be
131
+ # Sequel::Error or a subclass. Returns an instance of klass with
132
+ # the message and backtrace of exception.
133
+ def self.convert_exception_class(exception, klass)
134
+ return exception if exception.is_a?(klass)
135
+ e = klass.new("#{exception.class}: #{exception.message}")
136
+ e.wrapped_exception = exception
137
+ e.set_backtrace(exception.backtrace)
138
+ e
139
+ end
140
+
141
+ # Load all Sequel extensions given. Only loads extensions included in this
142
+ # release of Sequel, doesn't load external extensions.
143
+ #
144
+ # Sequel.extension(:schema_dumper)
145
+ # Sequel.extension(:pagination, :query)
146
+ def self.extension(*extensions)
147
+ extensions.each{|e| tsk_require "sequel/extensions/#{e}"}
148
+ end
149
+
150
+ # Set the method to call on identifiers going into the database. This affects
151
+ # the literalization of identifiers by calling this method on them before they are input.
152
+ # Sequel upcases identifiers in all SQL strings for most databases, so to turn that off:
153
+ #
154
+ # Sequel.identifier_input_method = nil
155
+ #
156
+ # to downcase instead:
157
+ #
158
+ # Sequel.identifier_input_method = :downcase
159
+ #
160
+ # Other String instance methods work as well.
161
+ def self.identifier_input_method=(value)
162
+ Database.identifier_input_method = value
163
+ end
164
+
165
+ # Set the method to call on identifiers coming out of the database. This affects
166
+ # the literalization of identifiers by calling this method on them when they are
167
+ # retrieved from the database. Sequel downcases identifiers retrieved for most
168
+ # databases, so to turn that off:
169
+ #
170
+ # Sequel.identifier_output_method = nil
171
+ #
172
+ # to upcase instead:
173
+ #
174
+ # Sequel.identifier_output_method = :upcase
175
+ #
176
+ # Other String instance methods work as well.
177
+ def self.identifier_output_method=(value)
178
+ Database.identifier_output_method = value
179
+ end
180
+
181
+ # Set whether to quote identifiers for all databases by default. By default,
182
+ # Sequel quotes identifiers in all SQL strings, so to turn that off:
183
+ #
184
+ # Sequel.quote_identifiers = false
185
+ def self.quote_identifiers=(value)
186
+ Database.quote_identifiers = value
187
+ end
188
+
189
+ # Require all given files which should be in the same or a subdirectory of
190
+ # this file. If a subdir is given, assume all files are in that subdir.
191
+ def self.require(files, subdir=nil)
192
+ Array(files).each{|f| super("#{File.dirname(__FILE__)}/#{"#{subdir}/" if subdir}#{f}")}
193
+ end
194
+
195
+ # Set whether to set the single threaded mode for all databases by default. By default,
196
+ # Sequel uses a threadsafe connection pool, which isn't as fast as the
197
+ # single threaded connection pool. If your program will only have one thread,
198
+ # and speed is a priority, you may want to set this to true:
199
+ #
200
+ # Sequel.single_threaded = true
201
+ def self.single_threaded=(value)
202
+ Database.single_threaded = value
203
+ end
204
+
205
+ # Converts the given string into a Date object.
206
+ def self.string_to_date(s)
207
+ begin
208
+ Date.parse(s, Sequel.convert_two_digit_years)
209
+ rescue => e
210
+ raise convert_exception_class(e, InvalidValue)
211
+ end
212
+ end
213
+
214
+ # Converts the given string into a Time or DateTime object, depending on the
215
+ # value of Sequel.datetime_class.
216
+ def self.string_to_datetime(s)
217
+ begin
218
+ if datetime_class == DateTime
219
+ DateTime.parse(s, convert_two_digit_years)
220
+ else
221
+ datetime_class.parse(s)
222
+ end
223
+ rescue => e
224
+ raise convert_exception_class(e, InvalidValue)
225
+ end
226
+ end
227
+
228
+ # Converts the given string into a Time object.
229
+ def self.string_to_time(s)
230
+ begin
231
+ Time.parse(s)
232
+ rescue => e
233
+ raise convert_exception_class(e, InvalidValue)
234
+ end
235
+ end
236
+
237
+ # Same as Sequel.require, but wrapped in a mutex in order to be thread safe.
238
+ def self.ts_require(*args)
239
+ check_requiring_thread{require(*args)}
240
+ end
241
+
242
+ # Same as Kernel.require, but wrapped in a mutex in order to be thread safe.
243
+ def self.tsk_require(*args)
244
+ check_requiring_thread{k_require(*args)}
245
+ end
246
+
247
+ # If the supplied block takes a single argument,
248
+ # yield a new SQL::VirtualRow instance to the block
249
+ # argument. Otherwise, evaluate the block in the context of a new
250
+ # SQL::VirtualRow instance.
251
+ def self.virtual_row(&block)
252
+ vr = SQL::VirtualRow.new
253
+ case block.arity
254
+ when -1, 0
255
+ vr.instance_eval(&block)
256
+ else
257
+ block.call(vr)
258
+ end
259
+ end
260
+
261
+ ### Private Class Methods ###
262
+
263
+ # Helper method that the database adapter class methods that are added to Sequel via
264
+ # metaprogramming use to parse arguments.
265
+ def self.adapter_method(adapter, *args, &block) # :nodoc:
266
+ raise(::Sequel::Error, "Wrong number of arguments, 0-2 arguments valid") if args.length > 2
267
+ opts = {:adapter=>adapter.to_sym}
268
+ opts[:database] = args.shift if args.length >= 1 && !(args[0].is_a?(Hash))
269
+ if Hash === (arg = args[0])
270
+ opts.merge!(arg)
271
+ elsif !arg.nil?
272
+ raise ::Sequel::Error, "Wrong format of arguments, either use (), (String), (Hash), or (String, Hash)"
273
+ end
274
+ connect(opts, &block)
275
+ end
276
+
277
+ # Method that adds a database adapter class method to Sequel that calls
278
+ # Sequel.adapter_method.
279
+ def self.def_adapter_method(*adapters) # :nodoc:
280
+ adapters.each do |adapter|
281
+ instance_eval("def #{adapter}(*args, &block); adapter_method('#{adapter}', *args, &block) end", __FILE__, __LINE__)
282
+ end
283
+ end
284
+
285
+ private_class_method :adapter_method, :def_adapter_method
286
+
287
+ require(%w"metaprogramming sql connection_pool exceptions dataset database timezones version")
288
+ require(%w"schema_generator schema_methods schema_sql", 'database')
289
+ require('core_sql') if !defined?(::SEQUEL_NO_CORE_EXTENSIONS) && !ENV.has_key?('SEQUEL_NO_CORE_EXTENSIONS')
290
+
291
+ # Add the database adapter class methods to Sequel via metaprogramming
292
+ def_adapter_method(*Database::ADAPTERS)
293
+ end