A collection of useful SQL utilities.
- blob
- build_join_name
- create_join_table_sql
- date
- escape
- join_class_ordering
- join_object_ordering
- join_table
- join_table_index
- join_table_info
- join_table_key
- join_table_keys
- ordered_join_table_keys
- parse_blob
- parse_boolean
- parse_date
- parse_float
- parse_int
- parse_timestamp
- quote
- quote_array
- quotea
- table
- tableize
- timestamp
[ show source ]
# File lib/og/store/sql/utils.rb, line 40
40: def blob(val)
41: val
42: end
[ show source ]
# 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
Subclasses can override this if they need a different syntax.
[ show source ]
# 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
Output YYY-mm-dd
[ show source ]
# 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 an SQL string
[ show source ]
# File lib/og/store/sql/utils.rb, line 11
11: def escape(str)
12: return nil unless str
13: return str.gsub(/'/, "''")
14: end
[ show source ]
# 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
[ show source ]
# 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
[ show source ]
# 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
[ show source ]
# File lib/og/store/sql/join.rb, line 37
37: def join_table_index(key)
38: "#{key}_idx"
39: end
[ show source ]
# 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
[ show source ]
# 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
[ show source ]
# 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
[ show source ]
# 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
[ show source ]
# File lib/og/store/sql/utils.rb, line 91
91: def parse_blob(val)
92: val
93: end
Parse a boolean true, 1, t => true other => false
[ show source ]
# 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
Input YYYY-mm-dd
[ show source ]
# 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 a float.
[ show source ]
# 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 an integer.
[ show source ]
# 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 sql datetime
[ show source ]
# 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
Escape the various Ruby types.
[ show source ]
# 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
Escape the Array Ruby type.
[ show source ]
# 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
Alias for #quote_array
Return the table name for the given class.
[ show source ]
# 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
Apply table name conventions to a class name.
[ show source ]
# File lib/og/store/sql/utils.rb, line 134
134: def tableize(klass)
135: "#{klass.to_s.gsub(/::/, "_").downcase}"
136: end
Convert a ruby time to an sql timestamp.
[ show source ]
# 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