Class | Sequel::Schema::Generator |
In: |
lib/sequel_core/schema/generator.rb
|
Parent: | Object |
Schema::Generator is an internal class that the user is not expected to instantiate directly. Instances are created by Database#create_table. It is used to specify table creation parameters. It takes a Database object and a block of column/index/constraint specifications, and gives the Database a table description, which the database uses to create a table.
Schema::Generator has some methods but also includes method_missing, allowing users to specify column type as a method instead of using the column method, which makes for a nicer DSL.
GENERIC_TYPES | = | [String, Integer, Fixnum, Bignum, Float, Numeric, BigDecimal, Date, DateTime, Time, File, TrueClass, FalseClass] | Classes specifying generic types that Sequel will convert to database-specific types. |
Add a method for each of the given types that creates a column with that type as a constant. Types given should either already be constants/classes or a capitalized string/symbol with the same name as a constant/class.
# File lib/sequel_core/schema/generator.rb, line 34 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
Set the database in which to create the table, and evaluate the block in the context of this object.
# File lib/sequel_core/schema/generator.rb, line 22 22: def initialize(db, &block) 23: @db = db 24: @columns = [] 25: @indexes = [] 26: @primary_key = nil 27: instance_eval(&block) if block 28: end
Add a unnamed constraint to the DDL, specified by the given block or args.
# File lib/sequel_core/schema/generator.rb, line 42 42: def check(*args, &block) 43: constraint(nil, *args, &block) 44: end
Add a column with the given name, type, and opts to the DDL.
You can also create columns via method missing, so the following are equivalent:
column :number, :integer integer :number
The following options are supported:
# File lib/sequel_core/schema/generator.rb, line 74 74: def column(name, type, opts = {}) 75: @columns << {:name => name, :type => type}.merge(opts) 76: index(name) if opts[:index] 77: end
Adds a named constraint (or unnamed if name is nil) to the DDL, with the given block or args.
# File lib/sequel_core/schema/generator.rb, line 81 81: def constraint(name, *args, &block) 82: @columns << {:name => name, :type => :check, :check => block || args, 83: :constraint_type => :check} 84: end
Return the DDL created by the generator as a array of two elements, the first being the columns and the second being the indexes.
# File lib/sequel_core/schema/generator.rb, line 88 88: def create_info 89: @columns.unshift(@primary_key) if @primary_key && !has_column?(primary_key_name) 90: [@columns, @indexes] 91: end
Add a foreign key in the table that references another table to the DDL. See column for available options.
# File lib/sequel_core/schema/generator.rb, line 95 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
Add an index on the given column(s) with the given options to the DDL. The available options are:
# File lib/sequel_core/schema/generator.rb, line 126 126: def index(columns, opts = {}) 127: @indexes << {:columns => Array(columns)}.merge(opts) 128: end
Add primary key information to the DDL. Takes between one and three arguments. The last one is an options hash as for Generator#column. The first one distinguishes two modes: an array of existing column names adds a composite primary key constraint. A single symbol adds a new column of that name and makes it the primary key. In that case the optional middle argument denotes the type.
Examples:
primary_key(:id) primary_key(:zip_code, :null => false) primary_key([:street_number, :house_number]) primary_key(:id, :string, :auto_increment => false)
# File lib/sequel_core/schema/generator.rb, line 148 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
The name of the primary key for this table, if it has a primary key.
# File lib/sequel_core/schema/generator.rb, line 163 163: def primary_key_name 164: @primary_key[:name] if @primary_key 165: end
Add a unique constraint on the given columns to the DDL.
# File lib/sequel_core/schema/generator.rb, line 173 173: def unique(columns, opts = {}) 174: @columns << {:type => :check, :constraint_type => :unique, 175: :name => nil, :columns => Array(columns)}.merge(opts) 176: end