smql 0.0.4.3 → 0.0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4.3
1
+ 0.0.4.4
@@ -107,9 +107,8 @@ class SmqlToAR
107
107
 
108
108
  # Model der Relation `rel` von `model`
109
109
  def self.model_of model, rel
110
- rel = rel.to_sym
111
- r = model.reflections[ rel].andand.klass
112
- r.nil? && :self == rel ? model : r
110
+ r = model.reflections[ rel.to_sym].andand.klass
111
+ r.nil? && rel === :self ? model : r
113
112
  end
114
113
 
115
114
  # Eine Spalte in einer Tabelle, relativ zu `Column#model`.
@@ -120,10 +119,43 @@ class SmqlToAR
120
119
  attr_reader :path, :col
121
120
  attr_accessor :model
122
121
 
122
+ class Col
123
+ attr_accessor :col, :as
124
+ def initialize col, as = nil
125
+ if col.kind_of? Col
126
+ @col, @as = col.col, col.as
127
+ elsif /^(.*?)\[(.*)\]$/ =~ col.to_s
128
+ @col, @as = $1, $2
129
+ else
130
+ @col, @as = col.to_s, as.to_s
131
+ @as = nil if @as.blank?
132
+ end
133
+ end
134
+
135
+ def to_s() @col end
136
+ def to_sym() to_s.to_sym end
137
+ def inspect() "#{@col}[@#{@as}]" end
138
+ def to_alias() "#{@col}[#{@as}]" end
139
+
140
+ def == other
141
+ other = Col.new other
142
+ @col = other.col && @as == other.col
143
+ end
144
+
145
+ def === other
146
+ other = Col.new other
147
+ @col == other.col
148
+ end
149
+ end
150
+
123
151
  def initialize model, *col
124
152
  @model = model
125
153
  @last_model = nil
126
- *@path, @col = *Array.wrap( col).collect( &it.to_s.split( /[.\/]/)).flatten.collect( &:to_sym).reject( &it==:self)
154
+ *@path, @col = *Array.wrap( col).
155
+ collect( &it.to_s.split( /[.\/]/)).
156
+ flatten.
157
+ collect( &Col.method( :new)).
158
+ reject( &it===:self)
127
159
  end
128
160
 
129
161
  def last_model
@@ -133,8 +165,7 @@ class SmqlToAR
133
165
  def each
134
166
  model = @model
135
167
  @path.each do |rel|
136
- rel = rel.to_sym
137
- unless :self == rel
168
+ unless rel === :self
138
169
  model = SmqlToAR.model_of model, rel
139
170
  return false unless model
140
171
  yield rel, model
@@ -169,6 +200,7 @@ class SmqlToAR
169
200
  exe.call pp, model
170
201
  end
171
202
  end
203
+
172
204
  def self?() !@col end
173
205
  def length() @path.length+(self.self? ? 0 : 1) end
174
206
  def size() @path.size+(self.self? ? 0 : 1) end
@@ -56,7 +56,7 @@ class SmqlToAR
56
56
  r = nil
57
57
  #p :try_parse => { :model => model, :colop => colop, :value => val }
58
58
  conditions.each do |c|
59
- raise_unless colop =~ /^(?:\d*:)?(.*?)(\W*)$/, UnexpectedColOpError.new( model, colop, val)
59
+ raise_unless colop =~ /^(?:\d*:)?(.*?)((?:\W*(?!\])\W)?)$/, UnexpectedColOpError.new( model, colop, val)
60
60
  col, op = $1, $2
61
61
  col = split_keys( col).collect {|c| Column.new model, c }
62
62
  r = c.try_parse model, col, op, val
@@ -248,7 +248,7 @@ class SmqlToAR
248
248
  col.joins.each {|j, m| builder.joins table+j, m }
249
249
  builder.joins t, model
250
250
  b2 = 1 == sub.length ? builder : Or.new( builder)
251
- sub.each {|i| i.collect( &it.build( And.new( b2), t)); p 'or' => b2 }
251
+ sub.each {|i| i.collect( &it.build( And.new( b2), t)) }
252
252
  end
253
253
  self
254
254
  end
@@ -28,20 +28,27 @@ class SmqlToAR
28
28
  def to_i() @vid end
29
29
  end
30
30
 
31
+ class Aliases < Hash
32
+ def self.new prefix, *a, &e
33
+ e ||= lambda do |h, k|
34
+ j = Array.wrap( k).compact
35
+ h[k] = h.key?(j) ? h[j] : "#{prefix},#{j.collect( &:to_alias).join( ',')}"
36
+ end
37
+ super *a, &e
38
+ end
39
+ end
40
+
31
41
  attr_reader :table_alias, :model, :table_model, :base_table, :_where, :_select, :_wobs, :_joins, :prefix, :_vid
32
42
  attr_accessor :logger, :limit, :offset
33
43
 
34
44
  def initialize model, prefix = nil
35
45
  @prefix = "smql"
36
46
  @logger = SmqlToAR.logger
37
- @table_alias = Hash.new do |h, k|
38
- j = Array.wrap( k).compact
39
- h[k] = h.key?(j) ? h[j] : "#{@prefix},#{j.join(',')}"
40
- end
47
+ @table_alias = Aliases.new @prefix
41
48
  @_vid, @_where, @_wobs, @model, @quoter = 0, SmqlToAR::And[], {}, model, model.connection
42
- @base_table = [model.table_name.to_sym]
49
+ @base_table = [Column::Col.new( model.table_name)]
43
50
  @table_alias[ @base_table] = @base_table.first
44
- t = quote_table_name @table_alias[ @base_table]
51
+ t = quote_table_name @base_table.first.col
45
52
  @_select, @_joins, @_joined, @_includes, @_order = ["DISTINCT #{t}.*"], "", [@base_table], [], []
46
53
  @table_model = {@base_table => @model}
47
54
  end
@@ -49,7 +56,7 @@ class SmqlToAR
49
56
  def vid() Vid.new( @_vid+=1) end
50
57
 
51
58
  def inspect
52
- "#<#{self.class.name}:#{"0x%x"% (self.object_id<<1)}|#{@prefix}:#{@base_table}:#{@model} vid=#{@_vid} where=#{@_where} wobs=#{@_wobs} select=#{@_select} aliases=#{@_table_alias}>"
59
+ "#<#{self.class.name}:#{"0x%x"% (self.object_id<<1)}|#{@prefix}:#{@base_table}:#{@model} vid=#{@_vid} where=#{@_where} wobs=#{@_wobs} select=#{@_select} aliases=#{@table_alias}>"
53
60
  end
54
61
 
55
62
  # Jede via where uebergebene Condition wird geodert und alle zusammen werden geundet.
@@ -71,11 +78,15 @@ class SmqlToAR
71
78
  end
72
79
 
73
80
  def quote_table_name name
81
+ name = case name
82
+ when Array, Column::Col then @table_alias[Array.wrap name]
83
+ else name.to_s
84
+ end
74
85
  @quoter.quote_table_name( name).gsub /"\."/, ','
75
86
  end
76
87
 
77
88
  def column table, name
78
- "#{quote_table_name table.kind_of?(String) ? table : @table_alias[table]}.#{quote_column_name name}"
89
+ "#{quote_table_name table}.#{quote_column_name name}"
79
90
  end
80
91
 
81
92
  def build_join orig, pretable, table, prekey, key
@@ -92,13 +103,13 @@ class SmqlToAR
92
103
  @table_model[ table] = model
93
104
  premodel = @table_model[ pretable]
94
105
  t = @table_alias[ table]
95
- pt = quote_table_name @table_alias[ table[ 0...-1]]
96
- refl = premodel.reflections[table.last]
106
+ pt = quote_table_name table[ 0...-1]
107
+ refl = premodel.reflections[table.last.to_sym]
97
108
  case refl
98
109
  when ActiveRecord::Reflection::ThroughReflection
99
110
  through = refl.through_reflection
100
- throughtable = table[0...-1]+[through.name.to_sym]
101
- srctable = throughtable+[refl.source_reflection.name]
111
+ throughtable = table[0...-1]+[Column::Col.new( through.name, table.last.as)]
112
+ srctable = throughtable+[Column::Col.new( refl.source_reflection.name, table.last.as)]
102
113
  @table_model[ srctable] = model
103
114
  @table_alias[ table] = @table_alias[ srctable]
104
115
  join_ throughtable, through.klass, quote_table_name( through.table_name)
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 0
8
8
  - 4
9
- - 3
10
- version: 0.0.4.3
9
+ - 4
10
+ version: 0.0.4.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Denis Knauf
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-27 00:00:00 +02:00
18
+ date: 2011-11-22 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency