Class: Groonga::Schema
- Inherits:
-
Object
- Object
- Groonga::Schema
- Defined in:
- lib/groonga/schema.rb
Overview
groongaのスキーマ(データ構造)を管理するクラス。
Schema を使うことにより簡単にテーブルやカラムを 追加・削除することができる。
Defined Under Namespace
Modules: Path Classes: ColumnCreationWithDifferentOptions, ColumnDefinition, ColumnNotExists, ColumnRemoveDefinition, ColumnRenameDefinition, Error, IndexColumnDefinition, TableCreationWithDifferentOptions, TableDefinition, TableNotExists, TableRemoveDefinition, TableRenameDefinition, UnguessableReferenceTable, UnknownIndexTarget, UnknownIndexTargetTable, UnknownOptions, UnknownTableType
Constant Summary collapse
- NORMALIZE_TYPE_TABLE =
{ "short_text" => "ShortText", "string" => "ShortText", "text" => "Text", "binary" => "LongText", "long_text" => "LongText", "int8" => "Int8", "integer8" => "Int8", "int16" => "Int16", "integer16" => "Int16", "int" => "Int32", "integer" => "Int32", "int32" => "Int32", "integer32" => "Int32", "decimal" => "Int64", "int64" => "Int64", "integer64" => "Int64", "uint8" => "UInt8", "unsigned_integer8" => "UInt8", "uint16" => "UInt16", "unsigned_integer16" => "UInt16", "uint" => "UInt32", "unsigned_integer" => "UInt32", "uint32" => "UInt32", "unsigned_integer32" => "UInt32", "uint64" => "UInt64", "unsigned_integer64" => "UInt64", "float" => "Float", "datetime" => "Time", "timestamp" => "Time", "time" => "Time", "date" => "Time", "boolean" => "Bool", "tokyo_geo_point" => "TokyoGeoPoint", "geo_point" => "WGS84GeoPoint", "wgs84_geo_point" => "WGS84GeoPoint", "delimit" => "TokenDelimit", "token_delimit" => "TokenDelimit", "unigram" => "TokenUnigram", "token_unigram" => "TokenUnigram", "bigram" => "TokenBigram", "token_bigram" => "TokenBigram", "bigram_split_symbol" => "TokenBigramSplitSymbol", "token_bigram_split_symbol" => "TokenBigramSplitSymbol", "bigram_split_symbol_alpha" => "TokenBigramSplitSymbolAlpha", "token_bigram_split_symbol_alpha" => "TokenBigramSplitSymbolAlpha", "bigram_split_symbol_alpha_digit" => "TokenBigramSplitSymbolAlphaDigit", "token_bigram_split_symbol_alpha_digit" => "TokenBigramSplitSymbolAlphaDigit", "bigram_ignore_blank" => "TokenBigramIgnoreBlank", "token_bigram_ignore_blank" => "TokenBigramIgnoreBlank", "bigram_ignore_blank_split_symbol" => "TokenBigramIgnoreBlankSplitSymbol", "token_bigram_ignore_blank_split_symbol" => "TokenBigramIgnoreBlankSplitSymbol", "bigram_ignore_blank_split_symbol_alpha" => "TokenBigramIgnoreBlankSplitSymbolAlpha", "token_bigram_ignore_blank_split_symbol_alpha" => "TokenBigramIgnoreBlankSplitSymbolAlpha", "bigram_ignore_blank_split_symbol_alpha_digit" => "TokenBigramIgnoreBlankSplitSymbolAlphaDigit", "token_bigram_ignore_blank_split_symbol_alpha_digit" => "TokenBigramIgnoreBlankSplitSymbolAlphaDigit", "trigram" => "TokenTrigram", "token_trigram" => "TokenTrigram", "mecab" => "TokenMecab", "token_mecab"=> "TokenMecab", "regexp" => "TokenRegexp", "token_regexp"=> "TokenRegexp", }
Class Method Summary collapse
-
.change_table(name, options = {}, &block) ⇒ Object
名前が name のテーブルを変更する。以下の省略形。.
-
.create_table(name, options = {}, &block) ⇒ Object
名前が name のテーブルを作成する。以下の省略形。.
-
.define(options = {}) {|schema| ... } ⇒ Object
スキーマを定義する。ブロックには Schema オブ ジェクトがわたるので、そのオブジェクトを利用してスキー マを定義する。以下の省略形。.
-
.dump(options = {}) ⇒ Object
スキーマの内容を文字列をRubyスクリプト形式またはgrn式 形式で返す。デフォルトはRubyスクリプト形式である。 Rubyスクリプト形式で返された値は Schema.restore することによりスキーマ内に組み 込むことができる。.
- .normalize_type(type, options = {}) ⇒ Object
-
.remove_column(table_name, column_name) ⇒ Object
以下と同様:.
-
.remove_table(name, options = {}) ⇒ Object
名前が name のテーブルを削除する。.
-
.rename_column(table_name, current_column_name, new_column_name) ⇒ Object
This is a syntax sugar of the following:.
-
.rename_table(current_name, new_name, options = {}) ⇒ Object
(See Groonga::Schema#rename_table).
-
.restore(dumped_text, options = {}) ⇒ Object
Schema.dump で文字列化したスキーマを組み込む。.
Instance Method Summary collapse
-
#change_table(name, options = {}) {|definition| ... } ⇒ Object
名前が name のテーブルを変更する。.
- #context ⇒ Object
-
#create_table(name, options = {}) {|definition| ... } ⇒ Object
名前が name のテーブルを作成する。.
-
#define ⇒ Object
定義されたスキーマ定義を実際に実行する。.
-
#dump ⇒ Object
スキーマの内容を文字列で返す。返された値は #restore することによりスキーマ内に組み込むことができる。.
-
#initialize(options = {}) ⇒ Schema
constructor
スキーマ定義を開始する。.
-
#remove_column(table_name, column_name) ⇒ Object
以下と同様:.
-
#remove_table(name, options = {}) ⇒ Object
名前が name のテーブルを削除する。.
-
#rename_column(table_name, current_column_name, new_column_name) ⇒ Object
It is a syntax sugar of the following:.
-
#rename_table(current_name, new_name, options = {}) ⇒ Object
Renames current_name table to _new_name.
-
#restore(dumped_text) ⇒ Object
(also: #load)
#dump で返されたスキーマの内容を読み込む。.
Constructor Details
Class Method Details
.change_table(name, options = {}, &block) ⇒ Object
名前が name のテーブルを変更する。以下の省略形。
Groonga::Schema.define do |schema|
schema.change_table(name, ) do |table|
# ...
end
end
ブロックには TableDefinition オブジェ クトがわたるので、そのオブジェクトを利用してテーブル の詳細を定義する。
416 417 418 419 420 |
# File 'lib/groonga/schema.rb', line 416 def change_table(name, ={}, &block) define do |schema| schema.change_table(name, , &block) end end |
.create_table(name, options = {:type => :array}, &block) ⇒ Object .create_table(name, options = {:type => :hash}, &block) ⇒ Object .create_table(name, options = {:type => :patricia_trie}, &block) ⇒ Object .create_table(name, options = {:type => :double_array_trie}) ⇒ Object
名前が name のテーブルを作成する。以下の省略形。
Groonga::Schema.define do |schema|
schema.create_table(name, ) do |table|
# ...
end
end
ブロックには TableDefinition オブジェ クトがわたるので、そのオブジェクトを利用してテーブル の詳細を定義する。
options に指定可能な値は以下の通り。
379 380 381 382 383 |
# File 'lib/groonga/schema.rb', line 379 def create_table(name, ={}, &block) define do |schema| schema.create_table(name, , &block) end end |
.define(options = {}) {|schema| ... } ⇒ Object
スキーマを定義する。ブロックには Groonga::Schema オブ ジェクトがわたるので、そのオブジェクトを利用してスキー マを定義する。以下の省略形。
schema = Groonga::Scheme.new()
# ...
schema.define
168 169 170 171 172 |
# File 'lib/groonga/schema.rb', line 168 def define(={}) schema = new() yield(schema) schema.define end |
.dump(options = {}) ⇒ Object
スキーマの内容を文字列をRubyスクリプト形式またはgrn式 形式で返す。デフォルトはRubyスクリプト形式である。 Rubyスクリプト形式で返された値は restore することによりスキーマ内に組み 込むことができる。
dump.rb:
File.open("/tmp/groonga-schema.rb", "w") do |schema|
dumped_text = Groonga::Schema.dump
end
restore.rb:
dumped_text = Groonga::Schema.dump
Groonga::Database.create(:path => "/tmp/new-db.grn")
Groonga::Schema.restore(dumped_text)
grn式形式で返された値はgroongaコマンドで読み込むこと ができる。
dump.rb:
File.open("/tmp/groonga-schema.grn", "w") do |schema|
dumped_text = Groonga::Schema.dump(:syntax => :command)
end
% groonga db/path < /tmp/groonga-schema.grn
520 521 522 523 524 |
# File 'lib/groonga/schema.rb', line 520 def dump(={}) schema = new(:context => [:context], :syntax => [:syntax]) schema.dump end |
.normalize_type(type, options = {}) ⇒ Object
605 606 607 608 609 610 611 |
# File 'lib/groonga/schema.rb', line 605 def normalize_type(type, ={}) return type if type.nil? return type if type.is_a?(Groonga::Object) type = type.to_s if type.is_a?(Symbol) return type if ([:context] || Groonga::Context.default)[type] NORMALIZE_TYPE_TABLE[type] || type end |
.remove_column(table_name, column_name) ⇒ Object
以下と同様:
Groonga::Schema.change_table(table_name) do |table|
table.remove_column(column_name)
end
446 447 448 449 450 |
# File 'lib/groonga/schema.rb', line 446 def remove_column(table_name, column_name) change_table(table_name) do |table| table.remove_column(column_name) end end |
.remove_table(name, options = {}) ⇒ Object
名前が name のテーブルを削除する。
390 391 392 393 394 |
# File 'lib/groonga/schema.rb', line 390 def remove_table(name, ={}) define do |schema| schema.remove_table(name, ) end end |
.rename_column(table_name, current_column_name, new_column_name) ⇒ Object
461 462 463 464 465 |
# File 'lib/groonga/schema.rb', line 461 def rename_column(table_name, current_column_name, new_column_name) define do |schema| schema.rename_column(table_name, current_column_name, new_column_name) end end |
.rename_table(current_name, new_name, options = {}) ⇒ Object
432 433 434 435 436 |
# File 'lib/groonga/schema.rb', line 432 def rename_table(current_name, new_name, ={}) define do |schema| schema.rename_table(current_name, new_name, ) end end |
Instance Method Details
#change_table(name, options = {}) {|definition| ... } ⇒ Object
名前が name のテーブルを変更する。
テーブルの変更は #define を呼び出すまでは実行されないこ とに注意すること。
700 701 702 703 704 705 |
# File 'lib/groonga/schema.rb', line 700 def change_table(name, ={}) = @options.merge( || {}).merge(:change => true) definition = TableDefinition.new(name, ) yield(definition) @definitions << definition end |
#context ⇒ Object
751 752 753 |
# File 'lib/groonga/schema.rb', line 751 def context @options[:context] || Groonga::Context.default end |
#create_table(name, options = {:type => :array}, &block) ⇒ Object #create_table(name, options = {:type => :hash}, &block) ⇒ Object #create_table(name, options = {:type => :patricia_trie}, &block) ⇒ Object #create_table(name, options = {:type => :double_array_trie}) ⇒ Object
名前が name のテーブルを作成する。
テーブルの作成は #define を呼び出すまでは実行されないこ とに注意すること。
669 670 671 672 673 |
# File 'lib/groonga/schema.rb', line 669 def create_table(name, ={}) definition = TableDefinition.new(name, @options.merge( || {})) yield(definition) if block_given? @definitions << definition end |
#define ⇒ Object
定義されたスキーマ定義を実際に実行する。
627 628 629 630 631 |
# File 'lib/groonga/schema.rb', line 627 def define @definitions.each do |definition| definition.define end end |
#dump ⇒ Object
スキーマの内容を文字列で返す。返された値は #restore することによりスキーマ内に組み込むことができる。
646 647 648 649 650 |
# File 'lib/groonga/schema.rb', line 646 def dump dumper = SchemaDumper.new(:context => @options[:context], :syntax => @options[:syntax] || :ruby) dumper.dump end |
#remove_column(table_name, column_name) ⇒ Object
以下と同様:
schema.change_table(table_name) do |table|
table.remove_column(column_name)
end
730 731 732 733 734 |
# File 'lib/groonga/schema.rb', line 730 def remove_column(table_name, column_name) change_table(table_name) do |table| table.remove_column(column_name) end end |
#remove_table(name, options = {}) ⇒ Object
名前が name のテーブルを削除する。
テーブルの削除は# define を呼び出すまでは実行されないこ とに注意すること。
685 686 687 688 |
# File 'lib/groonga/schema.rb', line 685 def remove_table(name, ={}) definition = TableRemoveDefinition.new(name, @options.merge( || {})) @definitions << definition end |
#rename_column(table_name, current_column_name, new_column_name) ⇒ Object
It is a syntax sugar of the following:
schema.change_table(table_name) do |table|
table.rename_column(current_column_name, new_column_name)
end
744 745 746 747 748 |
# File 'lib/groonga/schema.rb', line 744 def rename_column(table_name, current_column_name, new_column_name) change_table(table_name) do |table| table.rename_column(current_column_name, new_column_name) end end |
#rename_table(current_name, new_name, options = {}) ⇒ Object
Renames current_name table to _new_name.
Note that table renaming will will not be performed until #define is called.
716 717 718 719 720 |
# File 'lib/groonga/schema.rb', line 716 def rename_table(current_name, new_name, ={}) = @options.merge( || {}) definition = TableRenameDefinition.new(current_name, new_name, ) @definitions << definition end |