epugh-sequel 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/README.rdoc +652 -0
  2. data/VERSION.yml +4 -0
  3. data/bin/sequel +104 -0
  4. data/lib/sequel.rb +1 -0
  5. data/lib/sequel/adapters/ado.rb +85 -0
  6. data/lib/sequel/adapters/db2.rb +132 -0
  7. data/lib/sequel/adapters/dbi.rb +101 -0
  8. data/lib/sequel/adapters/do.rb +197 -0
  9. data/lib/sequel/adapters/do/mysql.rb +38 -0
  10. data/lib/sequel/adapters/do/postgres.rb +92 -0
  11. data/lib/sequel/adapters/do/sqlite.rb +31 -0
  12. data/lib/sequel/adapters/firebird.rb +307 -0
  13. data/lib/sequel/adapters/informix.rb +75 -0
  14. data/lib/sequel/adapters/jdbc.rb +485 -0
  15. data/lib/sequel/adapters/jdbc/h2.rb +62 -0
  16. data/lib/sequel/adapters/jdbc/mysql.rb +56 -0
  17. data/lib/sequel/adapters/jdbc/oracle.rb +23 -0
  18. data/lib/sequel/adapters/jdbc/postgresql.rb +101 -0
  19. data/lib/sequel/adapters/jdbc/sqlite.rb +43 -0
  20. data/lib/sequel/adapters/mysql.rb +370 -0
  21. data/lib/sequel/adapters/odbc.rb +184 -0
  22. data/lib/sequel/adapters/openbase.rb +57 -0
  23. data/lib/sequel/adapters/oracle.rb +140 -0
  24. data/lib/sequel/adapters/postgres.rb +453 -0
  25. data/lib/sequel/adapters/shared/mssql.rb +93 -0
  26. data/lib/sequel/adapters/shared/mysql.rb +341 -0
  27. data/lib/sequel/adapters/shared/oracle.rb +62 -0
  28. data/lib/sequel/adapters/shared/postgres.rb +743 -0
  29. data/lib/sequel/adapters/shared/progress.rb +34 -0
  30. data/lib/sequel/adapters/shared/sqlite.rb +263 -0
  31. data/lib/sequel/adapters/sqlite.rb +243 -0
  32. data/lib/sequel/adapters/utils/date_format.rb +21 -0
  33. data/lib/sequel/adapters/utils/stored_procedures.rb +75 -0
  34. data/lib/sequel/adapters/utils/unsupported.rb +62 -0
  35. data/lib/sequel/connection_pool.rb +258 -0
  36. data/lib/sequel/core.rb +204 -0
  37. data/lib/sequel/core_sql.rb +185 -0
  38. data/lib/sequel/database.rb +687 -0
  39. data/lib/sequel/database/schema_generator.rb +324 -0
  40. data/lib/sequel/database/schema_methods.rb +164 -0
  41. data/lib/sequel/database/schema_sql.rb +324 -0
  42. data/lib/sequel/dataset.rb +422 -0
  43. data/lib/sequel/dataset/convenience.rb +237 -0
  44. data/lib/sequel/dataset/prepared_statements.rb +220 -0
  45. data/lib/sequel/dataset/sql.rb +1105 -0
  46. data/lib/sequel/deprecated.rb +529 -0
  47. data/lib/sequel/exceptions.rb +44 -0
  48. data/lib/sequel/extensions/blank.rb +42 -0
  49. data/lib/sequel/extensions/inflector.rb +288 -0
  50. data/lib/sequel/extensions/pagination.rb +96 -0
  51. data/lib/sequel/extensions/pretty_table.rb +78 -0
  52. data/lib/sequel/extensions/query.rb +48 -0
  53. data/lib/sequel/extensions/string_date_time.rb +47 -0
  54. data/lib/sequel/metaprogramming.rb +44 -0
  55. data/lib/sequel/migration.rb +212 -0
  56. data/lib/sequel/model.rb +142 -0
  57. data/lib/sequel/model/association_reflection.rb +263 -0
  58. data/lib/sequel/model/associations.rb +1024 -0
  59. data/lib/sequel/model/base.rb +911 -0
  60. data/lib/sequel/model/deprecated.rb +188 -0
  61. data/lib/sequel/model/deprecated_hooks.rb +103 -0
  62. data/lib/sequel/model/deprecated_inflector.rb +335 -0
  63. data/lib/sequel/model/deprecated_validations.rb +384 -0
  64. data/lib/sequel/model/errors.rb +37 -0
  65. data/lib/sequel/model/exceptions.rb +7 -0
  66. data/lib/sequel/model/inflections.rb +230 -0
  67. data/lib/sequel/model/plugins.rb +74 -0
  68. data/lib/sequel/object_graph.rb +230 -0
  69. data/lib/sequel/plugins/caching.rb +122 -0
  70. data/lib/sequel/plugins/hook_class_methods.rb +122 -0
  71. data/lib/sequel/plugins/schema.rb +53 -0
  72. data/lib/sequel/plugins/single_table_inheritance.rb +63 -0
  73. data/lib/sequel/plugins/validation_class_methods.rb +373 -0
  74. data/lib/sequel/sql.rb +854 -0
  75. data/lib/sequel/version.rb +11 -0
  76. data/lib/sequel_core.rb +1 -0
  77. data/lib/sequel_model.rb +1 -0
  78. data/spec/adapters/ado_spec.rb +46 -0
  79. data/spec/adapters/firebird_spec.rb +376 -0
  80. data/spec/adapters/informix_spec.rb +96 -0
  81. data/spec/adapters/mysql_spec.rb +875 -0
  82. data/spec/adapters/oracle_spec.rb +272 -0
  83. data/spec/adapters/postgres_spec.rb +692 -0
  84. data/spec/adapters/spec_helper.rb +10 -0
  85. data/spec/adapters/sqlite_spec.rb +550 -0
  86. data/spec/core/connection_pool_spec.rb +526 -0
  87. data/spec/core/core_ext_spec.rb +156 -0
  88. data/spec/core/core_sql_spec.rb +528 -0
  89. data/spec/core/database_spec.rb +1214 -0
  90. data/spec/core/dataset_spec.rb +3513 -0
  91. data/spec/core/expression_filters_spec.rb +363 -0
  92. data/spec/core/migration_spec.rb +261 -0
  93. data/spec/core/object_graph_spec.rb +280 -0
  94. data/spec/core/pretty_table_spec.rb +58 -0
  95. data/spec/core/schema_generator_spec.rb +167 -0
  96. data/spec/core/schema_spec.rb +778 -0
  97. data/spec/core/spec_helper.rb +82 -0
  98. data/spec/core/version_spec.rb +7 -0
  99. data/spec/extensions/blank_spec.rb +67 -0
  100. data/spec/extensions/caching_spec.rb +201 -0
  101. data/spec/extensions/hook_class_methods_spec.rb +470 -0
  102. data/spec/extensions/inflector_spec.rb +122 -0
  103. data/spec/extensions/pagination_spec.rb +99 -0
  104. data/spec/extensions/pretty_table_spec.rb +91 -0
  105. data/spec/extensions/query_spec.rb +85 -0
  106. data/spec/extensions/schema_spec.rb +111 -0
  107. data/spec/extensions/single_table_inheritance_spec.rb +53 -0
  108. data/spec/extensions/spec_helper.rb +90 -0
  109. data/spec/extensions/string_date_time_spec.rb +93 -0
  110. data/spec/extensions/validation_class_methods_spec.rb +1054 -0
  111. data/spec/integration/dataset_test.rb +160 -0
  112. data/spec/integration/eager_loader_test.rb +683 -0
  113. data/spec/integration/prepared_statement_test.rb +130 -0
  114. data/spec/integration/schema_test.rb +183 -0
  115. data/spec/integration/spec_helper.rb +75 -0
  116. data/spec/integration/type_test.rb +96 -0
  117. data/spec/model/association_reflection_spec.rb +93 -0
  118. data/spec/model/associations_spec.rb +1780 -0
  119. data/spec/model/base_spec.rb +494 -0
  120. data/spec/model/caching_spec.rb +217 -0
  121. data/spec/model/dataset_methods_spec.rb +78 -0
  122. data/spec/model/eager_loading_spec.rb +1165 -0
  123. data/spec/model/hooks_spec.rb +472 -0
  124. data/spec/model/inflector_spec.rb +126 -0
  125. data/spec/model/model_spec.rb +588 -0
  126. data/spec/model/plugins_spec.rb +142 -0
  127. data/spec/model/record_spec.rb +1243 -0
  128. data/spec/model/schema_spec.rb +92 -0
  129. data/spec/model/spec_helper.rb +124 -0
  130. data/spec/model/validations_spec.rb +1080 -0
  131. data/spec/rcov.opts +6 -0
  132. data/spec/spec.opts +0 -0
  133. data/spec/spec_config.rb.example +10 -0
  134. metadata +202 -0
@@ -0,0 +1,324 @@
1
+ module Sequel
2
+ # The Schema module holds the schema generators and the SQL code relating
3
+ # to SQL DDL (Data Definition Language).
4
+ module Schema
5
+ # Schema::Generator is an internal class that the user is not expected
6
+ # to instantiate directly. Instances are created by Database#create_table.
7
+ # It is used to specify table creation parameters. It takes a Database
8
+ # object and a block of column/index/constraint specifications, and
9
+ # gives the Database a table description, which the database uses to
10
+ # create a table.
11
+ #
12
+ # Schema::Generator has some methods but also includes method_missing,
13
+ # allowing users to specify column type as a method instead of using
14
+ # the column method, which makes for a nicer DSL.
15
+ class Generator
16
+ # Classes specifying generic types that Sequel will convert to database-specific types.
17
+ GENERIC_TYPES=[String, Integer, Fixnum, Bignum, Float, Numeric, BigDecimal,
18
+ Date, DateTime, Time, File, TrueClass, FalseClass]
19
+
20
+ # Set the database in which to create the table, and evaluate the block
21
+ # in the context of this object.
22
+ def initialize(db, &block)
23
+ @db = db
24
+ @columns = []
25
+ @indexes = []
26
+ @primary_key = nil
27
+ instance_eval(&block) if block
28
+ end
29
+
30
+ # Add a method for each of the given types that creates a column
31
+ # with that type as a constant. Types given should either already
32
+ # be constants/classes or a capitalized string/symbol with the same name
33
+ # as a constant/class.
34
+ def self.add_type_method(*types)
35
+ types.each do |type|
36
+ class_eval "def #{type}(name, opts={}); column(name, #{type}, opts); end"
37
+ end
38
+ end
39
+
40
+ # Add a unnamed constraint to the DDL, specified by the given block
41
+ # or args.
42
+ def check(*args, &block)
43
+ constraint(nil, *args, &block)
44
+ end
45
+
46
+ # Add a column with the given name, type, and opts to the DDL.
47
+ #
48
+ # You can also create columns via method missing, so the following are
49
+ # equivalent:
50
+ #
51
+ # column :number, :integer
52
+ # integer :number
53
+ #
54
+ # The following options are supported:
55
+ #
56
+ # * :default - The default value for the column.
57
+ # * :index - Create an index on this column.
58
+ # * :key - For foreign key columns, the column in the associated table
59
+ # that this column references. Unnecessary if this column
60
+ # references the primary key of the associated table.
61
+ # * :null - Mark the column as allowing NULL values (if true),
62
+ # or not allowing NULL values (if false). If unspecified, will default
63
+ # to whatever the database default is.
64
+ # * :on_delete - Specify the behavior of this column when being deleted.
65
+ # See Dataset#on_delete_clause for options.
66
+ # * :on_update - Specify the behavior of this column when being updated.
67
+ # See Schema::SQL#on_delete_clause for options.
68
+ # * :size - The size of the column, generally used with string
69
+ # columns to specify the maximum number of characters the column will hold.
70
+ # * :unique - Mark the column is unique, generally has the same effect as
71
+ # creating a unique index on the column.
72
+ # * :unsigned - Make the column type unsigned, only useful for integer
73
+ # columns.
74
+ def column(name, type, opts = {})
75
+ @columns << {:name => name, :type => type}.merge(opts)
76
+ index(name) if opts[:index]
77
+ end
78
+
79
+ # Adds a named constraint (or unnamed if name is nil) to the DDL,
80
+ # with the given block or args.
81
+ def constraint(name, *args, &block)
82
+ @columns << {:name => name, :type => :check, :check => block || args,
83
+ :constraint_type => :check}
84
+ end
85
+
86
+ # Return the DDL created by the generator as a array of two elements,
87
+ # the first being the columns and the second being the indexes.
88
+ def create_info
89
+ @columns.unshift(@primary_key) if @primary_key && !has_column?(primary_key_name)
90
+ [@columns, @indexes]
91
+ end
92
+
93
+ # Add a foreign key in the table that references another table to the DDL. See column
94
+ # for available options.
95
+ def foreign_key(name, table=nil, opts = {})
96
+ opts = case table
97
+ when Hash
98
+ table.merge(opts)
99
+ when Symbol
100
+ opts.merge(:table=>table)
101
+ when NilClass
102
+ opts
103
+ else
104
+ raise(Error, "The second argument to foreign_key should be a Hash, Symbol, or nil")
105
+ end
106
+ return composite_foreign_key(name, opts) if name.is_a?(Array)
107
+ column(name, Integer, opts)
108
+ end
109
+
110
+ # Add a full text index on the given columns to the DDL.
111
+ def full_text_index(columns, opts = {})
112
+ index(columns, opts.merge(:type => :full_text))
113
+ end
114
+
115
+ # True if the DDL includes the creation of a column with the given name.
116
+ def has_column?(name)
117
+ @columns.any?{|c| c[:name] == name}
118
+ end
119
+
120
+ # Add an index on the given column(s) with the given options to the DDL.
121
+ # The available options are:
122
+ #
123
+ # * :type - The type of index to use (only supported by some databases)
124
+ # * :unique - Make the index unique, so duplicate values are not allowed.
125
+ # * :where - Create a partial index (only supported by some databases)
126
+ def index(columns, opts = {})
127
+ @indexes << {:columns => Array(columns)}.merge(opts)
128
+ end
129
+
130
+ # Add a column with the given type, name, and opts to the DDL. See column for available
131
+ # options.
132
+ def method_missing(type, name = nil, opts = {})
133
+ name ? column(name, type, opts) : super
134
+ end
135
+
136
+ # Add primary key information to the DDL. Takes between one and three
137
+ # arguments. The last one is an options hash as for Generator#column.
138
+ # The first one distinguishes two modes: an array of existing column
139
+ # names adds a composite primary key constraint. A single symbol adds a
140
+ # new column of that name and makes it the primary key. In that case the
141
+ # optional middle argument denotes the type.
142
+ #
143
+ # Examples:
144
+ # primary_key(:id)
145
+ # primary_key(:zip_code, :null => false)
146
+ # primary_key([:street_number, :house_number])
147
+ # primary_key(:id, :string, :auto_increment => false)
148
+ def primary_key(name, *args)
149
+ return composite_primary_key(name, *args) if name.is_a?(Array)
150
+ @primary_key = @db.serial_primary_key_options.merge({:name => name})
151
+
152
+ if opts = args.pop
153
+ opts = {:type => opts} unless opts.is_a?(Hash)
154
+ if type = args.pop
155
+ opts.merge!(:type => type)
156
+ end
157
+ @primary_key.merge!(opts)
158
+ end
159
+ @primary_key
160
+ end
161
+
162
+ # The name of the primary key for this table, if it has a primary key.
163
+ def primary_key_name
164
+ @primary_key[:name] if @primary_key
165
+ end
166
+
167
+ # Add a spatial index on the given columns to the DDL.
168
+ def spatial_index(columns, opts = {})
169
+ index(columns, opts.merge(:type => :spatial))
170
+ end
171
+
172
+ # Add a unique constraint on the given columns to the DDL.
173
+ def unique(columns, opts = {})
174
+ @columns << {:type => :check, :constraint_type => :unique,
175
+ :name => nil, :columns => Array(columns)}.merge(opts)
176
+ end
177
+
178
+ private
179
+
180
+ # Add a composite primary key constraint
181
+ def composite_primary_key(columns, *args)
182
+ opts = args.pop || {}
183
+ @columns << {:type => :check, :constraint_type => :primary_key,
184
+ :name => nil, :columns => columns}.merge(opts)
185
+ end
186
+
187
+ # Add a composite foreign key constraint
188
+ def composite_foreign_key(columns, opts)
189
+ @columns << {:type => :check, :constraint_type => :foreign_key,
190
+ :name => nil, :columns => columns }.merge(opts)
191
+ end
192
+
193
+ add_type_method(*GENERIC_TYPES)
194
+ end
195
+
196
+ # Schema::AlterTableGenerator is an internal class that the user is not expected
197
+ # to instantiate directly. Instances are created by Database#alter_table.
198
+ # It is used to specify table alteration parameters. It takes a Database
199
+ # object and a block of operations to perform on the table, and
200
+ # gives the Database a table an array of operations, which the database uses to
201
+ # alter a table's description.
202
+ class AlterTableGenerator
203
+ # An array of DDL operations to perform
204
+ attr_reader :operations
205
+
206
+ # Set the Database object to which to apply the DDL, and evaluate the
207
+ # block in the context of this object.
208
+ def initialize(db, &block)
209
+ @db = db
210
+ @operations = []
211
+ instance_eval(&block) if block
212
+ end
213
+
214
+ # Add a column with the given name, type, and opts to the DDL for the table.
215
+ # See Generator#column for the available options.
216
+ def add_column(name, type, opts = {})
217
+ @operations << {:op => :add_column, :name => name, :type => type}.merge(opts)
218
+ end
219
+
220
+ # Add a constraint with the given name and args to the DDL for the table.
221
+ # See Generator#constraint.
222
+ def add_constraint(name, *args, &block)
223
+ @operations << {:op => :add_constraint, :name => name, :type => :check, \
224
+ :constraint_type => :check, :check => block || args}
225
+ end
226
+
227
+ def add_unique_constraint(columns, opts = {})
228
+ @operations << {:op => :add_constraint, :type => :check,
229
+ :constraint_type => :unique, :columns => Array(columns)}.merge(opts)
230
+ end
231
+
232
+ # Add a foreign key with the given name and referencing the given table
233
+ # to the DDL for the table. See Generator#column for the available options.
234
+ #
235
+ # You can also pass an array of column names for creating composite foreign
236
+ # keys. In this case, it will assume the columns exists and will only add
237
+ # the constraint.
238
+ #
239
+ # NOTE: If you need to add a foreign key constraint to an existing column
240
+ # use the composite key syntax even if it is only one column.
241
+ def add_foreign_key(name, table, opts = {})
242
+ return add_composite_foreign_key(name, table, opts) if name.is_a?(Array)
243
+ add_column(name, Integer, {:table=>table}.merge(opts))
244
+ end
245
+
246
+ # Add a full text index on the given columns to the DDL for the table.
247
+ # See Generator#index for available options.
248
+ def add_full_text_index(columns, opts = {})
249
+ add_index(columns, {:type=>:full_text}.merge(opts))
250
+ end
251
+
252
+ # Add an index on the given columns to the DDL for the table. See
253
+ # Generator#index for available options.
254
+ def add_index(columns, opts = {})
255
+ @operations << {:op => :add_index, :columns => Array(columns)}.merge(opts)
256
+ end
257
+
258
+ # Add a primary key to the DDL for the table. See Generator#column
259
+ # for the available options.
260
+ def add_primary_key(name, opts = {})
261
+ return add_composite_primary_key(name, opts) if name.is_a?(Array)
262
+ opts = @db.serial_primary_key_options.merge(opts)
263
+ add_column(name, opts.delete(:type), opts)
264
+ end
265
+
266
+ # Add a spatial index on the given columns to the DDL for the table.
267
+ # See Generator#index for available options.
268
+ def add_spatial_index(columns, opts = {})
269
+ add_index(columns, {:type=>:spatial}.merge(opts))
270
+ end
271
+
272
+ # Remove a column from the DDL for the table.
273
+ def drop_column(name)
274
+ @operations << {:op => :drop_column, :name => name}
275
+ end
276
+
277
+ # Remove a constraint from the DDL for the table.
278
+ def drop_constraint(name)
279
+ @operations << {:op => :drop_constraint, :name => name}
280
+ end
281
+
282
+ # Remove an index from the DDL for the table.
283
+ def drop_index(columns)
284
+ @operations << {:op => :drop_index, :columns => Array(columns)}
285
+ end
286
+
287
+ # Modify a column's name in the DDL for the table.
288
+ def rename_column(name, new_name, opts = {})
289
+ @operations << {:op => :rename_column, :name => name, :new_name => new_name}.merge(opts)
290
+ end
291
+
292
+ # Modify a column's default value in the DDL for the table.
293
+ def set_column_default(name, default)
294
+ @operations << {:op => :set_column_default, :name => name, :default => default}
295
+ end
296
+
297
+ # Modify a column's type in the DDL for the table.
298
+ def set_column_type(name, type, opts={})
299
+ @operations << {:op => :set_column_type, :name => name, :type => type}.merge(opts)
300
+ end
301
+
302
+ # Modify a column's NOT NULL constraint.
303
+ def set_column_allow_null(name, allow_null)
304
+ @operations << {:op => :set_column_null, :name => name, :null => allow_null}
305
+ end
306
+
307
+ private
308
+
309
+ # Add a composite primary key constraint
310
+ def add_composite_primary_key(columns, opts)
311
+ @operations << {:op => :add_constraint, :type => :check,
312
+ :constraint_type => :primary_key, :columns => columns}.merge(opts)
313
+ end
314
+
315
+ # Add a composite foreign key constraint
316
+ def add_composite_foreign_key(columns, table, opts)
317
+ @operations << {:op => :add_constraint, :type => :check,
318
+ :constraint_type => :foreign_key, :columns => columns,
319
+ :table => table}.merge(opts)
320
+ end
321
+ end
322
+ end
323
+ end
324
+
@@ -0,0 +1,164 @@
1
+ module Sequel
2
+ class Database
3
+ # Adds a column to the specified table. This method expects a column name,
4
+ # a datatype and optionally a hash with additional constraints and options:
5
+ #
6
+ # DB.add_column :items, :name, :text, :unique => true, :null => false
7
+ # DB.add_column :items, :category, :text, :default => 'ruby'
8
+ #
9
+ # See alter_table.
10
+ def add_column(table, *args)
11
+ alter_table(table) {add_column(*args)}
12
+ end
13
+
14
+ # Adds an index to a table for the given columns:
15
+ #
16
+ # DB.add_index :posts, :title
17
+ # DB.add_index :posts, [:author, :title], :unique => true
18
+ #
19
+ # See alter_table.
20
+ def add_index(table, *args)
21
+ alter_table(table) {add_index(*args)}
22
+ end
23
+
24
+ # Alters the given table with the specified block. Here are the currently
25
+ # available operations:
26
+ #
27
+ # DB.alter_table :items do
28
+ # add_column :category, :text, :default => 'ruby'
29
+ # drop_column :category
30
+ # rename_column :cntr, :counter
31
+ # set_column_type :value, :float
32
+ # set_column_default :value, :float
33
+ # add_index [:group, :category]
34
+ # drop_index [:group, :category]
35
+ # end
36
+ #
37
+ # Note that #add_column accepts all the options available for column
38
+ # definitions using create_table, and #add_index accepts all the options
39
+ # available for index definition.
40
+ #
41
+ # See Schema::AlterTableGenerator.
42
+ def alter_table(name, generator=nil, &block)
43
+ remove_cached_schema(name)
44
+ generator ||= Schema::AlterTableGenerator.new(self, &block)
45
+ alter_table_sql_list(name, generator.operations).flatten.each {|sql| execute_ddl(sql)}
46
+ end
47
+
48
+ # Creates a table with the columns given in the provided block:
49
+ #
50
+ # DB.create_table :posts do
51
+ # primary_key :id
52
+ # column :title, :text
53
+ # column :content, :text
54
+ # index :title
55
+ # end
56
+ #
57
+ # See Schema::Generator.
58
+ def create_table(name, options={}, &block)
59
+ options = {:generator=>options} if options.is_a?(Schema::Generator)
60
+ create_table_sql_list(name, *((options[:generator] || Schema::Generator.new(self, &block)).create_info << options)).flatten.each {|sql| execute_ddl(sql)}
61
+ end
62
+
63
+ # Forcibly creates a table. If the table already exists it is dropped.
64
+ def create_table!(name, options={}, &block)
65
+ drop_table(name) rescue nil
66
+ create_table(name, options, &block)
67
+ end
68
+
69
+ # Creates a view, replacing it if it already exists:
70
+ #
71
+ # DB.create_or_replace_view(:cheap_items, "SELECT * FROM items WHERE price < 100")
72
+ # DB.create_or_replace_view(:ruby_items, DB[:items].filter(:category => 'ruby'))
73
+ def create_or_replace_view(name, source)
74
+ remove_cached_schema(name)
75
+ source = source.sql if source.is_a?(Dataset)
76
+ execute_ddl("CREATE OR REPLACE VIEW #{quote_schema_table(name)} AS #{source}")
77
+ end
78
+
79
+ # Creates a view based on a dataset or an SQL string:
80
+ #
81
+ # DB.create_view(:cheap_items, "SELECT * FROM items WHERE price < 100")
82
+ # DB.create_view(:ruby_items, DB[:items].filter(:category => 'ruby'))
83
+ def create_view(name, source)
84
+ source = source.sql if source.is_a?(Dataset)
85
+ execute_ddl("CREATE VIEW #{quote_schema_table(name)} AS #{source}")
86
+ end
87
+
88
+ # Removes a column from the specified table:
89
+ #
90
+ # DB.drop_column :items, :category
91
+ #
92
+ # See alter_table.
93
+ def drop_column(table, *args)
94
+ alter_table(table) {drop_column(*args)}
95
+ end
96
+
97
+ # Removes an index for the given table and column/s:
98
+ #
99
+ # DB.drop_index :posts, :title
100
+ # DB.drop_index :posts, [:author, :title]
101
+ #
102
+ # See alter_table.
103
+ def drop_index(table, columns)
104
+ alter_table(table) {drop_index(columns)}
105
+ end
106
+
107
+ # Drops one or more tables corresponding to the given table names:
108
+ #
109
+ # DB.drop_table(:posts, :comments)
110
+ def drop_table(*names)
111
+ names.each do |n|
112
+ remove_cached_schema(n)
113
+ execute_ddl(drop_table_sql(n))
114
+ end
115
+ end
116
+
117
+ # Drops a view:
118
+ #
119
+ # DB.drop_view(:cheap_items)
120
+ def drop_view(*names)
121
+ names.each do |n|
122
+ remove_cached_schema(n)
123
+ execute_ddl("DROP VIEW #{quote_schema_table(n)}")
124
+ end
125
+ end
126
+
127
+ # Renames a table:
128
+ #
129
+ # DB.tables #=> [:items]
130
+ # DB.rename_table :items, :old_items
131
+ # DB.tables #=> [:old_items]
132
+ def rename_table(*args)
133
+ execute_ddl(rename_table_sql(*args))
134
+ end
135
+
136
+ # Renames a column in the specified table. This method expects the current
137
+ # column name and the new column name:
138
+ #
139
+ # DB.rename_column :items, :cntr, :counter
140
+ #
141
+ # See alter_table.
142
+ def rename_column(table, *args)
143
+ alter_table(table) {rename_column(*args)}
144
+ end
145
+
146
+ # Sets the default value for the given column in the given table:
147
+ #
148
+ # DB.set_column_default :items, :category, 'perl!'
149
+ #
150
+ # See alter_table.
151
+ def set_column_default(table, *args)
152
+ alter_table(table) {set_column_default(*args)}
153
+ end
154
+
155
+ # Set the data type for the given column in the given table:
156
+ #
157
+ # DB.set_column_type :items, :price, :float
158
+ #
159
+ # See alter_table.
160
+ def set_column_type(table, *args)
161
+ alter_table(table) {set_column_type(*args)}
162
+ end
163
+ end
164
+ end