sequel 3.11.0 → 3.12.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +70 -0
- data/Rakefile +1 -1
- data/doc/active_record.rdoc +896 -0
- data/doc/advanced_associations.rdoc +46 -31
- data/doc/association_basics.rdoc +14 -9
- data/doc/dataset_basics.rdoc +3 -3
- data/doc/migration.rdoc +1011 -0
- data/doc/model_hooks.rdoc +198 -0
- data/doc/querying.rdoc +811 -86
- data/doc/release_notes/3.12.0.txt +304 -0
- data/doc/sharding.rdoc +17 -0
- data/doc/sql.rdoc +537 -0
- data/doc/validations.rdoc +501 -0
- data/lib/sequel/adapters/jdbc.rb +19 -27
- data/lib/sequel/adapters/jdbc/postgresql.rb +0 -7
- data/lib/sequel/adapters/mysql.rb +5 -4
- data/lib/sequel/adapters/odbc.rb +3 -2
- data/lib/sequel/adapters/shared/mssql.rb +7 -6
- data/lib/sequel/adapters/shared/mysql.rb +2 -7
- data/lib/sequel/adapters/shared/postgres.rb +2 -8
- data/lib/sequel/adapters/shared/sqlite.rb +2 -5
- data/lib/sequel/adapters/sqlite.rb +4 -4
- data/lib/sequel/core.rb +0 -1
- data/lib/sequel/database.rb +2 -1060
- data/lib/sequel/database/connecting.rb +227 -0
- data/lib/sequel/database/dataset.rb +58 -0
- data/lib/sequel/database/dataset_defaults.rb +127 -0
- data/lib/sequel/database/logging.rb +62 -0
- data/lib/sequel/database/misc.rb +246 -0
- data/lib/sequel/database/query.rb +390 -0
- data/lib/sequel/database/schema_generator.rb +7 -3
- data/lib/sequel/database/schema_methods.rb +351 -7
- data/lib/sequel/dataset/actions.rb +9 -2
- data/lib/sequel/dataset/misc.rb +6 -2
- data/lib/sequel/dataset/mutation.rb +3 -11
- data/lib/sequel/dataset/query.rb +49 -6
- data/lib/sequel/exceptions.rb +3 -0
- data/lib/sequel/extensions/migration.rb +395 -113
- data/lib/sequel/extensions/schema_dumper.rb +21 -13
- data/lib/sequel/model.rb +27 -25
- data/lib/sequel/model/associations.rb +72 -34
- data/lib/sequel/model/base.rb +74 -18
- data/lib/sequel/model/errors.rb +8 -1
- data/lib/sequel/plugins/active_model.rb +8 -0
- data/lib/sequel/plugins/association_pks.rb +87 -0
- data/lib/sequel/plugins/association_proxies.rb +8 -0
- data/lib/sequel/plugins/boolean_readers.rb +12 -6
- data/lib/sequel/plugins/caching.rb +14 -7
- data/lib/sequel/plugins/class_table_inheritance.rb +15 -9
- data/lib/sequel/plugins/composition.rb +2 -1
- data/lib/sequel/plugins/force_encoding.rb +10 -7
- data/lib/sequel/plugins/hook_class_methods.rb +12 -11
- data/lib/sequel/plugins/identity_map.rb +9 -0
- data/lib/sequel/plugins/instance_hooks.rb +23 -13
- data/lib/sequel/plugins/lazy_attributes.rb +4 -1
- data/lib/sequel/plugins/many_through_many.rb +18 -4
- data/lib/sequel/plugins/nested_attributes.rb +1 -0
- data/lib/sequel/plugins/optimistic_locking.rb +1 -1
- data/lib/sequel/plugins/rcte_tree.rb +9 -8
- data/lib/sequel/plugins/schema.rb +8 -0
- data/lib/sequel/plugins/serialization.rb +1 -3
- data/lib/sequel/plugins/sharding.rb +135 -0
- data/lib/sequel/plugins/single_table_inheritance.rb +117 -25
- data/lib/sequel/plugins/skip_create_refresh.rb +35 -0
- data/lib/sequel/plugins/string_stripper.rb +26 -0
- data/lib/sequel/plugins/tactical_eager_loading.rb +8 -0
- data/lib/sequel/plugins/timestamps.rb +15 -2
- data/lib/sequel/plugins/touch.rb +13 -0
- data/lib/sequel/plugins/update_primary_key.rb +48 -0
- data/lib/sequel/plugins/validation_class_methods.rb +8 -0
- data/lib/sequel/plugins/validation_helpers.rb +1 -1
- data/lib/sequel/sql.rb +17 -20
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/postgres_spec.rb +5 -5
- data/spec/core/core_sql_spec.rb +17 -1
- data/spec/core/database_spec.rb +17 -5
- data/spec/core/dataset_spec.rb +31 -8
- data/spec/core/schema_generator_spec.rb +8 -1
- data/spec/core/schema_spec.rb +13 -0
- data/spec/extensions/association_pks_spec.rb +85 -0
- data/spec/extensions/hook_class_methods_spec.rb +9 -9
- data/spec/extensions/migration_spec.rb +339 -219
- data/spec/extensions/schema_dumper_spec.rb +28 -17
- data/spec/extensions/sharding_spec.rb +272 -0
- data/spec/extensions/single_table_inheritance_spec.rb +92 -4
- data/spec/extensions/skip_create_refresh_spec.rb +17 -0
- data/spec/extensions/string_stripper_spec.rb +23 -0
- data/spec/extensions/update_primary_key_spec.rb +65 -0
- data/spec/extensions/validation_class_methods_spec.rb +5 -5
- data/spec/files/bad_down_migration/001_create_alt_basic.rb +4 -0
- data/spec/files/bad_down_migration/002_create_alt_advanced.rb +4 -0
- data/spec/files/bad_timestamped_migrations/1273253849_create_sessions.rb +9 -0
- data/spec/files/bad_timestamped_migrations/1273253851_create_nodes.rb +9 -0
- data/spec/files/bad_timestamped_migrations/1273253853_3_create_users.rb +3 -0
- data/spec/files/bad_up_migration/001_create_alt_basic.rb +4 -0
- data/spec/files/bad_up_migration/002_create_alt_advanced.rb +3 -0
- data/spec/files/convert_to_timestamp_migrations/001_create_sessions.rb +9 -0
- data/spec/files/convert_to_timestamp_migrations/002_create_nodes.rb +9 -0
- data/spec/files/convert_to_timestamp_migrations/003_3_create_users.rb +4 -0
- data/spec/files/convert_to_timestamp_migrations/1273253850_create_artists.rb +9 -0
- data/spec/files/convert_to_timestamp_migrations/1273253852_create_albums.rb +9 -0
- data/spec/files/duplicate_integer_migrations/001_create_alt_advanced.rb +4 -0
- data/spec/files/duplicate_integer_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/duplicate_timestamped_migrations/1273253849_create_sessions.rb +9 -0
- data/spec/files/duplicate_timestamped_migrations/1273253853_create_nodes.rb +9 -0
- data/spec/files/duplicate_timestamped_migrations/1273253853_create_users.rb +4 -0
- data/spec/files/integer_migrations/001_create_sessions.rb +9 -0
- data/spec/files/integer_migrations/002_create_nodes.rb +9 -0
- data/spec/files/integer_migrations/003_3_create_users.rb +4 -0
- data/spec/files/interleaved_timestamped_migrations/1273253849_create_sessions.rb +9 -0
- data/spec/files/interleaved_timestamped_migrations/1273253850_create_artists.rb +9 -0
- data/spec/files/interleaved_timestamped_migrations/1273253851_create_nodes.rb +9 -0
- data/spec/files/interleaved_timestamped_migrations/1273253852_create_albums.rb +9 -0
- data/spec/files/interleaved_timestamped_migrations/1273253853_3_create_users.rb +4 -0
- data/spec/files/missing_integer_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/missing_integer_migrations/003_create_alt_advanced.rb +4 -0
- data/spec/files/missing_timestamped_migrations/1273253849_create_sessions.rb +9 -0
- data/spec/files/missing_timestamped_migrations/1273253853_3_create_users.rb +4 -0
- data/spec/files/timestamped_migrations/1273253849_create_sessions.rb +9 -0
- data/spec/files/timestamped_migrations/1273253851_create_nodes.rb +9 -0
- data/spec/files/timestamped_migrations/1273253853_3_create_users.rb +4 -0
- data/spec/files/uppercase_timestamped_migrations/1273253849_CREATE_SESSIONS.RB +9 -0
- data/spec/files/uppercase_timestamped_migrations/1273253851_CREATE_NODES.RB +9 -0
- data/spec/files/uppercase_timestamped_migrations/1273253853_3_CREATE_USERS.RB +4 -0
- data/spec/integration/eager_loader_test.rb +20 -20
- data/spec/integration/migrator_test.rb +187 -0
- data/spec/integration/plugin_test.rb +150 -0
- data/spec/integration/schema_test.rb +13 -2
- data/spec/model/associations_spec.rb +41 -14
- data/spec/model/base_spec.rb +69 -0
- data/spec/model/eager_loading_spec.rb +7 -3
- data/spec/model/record_spec.rb +79 -4
- data/spec/model/validations_spec.rb +21 -9
- metadata +66 -5
- data/doc/schema.rdoc +0 -36
- data/lib/sequel/database/schema_sql.rb +0 -320
data/doc/schema.rdoc
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
= Schema Modifications with Sequel
|
2
|
-
|
3
|
-
In addition to its support for getting data from databases, Sequel has good support for modifying the schema of databases. Unfortunately, the documentation from this is spread out in various places in the Sequel RDoc.
|
4
|
-
|
5
|
-
The recommended way to set up schema modifications in Sequel is through migrations. Migrations are modifications to the schema that handle modifying the schema in two directions, up and down, with down reversing the changes made by up. Migrations are stored in sequentially numbered files inside a directory (see the Sequel::Migrator documentation for file naming format details). The migrations can be run on the database using the sequel command line tool (the -m and -M switches).
|
6
|
-
|
7
|
-
The format of the individual migration files themselves is explained in the Sequel::Migration documentation. Each migration file contains a single migration class. The migration class acts a proxy for the related database (given on the command line if the sequel command line tool is used, or by the db argument to Sequel::Migration#apply if the API is used). The methods that can be used inside Sequel::Migration#up or Sequel::Migration#down are just Sequel::Database instance methods, such as create_table, drop_table, and alter_table. Most database methods that alter the schema take regular arguments, but create_table and alter_table take a block. The methods you can use inside the create_table block are documented in Sequel::Schema::Generator, and the methods you can use inside the alter_table block are documented in Sequel::Schema::AlterTableGenerator.
|
8
|
-
|
9
|
-
Migrations are not required, you can just call the schema modification methods directly on the database object. This is often done in test code and examples. However, it is recommended that you use the migration framework unless the database schema will not be changing in the future, as it provides a way to easily handle modifications to existing database schema.
|
10
|
-
|
11
|
-
Sequel has the ability to have database independent migrations using ruby classes as types. When you use a ruby class as a type, Sequel translates it to the most comparable type in the database you are using. Here's an example using all supported types:
|
12
|
-
|
13
|
-
DB.create_table(:cats) do
|
14
|
-
primary_key :id, :type=>Integer # integer
|
15
|
-
String :a # varchar(255)
|
16
|
-
String :a2, :size=>50 # varchar(50)
|
17
|
-
String :a3, :fixed=>true # char(255)
|
18
|
-
String :a4, :fixed=>true, :size=>50 # char(50)
|
19
|
-
String :a5, :text=>true # text
|
20
|
-
column :b, File # blob
|
21
|
-
Fixnum :c # integer
|
22
|
-
foreign_key :d, :other_table, :type=>Bignum # bigint
|
23
|
-
Float :e # double precision
|
24
|
-
BigDecimal :f # numeric
|
25
|
-
BigDecimal :f2, :size=>10 # numeric(10)
|
26
|
-
BigDecimal :f3, :size=>[10, 2] # numeric(10, 2)
|
27
|
-
Date :g # date
|
28
|
-
DateTime :h # timestamp
|
29
|
-
Time :i # timestamp
|
30
|
-
Time :i2, :only_time=>true # time
|
31
|
-
Numeric :j # numeric
|
32
|
-
TrueClass :k # boolean
|
33
|
-
FalseClass :l # boolean
|
34
|
-
end
|
35
|
-
|
36
|
-
Basically, if you use one of the ruby classes above, it will translate into a database specific type. If you use a lowercase method, symbol, or string to specify the type, Sequel won't attempt to translate it.
|
@@ -1,320 +0,0 @@
|
|
1
|
-
module Sequel
|
2
|
-
class Database
|
3
|
-
AUTOINCREMENT = 'AUTOINCREMENT'.freeze
|
4
|
-
CASCADE = 'CASCADE'.freeze
|
5
|
-
COMMA_SEPARATOR = ', '.freeze
|
6
|
-
NO_ACTION = 'NO ACTION'.freeze
|
7
|
-
NOT_NULL = ' NOT NULL'.freeze
|
8
|
-
NULL = ' NULL'.freeze
|
9
|
-
PRIMARY_KEY = ' PRIMARY KEY'.freeze
|
10
|
-
RESTRICT = 'RESTRICT'.freeze
|
11
|
-
SET_DEFAULT = 'SET DEFAULT'.freeze
|
12
|
-
SET_NULL = 'SET NULL'.freeze
|
13
|
-
TEMPORARY = 'TEMPORARY '.freeze
|
14
|
-
UNDERSCORE = '_'.freeze
|
15
|
-
UNIQUE = ' UNIQUE'.freeze
|
16
|
-
UNSIGNED = ' UNSIGNED'.freeze
|
17
|
-
|
18
|
-
# Default serial primary key options.
|
19
|
-
def serial_primary_key_options
|
20
|
-
{:primary_key => true, :type => Integer, :auto_increment => true}
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
# The SQL to execute to modify the DDL for the given table name. op
|
26
|
-
# should be one of the operations returned by the AlterTableGenerator.
|
27
|
-
def alter_table_sql(table, op)
|
28
|
-
quoted_name = quote_identifier(op[:name]) if op[:name]
|
29
|
-
alter_table_op = case op[:op]
|
30
|
-
when :add_column
|
31
|
-
"ADD COLUMN #{column_definition_sql(op)}"
|
32
|
-
when :drop_column
|
33
|
-
"DROP COLUMN #{quoted_name}"
|
34
|
-
when :rename_column
|
35
|
-
"RENAME COLUMN #{quoted_name} TO #{quote_identifier(op[:new_name])}"
|
36
|
-
when :set_column_type
|
37
|
-
"ALTER COLUMN #{quoted_name} TYPE #{type_literal(op)}"
|
38
|
-
when :set_column_default
|
39
|
-
"ALTER COLUMN #{quoted_name} SET DEFAULT #{literal(op[:default])}"
|
40
|
-
when :set_column_null
|
41
|
-
"ALTER COLUMN #{quoted_name} #{op[:null] ? 'DROP' : 'SET'} NOT NULL"
|
42
|
-
when :add_index
|
43
|
-
return index_definition_sql(table, op)
|
44
|
-
when :drop_index
|
45
|
-
return drop_index_sql(table, op)
|
46
|
-
when :add_constraint
|
47
|
-
"ADD #{constraint_definition_sql(op)}"
|
48
|
-
when :drop_constraint
|
49
|
-
"DROP CONSTRAINT #{quoted_name}"
|
50
|
-
else
|
51
|
-
raise Error, "Unsupported ALTER TABLE operation"
|
52
|
-
end
|
53
|
-
"ALTER TABLE #{quote_schema_table(table)} #{alter_table_op}"
|
54
|
-
end
|
55
|
-
|
56
|
-
# Array of SQL DDL modification statements for the given table,
|
57
|
-
# corresponding to the DDL changes specified by the operations.
|
58
|
-
def alter_table_sql_list(table, operations)
|
59
|
-
operations.map{|op| alter_table_sql(table, op)}
|
60
|
-
end
|
61
|
-
|
62
|
-
# The SQL string specify the autoincrement property, generally used by
|
63
|
-
# primary keys.
|
64
|
-
def auto_increment_sql
|
65
|
-
AUTOINCREMENT
|
66
|
-
end
|
67
|
-
|
68
|
-
# SQL DDL fragment containing the column creation SQL for the given column.
|
69
|
-
def column_definition_sql(column)
|
70
|
-
sql = "#{quote_identifier(column[:name])} #{type_literal(column)}"
|
71
|
-
sql << UNIQUE if column[:unique]
|
72
|
-
null = column.fetch(:null, column[:allow_null])
|
73
|
-
sql << NOT_NULL if null == false
|
74
|
-
sql << NULL if null == true
|
75
|
-
sql << " DEFAULT #{literal(column[:default])}" if column.include?(:default)
|
76
|
-
sql << PRIMARY_KEY if column[:primary_key]
|
77
|
-
sql << " #{auto_increment_sql}" if column[:auto_increment]
|
78
|
-
sql << column_references_column_constraint_sql(column) if column[:table]
|
79
|
-
sql
|
80
|
-
end
|
81
|
-
|
82
|
-
# SQL DDL fragment containing the column creation
|
83
|
-
# SQL for all given columns, used inside a CREATE TABLE block.
|
84
|
-
def column_list_sql(generator)
|
85
|
-
(generator.columns.map{|c| column_definition_sql(c)} + generator.constraints.map{|c| constraint_definition_sql(c)}).join(COMMA_SEPARATOR)
|
86
|
-
end
|
87
|
-
|
88
|
-
# SQL DDL fragment for column foreign key references (column constraints)
|
89
|
-
def column_references_column_constraint_sql(column)
|
90
|
-
column_references_sql(column)
|
91
|
-
end
|
92
|
-
|
93
|
-
# SQL DDL fragment for column foreign key references
|
94
|
-
def column_references_sql(column)
|
95
|
-
sql = " REFERENCES #{quote_schema_table(column[:table])}"
|
96
|
-
sql << "(#{Array(column[:key]).map{|x| quote_identifier(x)}.join(COMMA_SEPARATOR)})" if column[:key]
|
97
|
-
sql << " ON DELETE #{on_delete_clause(column[:on_delete])}" if column[:on_delete]
|
98
|
-
sql << " ON UPDATE #{on_delete_clause(column[:on_update])}" if column[:on_update]
|
99
|
-
sql
|
100
|
-
end
|
101
|
-
|
102
|
-
# SQL DDL fragment for table foreign key references (table constraints)
|
103
|
-
def column_references_table_constraint_sql(constraint)
|
104
|
-
"FOREIGN KEY #{literal(constraint[:columns])}#{column_references_sql(constraint)}"
|
105
|
-
end
|
106
|
-
|
107
|
-
# SQL DDL fragment specifying a constraint on a table.
|
108
|
-
def constraint_definition_sql(constraint)
|
109
|
-
sql = constraint[:name] ? "CONSTRAINT #{quote_identifier(constraint[:name])} " : ""
|
110
|
-
case constraint[:type]
|
111
|
-
when :check
|
112
|
-
check = constraint[:check]
|
113
|
-
sql << "CHECK #{filter_expr((check.is_a?(Array) && check.length == 1) ? check.first : check)}"
|
114
|
-
when :primary_key
|
115
|
-
sql << "PRIMARY KEY #{literal(constraint[:columns])}"
|
116
|
-
when :foreign_key
|
117
|
-
sql << column_references_table_constraint_sql(constraint)
|
118
|
-
when :unique
|
119
|
-
sql << "UNIQUE #{literal(constraint[:columns])}"
|
120
|
-
else
|
121
|
-
raise Error, "Invalid constriant type #{constraint[:type]}, should be :check, :primary_key, :foreign_key, or :unique"
|
122
|
-
end
|
123
|
-
sql
|
124
|
-
end
|
125
|
-
|
126
|
-
# DDL statement for creating a table with the given name, columns, and options
|
127
|
-
def create_table_sql(name, generator, options)
|
128
|
-
"CREATE #{temporary_table_sql if options[:temp]}TABLE #{quote_schema_table(name)} (#{column_list_sql(generator)})"
|
129
|
-
end
|
130
|
-
|
131
|
-
# Default index name for the table and columns, may be too long
|
132
|
-
# for certain databases.
|
133
|
-
def default_index_name(table_name, columns)
|
134
|
-
schema, table = schema_and_table(table_name)
|
135
|
-
"#{"#{schema}_" if schema and schema != default_schema}#{table}_#{columns.map{|c| [String, Symbol].any?{|cl| c.is_a?(cl)} ? c : literal(c).gsub(/\W/, '_')}.join(UNDERSCORE)}_index"
|
136
|
-
end
|
137
|
-
|
138
|
-
# The SQL to drop an index for the table.
|
139
|
-
def drop_index_sql(table, op)
|
140
|
-
"DROP INDEX #{quote_identifier(op[:name] || default_index_name(table, op[:columns]))}"
|
141
|
-
end
|
142
|
-
|
143
|
-
# SQL DDL statement to drop the table with the given name.
|
144
|
-
def drop_table_sql(name)
|
145
|
-
"DROP TABLE #{quote_schema_table(name)}"
|
146
|
-
end
|
147
|
-
|
148
|
-
# Proxy the filter_expr call to the dataset, used for creating constraints.
|
149
|
-
def filter_expr(*args, &block)
|
150
|
-
schema_utility_dataset.literal(schema_utility_dataset.send(:filter_expr, *args, &block))
|
151
|
-
end
|
152
|
-
|
153
|
-
# SQL DDL statement for creating an index for the table with the given name
|
154
|
-
# and index specifications.
|
155
|
-
def index_definition_sql(table_name, index)
|
156
|
-
index_name = index[:name] || default_index_name(table_name, index[:columns])
|
157
|
-
if index[:type]
|
158
|
-
raise Error, "Index types are not supported for this database"
|
159
|
-
elsif index[:where]
|
160
|
-
raise Error, "Partial indexes are not supported for this database"
|
161
|
-
else
|
162
|
-
"CREATE #{'UNIQUE ' if index[:unique]}INDEX #{quote_identifier(index_name)} ON #{quote_schema_table(table_name)} #{literal(index[:columns])}"
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
# Array of SQL DDL statements, one for each index specification,
|
167
|
-
# for the given table.
|
168
|
-
def index_sql_list(table_name, indexes)
|
169
|
-
indexes.map{|i| index_definition_sql(table_name, i)}
|
170
|
-
end
|
171
|
-
|
172
|
-
# SQL DDL ON DELETE fragment to use, based on the given action.
|
173
|
-
# The following actions are recognized:
|
174
|
-
#
|
175
|
-
# * :cascade - Delete rows referencing this row.
|
176
|
-
# * :no_action (default) - Raise an error if other rows reference this
|
177
|
-
# row, allow deferring of the integrity check.
|
178
|
-
# * :restrict - Raise an error if other rows reference this row,
|
179
|
-
# but do not allow deferring the integrity check.
|
180
|
-
# * :set_default - Set columns referencing this row to their default value.
|
181
|
-
# * :set_null - Set columns referencing this row to NULL.
|
182
|
-
def on_delete_clause(action)
|
183
|
-
case action
|
184
|
-
when :restrict
|
185
|
-
RESTRICT
|
186
|
-
when :cascade
|
187
|
-
CASCADE
|
188
|
-
when :set_null
|
189
|
-
SET_NULL
|
190
|
-
when :set_default
|
191
|
-
SET_DEFAULT
|
192
|
-
else
|
193
|
-
NO_ACTION
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
# Proxy the quote_schema_table method to the dataset
|
198
|
-
def quote_schema_table(table)
|
199
|
-
schema_utility_dataset.quote_schema_table(table)
|
200
|
-
end
|
201
|
-
|
202
|
-
# Proxy the quote_identifier method to the dataset, used for quoting tables and columns.
|
203
|
-
def quote_identifier(v)
|
204
|
-
schema_utility_dataset.quote_identifier(v)
|
205
|
-
end
|
206
|
-
|
207
|
-
# SQL DDL statement for renaming a table.
|
208
|
-
def rename_table_sql(name, new_name)
|
209
|
-
"ALTER TABLE #{quote_schema_table(name)} RENAME TO #{quote_schema_table(new_name)}"
|
210
|
-
end
|
211
|
-
|
212
|
-
# SQL DDL fragment for temporary table
|
213
|
-
def temporary_table_sql
|
214
|
-
self.class.const_get(:TEMPORARY)
|
215
|
-
end
|
216
|
-
|
217
|
-
# SQL fragment specifying the type of a given column.
|
218
|
-
def type_literal(column)
|
219
|
-
column[:type].is_a?(Class) ? type_literal_generic(column) : type_literal_specific(column)
|
220
|
-
end
|
221
|
-
|
222
|
-
# SQL fragment specifying the full type of a column,
|
223
|
-
# consider the type with possible modifiers.
|
224
|
-
def type_literal_generic(column)
|
225
|
-
meth = "type_literal_generic_#{column[:type].name.to_s.downcase}"
|
226
|
-
if respond_to?(meth, true)
|
227
|
-
send(meth, column)
|
228
|
-
else
|
229
|
-
raise Error, "Unsupported ruby class used as database type: #{column[:type]}"
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
# Alias for type_literal_generic_numeric, to make overriding in a subclass easier.
|
234
|
-
def type_literal_generic_bigdecimal(column)
|
235
|
-
type_literal_generic_numeric(column)
|
236
|
-
end
|
237
|
-
|
238
|
-
# Sequel uses the bigint type by default for Bignums.
|
239
|
-
def type_literal_generic_bignum(column)
|
240
|
-
:bigint
|
241
|
-
end
|
242
|
-
|
243
|
-
# Sequel uses the date type by default for Dates.
|
244
|
-
def type_literal_generic_date(column)
|
245
|
-
:date
|
246
|
-
end
|
247
|
-
|
248
|
-
# Sequel uses the timestamp type by default for DateTimes.
|
249
|
-
def type_literal_generic_datetime(column)
|
250
|
-
:timestamp
|
251
|
-
end
|
252
|
-
|
253
|
-
# Alias for type_literal_generic_trueclass, to make overriding in a subclass easier.
|
254
|
-
def type_literal_generic_falseclass(column)
|
255
|
-
type_literal_generic_trueclass(column)
|
256
|
-
end
|
257
|
-
|
258
|
-
# Sequel uses the blob type by default for Files.
|
259
|
-
def type_literal_generic_file(column)
|
260
|
-
:blob
|
261
|
-
end
|
262
|
-
|
263
|
-
# Alias for type_literal_generic_integer, to make overriding in a subclass easier.
|
264
|
-
def type_literal_generic_fixnum(column)
|
265
|
-
type_literal_generic_integer(column)
|
266
|
-
end
|
267
|
-
|
268
|
-
# Sequel uses the double precision type by default for Floats.
|
269
|
-
def type_literal_generic_float(column)
|
270
|
-
:"double precision"
|
271
|
-
end
|
272
|
-
|
273
|
-
# Sequel uses the integer type by default for integers
|
274
|
-
def type_literal_generic_integer(column)
|
275
|
-
:integer
|
276
|
-
end
|
277
|
-
|
278
|
-
# Sequel uses the numeric type by default for Numerics and BigDecimals.
|
279
|
-
# If a size is given, it is used, otherwise, it will default to whatever
|
280
|
-
# the database default is for an unsized value.
|
281
|
-
def type_literal_generic_numeric(column)
|
282
|
-
column[:size] ? "numeric(#{Array(column[:size]).join(', ')})" : :numeric
|
283
|
-
end
|
284
|
-
|
285
|
-
# Sequel uses the varchar type by default for Strings. If a
|
286
|
-
# size isn't present, Sequel assumes a size of 255. If the
|
287
|
-
# :fixed option is used, Sequel uses the char type. If the
|
288
|
-
# :text option is used, Sequel uses the :text type.
|
289
|
-
def type_literal_generic_string(column)
|
290
|
-
if column[:text]
|
291
|
-
:text
|
292
|
-
elsif column[:fixed]
|
293
|
-
"char(#{column[:size]||255})"
|
294
|
-
else
|
295
|
-
"varchar(#{column[:size]||255})"
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
# Sequel uses the timestamp type by default for Time values.
|
300
|
-
# If the :only_time option is used, the time type is used.
|
301
|
-
def type_literal_generic_time(column)
|
302
|
-
column[:only_time] ? :time : :timestamp
|
303
|
-
end
|
304
|
-
|
305
|
-
# Sequel uses the boolean type by default for TrueClass and FalseClass.
|
306
|
-
def type_literal_generic_trueclass(column)
|
307
|
-
:boolean
|
308
|
-
end
|
309
|
-
|
310
|
-
# SQL fragment for the given type of a column if the column is not one of the
|
311
|
-
# generic types specified with a ruby class.
|
312
|
-
def type_literal_specific(column)
|
313
|
-
type = column[:type]
|
314
|
-
type = "double precision" if type.to_s == 'double'
|
315
|
-
column[:size] ||= 255 if type.to_s == 'varchar'
|
316
|
-
elements = column[:size] || column[:elements]
|
317
|
-
"#{type}#{literal(Array(elements)) if elements}#{UNSIGNED if column[:unsigned]}"
|
318
|
-
end
|
319
|
-
end
|
320
|
-
end
|