Skip to content
Browse files

Now changed to work correctly with DBslayers insert and update semantics

  • Loading branch information...
1 parent 6835933 commit 57e5901792cf6e01c6d3066eade423d061458eb7 Jacob Harris committed Jun 5, 2008
View
6 History.txt
@@ -1,3 +1,9 @@
+== 0.3.0 2008-06-05
+
+* 2 major enhancements
+ * insert now supports the DBSlayer's INSERT_ID
+ * update now correctly uses the DBSlayer's AFFECTED_ROWS
+
== 0.2.5 2008-05-06
* 3 minor enhancements:
View
7 lib/active_record/connection_adapters/dbslayer_adapter.rb
@@ -339,9 +339,10 @@ def select(sql, name = nil)
end
# Executes the update statement and returns the number of rows affected.
- def update_sql(sql, name = nil)
- execute(sql, name).rows[0][0]
- end
+ # def update_sql(sql, name = nil)
+ # execute(sql, name)
+ # end
+ #
def supports_views?
## get mysql version
View
34 lib/active_record/connection_adapters/dbslayer_connection.rb
@@ -26,6 +26,18 @@ def header
@hash['HEADER']
end
+ def success?
+ @hash['SUCCESS']
+ end
+
+ def affected_rows
+ @hash['AFFECTED_ROWS']
+ end
+
+ def insert_id
+ @hash['INSERT_ID']
+ end
+
## Compatibility to the MySQL ones
def num_rows
return 0 if rows.nil?
@@ -58,11 +70,13 @@ def each_hash
end
class DbslayerConnection
- attr_reader :host, :port
+ attr_reader :host, :port, :insert_id, :affected_rows
def initialize(host='localhost', port=9090)
@host = host
@port = port
+ @insert_id = nil
+ @affected_rows = nil
end
##
@@ -73,18 +87,19 @@ def sql_query(sql)
# check for an error
if dbslay_results['MYSQL_ERROR']
raise DbslayerException, "MySQL Error #{dbslay_results['MYSQL_ERRNO']}: #{dbslay_results['MYSQL_ERROR']}"
- elsif dbslay_results['RESULT']
- result = dbslay_results['RESULT']
+ elsif result = dbslay_results['RESULT']
case result
when Hash
- DbslayerResult.new(result)
+ out = DbslayerResult.new(result)
+ set_affected!(out)
+ out
when Array
- result.map {|r| DbslayerResult.new(r) }
+ out = result.map {|r| DbslayerResult.new(r) }
+ set_affected!(out.last)
+ out
else
raise DbslayerException, "Unknown format for SQL results from DBSlayer"
end
- elsif dbslay_results['SUCCESS']
- return dbslay_results['SUCCESS']
else
raise DbslayerException, "Unknown format for SQL results from DBSlayer"
end
@@ -149,6 +164,11 @@ def cmd_execute(endpoint, commands)
JSON.parse(file.read)
end
end
+
+ def set_affected!(result)
+ @insert_id = result.insert_id
+ @affected_rows = result.affected_rows
+ end
end
end
end
View
2 lib/activerecord-dbslayer-adapter.rb
@@ -27,7 +27,7 @@
module ActiveRecord
module ConnectionAdapters
class DbslayerAdapter
- VERSION = '0.2.5'
+ VERSION = '0.3.0'
end
end
end
View
10 test/helper.rb
@@ -1,7 +1,7 @@
require 'rubygems'
+require 'test/unit'
require 'active_support'
require 'active_record'
-require 'test/unit'
require 'mocha'
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -58,7 +58,13 @@
]
}.freeze
-NULL_RESULT = {"SUCCESS" => true}
+NULL_RESULT = {"RESULT" => {"SUCCESS" => true}}.freeze
+
+INSERT_ID_RESULT = {"RESULT" => {"AFFECTED_ROWS" => 1 , "INSERT_ID" => 1 , "SUCCESS" => true} , "SERVER" => "dbslayer"}.freeze
+
+UPDATE_RESULT = {"RESULT" => {"AFFECTED_ROWS" => 42 , "SUCCESS" => true} , "SERVER" => "dbslayer"}.freeze
+
+INSERT_THEN_SELECT_RESULT = {"RESULT"=> [{"AFFECTED_ROWS"=>1, "INSERT_ID"=>5, "SUCCESS"=>true}, {"HEADER"=>["id", "name"], "ROWS"=>[[1, "Brooklyn"], [2, "Queens"], [3, "Staten Island"], [4, "Queens"], [5, "Paramus"]], "TYPES"=>["MYSQL_TYPE_LONG", "MYSQL_TYPE_VAR_STRING"]}], "SERVER"=>"dbslayer"}.freeze
SHOW_TABLES_REPLY = {"RESULT"=> {"HEADER"=> ["Tables_in_Test_Database"],
"ROWS" => [["table1"], ["table2"]],
View
12 test/test_dbslayer_adapter.rb
@@ -29,12 +29,20 @@ def test_select_rows
assert_equal CITY_ROWS, rows
end
- def test_insert_sql_with_id
+ def test_insert_returns_id
+ insert_sql = "insert into cities(name) values(\"Seattle\")"
+ @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(insert_sql)).returns(INSERT_ID_RESULT)
+ id = @adapter.insert_sql(insert_sql)
+ assert_equal 1, id
end
- def test_insert_sql_no_id
+ def test_update_affected_rows
+ update_sql = "update cities set urban=1"
+ @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(update_sql)).returns(UPDATE_RESULT)
+ affected_rows = @adapter.send :update_sql, update_sql
+ assert_equal 42, affected_rows
end
def test_tables
View
8 test/test_dbslayer_connection.rb
@@ -34,14 +34,6 @@ def test_sql_query
assert_not_nil reply.rows
end
- def test_sql_null_return
- sql_command = "update set posted = 1"
- @slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(NULL_RESULT)
-
- status = @slayer.sql_query(sql_command)
- assert_equal true, status
- end
-
def test_multiple_results
sql_command = "select * from cities limit 10; select * from countries limit 3"
@slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(MULTIPLE_RESULTS)
View
24 test/test_dbslayer_results.rb
@@ -30,6 +30,10 @@ def test_types
assert_equal CITY_TYPES, @result.types
end
+ def test_success?
+ assert !@result.success?
+ end
+
def test_hash_rows
assert_equal CITY_HASH_ROWS, @result.hash_rows
end
@@ -52,3 +56,23 @@ def test_each_hash
assert_equal CITY_HASH_ROWS, output
end
end
+
+class Test_ActiveRecord_ConnectionAdapters_DbslayerResults_Insert < Test::Unit::TestCase
+ include ActiveRecord::ConnectionAdapters
+
+ def setup
+ @result = ActiveRecord::ConnectionAdapters::DbslayerResult.new(INSERT_ID_RESULT["RESULT"])
+ end
+
+ def test_success?
+ assert @result.success?
+ end
+
+ def test_affected_rows
+ assert_equal 1, @result.affected_rows
+ end
+
+ def test_insert_id
+ assert_equal 1, @result.insert_id
+ end
+end

0 comments on commit 57e5901

Please sign in to comment.
Something went wrong with that request. Please try again.