Class: KSExpression

Inherits:
Object show all
Includes:
DRbUndumped
Defined in:
lib/kansas/Expression.rb

Defined Under Namespace

Classes: Context, KSBetweenFunction, KSBinaryOperator, KSFunctionOperator, KSOperator, KSUnaryFunction, KSUnaryOperator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.binary_function(name, keyword, op = nil, class_to_use = KSFunctionOperator) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/kansas/Expression.rb', line 109

def KSExpression.binary_function(name, keyword, op = nil, class_to_use = KSFunctionOperator)
  opClass = Class.new(class_to_use)
  opClass.setKeyword(keyword)
  const_set(name,opClass)
  
  class_eval "define_method(:\"\#{name}\") {|*val| \#{name}.new(self,val,@context) }\n"
  alias_method op, name if op
end

.operator(name, keyword, op = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kansas/Expression.rb', line 97

def KSExpression.operator(name, keyword, op=nil)
  opClass = Class.new(KSBinaryOperator)
  opClass.setKeyword(keyword)
  const_set(name, opClass)

  fn = op ? op : name
  class_eval "define_method(:\"\#{fn}\") {|val| \#{name}.new(self, val, @context) }\n"
  alias_method name, op if op
end

.unary_function(name, keyword, op = nil, class_to_use = KSUnaryFunction) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/kansas/Expression.rb', line 120

def KSExpression.unary_function(name, keyword, op = nil, class_to_use = KSUnaryFunction)
  opClass = Class.new(KSUnaryFunction)
  opClass.setKeyword(keyword)
  const_set(name,opClass)
  
  class_eval "define_method(:\"\#{name}\") {|*val| \#{name}.new(val, @context) }\n"
  alias_method op, name if op
end

.unary_operator(name, keyword, op = nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/kansas/Expression.rb', line 131

def KSExpression.unary_operator(name, keyword, op=nil)
  opClass = Class.new(KSUnaryOperator)
  opClass.setKeyword(keyword)
  const_set(name, opClass)
   
  class_eval "define_method(:\"\#{name}\") { \#{name}.new(self, @context) }\n"
   
  alias_method op, name if op
end

Instance Method Details

#count_sqlObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kansas/Expression.rb', line 69

def count_sql
  selectedTables = @context.tables.compact.flatten.uniq.join(',')
  joinConstraints = @context.joins.compact.flatten.uniq.join(' AND ')
  #selected_rows = @context.select.compact.flatten.uniq.collect {|t| "#{t}.*"}.join(',')
  
  if joinConstraints != ""
    joinConstraints << " AND "
  end
   
  statement = "SELECT count(*) FROM #{selectedTables} WHERE #{joinConstraints} #{expr_body}"
  statement << ' ORDER BY ' << @context.sort_fields.collect {|f| "#{f[0].respond_to?(:expr_body) ? f[0].expr_body : f[0].to_s} #{f[1]}"}.join(',') if @context.sort_fields.length > 0
  statement << ' LIMIT ' << @context.limits.join(',') if @context.limits.length > 0

  statement
end

#delete_sqlObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kansas/Expression.rb', line 85

def delete_sql
  selectedTables = @context.tables.compact.flatten.uniq.join(",")
  joinConstraints = @context.joins.compact.flatten.uniq.join(" AND ")
  if joinConstraints != ""
    joinConstraints << " AND "
  end
   
  statement = "DELETE FROM #{selectedTables} WHERE #{joinConstraints} #{expr_body}"
  
  statement
end

#select_sqlObject Also known as: sql



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kansas/Expression.rb', line 38

def select_sql
  distinct_fields = []
  fields = []
  @context.select_table.fields.each_value do |f|
    if @context.distinct[f]
      distinct_fields.push "distinct(#{@context.select}.#{f})"
    else
      fields.push "#{@context.select}.#{f}"
    end
  end
  
  fields = distinct_fields.concat(fields)
  
  selectedTables = @context.tables.compact.flatten.uniq.join(',')
  joinConstraints = @context.joins.compact.flatten.uniq.join(' AND ')
  #selected_rows = @context.select.compact.flatten.uniq.collect {|t| "#{t}.*"}.join(',')
  
  if joinConstraints != ""
    joinConstraints << " AND "
  end
   
  #statement = "SELECT #{@context.select}.* FROM #{selectedTables} WHERE #{joinConstraints} #{expr_body}"
  statement = "SELECT #{fields.join(',')} FROM #{selectedTables} WHERE #{joinConstraints} #{expr_body}"
  statement << ' ORDER BY ' << @context.sort_fields.collect {|f| "#{f[0].respond_to?(:expr_body) ? f[0].expr_body : f[0].to_s} #{f[1]}"}.join(',') if @context.sort_fields.length > 0
  statement << ' LIMIT ' << @context.limits.join(',') if @context.limits.length > 0
  
  statement
end