Module: Og::SqlUtils

Included in:
MysqlUtils, PsqlUtils, SqlStore, SqlStore, SqlserverUtils
Defined in:
lib/og/store/sql.rb

Instance Method Summary collapse

Instance Method Details

#date(date) ⇒ Object

Output YYY-mm-dd – TODO: Optimize this. ++



29
30
31
32
# File 'lib/og/store/sql.rb', line 29

def date(date)
	return nil unless date
	return "#{date.year}-#{date.month}-#{date.mday}" 
end

#escape(str) ⇒ Object

Escape an SQL string



9
10
11
12
# File 'lib/og/store/sql.rb', line 9

def escape(str)
	return nil unless str
	return str.gsub(/'/, "''")
end

#join_table(class1, class2, postfix = nil) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/og/store/sql.rb', line 90

def join_table(class1, class2, postfix = nil)
	if class1.to_s < class2.to_s
		return "j#{table(class1)}#{table(class2)}#{postfix}", 1, 2
	else
		return "j#{table(class2)}#{table(class1)}#{postfix}", 2, 1
	end
end

#parse_date(str) ⇒ Object

Input YYYY-mm-dd – TODO: Optimize this. ++



63
64
65
66
# File 'lib/og/store/sql.rb', line 63

def parse_date(str)
	return nil unless str
	return Date.strptime(str)
end

#parse_float(fl) ⇒ Object

Parse a float.



43
44
45
46
# File 'lib/og/store/sql.rb', line 43

def parse_float(fl)
	fl = fl.to_f if fl
	fl
end

#parse_int(int) ⇒ Object

Parse an integer.



36
37
38
39
# File 'lib/og/store/sql.rb', line 36

def parse_int(int)
	int = int.to_i if int
	int
end

#parse_timestamp(str) ⇒ Object

Parse sql datetime – TODO: Optimize this. ++



53
54
55
56
# File 'lib/og/store/sql.rb', line 53

def parse_timestamp(str)
	return nil unless str
	return Time.parse(str)		
end

#quote(val) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/og/store/sql.rb', line 68

def quote(val)
	case val
		when Fixnum, Integer, Float
			val ? val.to_s : 'NULL'
		when String
			val ? "'#{escape(val)}'" : 'NULL'
		when Time
			val ? "'#{timestamp(val)}'" : 'NULL'
		when Date
			val ? "'#{date(val)}'" : 'NULL'
		when TrueClass
			val ? "'t'" : 'NULL'
		else 
			# gmosx: keep the '' for nil symbols.
			val ? escape(val.to_yaml) : ''
	end 
end

#table(klass) ⇒ Object



86
87
88
# File 'lib/og/store/sql.rb', line 86

def table(klass)
	"#{Og.table_prefix}#{klass.to_s.gsub(/::/, "_").downcase}"
end

#timestamp(time = Time.now) ⇒ Object

Convert a ruby time to an sql timestamp. – TODO: Optimize this ++



19
20
21
22
# File 'lib/og/store/sql.rb', line 19

def timestamp(time = Time.now)
	return nil unless time
	return time.strftime("%Y-%m-%d %H:%M:%S")
end