Class: Groonga::Schema::TableDefinition

Inherits:
Object
  • Object
show all
Includes:
Path
Defined in:
lib/groonga/schema.rb

Overview

スキーマ定義時に create_table#create_table からブロックに渡されてくる オブジェクト

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Path

#columns_directory_path, #rmdir_if_available, #tables_directory_path

Constructor Details

#initialize(name, options) ⇒ TableDefinition

Returns a new instance of TableDefinition.



819
820
821
822
823
824
825
826
# File 'lib/groonga/schema.rb', line 819

def initialize(name, options)
  @name = name
  @name = @name.to_s if @name.is_a?(Symbol)
  @definitions = []
  validate_options(options)
  @options = options
  @table_type = table_type
end

Instance Attribute Details

#nameObject (readonly)

テーブルの名前



816
817
818
# File 'lib/groonga/schema.rb', line 816

def name
  @name
end

Instance Method Details

#[](name, definition_class = nil) ⇒ Object



1195
1196
1197
1198
1199
1200
# File 'lib/groonga/schema.rb', line 1195

def [](name, definition_class=nil)
  @definitions.find do |definition|
    definition.name.to_s == name.to_s and
      (definition_class.nil? or definition.is_a?(definition_class))
  end
end

#boolean(name, options = {}) ⇒ Object Also known as: bool

名前が name の真偽値を格納できるカラムを作成する。

options に指定可能な値は #column を参照。



1168
1169
1170
# File 'lib/groonga/schema.rb', line 1168

def boolean(name, options={})
  column(name, "Bool", options)
end

#column(name, type, options = {}) ⇒ Object

名前が name で型が type のカラムを作成する。

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :force (Object)

    true を指定すると既存の同名のカラムが 存在していても、強制的に新しいカラムを作成する。

  • :path (Object)

    カラムを保存するパス。

  • :persistent (Object)

    true を指定すると永続カラムとなる。 :path を省略 した場合は自動的にパスが付加される。

  • :type (Object) — default: :scalar

    カラムの値の格納方法について指定する。

    :scalar
    スカラ値(単独の値)を格納する。
    :vector
    値の配列を格納する。
  • :with_weight (Boolean) — default: false

    It specifies whether making the column weight vector column or not. Weight vector column can store weight for each element.

    You can’t use this option for scalar column.

  • :compress (Object)

    値の圧縮方法を指定する。省略した場合は、圧縮しない。

    :zlib
    値をzlib圧縮して格納する。
    :lzo
    値をlzo圧縮して格納する。


885
886
887
888
889
890
891
892
893
894
# File 'lib/groonga/schema.rb', line 885

def column(name, type, options={})
  definition = self[name, ColumnDefinition]
  if definition.nil?
    definition = ColumnDefinition.new(name, options)
    update_definition(name, ColumnDefinition, definition)
  end
  definition.type = type
  definition.options.merge!(column_options.merge(options))
  self
end

#contextObject



1203
1204
1205
# File 'lib/groonga/schema.rb', line 1203

def context
  @options[:context] || Groonga::Context.default
end

#defineObject



829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/groonga/schema.rb', line 829

def define
  table = context[@name]
  if @options[:change]
    raise TableNotExists.new(@name) if table.nil?
  else
    if table
      unless same_table?(table, create_options)
        if @options[:force]
          table.remove
          table = nil
        else
          options = create_options
          raise TableCreationWithDifferentOptions.new(table, options)
        end
      end
    end
    table ||= @table_type.create(create_options)
  end
  @definitions.each do |definition|
    definition.define(self, table)
  end
  table
end

#float(name, options = {}) ⇒ Object

名前が name のieee754形式の64bit浮動小数点数のカラム を作成する。

options に指定可能な値は #column を参照。



1099
1100
1101
# File 'lib/groonga/schema.rb', line 1099

def float(name, options={})
  column(name, "Float", options)
end

#index(target_table_or_target_column_full_name, *args) ⇒ Object

target_tabletarget_column を対象とするインデック スカラムを作成します。複数のカラムを指定することもで きます。

target_column_full_name で指定するときはテーブル名 とカラム名を"."でつなげます。

例えば、「Users」テーブルの「name」カラムのインデックスカラムを 指定する場合はこうなります。

Examples:

table.index("Users.name")

Parameters:

  • args (Array)

    インデックスカラム作成時に指定できるオプション。 ハッシュを使って次の要素を指定することができる。

    :name
    インデックスカラムのカラム名を任意に指定する。
    :force
    true を指定すると既存の同名のカラムが 存在していても、強制的に新しいカラムを作成する。
    :path
    カラムを保存するパス。
    :persistent
    true を指定すると永続カラムとなる。 :path を省略した場合は自動的にパスが付加される。
    :with_section
    true を指定すると転置索引にsection(段落情報)を 合わせて格納する。未指定または nil を指定した場合、 複数のカラムを指定すると自動的に有効になる。
    :with_weight
    true を指定すると転置索引にweight情報を合わせて 格納する。
    :with_position
    true を指定すると転置索引に出現位置情報を合わせて 格納する。未指定または nil を指定した場合、テーブル がN-gram系のトークナイザーを利用している場合は 自動的に有効になる。


967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/groonga/schema.rb', line 967

def index(target_table_or_target_column_full_name, *args)
  key, target_table, target_columns, options =
    parse_index_argument(target_table_or_target_column_full_name, *args)

  name = options.delete(:name)
  definition = self[key, IndexColumnDefinition]
  if definition.nil?
    definition = IndexColumnDefinition.new(name, options)
    update_definition(key, IndexColumnDefinition, definition)
  end
  definition.target_table = target_table
  definition.target_columns = target_columns
  definition.options.merge!(column_options.merge(options))
  self
end

#integer16(name, options = {}) ⇒ Object Also known as: int16

Defines a 16 bit signed integer column named name.

Parameters:

  • name (String or Symbol)

    the column name

  • options (Hash) (defaults to: {})

    ({}) the options

See Also:



1031
1032
1033
# File 'lib/groonga/schema.rb', line 1031

def integer16(name, options={})
  column(name, "Int16", options)
end

#integer32(name, options = {}) ⇒ Object Also known as: integer, int32

名前が name の32bit符号付き整数のカラムを作成する。

options に指定可能な値は #column を参照。



1040
1041
1042
# File 'lib/groonga/schema.rb', line 1040

def integer32(name, options={})
  column(name, "Int32", options)
end

#integer64(name, options = {}) ⇒ Object Also known as: int64

名前が name の64bit符号付き整数のカラムを作成する。

options に指定可能な値は #column を参照。



1050
1051
1052
# File 'lib/groonga/schema.rb', line 1050

def integer64(name, options={})
  column(name, "Int64", options)
end

#integer8(name, options = {}) ⇒ Object Also known as: int8

Defines a 8 bit signed integer column named name.

Parameters:

  • name (String or Symbol)

    the column name

  • options (Hash) (defaults to: {})

    ({}) the options

See Also:



1021
1022
1023
# File 'lib/groonga/schema.rb', line 1021

def integer8(name, options={})
  column(name, "Int8", options)
end

#long_text(name, options = {}) ⇒ Object

名前が name の2Gbyte以下の文字列を格納できるカラムを 作成する。

options に指定可能な値は #column を参照。



1147
1148
1149
# File 'lib/groonga/schema.rb', line 1147

def long_text(name, options={})
  column(name, "LongText", options)
end

#reference(name, table = nil, options = {}) ⇒ Object

名前が nametable のレコードIDを格納する参照カラ ムを作成する。

table が省略された場合は name の複数形が使われる。 例えば、 name が"user"な場合は table は"users"になる。

options に指定可能な値は #column を参照。



1159
1160
1161
1162
# File 'lib/groonga/schema.rb', line 1159

def reference(name, table=nil, options={})
  table ||= lambda {|context| guess_table_name(context, name)}
  column(name, table, options)
end

#remove_column(name, options = {}) ⇒ Object

名前が name のカラムを削除します。

options に指定可能な値はありません(TODO options は不要?)。



900
901
902
903
904
905
906
907
908
# File 'lib/groonga/schema.rb', line 900

def remove_column(name, options={})
  definition = self[name, ColumnRemoveDefinition]
  if definition.nil?
    definition = ColumnRemoveDefinition.new(name, options)
    update_definition(name, ColumnRemoveDefinition, definition)
  end
  definition.options.merge!(options)
  self
end

#remove_index(target_table_or_target_column_full_name, *args) ⇒ Object

target_tabletarget_column を対象とするインデッ クスカラムを削除します。

target_column_full_name で指定するときはテーブル名 とカラム名を"."でつなげます。

例えば、「Users」テーブルの「name」カラムのインデックスカラムを 削除する場合はこうなります。

Examples:

table.remove_index("Users.name")

Parameters:

  • args (::Hash)

    { :name => target_column }と指定す ることでインデックスカラムの任意のカラム名を指定することができる。



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/groonga/schema.rb', line 997

def remove_index(target_table_or_target_column_full_name, *args)
  key, target_table, target_columns, options =
    parse_index_argument(target_table_or_target_column_full_name, *args)

  name = options.delete(:name)
  name ||= lambda do |context|
    IndexColumnDefinition.column_name(context,
                                      target_table,
                                      target_columns)
  end
  definition = self[key, ColumnRemoveDefinition]
  if definition.nil?
    definition = ColumnRemoveDefinition.new(name, options)
    update_definition(key, ColumnRemoveDefinition, definition)
  end
  definition.options.merge!(options)
  self
end

#rename_column(current_name, new_name, options = {}) ⇒ Object

Renames current_name column to new_name column.

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Groonga:Context) — default: Groonga::Context.default

    The context to be used in renaming.



917
918
919
920
921
922
923
924
925
926
# File 'lib/groonga/schema.rb', line 917

def rename_column(current_name, new_name, options={})
  definition = self[name, ColumnRenameDefinition]
  if definition.nil?
    definition = ColumnRenameDefinition.new(current_name, new_name,
                                            options)
    update_definition(name, ColumnRenameDefinition, definition)
  end
  definition.options.merge!(options)
  self
end

#short_text(name, options = {}) ⇒ Object Also known as: string

名前が name の4Kbyte以下の文字列を格納できるカラムを 作成する。

options に指定可能な値は #column を参照。



1128
1129
1130
# File 'lib/groonga/schema.rb', line 1128

def short_text(name, options={})
  column(name, "ShortText", options)
end

#text(name, options = {}) ⇒ Object

名前が name の64Kbyte以下の文字列を格納できるカラムを 作成する。

options に指定可能な値は #column を参照。



1138
1139
1140
# File 'lib/groonga/schema.rb', line 1138

def text(name, options={})
  column(name, "Text", options)
end

#time(name, options = {}) ⇒ Object

名前が name の64bit符号付き整数で1970年1月1日0時0分 0秒からの経過マイクロ秒数を格納するカラムを作成する。

options に指定可能な値は #column を参照。



1108
1109
1110
# File 'lib/groonga/schema.rb', line 1108

def time(name, options={})
  column(name, "Time", options)
end

#timestamps(options = {}) ⇒ Object

以下と同様:

table.time(“updated_at”)
table.time(“created_at”)



1118
1119
1120
1121
# File 'lib/groonga/schema.rb', line 1118

def timestamps(options={})
  time("created_at", options)
  time("updated_at", options)
end

#tokyo_geo_point(name, options = {}) ⇒ Object

Defines a geo point in Tokyo geodetic system column named name.

Parameters:

  • name (String or Symbol)

    the column name

  • options (Hash) (defaults to: {})

    ({}) the options

See Also:



1179
1180
1181
# File 'lib/groonga/schema.rb', line 1179

def tokyo_geo_point(name, options={})
  column(name, "TokyoGeoPoint", options)
end

#unsigned_integer16(name, options = {}) ⇒ Object Also known as: uint16

Defines a 16 bit unsigned integer column named name.

Parameters:

  • name (String or Symbol)

    the column name

  • options (Hash) (defaults to: {})

    ({}) the options

See Also:



1070
1071
1072
# File 'lib/groonga/schema.rb', line 1070

def unsigned_integer16(name, options={})
  column(name, "UInt16", options)
end

#unsigned_integer32(name, options = {}) ⇒ Object Also known as: unsigned_integer, uint32

名前が name の32bit符号なし整数のカラムを作成する。

options に指定可能な値は #column を参照。



1079
1080
1081
# File 'lib/groonga/schema.rb', line 1079

def unsigned_integer32(name, options={})
  column(name, "UInt32", options)
end

#unsigned_integer64(name, options = {}) ⇒ Object Also known as: uint64

名前が name の64bit符号なし整数のカラムを作成する。

options に指定可能な値は #column を参照。



1089
1090
1091
# File 'lib/groonga/schema.rb', line 1089

def unsigned_integer64(name, options={})
  column(name, "UInt64", options)
end

#unsigned_integer8(name, options = {}) ⇒ Object Also known as: uint8

Defines a 8 bit unsigned integer column named name.

Parameters:

  • name (String or Symbol)

    the column name

  • options (Hash) (defaults to: {})

    ({}) the options

See Also:



1060
1061
1062
# File 'lib/groonga/schema.rb', line 1060

def unsigned_integer8(name, options={})
  column(name, "UInt8", options)
end

#wgs84_geo_point(name, options = {}) ⇒ Object Also known as: geo_point

Defines a geo point in WGS 84 (World Geodetic System) column named name.

Parameters:

  • name (String or Symbol)

    the column name

  • options (Hash) (defaults to: {})

    ({}) the options

See Also:



1189
1190
1191
# File 'lib/groonga/schema.rb', line 1189

def wgs84_geo_point(name, options={})
  column(name, "WGS84GeoPoint", options)
end