A collection of useful SQL utilities.

Methods
Public Instance methods
blob(val)
    # File lib/og/store/sql/utils.rb, line 40
40:   def blob(val)
41:     val
42:   end
build_join_name(class1, class2, postfix = nil)
    # File lib/og/store/sql/join.rb, line 26
26:   def build_join_name(class1, class2, postfix = nil)
27:     # Don't reorder arguments, as this is used in places that
28:     # have already determined the order they want.
29:     "#{Og.table_prefix}j_#{tableize(class1)}_#{tableize(class2)}#{postfix}"
30:   end
create_join_table_sql(join_table_info, suffix = 'NOT NULL', key_type = 'integer')

Subclasses can override this if they need a different syntax.

     # File lib/og/store/sql/join.rb, line 104
104:   def create_join_table_sql(join_table_info, suffix = 'NOT NULL', key_type = 'integer')
105:     join_table = join_table_info[:table]
106:     first_index = join_table_info[:first_index]
107:     first_key = join_table_info[:first_key]
108:     second_key = join_table_info[:second_key]
109:     second_index = join_table_info[:second_index]
110: 
111:     sql = []
112: 
113:     sql << %{      
114:       CREATE TABLE #{join_table} (
115:         #{first_key} integer NOT NULL,
116:         #{second_key} integer NOT NULL,
117:         PRIMARY KEY(#{first_key}, #{second_key})
118:       )
119:     }
120: 
121:     # gmosx: not that useful?
122:     # sql << "CREATE INDEX #{first_index} ON #{join_table} (#{first_key})"
123:     # sql << "CREATE INDEX #{second_index} ON #{join_table} (#{second_key})"
124: 
125:     return sql
126:   end
date(date)

Output YYY-mm-dd

    # File lib/og/store/sql/utils.rb, line 31
31:   def date(date)
32:     return nil unless date
33:     return "#{date.year}-#{date.month}-#{date.mday}" 
34:   end
escape(str)

Escape an SQL string

    # File lib/og/store/sql/utils.rb, line 11
11:   def escape(str)
12:     return nil unless str
13:     return str.gsub(/'/, "''")
14:   end
join_class_ordering(class1, class2)
    # File lib/og/store/sql/join.rb, line 18
18:   def join_class_ordering(class1, class2)
19:     if class1.to_s <= class2.to_s
20:       return class1, class2
21:     else
22:       return class2, class1, true
23:     end
24:   end
join_object_ordering(obj1, obj2)
    # File lib/og/store/sql/join.rb, line 10
10:   def join_object_ordering(obj1, obj2)
11:     if obj1.class.to_s <= obj2.class.to_s
12:       return obj1, obj2
13:     else
14:       return obj2, obj1, true
15:     end
16:   end
join_table(class1, class2, postfix = nil)
    # File lib/og/store/sql/join.rb, line 32
32:   def join_table(class1, class2, postfix = nil)
33:     first, second = join_class_ordering(class1, class2)
34:     build_join_name(first, second, postfix)
35:   end
join_table_index(key)
    # File lib/og/store/sql/join.rb, line 37
37:   def join_table_index(key)
38:     "#{key}_idx"
39:   end
join_table_info(relation, postfix = nil)
    # File lib/og/store/sql/join.rb, line 60
60:   def join_table_info(relation, postfix = nil)
61: 
62:     # some fixes for schema inheritance.
63: 
64:     owner_class, target_class = relation.owner_class, relation.target_class
65:     
66:     raise "Undefined owner_class in #{target_class}" unless owner_class
67:     raise "Undefined target_class in #{owner_class}" unless target_class
68:     
69:     owner_class = owner_class.schema_inheritance_root_class if owner_class.schema_inheritance_child?
70:     target_class = target_class.schema_inheritance_root_class if target_class.schema_inheritance_child?
71: 
72:     owner_key, target_key = join_table_keys(owner_class, target_class)
73:     first, second, changed = join_class_ordering(owner_class, target_class)
74: 
75:     if changed
76:       first_key, second_key = target_key, owner_key
77:     else
78:       first_key, second_key = owner_key, target_key
79:     end
80: 
81:     table = (relation.table ?
82:       relation.table :
83:       join_table(owner_class, target_class, postfix)
84:     )
85:     
86:     return {
87:       :table => table,
88:       :owner_key => owner_key,
89:       :owner_table => table(owner_class),
90:       :target_key => target_key,
91:       :target_table => table(target_class),
92:       :first_table => table(first),
93:       :first_key => first_key,
94:       :first_index => join_table_index(first_key),
95:       :second_table => table(second),
96:       :second_key => second_key,
97:       :second_index => join_table_index(second_key)
98:     }
99:   end
join_table_key(klass)
    # File lib/og/store/sql/join.rb, line 41
41:   def join_table_key(klass)
42:     klass = klass.schema_inheritance_root_class if klass.schema_inheritance_child?
43:     "#{klass.to_s.demodulize.underscore.downcase}_oid"
44:   end
join_table_keys(class1, class2)
    # File lib/og/store/sql/join.rb, line 46
46:   def join_table_keys(class1, class2)
47:     if class1 == class2
48:       # Fix for the self-join case.
49:       return join_table_key(class1), "#{join_table_key(class2)}2"
50:     else
51:       return join_table_key(class1), join_table_key(class2)
52:     end
53:   end
ordered_join_table_keys(class1, class2)
    # File lib/og/store/sql/join.rb, line 55
55:   def ordered_join_table_keys(class1, class2)
56:     first, second = join_class_ordering(class1, class2)
57:     return join_table_keys(first, second)
58:   end
parse_blob(val)
    # File lib/og/store/sql/utils.rb, line 91
91:   def parse_blob(val)
92:     val
93:   end
parse_boolean(str)

Parse a boolean true, 1, t => true other => false

    # File lib/og/store/sql/utils.rb, line 82
82:   def parse_boolean(str)
83:     return true if (str=='true' || str=='t' || str=='1')
84:     return false
85:   end
parse_date(str)

Input YYYY-mm-dd

    # File lib/og/store/sql/utils.rb, line 73
73:   def parse_date(str)
74:     return nil unless str
75:     return Date.strptime(str)
76:   end
parse_float(fl)

Parse a float.

    # File lib/og/store/sql/utils.rb, line 53
53:   def parse_float(fl)
54:     fl = fl.to_f if fl
55:     fl
56:   end
parse_int(int)

Parse an integer.

    # File lib/og/store/sql/utils.rb, line 46
46:   def parse_int(int)
47:     int = int.to_i if int
48:     return int
49:   end
parse_timestamp(str)

Parse sql datetime

    # File lib/og/store/sql/utils.rb, line 63
63:   def parse_timestamp(str)
64:     return nil unless str
65:     return Time.parse(str)    
66:   end
quote(vals)

Escape the various Ruby types.

     # File lib/og/store/sql/utils.rb, line 97
 97:   def quote(vals)
 98:     vals = [vals] unless vals.is_a?(Array)
 99:     quoted = vals.inject('') do |s,val|
100:       s += case val
101:         when Fixnum, Integer, Float
102:           val ? val.to_s : 'NULL'
103:         when String
104:           val ? "'#{escape(val)}'" : 'NULL'
105:         when Time
106:           val ? "'#{timestamp(val)}'" : 'NULL'
107:         when Date
108:           val ? "'#{date(val)}'" : 'NULL'
109:         when TrueClass, FalseClass
110:           val ? "'t'" : 'NULL'
111:         else
112:           # gmosx: keep the '' for nil symbols.
113:           val ? escape(val.to_yaml) : ''
114:       end + ','
115:     end
116:     quoted.chop!
117:     vals.size > 1 ? "(#{quoted})" : quoted
118:   end
quote_array(val)

Escape the Array Ruby type.

This method is also aliased as quotea
     # File lib/og/store/sql/utils.rb, line 122
122:   def quote_array(val)
123:     case val
124:       when Array
125:         val.collect{ |v| quotea(v) }.join(',')
126:       else
127:         quote(val)
128:     end
129:   end
quotea(val)

Alias for #quote_array

table(klass)

Return the table name for the given class.

     # File lib/og/store/sql/utils.rb, line 140
140:   def table(klass)
141:     klass.ann(:self, :sql_table) || klass.ann(:self, :table) || "#{Og.table_prefix}#{tableize(klass)}"
142:   end
tableize(klass)

Apply table name conventions to a class name.

     # File lib/og/store/sql/utils.rb, line 134
134:   def tableize(klass)
135:     "#{klass.to_s.gsub(/::/, "_").downcase}"
136:   end
timestamp(time = Time.now)

Convert a ruby time to an sql timestamp.

    # File lib/og/store/sql/utils.rb, line 21
21:   def timestamp(time = Time.now)
22:     return nil unless time
23:     return time.strftime("%Y-%m-%d %H:%M:%S")
24:   end