Class: Jabara::MySQL::Schema::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/jabara/mysql/schema.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



129
130
131
# File 'lib/jabara/mysql/schema.rb', line 129

def initialize
  @schema = Schema.new
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



127
128
129
# File 'lib/jabara/mysql/schema.rb', line 127

def schema
  @schema
end

Class Method Details

.build(&block) ⇒ Object



133
134
135
136
137
# File 'lib/jabara/mysql/schema.rb', line 133

def self.build(&block)
  this = self.new
  this.instance_eval(&block)
  return this.schema
end

.build_from_sql(query_str) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/jabara/mysql/schema.rb', line 139

def self.build_from_sql(query_str)
  parser = Scheman::Parsers::Mysql.new

  create_stmt_hashs = parser.parse(query_str)
    .to_hash
    .select {|stmt| not stmt[:create_table].nil? }
    .map{|stmt| stmt[:create_table] }

  create_stmt_hashs.map { |create_stmt_hash|
    schema = stmt_to_schema(create_stmt_hash)
    [schema.table_name, schema]
  }.to_h
end

Instance Method Details

#booleanObject



167
168
169
# File 'lib/jabara/mysql/schema.rb', line 167

def boolean
  Boolean.new
end

#char(max, char_set: nil) ⇒ Object



179
180
181
182
# File 'lib/jabara/mysql/schema.rb', line 179

def char(max, char_set:nil)
  # TODO char_set
  Char.new max: max
end

#column(key, type, *args) ⇒ Object



159
160
161
# File 'lib/jabara/mysql/schema.rb', line 159

def column(key, type, *args)
  @schema.columns.push({key: key, type: type, constraints: args})
end

#datetimeObject



184
185
186
# File 'lib/jabara/mysql/schema.rb', line 184

def datetime
  DateTime.new
end

#floatObject



188
189
190
# File 'lib/jabara/mysql/schema.rb', line 188

def float
  Float.new
end

#integerObject



171
172
173
# File 'lib/jabara/mysql/schema.rb', line 171

def integer
  Integer.new
end

#not_nullObject



163
164
165
# File 'lib/jabara/mysql/schema.rb', line 163

def not_null
  :not_null
end

#table_name(table_name) ⇒ Object

以下DSLメソッド。buildに渡すblock内で使う。



155
156
157
# File 'lib/jabara/mysql/schema.rb', line 155

def table_name(table_name)
  @schema.table_name = table_name
end

#varchar(max) ⇒ Object



175
176
177
# File 'lib/jabara/mysql/schema.rb', line 175

def varchar(max)
  Char.new max: max
end