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.



958
959
960
961
962
963
964
965
# File 'lib/groonga/schema.rb', line 958

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)

テーブルの名前



955
956
957
# File 'lib/groonga/schema.rb', line 955

def name
  @name
end

Instance Method Details

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



1329
1330
1331
1332
1333
1334
# File 'lib/groonga/schema.rb', line 1329

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 を参照。



1302
1303
1304
# File 'lib/groonga/schema.rb', line 1302

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 := 値の配列を格納する。

  • :compress (Object)

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

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

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



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/groonga/schema.rb', line 1019

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



1337
1338
1339
# File 'lib/groonga/schema.rb', line 1337

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

#defineObject



968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/groonga/schema.rb', line 968

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 を参照。



1233
1234
1235
# File 'lib/groonga/schema.rb', line 1233

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系のトークナイザーを利用している場合は 自動的に有効になる。 =:



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/groonga/schema.rb', line 1101

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:



1165
1166
1167
# File 'lib/groonga/schema.rb', line 1165

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

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

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

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



1174
1175
1176
# File 'lib/groonga/schema.rb', line 1174

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

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

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

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



1184
1185
1186
# File 'lib/groonga/schema.rb', line 1184

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:



1155
1156
1157
# File 'lib/groonga/schema.rb', line 1155

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

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

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

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



1281
1282
1283
# File 'lib/groonga/schema.rb', line 1281

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 を参照。



1293
1294
1295
1296
# File 'lib/groonga/schema.rb', line 1293

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 は不要?)。



1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/groonga/schema.rb', line 1034

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 }と指定す ることでインデックスカラムの任意のカラム名を指定することができる。



1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
# File 'lib/groonga/schema.rb', line 1131

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.



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/groonga/schema.rb', line 1051

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 を参照。



1262
1263
1264
# File 'lib/groonga/schema.rb', line 1262

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

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

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

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



1272
1273
1274
# File 'lib/groonga/schema.rb', line 1272

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

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

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

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



1242
1243
1244
# File 'lib/groonga/schema.rb', line 1242

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

#timestamps(options = {}) ⇒ Object

以下と同様: <pre> !!!ruby table.time(“updated_at”) table.time(“created_at”) </pre>



1252
1253
1254
1255
# File 'lib/groonga/schema.rb', line 1252

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:



1313
1314
1315
# File 'lib/groonga/schema.rb', line 1313

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:



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

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 を参照。



1213
1214
1215
# File 'lib/groonga/schema.rb', line 1213

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

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

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

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



1223
1224
1225
# File 'lib/groonga/schema.rb', line 1223

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:



1194
1195
1196
# File 'lib/groonga/schema.rb', line 1194

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:



1323
1324
1325
# File 'lib/groonga/schema.rb', line 1323

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