Class: FluidDb::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/FluidDb.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#close ⇒ Object
-
#connect ⇒ Object
-
#convertTupleToHash(fields, tuple, j) ⇒ Object
-
#escape_string(input) ⇒ Object
-
#execute(sql, params, expected_affected_rows) ⇒ Object
Execute an insert, update or delete, then check the impact that statement has on the data.
-
#format_to_sql(sql, params = nil) ⇒ Object
-
#initialize(uri) ⇒ Base
constructor
-
#queryForArray(sql, params) ⇒ Object
Return a single row from the database, given the sql parameter.
-
#queryForResultset(sql, params) ⇒ Object
-
#queryForValue(sql, params) ⇒ Object
Return a single value is returned from a single row from the database, given the sql parameter.
-
#reconnect ⇒ Object
-
#splice_sql(sql, params) ⇒ Object
-
#verboseLog(string) ⇒ Object
Constructor Details
#initialize(uri) ⇒ Base
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/FluidDb.rb', line 36
def initialize(uri)
if uri.kind_of? String then
@uri = URI.parse( uri )
else
@uri = uri
end
self.connect
@verbose = !ENV["VERBOSE"].nil?
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
27
28
29
|
# File 'lib/FluidDb.rb', line 27
def connection
@connection
end
|
#verbose=(value) ⇒ Object
Sets the attribute verbose
26
27
28
|
# File 'lib/FluidDb.rb', line 26
def verbose=(value)
@verbose = value
end
|
Instance Method Details
#close ⇒ Object
121
122
123
|
# File 'lib/FluidDb.rb', line 121
def close
raise NotImplementedError.new("You must implement 'close'.")
end
|
#connect ⇒ Object
117
118
119
|
# File 'lib/FluidDb.rb', line 117
def connect
raise NotImplementedError.new("You must implement 'connect'.")
end
|
#convertTupleToHash(fields, tuple, j) ⇒ Object
108
109
110
111
112
113
114
115
|
# File 'lib/FluidDb.rb', line 108
def convertTupleToHash( fields, tuple, j )
hash = Hash.new
0.upto( fields.length-1 ).each do |i|
hash[fields[i].to_s] = tuple.getvalue(j, i)
end
return hash
end
|
#escape_string(input) ⇒ Object
67
68
69
|
# File 'lib/FluidDb.rb', line 67
def escape_string( input )
return input.split( "'" ).join( "''" )
end
|
#execute(sql, params, expected_affected_rows) ⇒ Object
Execute an insert, update or delete, then check the impact that statement has on the data.
159
160
161
|
# File 'lib/FluidDb.rb', line 159
def execute( sql, params, expected_affected_rows )
raise NotImplementedError.new("You must implement 'execute'.")
end
|
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/FluidDb.rb', line 71
def format_to_sql( sql, params=nil )
if params.nil? || params.count == 0 then
return sql
end
params.each_with_index do |v, idx|
if v.kind_of? String then
v = "'" + self.escape_string( v ) + "'"
elsif v.is_a? DateTime then
v = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
elsif v.is_a? Time then
v = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
elsif v.kind_of? Date then
v = "'" + v.to_s + "'"
elsif v.is_a?(Numeric) then
v = v.to_s
elsif v.nil? then
v = 'NULL'
else
raise ParamTypeNotSupportedError.new( "Name of unknown param type, #{v.class.name}, for sql, #{sql}" )
end
params[idx] = v
end
sql_out = self.splice_sql( sql, params )
if @verbose == true then
puts self.class.name
puts sql
puts params.join(",")
puts sql_out
end
return sql_out
end
|
#queryForArray(sql, params) ⇒ Object
Return a single row from the database, given the sql parameter. Throwa an error for no data. Throws an error for more than 1 row
136
137
138
|
# File 'lib/FluidDb.rb', line 136
def queryForArray( sql, params )
raise NotImplementedError.new("You must implement 'queryForArray'.")
end
|
#queryForResultset(sql, params) ⇒ Object
150
151
152
|
# File 'lib/FluidDb.rb', line 150
def queryForResultset( sql, params )
raise NotImplementedError.new("You must implement 'queryForResultset'.")
end
|
#queryForValue(sql, params) ⇒ Object
Return a single value is returned from a single row from the database, given the sql parameter. Throwa an error for no data. Throws an error for more than 1 row
146
147
148
|
# File 'lib/FluidDb.rb', line 146
def queryForValue( sql, params )
raise NotImplementedError.new("You must implement 'queryForValue'.")
end
|
#reconnect ⇒ Object
125
126
127
128
|
# File 'lib/FluidDb.rb', line 125
def reconnect
self.close
self.connect
end
|
#splice_sql(sql, params) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/FluidDb.rb', line 52
def splice_sql( sql, params )
if params.length != sql.count( "?" ) then
raise IncorrectNumberOfParametersError.new
end
sql_out = ""
sql.split( "?" ).each_with_index do |s,idx|
sql_out = sql_out + s
sql_out = sql_out + params[idx] unless params[idx].nil?
end
return sql_out
end
|
#verboseLog(string) ⇒ Object
48
49
50
|
# File 'lib/FluidDb.rb', line 48
def verboseLog( string )
puts string if @verbose == true
end
|