rgviz-rails 0.8 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+ module Rgviz
2
+ class MySqlAdapter
3
+ def accept_scalar_function_column(node, visitor)
4
+ case node.function
5
+ when ScalarFunctionColumn::Year
6
+ visitor << "year("
7
+ node.arguments[0].accept visitor
8
+ visitor << ")"
9
+ when ScalarFunctionColumn::Month
10
+ visitor << "month("
11
+ node.arguments[0].accept visitor
12
+ visitor << ")"
13
+ when ScalarFunctionColumn::Day
14
+ visitor << "day("
15
+ node.arguments[0].accept visitor
16
+ visitor << ")"
17
+ when ScalarFunctionColumn::Hour
18
+ visitor << "hour("
19
+ node.arguments[0].accept visitor
20
+ visitor << ")"
21
+ when ScalarFunctionColumn::Minute
22
+ visitor << "minute("
23
+ node.arguments[0].accept visitor
24
+ visitor << ")"
25
+ when ScalarFunctionColumn::Second
26
+ visitor << "second("
27
+ node.arguments[0].accept visitor
28
+ visitor << ")"
29
+ when ScalarFunctionColumn::Millisecond
30
+ raise NotSupported.new "The millisecond function is not supported"
31
+ when ScalarFunctionColumn::Quarter
32
+ visitor << "quarter("
33
+ node.arguments[0].accept visitor
34
+ visitor << ")"
35
+ when ScalarFunctionColumn::DayOfWeek
36
+ visitor << "dayofweek("
37
+ node.arguments[0].accept visitor
38
+ visitor << ")"
39
+ when ScalarFunctionColumn::Now
40
+ visitor << "now()"
41
+ when ScalarFunctionColumn::DateDiff
42
+ visitor << "datediff("
43
+ node.arguments[0].accept visitor
44
+ visitor << ","
45
+ node.arguments[1].accept visitor
46
+ visitor << ")"
47
+ when ScalarFunctionColumn::ToDate
48
+ visitor << "date_format("
49
+ node.arguments[0].accept visitor
50
+ visitor << ", '%Y-%m-%d')"
51
+ when ScalarFunctionColumn::Upper
52
+ visitor << "upper("
53
+ node.arguments[0].accept visitor
54
+ visitor << ")"
55
+ when ScalarFunctionColumn::Lower
56
+ visitor << "lower("
57
+ node.arguments[0].accept visitor
58
+ visitor << ")"
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ module Rgviz
2
+ class SqliteAdapter
3
+ def accept_scalar_function_column(node, visitor)
4
+ case node.function
5
+ when ScalarFunctionColumn::Year
6
+ visitor << "strftime('%Y', "
7
+ node.arguments[0].accept visitor
8
+ visitor << ")"
9
+ when ScalarFunctionColumn::Month
10
+ visitor << "strftime('%m', "
11
+ node.arguments[0].accept visitor
12
+ visitor << ")"
13
+ when ScalarFunctionColumn::Day
14
+ visitor << "strftime('%d', "
15
+ node.arguments[0].accept visitor
16
+ visitor << ")"
17
+ when ScalarFunctionColumn::Hour
18
+ visitor << "strftime('%H', "
19
+ node.arguments[0].accept visitor
20
+ visitor << ")"
21
+ when ScalarFunctionColumn::Minute
22
+ visitor << "strftime('%M', "
23
+ node.arguments[0].accept visitor
24
+ visitor << ")"
25
+ when ScalarFunctionColumn::Second
26
+ visitor << "strftime('%S', "
27
+ node.arguments[0].accept visitor
28
+ visitor << ")"
29
+ when ScalarFunctionColumn::Millisecond
30
+ raise NotSupported.new "The millisecond function is not supported"
31
+ when ScalarFunctionColumn::Quarter
32
+ raise NotSupported.new "The quarter function is not supported"
33
+ when ScalarFunctionColumn::DayOfWeek
34
+ visitor << "1 + strftime('%w', "
35
+ node.arguments[0].accept visitor
36
+ visitor << ")"
37
+ when ScalarFunctionColumn::Now
38
+ visitor << "strftime('%Y-%m-%d %H:%M:%S')"
39
+ when ScalarFunctionColumn::DateDiff
40
+ visitor << "julianday("
41
+ node.arguments[0].accept visitor
42
+ visitor << ") - julianday("
43
+ node.arguments[1].accept visitor
44
+ visitor << ")"
45
+ when ScalarFunctionColumn::ToDate
46
+ visitor << "strftime('%Y-%m-%d', "
47
+ node.arguments[0].accept visitor
48
+ visitor << ")"
49
+ when ScalarFunctionColumn::Upper
50
+ visitor << "upper("
51
+ node.arguments[0].accept visitor
52
+ visitor << ")"
53
+ when ScalarFunctionColumn::Lower
54
+ visitor << "lower("
55
+ node.arguments[0].accept visitor
56
+ visitor << ")"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,11 +1,18 @@
1
1
  module Rgviz
2
2
  class Executor
3
3
  attr_reader :model_class
4
+ attr_reader :adapter
4
5
 
5
6
  def initialize(model_class, query)
6
7
  @model_class = model_class
7
8
  @query = query
8
9
  @joins = {}
10
+ case ActiveRecord::Base.connection.adapter_name.downcase
11
+ when 'sqlite'
12
+ @adapter = SqliteAdapter.new
13
+ when 'mysql'
14
+ @adapter = MySqlAdapter.new
15
+ end
9
16
  end
10
17
 
11
18
  def execute(options = {})
@@ -184,7 +191,11 @@ module Rgviz
184
191
  @executor = executor
185
192
  end
186
193
 
187
- def visitIdColumn(node)
194
+ def <<(string)
195
+ @string += string
196
+ end
197
+
198
+ def visit_id_column(node)
188
199
  klass, rails_col, joins = Rgviz::find_rails_col @executor.model_class, node.name
189
200
  raise "Unknown column '#{node.name}'" unless rails_col
190
201
  @string += ActiveRecord::Base.connection.quote_column_name(klass.table_name)
@@ -194,42 +205,44 @@ module Rgviz
194
205
  @executor.add_joins joins
195
206
  end
196
207
 
197
- def visitNumberColumn(node)
208
+ def visit_number_column(node)
198
209
  @string += node.value.to_s
199
210
  end
200
211
 
201
- def visitStringColumn(node)
212
+ def visit_string_column(node)
202
213
  @string += escaped_string(node.value)
203
214
  end
204
215
 
205
- def visitBooleanColumn(node)
216
+ def visit_boolean_column(node)
206
217
  @string += node.value ? '1' : '0'
207
218
  end
208
219
 
209
- def visitDateColumn(node)
220
+ def visit_date_column(node)
210
221
  @string += escaped_string(node.value.to_s)
211
222
  end
212
223
 
213
- def visitDateTimeColumn(node)
224
+ def visit_date_time_column(node)
214
225
  @string += escaped_string(node.value.strftime "%Y-%m-%d %H:%M:%S")
215
226
  end
216
227
 
217
- def visitTimeOfDayColumn(node)
228
+ def visit_time_of_day_column(node)
218
229
  @string += escaped_string(node.value.strftime "%H:%M:%S")
219
230
  end
220
231
 
221
- def visitScalarFunctionColumn(node)
232
+ def visit_scalar_function_column(node)
222
233
  case node.function
223
234
  when ScalarFunctionColumn::Sum, ScalarFunctionColumn::Difference,
224
235
  ScalarFunctionColumn::Product, ScalarFunctionColumn::Quotient
225
236
  node.arguments[0].accept self
226
237
  @string += node.function.to_s
227
238
  node.arguments[1].accept self
239
+ else
240
+ @string += @executor.adapter.accept_scalar_function_column(node, self)
228
241
  end
229
242
  false
230
243
  end
231
244
 
232
- def visitAggregateColumn(node)
245
+ def visit_aggregate_column(node)
233
246
  @string += node.function.to_s
234
247
  @string += '('
235
248
  node.argument.accept self
@@ -237,15 +250,15 @@ module Rgviz
237
250
  false
238
251
  end
239
252
 
240
- def visitLabel(node)
253
+ def visit_label(node)
241
254
  false
242
255
  end
243
256
 
244
- def visitFormat(node)
257
+ def visit_format(node)
245
258
  false
246
259
  end
247
260
 
248
- def visitOption(node)
261
+ def visit_option(node)
249
262
  false
250
263
  end
251
264
 
@@ -256,14 +269,14 @@ module Rgviz
256
269
  end
257
270
 
258
271
  class WhereVisitor < ColumnVisitor
259
- def visitBinaryExpression(node)
272
+ def visit_binary_expression(node)
260
273
  node.left.accept self
261
274
  @string += " #{node.operator} "
262
275
  node.right.accept self
263
276
  false
264
277
  end
265
278
 
266
- def visitUnaryExpression(node)
279
+ def visit_unary_expression(node)
267
280
  case node.operator
268
281
  when UnaryExpression::Not
269
282
  @string += 'not ('
@@ -281,7 +294,7 @@ module Rgviz
281
294
  end
282
295
 
283
296
  class OrderVisitor < ColumnVisitor
284
- def visitOrderBy(node)
297
+ def visit_order_by(node)
285
298
  i = 0
286
299
  node.sorts.each do |sort|
287
300
  @string += ',' if i > 0
@@ -290,7 +303,7 @@ module Rgviz
290
303
  false
291
304
  end
292
305
 
293
- def visitSort(node)
306
+ def visit_sort(node)
294
307
  node.column.accept self
295
308
  @string += node.order == Sort::Asc ? ' asc' : ' desc'
296
309
  false
@@ -316,4 +329,7 @@ module Rgviz
316
329
  joins << assoc
317
330
  end
318
331
  end
332
+
333
+ class NotSupported < ::Exception
334
+ end
319
335
  end
data/lib/rgviz_rails.rb CHANGED
@@ -1 +1,3 @@
1
1
  require 'rgviz_rails/executor.rb'
2
+ require 'rgviz_rails/adapters/mysql_adapter.rb'
3
+ require 'rgviz_rails/adapters/sqlite_adapter.rb'
@@ -230,4 +230,40 @@ describe Executor do
230
230
  table.rows[0].c[0].v.should == 2
231
231
  end
232
232
 
233
+ [['year', 2006], ['month', 5], ['day', 2],
234
+ ['hour', 3], ['minute', 4], ['second', 9],
235
+ ['quarter', 2], ['dayOfWeek', 3]].each do |str, val|
236
+ it_processes_single_select_column "#{str}(created_at)", 'c0', :number, val do
237
+ Person.make :created_at => Time.parse('2006-05-02 3:04:09')
238
+ end
239
+ end
240
+
241
+ it_processes_single_select_column "dateDiff(date '2008-03-13', date '2008-03-10')", 'c0', :number, 3 do
242
+ Person.make
243
+ end
244
+
245
+ it_processes_single_select_column "now()", 'c0', :datetime, Time.now.utc.strftime("%Y-%m-%d %H:%M:%S") do
246
+ Person.make :created_at => Time.parse('2006-05-02 3:04:09')
247
+ end
248
+
249
+ it_processes_single_select_column "toDate('2008-03-13')", 'c0', :date, Date.parse('2008-03-13').to_s do
250
+ Person.make
251
+ end
252
+
253
+ it_processes_single_select_column "toDate(created_at)", 'c0', :date, Date.parse('2008-03-13').to_s do
254
+ Person.make :created_at => Time.parse('2008-03-13 3:04:09')
255
+ end
256
+
257
+ it_processes_single_select_column "toDate(1234567890000)", 'c0', :date, Date.parse('2009-02-13').to_s do
258
+ Person.make
259
+ end
260
+
261
+ it_processes_single_select_column "upper(name)", 'c0', :string, 'FOO' do
262
+ Person.make :name => 'foo'
263
+ end
264
+
265
+ it_processes_single_select_column "lower(name)", 'c0', :string, 'foo' do
266
+ Person.make :name => 'FOO'
267
+ end
268
+
233
269
  end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,7 @@ require 'logger'
7
7
  require 'active_record'
8
8
 
9
9
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
10
+ #ActiveRecord::Base.establish_connection(:adapter => 'mysql', :database => 'rgviz_rails', :username => 'root', :password => 'root')
10
11
 
11
12
  ActiveRecord::Schema.define do
12
13
  create_table "cities", :force => true do |t|
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgviz-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 8
9
- version: "0.8"
8
+ - 9
9
+ version: "0.9"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ary Borenszweig
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-16 00:00:00 +07:00
17
+ date: 2010-08-17 00:00:00 +07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -29,6 +29,8 @@ extra_rdoc_files:
29
29
  files:
30
30
  - lib/rgviz_rails.rb
31
31
  - lib/rgviz_rails/executor.rb
32
+ - lib/rgviz_rails/adapters/mysql_adapter.rb
33
+ - lib/rgviz_rails/adapters/sqlite_adapter.rb
32
34
  - rails/init.rb
33
35
  - spec/blueprints.rb
34
36
  - spec/spec.opts