jabara 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c822f3a515941c87b02d841f72f0558f9e862820
4
- data.tar.gz: 801383a2e6e540d54dccb8a77b6aa3641dd5ef39
3
+ metadata.gz: 921c5db1c328803a7238637ddc12ef2b01dea0a8
4
+ data.tar.gz: 1c0a3f478163eb62a71f9cc2ad6b15fc4e0eef84
5
5
  SHA512:
6
- metadata.gz: 0f6b2425f51ef4b67740efa08303a8826226999fecba4f92bcc44597fdcf5bff28d40433a5fdf3b0d17de9f04626b5c3872fb9d19d5d1e29736b147891dc5912
7
- data.tar.gz: b8217aa3f3eaedd5ef05855d2d7f73b2e69ed08d49a83bcf66ed8ba664c2c93a37d13fb93a83ab98578aac35cba4f40b58cce6c65766ee64b7330ef1363dfb30
6
+ metadata.gz: 2d2b8007f5d9ecf6ddd5fce7c1ce8190bc9245a8f373e725afb8cbc4a803912554ea3747cecd254cac4d2a453168d5bf0a7b33c96b0eab9a6000cf7a436972ff
7
+ data.tar.gz: 5bfc494d5b8fbf10fc94039260f5d736d847da0acaada34ce867de42a820670e2e843ce71ead59c4f22f49447e315801eac97cee1a73a287d2c67a596e9c6591
@@ -98,6 +98,20 @@ module Jabara
98
98
  return ::Jabara.data(repr).is_a? ::DateTime
99
99
  end
100
100
  end
101
+
102
+ class Float
103
+ def self.tag
104
+ :float
105
+ end
106
+
107
+ def parse(parse)
108
+ Float(data) # raises ArgumentError, TypeError
109
+ end
110
+
111
+ def validate(repr)
112
+ return ::Jabara.data(repr).is_a? ::Float
113
+ end
114
+ end
101
115
 
102
116
  class Schema
103
117
 
@@ -122,41 +136,18 @@ module Jabara
122
136
  return this.schema
123
137
  end
124
138
 
125
- def self.build_from_create_stmt(crete_table_str)
139
+ def self.build_from_sql(query_str)
126
140
  parser = Scheman::Parsers::Mysql.new
127
- create_stmt_hash = parser.parse(crete_table_str).to_hash[0][:create_table]
128
141
 
129
- this = Builder.new
130
- this.table_name create_stmt_hash[:name]
131
- create_stmt_hash[:fields].each do |field_hash|
132
- field_hash = field_hash[:field]
133
- type = case field_hash[:type]
134
- when "char", "varchar"
135
- max = field_hash[:values][0]
136
- if max.nil? then Char.new else Char.new max: max end
137
- when "text"
138
- Text.new
139
- when "integer"
140
- max = field_hash[:values][0]
141
- if max.nil? then Integer.new else Integer.new max: max end
142
- when "boolean"
143
- Boolean.new
144
- when "double"
145
- Double.new
146
- when "datetime"
147
- DateTime.new
148
- else
149
- raise ArgumentError, 'invalid type'
150
- end
151
-
152
- if field_hash[:qualifiers].include?({qualifier: {type: "not_null"}}) then
153
- this.column field_hash[:name], type, :not_null
154
- else
155
- this.column field_hash[:name], type
156
- end
157
- end
142
+ create_stmt_hashs = parser.parse(query_str)
143
+ .to_hash
144
+ .select {|stmt| not stmt[:create_table].nil? }
145
+ .map{|stmt| stmt[:create_table] }
158
146
 
159
- return this.schema
147
+ create_stmt_hashs.map { |create_stmt_hash|
148
+ schema = stmt_to_schema(create_stmt_hash)
149
+ [schema.table_name, schema]
150
+ }.to_h
160
151
  end
161
152
 
162
153
  # 以下DSLメソッド。buildに渡すblock内で使う。
@@ -193,6 +184,47 @@ module Jabara
193
184
  def datetime
194
185
  DateTime.new
195
186
  end
187
+
188
+ def float
189
+ Float.new
190
+ end
191
+
192
+ private
193
+
194
+ def self.stmt_to_schema(create_stmt_hash)
195
+ this = Builder.new
196
+ this.schema.table_name = create_stmt_hash[:name]
197
+ create_stmt_hash[:fields].each do |field_hash|
198
+ field_hash = field_hash[:field]
199
+ type = case field_hash[:type]
200
+ when "char", "varchar"
201
+ max = field_hash[:values][0]
202
+ if max.nil? then Char.new else Char.new max: max end
203
+ when "text"
204
+ Text.new
205
+ when "integer"
206
+ max = field_hash[:values][0]
207
+ if max.nil? then Integer.new else Integer.new max: max end
208
+ when "boolean"
209
+ Boolean.new
210
+ when "double", "float"
211
+ Float.new
212
+ when "datetime"
213
+ DateTime.new
214
+ else
215
+ raise ArgumentError, 'invalid type'
216
+ end
217
+
218
+ if field_hash[:qualifiers].include?({qualifier: {type: "not_null"}}) then
219
+ this.column field_hash[:name], type, :not_null
220
+ else
221
+ this.column field_hash[:name], type
222
+ end
223
+ end
224
+
225
+ this.schema
226
+ end
227
+
196
228
  end
197
229
 
198
230
  end
@@ -1,3 +1,3 @@
1
1
  module Jabara
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jabara
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Takeichi