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.



789
790
791
792
793
794
795
796
# File 'lib/groonga/schema.rb', line 789

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)

テーブルの名前



786
787
788
# File 'lib/groonga/schema.rb', line 786

def name
  @name
end

Instance Method Details

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



1169
1170
1171
1172
1173
1174
# File 'lib/groonga/schema.rb', line 1169

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



1142
1143
1144
# File 'lib/groonga/schema.rb', line 1142

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: Compressed by zlib.
    • :lz4: Compzressed by LZ4.
    • :zstd: Compressed by Zstandard.
    • :zstandard: Compressed by Zstandard.


857
858
859
860
861
862
863
864
865
866
# File 'lib/groonga/schema.rb', line 857

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



1177
1178
1179
# File 'lib/groonga/schema.rb', line 1177

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

#defineObject



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/groonga/schema.rb', line 799

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



1073
1074
1075
# File 'lib/groonga/schema.rb', line 1073

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+ を指定した場合、テーブル がトークナイザー利用している場合は自動的に有効になる。 +TokenDelimit+ など全文検索用ではないトークナイザーを 使う場合は明示的に +false+ を指定することで使用リソース を少なくできる。=:


941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'lib/groonga/schema.rb', line 941

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:



1005
1006
1007
# File 'lib/groonga/schema.rb', line 1005

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

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

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

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



1014
1015
1016
# File 'lib/groonga/schema.rb', line 1014

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

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

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

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



1024
1025
1026
# File 'lib/groonga/schema.rb', line 1024

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:



995
996
997
# File 'lib/groonga/schema.rb', line 995

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

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

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

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



1121
1122
1123
# File 'lib/groonga/schema.rb', line 1121

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



1133
1134
1135
1136
# File 'lib/groonga/schema.rb', line 1133

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



872
873
874
875
876
877
878
879
880
# File 'lib/groonga/schema.rb', line 872

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



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

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.



889
890
891
892
893
894
895
896
897
898
# File 'lib/groonga/schema.rb', line 889

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



1102
1103
1104
# File 'lib/groonga/schema.rb', line 1102

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

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

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

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



1112
1113
1114
# File 'lib/groonga/schema.rb', line 1112

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

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

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

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



1082
1083
1084
# File 'lib/groonga/schema.rb', line 1082

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

#timestamps(options = {}) ⇒ Object

以下と同様:

table.time("updated_at")
table.time("created_at")


1092
1093
1094
1095
# File 'lib/groonga/schema.rb', line 1092

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:



1153
1154
1155
# File 'lib/groonga/schema.rb', line 1153

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:



1044
1045
1046
# File 'lib/groonga/schema.rb', line 1044

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



1053
1054
1055
# File 'lib/groonga/schema.rb', line 1053

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

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

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

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



1063
1064
1065
# File 'lib/groonga/schema.rb', line 1063

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:



1034
1035
1036
# File 'lib/groonga/schema.rb', line 1034

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:



1163
1164
1165
# File 'lib/groonga/schema.rb', line 1163

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