Class Sequel::Schema::Generator
In: lib/sequel/database/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.

Methods

Constants

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.

Public Class methods

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.

[Source]

    # File lib/sequel/database/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.

[Source]

    # File lib/sequel/database/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

Public Instance methods

Add a unnamed constraint to the DDL, specified by the given block or args.

[Source]

    # File lib/sequel/database/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:

  • :default - The default value for the column.
  • :index - Create an index on this column.
  • :key - For foreign key columns, the column in the associated table that this column references. Unnecessary if this column references the primary key of the associated table.
  • :null - Mark the column as allowing NULL values (if true), or not allowing NULL values (if false). If unspecified, will default to whatever the database default is.
  • :on_delete - Specify the behavior of this column when being deleted. See Dataset#on_delete_clause for options.
  • :on_update - Specify the behavior of this column when being updated. See Schema::SQL#on_delete_clause for options.
  • :size - The size of the column, generally used with string columns to specify the maximum number of characters the column will hold.
  • :unique - Mark the column as unique, generally has the same effect as creating a unique index on the column.
  • :unsigned - Make the column type unsigned, only useful for integer columns.

[Source]

    # File lib/sequel/database/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.

[Source]

    # File lib/sequel/database/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.

[Source]

    # File lib/sequel/database/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.

[Source]

     # File lib/sequel/database/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 a full text index on the given columns to the DDL.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 111
111:       def full_text_index(columns, opts = {})
112:         index(columns, opts.merge(:type => :full_text))
113:       end

True if the DDL includes the creation of a column with the given name.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 116
116:       def has_column?(name)
117:         @columns.any?{|c| c[:name] == name}
118:       end

Add an index on the given column(s) with the given options to the DDL. The available options are:

  • :type - The type of index to use (only supported by some databases)
  • :unique - Make the index unique, so duplicate values are not allowed.
  • :where - Create a partial index (only supported by some databases)

[Source]

     # File lib/sequel/database/schema_generator.rb, line 126
126:       def index(columns, opts = {})
127:         @indexes << {:columns => Array(columns)}.merge(opts)
128:       end

Add a column with the given type, name, and opts to the DDL. See column for available options.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 132
132:       def method_missing(type, name = nil, opts = {})
133:         name ? column(name, type, opts) : super
134:       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)

[Source]

     # File lib/sequel/database/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.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 163
163:       def primary_key_name
164:         @primary_key[:name] if @primary_key
165:       end

Add a spatial index on the given columns to the DDL.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 168
168:       def spatial_index(columns, opts = {})
169:         index(columns, opts.merge(:type => :spatial))
170:       end

Add a unique constraint on the given columns to the DDL.

[Source]

     # File lib/sequel/database/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

[Validate]