Module: N::DbUtils

Included in:
DbConnection
Defined in:
lib/n/db/psql.rb,
lib/n/db/mysql.rb,
lib/n/db/utils.rb

Overview

DbUtils

A collection of Database utilities. Additional backend specific utilities are defined in the backend implementations.

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object

Escape an sql string



25
26
27
28
# File 'lib/n/db/psql.rb', line 25

def self.escape(str)
	return nil unless str
	return PGconn.escape(str)
end

.escape_bytes(bytes) ⇒ Object

Escape bytes



32
33
34
35
# File 'lib/n/db/psql.rb', line 32

def self.escape_bytes(bytes)
	return nil unless bytes
	return PGconn.escape_bytea(bytes)
end

.parse_sql_date(str) ⇒ Object

Input YYYY-mm-dd



70
71
72
73
# File 'lib/n/db/psql.rb', line 70

def self.parse_sql_date(str)
	return nil unless str
	return Date.strptime(str)
end

.parse_sql_timestamp(str) ⇒ Object

Parse sql datetime



64
65
66
# File 'lib/n/db/psql.rb', line 64

def self.parse_sql_timestamp(str)
	return Time.parse(str)		
end

.read_prop(p) ⇒ Object

Return an evaluator for reading the property No need to optimize this, used only to precalculate code.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/n/db/psql.rb', line 78

def self.read_prop(p, idx)
	case p.klass.to_s
		when Fixnum.name
			return "rows.getvalue(tuple, #{idx}).to_i()"
		when Float.name
			return "rows.getvalue(tuple, #{idx}).to_f()"
		when Time.name
			return "N::DbUtils.parse_sql_timestamp(rows.getvalue(tuple, #{idx}))"
		when Date.name
			return "N::DbUtils.parse_sql_date(rows.getvalue(tuple, #{idx}))"
		when TrueClass.name
			return "('true' == rows.getvalue(tuple, #{idx}))"
		when Object.name
			return "Marshal.load(N::DbUtils.unescape_bytes(rows.getvalue(tuple, #{idx})))"
		when Array.name
			return "Marshal.load(N::DbUtils.unescape_bytes(rows.getvalue(tuple, #{idx})))"
		when Hash.name
			return "Marshal.load(N::DbUtils.unescape_bytes(rows.getvalue(tuple, #{idx})))"
		else # String 
			return "rows.getvalue(tuple, #{idx})"
	end		
end

.sql_date(date) ⇒ Object

Output YYY-mm-dd



55
56
57
58
# File 'lib/n/db/psql.rb', line 55

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

.sql_table(klass) ⇒ Object

The name of the SQL table where entities of this class are stored. The Module separators are replaced with _ and NOT stripped out so that we can convert back to the original notation if needed.



41
42
43
# File 'lib/n/db/utils.rb', line 41

def self.sql_table(klass)
	return "#{klass}".gsub(/::/, "_").downcase
end

.sql_timestamp(time = Time.now) ⇒ Object

Convert a ruby time to an sql timestamp.



48
49
50
51
# File 'lib/n/db/psql.rb', line 48

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

.unescape_bytes(bytes) ⇒ Object

Unescape bytes FIXME: optimize this! Even better integrate this in the libpq library, or find out why the default method doesnt work.



42
43
44
# File 'lib/n/db/psql.rb', line 42

def self.unescape_bytes(bytes)
	return bytes.gsub(/(\\\d+)/) { |m| m.gsub(/\\/, "").oct.chr }
end

.write_prop(p) ⇒ Object

Return an sql string evaluator for the property. No need to optimize this, used only to precalculate code.

FIXME: add extra handling for float.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/n/db/utils.rb', line 74

def self.write_prop(p)
	if String == p.klass
		return "'#\{DbUtils.escape(@#{p.symbol})\}'"
	elsif Time == p.klass
		return %|#\{@#{p.symbol} ? "'#\{DbUtils.sql_timestamp(@#{p.symbol})\}'" : 'NULL'\}|
	elsif Date == p.klass
		return %|#\{@#{p.symbol} ? "'#\{DbUtils.sql_date(@#{p.symbol})\}'" : 'NULL'\}|
	elsif Object == p.klass or Array == p.klass or Hash == p.klass
		#return "'#\{DbUtils.escape_bytes(Marshal.dump(@#{p.symbol}))\}'"
		return "'#\{DbUtils.escape_bytes(Marshal.dump(@#{p.symbol}))\}'"
	else
		# Fixnum, TrueClass
		return "#\{@#{p.symbol} || 'NULL'\}"
	end
end