Module: Phoenix::Common::ClassMethods

Defined in:
lib/phoenix/common.rb

Instance Method Summary collapse

Instance Method Details

#allObject



85
86
87
88
# File 'lib/phoenix/common.rb', line 85

def all
  sql = "SELECT * FROM #{self.table_name}"
  Phoenix::Rjb.execute(sql)
end

#associate_table_name(klass) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/phoenix/common.rb', line 56

def associate_table_name(klass)
  case klass
  when "Base"
    "BASE"
  else
    klass.tableize.singularize
  end
end

#belongs_to(t) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/phoenix/common.rb', line 141

def belongs_to(t)
  self.class_eval do
    define_method t do
      key = "#{t.upcase}_ID"
      sql = %(SELECT * FROM #{t.upcase} WHERE APP_ID = '#{self.APP_ID}' AND #{key} = '#{self.send(key)}')
      digest = Digest::MD5.hexdigest(sql)
      Datacraft::Cache.get digest do
        arr = Phoenix::Rjb.execute(sql)
        arr.first
      end
    end
  end
end

#build(h) ⇒ Object



137
138
139
# File 'lib/phoenix/common.rb', line 137

def build(h)
  self.new(h)
end

#find(id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/phoenix/common.rb', line 65

def find(id)
  column = "#{self.to_s}_ID"
  unless self.is_int_column?(column)
    id = self.quote(id)
  end
  
  sql = "SELECT * FROM #{self.to_s} WHERE #{column} = #{id}"

  digest = Digest::MD5.hexdigest(sql)

  Datacraft::Cache.get digest do
    arr = Phoenix::Rjb.execute(sql)  
    arr.first
  end
end

#find_by_sql(sql) ⇒ Object



81
82
83
# File 'lib/phoenix/common.rb', line 81

def find_by_sql(sql)
  Phoenix::Rjb.execute(sql)
end

#firstObject



13
14
15
16
# File 'lib/phoenix/common.rb', line 13

def first
  sql = "select * from #{self.table_name} limit 1"
  Phoenix::Rjb.execute(sql)[0]
end

#has_many(*args) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/phoenix/common.rb', line 155

def has_many(*args)
  t = args[0].to_s
  options = args.extract_options!
  self.class_eval do
    define_method t do
      if options[:through]
        key = "#{self.class.to_s.upcase}_ID"
        assoite_sql = %(SELECT * FROM #{options[:through]} WHERE APP_ID = '#{self.APP_ID}' AND #{key} = '#{self.send(key)}')
        assoite_rs = Phoenix::Rjb.execute(assoite_sql)
        assoite_key = "#{t.singularize.upcase}_ID"
        ids = assoite_rs.collect { |ass| ass.send("#{assoite_key}") }
        if ids.present?
          sql = %(SELECT * FROM #{t.singularize} WHERE APP_ID = '#{self.APP_ID}' AND #{assoite_key} IN (#{ids.map { |_v| self.class.quote(_v) }.join(',')}))
          Phoenix::Rjb.execute(sql)
        else
          []
        end
      else
        # table = "#{t[0..-2]}"
        table = self.class.associate_table_name(t.singularize)
        key = "#{self.class.to_s.upcase}_ID"
        v = self.send(key)
        unless self.class.is_int_column?(key)
          v = self.class.quote(v)
        end
        sql = %(SELECT * FROM #{table} WHERE #{key} = #{v})
        Phoenix::Rjb.execute(sql)
      end
    end
  end
end

#in_cond(arr) ⇒ Object



29
30
31
# File 'lib/phoenix/common.rb', line 29

def in_cond(arr)
  arr.map { |_v| self.quote(_v) }.join(',')
end

#is_int_column?(column) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/phoenix/common.rb', line 33

def is_int_column?(column)
  meta = self.class_variable_get("@@meta_data")
  if meta.blank?
    table_name = self.table_name
    meta = Phoenix::Rjb.(table_name)
    self.class_variable_set("@@meta_data", meta)
  end
  type = meta.select { |ele| ele[:column_name] == column.to_s.upcase }.first[:column_type]
  if type =~ /int|long/i
    return true
  end
  return false
end

#quote(s) ⇒ Object



23
24
25
26
27
# File 'lib/phoenix/common.rb', line 23

def quote(s)
  return s if s.blank?
  return s if s.is_a? Integer
  "'#{s.gsub(/\\/, '\&\&').gsub(/'/, "''")}'"
end

#select(*fields) ⇒ Object



18
19
20
21
# File 'lib/phoenix/common.rb', line 18

def select(*fields)
  sql = "SELECT #{fields.join(', ')} FROM #{table_name}"
  Phoenix::Rjb.execute(sql)
end

#table_nameObject



47
48
49
50
51
52
53
54
# File 'lib/phoenix/common.rb', line 47

def table_name
  last_3_letter = self.to_s[-3..-1]
  if last_3_letter.downcase == 'new'
    return self.to_s.tableize.singularize.upcase[0..-2]
  else
    return self.to_s.tableize.singularize.upcase
  end
end

#where(*args) ⇒ Object

组装 sql Hash

Model.where(field1: value1, field2: value2)

String

Model.where('field1 = value1 AND field2 = value2')

Blank

Model.where() / Model.where('')


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/phoenix/common.rb', line 98

def where(*args)
  table_name = self.table_name
  if args.all? { |ele| ele.blank? }
    sql = %(SELECT * FROM #{table_name})
    return Relation.new(sql)
  end
  if args.size < 2 and args[0].is_a? String
    sql = %(SELECT * FROM #{table_name} WHERE #{args[0]})
    return Relation.new(sql)
  end
  options = args.extract_options!
  cond = ""
  options.each_with_index do |(k,v), index|
    if index > 0
      if v.is_a? Array
        cond << " AND #{k} IN " + " (#{v.map { |_v| self.quote(_v) }.join(',')})"
      else
        if self.is_int_column? k
          cond << " AND #{k} = #{v} "
        else
          cond << " AND #{k} = '#{v}' "
        end
      end
    else
      if v.is_a? Array
        cond << " #{k} IN " + " (#{v.map { |_v| self.quote(_v) }.join(',')})"
      else
        if self.is_int_column?(k)
          cond << " #{k} = #{v} "
        else
          cond << " #{k} = '#{v}' "
        end
      end
    end
  end
  sql = %(SELECT * FROM #{table_name} WHERE #{cond})
  Relation.new(sql)
end