Class: Groovy::Schema
- Inherits:
-
Object
- Object
- Groovy::Schema
- Defined in:
- lib/groovy/schema.rb
Constant Summary collapse
- SEARCH_TABLE_NAME =
'Terms'.freeze
- SEARCH_TABLE_OPTIONS =
{ type: :patricia_trie, normalizer: :NormalizerAuto, key_type: "ShortText", default_tokenizer: "TokenBigram" }.freeze
- COLUMN_DEFAULTS =
{ compress: :zstandard }.freeze
- TYPES =
{ 'string' => 'short_text', 'text' => 'text', 'float' => 'float', # 'Bool' => 'boolean', 'boolean' => 'boolean', 'integer' => 'int32', 'big_integer' => 'int64', 'date' => 'date', 'time' => 'time', 'datetime' => 'time' }.freeze
Instance Attribute Summary collapse
-
#index_columns ⇒ Object
readonly
Returns the value of attribute index_columns.
Instance Method Summary collapse
- #attribute_columns ⇒ Object
- #boolean_columns ⇒ Object
- #column(name, type, options = {}) ⇒ Object
- #column_names ⇒ Object
- #columns_by_type(type) ⇒ Object
-
#initialize(context, table_name, opts = {}) ⇒ Schema
constructor
A new instance of Schema.
- #integer_columns ⇒ Object
- #many(name, table_name = nil, options = {}) ⇒ Object
- #one(name, table_name = nil, options = {}) ⇒ Object
- #plural_references ⇒ Object
- #rebuild! ⇒ Object
- #reference(name, table_name = nil, options = {}) ⇒ Object
- #refs_by_name ⇒ Object
- #refs_by_table ⇒ Object
- #reload ⇒ Object
- #reverse_reference_of(ref_name) ⇒ Object
-
#search_table ⇒ Object
def [](key) # @spec table.columns.select { |c| c.column? && c.name.to_s == ā##table_name.#keykey.to_sā } end.
- #singular_references ⇒ Object
- #sync(db_context = nil) ⇒ Object
- #table ⇒ Object
- #table_type ⇒ Object
- #time_columns ⇒ Object
- #timestamps(opts = {}) ⇒ Object
Constructor Details
#initialize(context, table_name, opts = {}) ⇒ Schema
Returns a new instance of Schema.
34 35 36 37 38 |
# File 'lib/groovy/schema.rb', line 34 def initialize(context, table_name, opts = {}) @context, @table_name, @opts = context, table_name, opts || {} @spec, @index_columns = {}, [] @cache = {} end |
Instance Attribute Details
#index_columns ⇒ Object (readonly)
Returns the value of attribute index_columns.
32 33 34 |
# File 'lib/groovy/schema.rb', line 32 def index_columns @index_columns end |
Instance Method Details
#attribute_columns ⇒ Object
69 70 71 |
# File 'lib/groovy/schema.rb', line 69 def attribute_columns @cache[:attribute_columns] ||= get_names(table.columns.select { |c| c.column? && !c.reference_column? && !c.vector? }) end |
#boolean_columns ⇒ Object
81 82 83 |
# File 'lib/groovy/schema.rb', line 81 def boolean_columns @cache[:boolean_columns] ||= columns_by_type('Bool') end |
#column(name, type, options = {}) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/groovy/schema.rb', line 116 def column(name, type, = {}) # groonga_type = Types.map(type) groonga_type = type @index_columns.push(name) if .delete(:index) @spec[name.to_sym] = { type: groonga_type, args: [COLUMN_DEFAULTS.merge()] } end |
#column_names ⇒ Object
57 58 59 |
# File 'lib/groovy/schema.rb', line 57 def column_names @cache[:column_names] ||= get_names(table.columns) end |
#columns_by_type(type) ⇒ Object
85 86 87 |
# File 'lib/groovy/schema.rb', line 85 def columns_by_type(type) get_names(table.columns.select { |c| c.column? && c.range.name == type }) end |
#integer_columns ⇒ Object
77 78 79 |
# File 'lib/groovy/schema.rb', line 77 def integer_columns @cache[:integer_columns] ||= columns_by_type('Int32') end |
#many(name, table_name = nil, options = {}) ⇒ Object
146 147 148 |
# File 'lib/groovy/schema.rb', line 146 def many(name, table_name = nil, = {}) reference(name, table_name, .merge(type: :vector)) end |
#one(name, table_name = nil, options = {}) ⇒ Object
142 143 144 |
# File 'lib/groovy/schema.rb', line 142 def one(name, table_name = nil, = {}) reference(name, table_name, ) end |
#plural_references ⇒ Object
65 66 67 |
# File 'lib/groovy/schema.rb', line 65 def plural_references @cache[:plural_references] ||= get_names(table.columns.select(&:vector?)) end |
#rebuild! ⇒ Object
109 110 111 112 113 114 |
# File 'lib/groovy/schema.rb', line 109 def rebuild! log("Rebuilding!") # remove_table! if table remove_columns_not_in([]) sync end |
#reference(name, table_name = nil, options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/groovy/schema.rb', line 129 def reference(name, table_name = nil, = {}) table_name = Utils.pluralize_and_classify(name) if table_name.nil? unless context[table_name] log "Table #{table_name} doesn't exist yet! Creating now..." create_table!(table_name, nil) end refs_by_table[table_name] = name.to_sym refs_by_name[name.to_sym] = table_name @spec[name.to_sym] = { type: :reference, args: [table_name, ] } end |
#refs_by_name ⇒ Object
93 94 95 |
# File 'lib/groovy/schema.rb', line 93 def refs_by_name @refs_by_name ||= {} end |
#refs_by_table ⇒ Object
89 90 91 |
# File 'lib/groovy/schema.rb', line 89 def refs_by_table @refs_by_table ||= {} end |
#reload ⇒ Object
105 106 107 |
# File 'lib/groovy/schema.rb', line 105 def reload @cache = {} end |
#reverse_reference_of(ref_name) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/groovy/schema.rb', line 97 def reverse_reference_of(ref_name) if ref_table = refs_by_name[ref_name.to_sym] if foreign_model = Groovy.models[ref_table] foreign_model.schema.refs_by_table[@table_name] end end end |
#search_table ⇒ Object
def [](key)
# @spec[key.to_sym]
table.columns.select { |c| c.column? && c.name.to_s == "#{table_name}.#{key.to_s}" }
end
53 54 55 |
# File 'lib/groovy/schema.rb', line 53 def search_table @cache[:search_table] ||= context[SEARCH_TABLE_NAME] end |
#singular_references ⇒ Object
61 62 63 |
# File 'lib/groovy/schema.rb', line 61 def singular_references @cache[:singular_references] ||= get_names(table.columns.select(&:reference_column?).reject(&:vector?)) end |
#sync(db_context = nil) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/groovy/schema.rb', line 155 def sync(db_context = nil) switch_to_context(db_context) if db_context ensure_created! remove_columns_not_in(@spec.keys) @spec.each do |col, spec| check_and_add_column(col, spec[:type], spec[:args]) end @index_columns.each do |col| add_index_on(col) end reload self end |
#table ⇒ Object
40 41 42 |
# File 'lib/groovy/schema.rb', line 40 def table @table ||= context[table_name] end |
#table_type ⇒ Object
44 45 46 |
# File 'lib/groovy/schema.rb', line 44 def table_type (@opts[:type] || :array).to_sym end |
#time_columns ⇒ Object
73 74 75 |
# File 'lib/groovy/schema.rb', line 73 def time_columns @cache[:time_columns] ||= columns_by_type('Time') end |
#timestamps(opts = {}) ⇒ Object
150 151 152 153 |
# File 'lib/groovy/schema.rb', line 150 def (opts = {}) column(:created_at, 'time') column(:updated_at, 'time') end |