Module: Marjoree
- Defined in:
- lib/marjoree/core.rb
Overview
This is the main Marjoree mixin. Before you call anything you will need to call ‘connect’ or no messages will reach your DB :o) You might like to stick something like this in your test code to ensure the connection is dropped when you are finished. Kernel.at_exit { disconnect }
Instance Method Summary collapse
-
#assert_contains(table_name, value_map) ⇒ Object
Helpful Assert that wraps the contains? method.
-
#assert_db_error ⇒ Object
Asserts when the DB throws an error.
-
#assert_does_not_contain(table_name, value_map) ⇒ Object
Helpful Assert that wraps the contains? method.
-
#assert_empty(result_set) ⇒ Object
Asserts that the ResultSet is empty.
-
#assert_error_thrown(expected_error_code, expected_error_message) ⇒ Object
Asserts that an expected_error_code and error_message are returned when running the block.
-
#assert_not_equal_results(expected, result_set) ⇒ Object
Asserts that the values in the ExpectedResultSet are NOT contained within the actual ResultSet.
-
#assert_results(expected, result_set) ⇒ Object
Asserts that the values in the ExpectedResultSet are contained within the actual ResultSet.
-
#connect_me_to(odbc_name) ⇒ Object
Connect to <odbc_name> server.
-
#contains?(table_name, map = {}) ⇒ Boolean
Returns true if table ‘table_name’ contains the data specified in the where_map.
-
#count(table_name, where_map = nil) ⇒ Object
Does the same as num_rows.
-
#delete(table_name, map = {}) ⇒ Object
Performs an DELETE FROM ‘table_name’.
-
#disconnect_me ⇒ Object
Remove connection to dataserver.
-
#drop_user_sprocs ⇒ Object
DROPs all user sprocs.
-
#drop_user_tables ⇒ Object
DROPs all user tables.
-
#drop_user_views ⇒ Object
DROPs all user views.
-
#establish_connection_to(connection) ⇒ Object
Establish a connection to dataserver.
-
#execute(sql, output_params = []) ⇒ Object
Executes raw sql against the db.
- #get_user_sprocs ⇒ Object
- #get_user_tables ⇒ Object
- #get_user_views ⇒ Object
- #has_column_headers?(expected, result_set) ⇒ Boolean
- #has_correct_data?(expected, result_set) ⇒ Boolean
-
#insert(table_name, map = {}) ⇒ Object
Performs an INSERT INTO ‘table_name’.
-
#num_rows(table_name, where_map = nil) ⇒ Object
Provides the number of rows in table ‘table_name’.
-
#run_sproc(proc_name, map = {}) ⇒ Object
Run Store Procedure ‘proc_name’.
-
#select(table_name, where_map = nil) ⇒ Object
Performs a select * on table_name.
-
#truncate(table_name) ⇒ Object
Performs an TRUNCATE TABLE ‘table_name’.
-
#update(table_name, where_map = {}, map = {}) ⇒ Object
Performs an UPDATE ‘table_name’.
Instance Method Details
#assert_contains(table_name, value_map) ⇒ Object
Helpful Assert that wraps the contains? method.
226 227 228 |
# File 'lib/marjoree/core.rb', line 226 def assert_contains(table_name, value_map) assert contains?(table_name, value_map) end |
#assert_db_error ⇒ Object
Asserts when the DB throws an error.
ie: Inserting into a table that does not exist.
199 200 201 202 203 204 205 |
# File 'lib/marjoree/core.rb', line 199 def assert_db_error begin yield fail rescue ODBC::Error end end |
#assert_does_not_contain(table_name, value_map) ⇒ Object
Helpful Assert that wraps the contains? method.
231 232 233 |
# File 'lib/marjoree/core.rb', line 231 def assert_does_not_contain(table_name, value_map) assert !contains?(table_name, value_map) end |
#assert_empty(result_set) ⇒ Object
Asserts that the ResultSet is empty
164 165 166 |
# File 'lib/marjoree/core.rb', line 164 def assert_empty(result_set) assert_equal 0, result_set.hashes.size end |
#assert_error_thrown(expected_error_code, expected_error_message) ⇒ Object
Asserts that an expected_error_code and error_message are returned when running the block.
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/marjoree/core.rb', line 183 def assert_error_thrown( expected_error_code, ) begin yield fail rescue ODBC::Error exception = $! assert_equal( expected_error_code, exception.error_code ) if != nil assert_equal( , exception. ) end end end |
#assert_not_equal_results(expected, result_set) ⇒ Object
Asserts that the values in the ExpectedResultSet are NOT contained within the actual ResultSet.
175 176 177 178 179 180 |
# File 'lib/marjoree/core.rb', line 175 def assert_not_equal_results(expected, result_set) flag1 = !has_column_headers?(expected, result_set) flag2 = !has_correct_data?(expected, result_set) assert( !flag1 || !has_correct_data?(expected, result_set) ) end |
#assert_results(expected, result_set) ⇒ Object
Asserts that the values in the ExpectedResultSet are contained within the actual ResultSet.
169 170 171 172 |
# File 'lib/marjoree/core.rb', line 169 def assert_results(expected, result_set) assert_column_headers expected, result_set assert_row_data expected, result_set end |
#connect_me_to(odbc_name) ⇒ Object
Connect to <odbc_name> server
22 23 24 25 |
# File 'lib/marjoree/core.rb', line 22 def connect_me_to( odbc_name ) $connection.connect_me_to( odbc_name ) end |
#contains?(table_name, map = {}) ⇒ Boolean
Returns true if table ‘table_name’ contains the data specified in the where_map.
‘where_map’ supplies column_name_as_sym => value
These are AND’ed together.
90 91 92 |
# File 'lib/marjoree/core.rb', line 90 def contains?(table_name, map={}) return count(table_name, map) > 0 end |
#count(table_name, where_map = nil) ⇒ Object
Does the same as num_rows
81 82 83 |
# File 'lib/marjoree/core.rb', line 81 def count(table_name, where_map = nil) return num_rows(table_name, where_map) end |
#delete(table_name, map = {}) ⇒ Object
Performs an DELETE FROM ‘table_name’
‘where_map’ supplies column_name_as_sym => value for the WHERE section of the UPDATE statement.
These are AND’ed together.
120 121 122 123 |
# File 'lib/marjoree/core.rb', line 120 def delete(table_name, map={}) where_clause = build_where_statement(map) execute "DELETE FROM #{table_name} #{where_clause}" end |
#disconnect_me ⇒ Object
Remove connection to dataserver.
33 34 35 |
# File 'lib/marjoree/core.rb', line 33 def disconnect_me $connection.disconnect_me end |
#drop_user_sprocs ⇒ Object
DROPs all user sprocs
157 158 159 160 161 |
# File 'lib/marjoree/core.rb', line 157 def drop_user_sprocs drop_objects('PROCEDURE') do get_user_sprocs end end |
#drop_user_tables ⇒ Object
DROPs all user tables
143 144 145 146 147 |
# File 'lib/marjoree/core.rb', line 143 def drop_user_tables drop_objects('TABLE') do get_user_tables end end |
#drop_user_views ⇒ Object
DROPs all user views
150 151 152 153 154 |
# File 'lib/marjoree/core.rb', line 150 def drop_user_views drop_objects('VIEW') do get_user_views end end |
#establish_connection_to(connection) ⇒ Object
Establish a connection to dataserver.
28 29 30 |
# File 'lib/marjoree/core.rb', line 28 def establish_connection_to( connection ) $connection = connection end |
#execute(sql, output_params = []) ⇒ Object
Executes raw sql against the db.
236 237 238 |
# File 'lib/marjoree/core.rb', line 236 def execute(sql, output_params = []) return $connection.execute( sql, output_params ) end |
#get_user_sprocs ⇒ Object
138 139 140 |
# File 'lib/marjoree/core.rb', line 138 def get_user_sprocs return get_objects( 'P' ) end |
#get_user_tables ⇒ Object
130 131 132 |
# File 'lib/marjoree/core.rb', line 130 def get_user_tables return get_objects( 'U' ) end |
#get_user_views ⇒ Object
134 135 136 |
# File 'lib/marjoree/core.rb', line 134 def get_user_views return get_objects( 'V' ) end |
#has_column_headers?(expected, result_set) ⇒ Boolean
207 208 209 210 211 212 213 214 |
# File 'lib/marjoree/core.rb', line 207 def has_column_headers?(expected, result_set) begin assert_column_headers expected, result_set return true rescue return false end end |
#has_correct_data?(expected, result_set) ⇒ Boolean
216 217 218 219 220 221 222 223 |
# File 'lib/marjoree/core.rb', line 216 def has_correct_data?(expected, result_set) begin assert_row_data expected, result_set return true rescue return false end end |
#insert(table_name, map = {}) ⇒ Object
Performs an INSERT INTO ‘table_name’. ‘value_map’ supplies column_name_as_sym => value
96 97 98 99 100 |
# File 'lib/marjoree/core.rb', line 96 def insert(table_name, map={}) columns = build_column_headers(map) values = build_column_values(map) execute "INSERT INTO #{table_name} (#{columns}) VALUES (#{values})" end |
#num_rows(table_name, where_map = nil) ⇒ Object
Provides the number of rows in table ‘table_name’.
The where_map entries are AND’ed together eg:
{ :table_column_name_one => 1,
:table_column_name_two => 2 }
would produce
SELECT
*
FROM
table_name
WHERE
table_column_name_one = 1
AND table_column_name_two = 2
74 75 76 77 78 |
# File 'lib/marjoree/core.rb', line 74 def num_rows( table_name, where_map = nil ) where_clause = build_where_statement( where_map ) dbCall = execute( "SELECT COUNT(*) AS number_of_rows FROM #{table_name} #{where_clause}" ) return dbCall.hashes.first[:number_of_rows] end |
#run_sproc(proc_name, map = {}) ⇒ Object
Run Store Procedure ‘proc_name’
The input_param_map takes the form of
{ :procedure_input_param_name => procedure_input_param_value }
eg: result_set = EXEC ‘proc_name’ @procedure_input_param_name = procedure_input_param_value
If a proc has output parameters these are automatically bound onto the ResultSet object. eg: assert_equal( expected_output_parameter_value, result_set.output_parameter_name )
47 48 49 50 51 52 53 |
# File 'lib/marjoree/core.rb', line 47 def run_sproc( proc_name, map={} ) if has_output_params?( proc_name ) return run_complex_sproc( proc_name, map ) else return run_simple_sproc( proc_name, map ) end end |
#select(table_name, where_map = nil) ⇒ Object
Performs a select * on table_name
56 57 58 59 |
# File 'lib/marjoree/core.rb', line 56 def select(table_name, where_map = nil) where_clause = build_where_statement( where_map ) execute "SELECT * FROM #{table_name} #{where_clause}" end |
#truncate(table_name) ⇒ Object
Performs an TRUNCATE TABLE ‘table_name’
126 127 128 |
# File 'lib/marjoree/core.rb', line 126 def truncate(table_name) execute "TRUNCATE TABLE #{table_name}" end |
#update(table_name, where_map = {}, map = {}) ⇒ Object
Performs an UPDATE ‘table_name’
‘where_map’ supplies column_name_as_sym => value for the WHERE section of the UPDATE statement.
These are AND’ed together.
‘set_map’ supplies column_name_as_sym => value for the SET section of the UPDATE statement.
109 110 111 112 113 |
# File 'lib/marjoree/core.rb', line 109 def update(table_name, where_map={}, map={}) where_clause = build_where_statement(where_map) assignments = build_set_statement(map) execute "UPDATE #{table_name} SET #{assignments} #{where_clause}" end |