public
Rubygem
Description: An ActiveRecord to the DBSlayer lightweight proxy/pooling layer (http://code.nytimes.com/projects/dbslayer)
Clone URL: git://github.com/harrisj/activerecord-dbslayer-adapter.git
Now changed to work correctly with DBslayers insert and update semantics
Jacob Harris (author)
Thu Jun 05 18:59:49 -0700 2008
commit  57e5901792cf6e01c6d3066eade423d061458eb7
tree    d8fdd6ce3fb768f86928707917e34b4abdb7f9de
parent  68359331bee492f794ab52fa80b7d1a005013d3b
...
 
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
9
0
@@ -1,3 +1,9 @@
0
+== 0.3.0 2008-06-05
0
+
0
+* 2 major enhancements
0
+ * insert now supports the DBSlayer's INSERT_ID
0
+ * update now correctly uses the DBSlayer's AFFECTED_ROWS
0
+
0
 == 0.2.5 2008-05-06
0
 
0
 * 3 minor enhancements:
...
339
340
341
342
343
344
 
 
 
 
345
346
347
...
339
340
341
 
 
 
342
343
344
345
346
347
348
0
@@ -339,9 +339,10 @@ module ActiveRecord
0
         end
0
 
0
         # Executes the update statement and returns the number of rows affected.
0
- def update_sql(sql, name = nil)
0
- execute(sql, name).rows[0][0]
0
- end
0
+ # def update_sql(sql, name = nil)
0
+ # execute(sql, name)
0
+ # end
0
+ #
0
         
0
         def supports_views?
0
           ## get mysql version
...
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
...
58
59
60
61
 
62
63
64
65
 
 
66
67
68
...
73
74
75
76
77
 
78
79
80
 
 
 
81
82
 
 
 
83
84
85
86
87
88
89
90
...
149
150
151
 
 
 
 
 
152
153
154
155
...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
70
71
72
 
73
74
75
76
77
78
79
80
81
82
...
87
88
89
 
 
90
91
92
 
93
94
95
96
 
97
98
99
100
101
102
 
 
103
104
105
...
164
165
166
167
168
169
170
171
172
173
174
175
0
@@ -26,6 +26,18 @@ module ActiveRecord
0
         @hash['HEADER']
0
       end
0
       
0
+ def success?
0
+ @hash['SUCCESS']
0
+ end
0
+
0
+ def affected_rows
0
+ @hash['AFFECTED_ROWS']
0
+ end
0
+
0
+ def insert_id
0
+ @hash['INSERT_ID']
0
+ end
0
+
0
       ## Compatibility to the MySQL ones
0
       def num_rows
0
         return 0 if rows.nil?
0
@@ -58,11 +70,13 @@ module ActiveRecord
0
     end
0
 
0
     class DbslayerConnection
0
- attr_reader :host, :port
0
+ attr_reader :host, :port, :insert_id, :affected_rows
0
       
0
       def initialize(host='localhost', port=9090)
0
         @host = host
0
         @port = port
0
+ @insert_id = nil
0
+ @affected_rows = nil
0
       end
0
 
0
       ##
0
@@ -73,18 +87,19 @@ module ActiveRecord
0
         # check for an error
0
         if dbslay_results['MYSQL_ERROR']
0
           raise DbslayerException, "MySQL Error #{dbslay_results['MYSQL_ERRNO']}: #{dbslay_results['MYSQL_ERROR']}"
0
- elsif dbslay_results['RESULT']
0
- result = dbslay_results['RESULT']
0
+ elsif result = dbslay_results['RESULT']
0
           case result
0
           when Hash
0
- DbslayerResult.new(result)
0
+ out = DbslayerResult.new(result)
0
+ set_affected!(out)
0
+ out
0
           when Array
0
- result.map {|r| DbslayerResult.new(r) }
0
+ out = result.map {|r| DbslayerResult.new(r) }
0
+ set_affected!(out.last)
0
+ out
0
           else
0
             raise DbslayerException, "Unknown format for SQL results from DBSlayer"
0
           end
0
- elsif dbslay_results['SUCCESS']
0
- return dbslay_results['SUCCESS']
0
         else
0
           raise DbslayerException, "Unknown format for SQL results from DBSlayer"
0
         end
0
@@ -149,6 +164,11 @@ module ActiveRecord
0
           JSON.parse(file.read)
0
         end
0
       end
0
+
0
+ def set_affected!(result)
0
+ @insert_id = result.insert_id
0
+ @affected_rows = result.affected_rows
0
+ end
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
27
28
29
30
 
31
32
33
34
...
27
28
29
 
30
31
32
33
34
0
@@ -27,7 +27,7 @@ end
0
 module ActiveRecord
0
   module ConnectionAdapters
0
     class DbslayerAdapter
0
- VERSION = '0.2.5'
0
+ VERSION = '0.3.0'
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
1
 
2
3
4
5
6
7
...
58
59
60
61
 
 
 
 
 
 
 
62
63
64
...
1
2
3
4
 
5
6
7
...
58
59
60
 
61
62
63
64
65
66
67
68
69
70
0
@@ -1,7 +1,7 @@
0
 require 'rubygems'
0
+require 'test/unit'
0
 require 'active_support'
0
 require 'active_record'
0
-require 'test/unit'
0
 require 'mocha'
0
 
0
 $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
0
@@ -58,7 +58,13 @@ MULTIPLE_RESULTS = {
0
               ]
0
 }.freeze
0
 
0
-NULL_RESULT = {"SUCCESS" => true}
0
+NULL_RESULT = {"RESULT" => {"SUCCESS" => true}}.freeze
0
+
0
+INSERT_ID_RESULT = {"RESULT" => {"AFFECTED_ROWS" => 1 , "INSERT_ID" => 1 , "SUCCESS" => true} , "SERVER" => "dbslayer"}.freeze
0
+
0
+UPDATE_RESULT = {"RESULT" => {"AFFECTED_ROWS" => 42 , "SUCCESS" => true} , "SERVER" => "dbslayer"}.freeze
0
+
0
+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
0
 
0
 SHOW_TABLES_REPLY = {"RESULT"=> {"HEADER"=> ["Tables_in_Test_Database"],
0
                      "ROWS" => [["table1"], ["table2"]],
...
29
30
31
32
 
 
 
33
 
 
34
35
36
 
 
 
37
 
 
38
39
40
...
29
30
31
 
32
33
34
35
36
37
38
39
 
40
41
42
43
44
45
46
47
48
0
@@ -29,12 +29,20 @@ class Test_ActiveRecord_ConnectionAdapters_DbslayerAdapter < Test::Unit::TestCas
0
     assert_equal CITY_ROWS, rows
0
   end
0
   
0
- def test_insert_sql_with_id
0
+ def test_insert_returns_id
0
+ insert_sql = "insert into cities(name) values(\"Seattle\")"
0
+ @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(insert_sql)).returns(INSERT_ID_RESULT)
0
     
0
+ id = @adapter.insert_sql(insert_sql)
0
+ assert_equal 1, id
0
   end
0
   
0
- def test_insert_sql_no_id
0
+ def test_update_affected_rows
0
+ update_sql = "update cities set urban=1"
0
+ @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(update_sql)).returns(UPDATE_RESULT)
0
     
0
+ affected_rows = @adapter.send :update_sql, update_sql
0
+ assert_equal 42, affected_rows
0
   end
0
   
0
   def test_tables
...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
...
34
35
36
 
 
 
 
 
 
 
 
37
38
39
0
@@ -34,14 +34,6 @@ class TestDbslayerConnection < Test::Unit::TestCase
0
     assert_not_nil reply.rows
0
   end
0
   
0
- def test_sql_null_return
0
- sql_command = "update set posted = 1"
0
- @slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(NULL_RESULT)
0
-
0
- status = @slayer.sql_query(sql_command)
0
- assert_equal true, status
0
- end
0
-
0
   def test_multiple_results
0
     sql_command = "select * from cities limit 10; select * from countries limit 3"
0
     @slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(MULTIPLE_RESULTS)
...
30
31
32
 
 
 
 
33
34
35
...
52
53
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
...
30
31
32
33
34
35
36
37
38
39
...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
0
@@ -30,6 +30,10 @@ class Test_ActiveRecord_ConnectionAdapters_DbslayerResults < Test::Unit::TestCas
0
     assert_equal CITY_TYPES, @result.types
0
   end
0
   
0
+ def test_success?
0
+ assert !@result.success?
0
+ end
0
+
0
   def test_hash_rows
0
     assert_equal CITY_HASH_ROWS, @result.hash_rows
0
   end
0
@@ -52,3 +56,23 @@ class Test_ActiveRecord_ConnectionAdapters_DbslayerResults < Test::Unit::TestCas
0
     assert_equal CITY_HASH_ROWS, output
0
   end
0
 end
0
+
0
+class Test_ActiveRecord_ConnectionAdapters_DbslayerResults_Insert < Test::Unit::TestCase
0
+ include ActiveRecord::ConnectionAdapters
0
+
0
+ def setup
0
+ @result = ActiveRecord::ConnectionAdapters::DbslayerResult.new(INSERT_ID_RESULT["RESULT"])
0
+ end
0
+
0
+ def test_success?
0
+ assert @result.success?
0
+ end
0
+
0
+ def test_affected_rows
0
+ assert_equal 1, @result.affected_rows
0
+ end
0
+
0
+ def test_insert_id
0
+ assert_equal 1, @result.insert_id
0
+ end
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.