activerecord-dbslayer-adapter 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.3.0 2008-06-05
2
+
3
+ * 2 major enhancements
4
+ * insert now supports the DBSlayer's INSERT_ID
5
+ * update now correctly uses the DBSlayer's AFFECTED_ROWS
6
+
1
7
  == 0.2.5 2008-05-06
2
8
 
3
9
  * 3 minor enhancements:
@@ -339,9 +339,10 @@ module ActiveRecord
339
339
  end
340
340
 
341
341
  # Executes the update statement and returns the number of rows affected.
342
- def update_sql(sql, name = nil)
343
- execute(sql, name).rows[0][0]
344
- end
342
+ # def update_sql(sql, name = nil)
343
+ # execute(sql, name)
344
+ # end
345
+ #
345
346
 
346
347
  def supports_views?
347
348
  ## get mysql version
@@ -26,6 +26,18 @@ module ActiveRecord
26
26
  @hash['HEADER']
27
27
  end
28
28
 
29
+ def success?
30
+ @hash['SUCCESS']
31
+ end
32
+
33
+ def affected_rows
34
+ @hash['AFFECTED_ROWS']
35
+ end
36
+
37
+ def insert_id
38
+ @hash['INSERT_ID']
39
+ end
40
+
29
41
  ## Compatibility to the MySQL ones
30
42
  def num_rows
31
43
  return 0 if rows.nil?
@@ -58,11 +70,13 @@ module ActiveRecord
58
70
  end
59
71
 
60
72
  class DbslayerConnection
61
- attr_reader :host, :port
73
+ attr_reader :host, :port, :insert_id, :affected_rows
62
74
 
63
75
  def initialize(host='localhost', port=9090)
64
76
  @host = host
65
77
  @port = port
78
+ @insert_id = nil
79
+ @affected_rows = nil
66
80
  end
67
81
 
68
82
  ##
@@ -73,18 +87,19 @@ module ActiveRecord
73
87
  # check for an error
74
88
  if dbslay_results['MYSQL_ERROR']
75
89
  raise DbslayerException, "MySQL Error #{dbslay_results['MYSQL_ERRNO']}: #{dbslay_results['MYSQL_ERROR']}"
76
- elsif dbslay_results['RESULT']
77
- result = dbslay_results['RESULT']
90
+ elsif result = dbslay_results['RESULT']
78
91
  case result
79
92
  when Hash
80
- DbslayerResult.new(result)
93
+ out = DbslayerResult.new(result)
94
+ set_affected!(out)
95
+ out
81
96
  when Array
82
- result.map {|r| DbslayerResult.new(r) }
97
+ out = result.map {|r| DbslayerResult.new(r) }
98
+ set_affected!(out.last)
99
+ out
83
100
  else
84
101
  raise DbslayerException, "Unknown format for SQL results from DBSlayer"
85
102
  end
86
- elsif dbslay_results['SUCCESS']
87
- return dbslay_results['SUCCESS']
88
103
  else
89
104
  raise DbslayerException, "Unknown format for SQL results from DBSlayer"
90
105
  end
@@ -149,6 +164,11 @@ module ActiveRecord
149
164
  JSON.parse(file.read)
150
165
  end
151
166
  end
167
+
168
+ def set_affected!(result)
169
+ @insert_id = result.insert_id
170
+ @affected_rows = result.affected_rows
171
+ end
152
172
  end
153
173
  end
154
174
  end
@@ -27,7 +27,7 @@ end
27
27
  module ActiveRecord
28
28
  module ConnectionAdapters
29
29
  class DbslayerAdapter
30
- VERSION = '0.2.5'
30
+ VERSION = '0.3.0'
31
31
  end
32
32
  end
33
33
  end
data/test/helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
+ require 'test/unit'
2
3
  require 'active_support'
3
4
  require 'active_record'
4
- require 'test/unit'
5
5
  require 'mocha'
6
6
 
7
7
  $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -58,7 +58,13 @@ MULTIPLE_RESULTS = {
58
58
  ]
59
59
  }.freeze
60
60
 
61
- NULL_RESULT = {"SUCCESS" => true}
61
+ NULL_RESULT = {"RESULT" => {"SUCCESS" => true}}.freeze
62
+
63
+ INSERT_ID_RESULT = {"RESULT" => {"AFFECTED_ROWS" => 1 , "INSERT_ID" => 1 , "SUCCESS" => true} , "SERVER" => "dbslayer"}.freeze
64
+
65
+ UPDATE_RESULT = {"RESULT" => {"AFFECTED_ROWS" => 42 , "SUCCESS" => true} , "SERVER" => "dbslayer"}.freeze
66
+
67
+ 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
62
68
 
63
69
  SHOW_TABLES_REPLY = {"RESULT"=> {"HEADER"=> ["Tables_in_Test_Database"],
64
70
  "ROWS" => [["table1"], ["table2"]],
@@ -29,12 +29,20 @@ class Test_ActiveRecord_ConnectionAdapters_DbslayerAdapter < Test::Unit::TestCas
29
29
  assert_equal CITY_ROWS, rows
30
30
  end
31
31
 
32
- def test_insert_sql_with_id
32
+ def test_insert_returns_id
33
+ insert_sql = "insert into cities(name) values(\"Seattle\")"
34
+ @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(insert_sql)).returns(INSERT_ID_RESULT)
33
35
 
36
+ id = @adapter.insert_sql(insert_sql)
37
+ assert_equal 1, id
34
38
  end
35
39
 
36
- def test_insert_sql_no_id
40
+ def test_update_affected_rows
41
+ update_sql = "update cities set urban=1"
42
+ @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(update_sql)).returns(UPDATE_RESULT)
37
43
 
44
+ affected_rows = @adapter.send :update_sql, update_sql
45
+ assert_equal 42, affected_rows
38
46
  end
39
47
 
40
48
  def test_tables
@@ -34,14 +34,6 @@ class TestDbslayerConnection < Test::Unit::TestCase
34
34
  assert_not_nil reply.rows
35
35
  end
36
36
 
37
- def test_sql_null_return
38
- sql_command = "update set posted = 1"
39
- @slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(NULL_RESULT)
40
-
41
- status = @slayer.sql_query(sql_command)
42
- assert_equal true, status
43
- end
44
-
45
37
  def test_multiple_results
46
38
  sql_command = "select * from cities limit 10; select * from countries limit 3"
47
39
  @slayer.stubs(:cmd_execute).with(:db, {"SQL" => sql_command}).returns(MULTIPLE_RESULTS)
@@ -30,6 +30,10 @@ class Test_ActiveRecord_ConnectionAdapters_DbslayerResults < Test::Unit::TestCas
30
30
  assert_equal CITY_TYPES, @result.types
31
31
  end
32
32
 
33
+ def test_success?
34
+ assert !@result.success?
35
+ end
36
+
33
37
  def test_hash_rows
34
38
  assert_equal CITY_HASH_ROWS, @result.hash_rows
35
39
  end
@@ -52,3 +56,23 @@ class Test_ActiveRecord_ConnectionAdapters_DbslayerResults < Test::Unit::TestCas
52
56
  assert_equal CITY_HASH_ROWS, output
53
57
  end
54
58
  end
59
+
60
+ class Test_ActiveRecord_ConnectionAdapters_DbslayerResults_Insert < Test::Unit::TestCase
61
+ include ActiveRecord::ConnectionAdapters
62
+
63
+ def setup
64
+ @result = ActiveRecord::ConnectionAdapters::DbslayerResult.new(INSERT_ID_RESULT["RESULT"])
65
+ end
66
+
67
+ def test_success?
68
+ assert @result.success?
69
+ end
70
+
71
+ def test_affected_rows
72
+ assert_equal 1, @result.affected_rows
73
+ end
74
+
75
+ def test_insert_id
76
+ assert_equal 1, @result.insert_id
77
+ end
78
+ end
data/website/index.txt CHANGED
@@ -21,6 +21,8 @@ h2. Forum
21
21
 
22
22
  "http://groups.google.com/group/activerecord_dbslayer_adapter":http://groups.google.com/group/activerecord_dbslayer_adapter
23
23
 
24
+ TODO - create Google Group - activerecord_dbslayer_adapter
25
+
24
26
  h2. How to submit patches
25
27
 
26
28
  Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-dbslayer-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Harris
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-05-08 00:00:00 -04:00
12
+ date: 2008-06-05 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements: []
82
82
 
83
83
  rubyforge_project: ar-dbslayer
84
- rubygems_version: 1.1.0
84
+ rubygems_version: 1.1.1
85
85
  signing_key:
86
86
  specification_version: 2
87
87
  summary: An ActiveRecord adapter to DBSlayer