querybuilder 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,8 @@
1
+ == 1.1.3 2012-08-30
2
+
3
+ * Major enhancement
4
+ * Added support for types in select and custom queries "nodes select created_at as foo:time".
5
+
1
6
  == 1.1.2 2012-08-24
2
7
 
3
8
  * Minor enhancement
@@ -1,3 +1,3 @@
1
1
  module QueryBuilder
2
- VERSION = '1.1.2'
2
+ VERSION = '1.1.3'
3
3
  end
@@ -758,6 +758,10 @@ module QueryBuilder
758
758
  def prepare_custom_query_arguments(key, value)
759
759
  if key == :main_class
760
760
  resolve_main_class(value)
761
+ elsif key == :types
762
+ value.each do |k,v|
763
+ value[k] = v.to_sym
764
+ end
761
765
  elsif value.kind_of?(Array)
762
766
  value.map {|e| parse_custom_query_argument(key, e)}
763
767
  elsif value.kind_of?(Hash)
@@ -2,9 +2,10 @@ require 'active_record'
2
2
 
3
3
  module QueryBuilder
4
4
  class Query
5
+ SELECT_WITH_TYPE_REGEX = /^(.*):(.*)$/
5
6
  attr_accessor :processor_class, :distinct, :select, :tables, :table_alias, :where,
6
7
  :limit, :offset, :page_size, :order, :group, :error, :attributes_alias,
7
- :pagination_key, :main_class, :context, :key_value_tables, :having
8
+ :pagination_key, :main_class, :context, :key_value_tables, :having, :types
8
9
 
9
10
  class << self
10
11
  def adapter
@@ -19,6 +20,8 @@ module QueryBuilder
19
20
  @join_tables = {}
20
21
  @needed_join_tables = {}
21
22
  @attributes_alias = {}
23
+ # Custom select foo as bar:time or 'types:' field in custom query.
24
+ @types = {}
22
25
  @key_value_tables = {}
23
26
  @where = []
24
27
  end
@@ -56,10 +59,18 @@ module QueryBuilder
56
59
  @where << filter
57
60
  end
58
61
 
59
- # Return all explicit selected keys (currently selection is only available in custom queries)
62
+ # Return all explicit selected keys
60
63
  # For example, sql such as "SELECT form.*, MAX(form.date) AS last_date" would provice 'last_date' key.
61
64
  def select_keys
62
- @select_keys ||= @attributes_alias.keys.compact
65
+ @select_keys ||= begin
66
+ keys = @attributes_alias.keys.compact
67
+ # When rebuilding select_keys, we rebuild @types
68
+ keys.each do |k|
69
+ # Default type is string
70
+ @types[k] ||= :string
71
+ end
72
+ keys
73
+ end
63
74
  end
64
75
 
65
76
  # Convert query object to a string. This string should then be evaluated.
@@ -131,6 +142,10 @@ module QueryBuilder
131
142
  end
132
143
 
133
144
  def add_select(field, fname)
145
+ if fname =~ SELECT_WITH_TYPE_REGEX
146
+ fname, type = $1, $2
147
+ @types[fname] = type.to_sym
148
+ end
134
149
  @select ||= ["#{main_table}.*"]
135
150
  @select << "#{field} AS #{quote_column_name(fname)}"
136
151
  @attributes_alias[fname] = field
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{querybuilder}
8
- s.version = "1.1.2"
8
+ s.version = "1.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gaspard Bucher"]
12
- s.date = %q{2012-08-24}
12
+ s.date = %q{2012-08-30}
13
13
  s.description = %q{QueryBuilder is an interpreter for the "pseudo sql" language. This language
14
14
  can be used for two purposes:
15
15
 
@@ -19,6 +19,8 @@ DummyProcessor:
19
19
  - a
20
20
  - 34 AS number
21
21
  - c
22
+ types:
23
+ number: integer
22
24
  tables:
23
25
  - test
24
26
  where:
@@ -78,6 +78,10 @@ class DummyQueryBuilder < Test::Unit::TestCase
78
78
  should 'respond to select_keys' do
79
79
  assert_equal %w{a c number}, subject.select_keys.sort
80
80
  end
81
+
82
+ should 'return type of custom key' do
83
+ assert_equal :integer, subject.types['number']
84
+ end
81
85
 
82
86
  should 'not include star keys' do
83
87
  assert !subject.select_keys.include?('*')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: querybuilder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 2
10
- version: 1.1.2
9
+ - 3
10
+ version: 1.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gaspard Bucher
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-08-24 00:00:00 +02:00
18
+ date: 2012-08-30 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency