Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

dbslayer: Renamed gem to dashes (n line with conventions)

git-svn-id: svn://newsprojects.nytimes.com/newsdev/gems/activerecord-dbslayer-adapter@4382 ae54da50-7589-804d-96e1-6ee4b2538e53
  • Loading branch information...
commit 7a8935e560865dc2ed9a906188e4c4e1318483ba 1 parent 6d31b09
harrisj authored
View
12 Manifest.txt
@@ -5,8 +5,8 @@ README.txt
Rakefile
config/hoe.rb
config/requirements.rb
-lib/activerecord_dbslayer_adapter.rb
-lib/activerecord_dbslayer_adapter/version.rb
+lib/active_record/connection_adapters/dbslayer_adapter.rb
+lib/active_record/connection_adapters/dbslayer_connection.rb
log/debug.log
script/console
script/destroy
@@ -16,8 +16,12 @@ setup.rb
tasks/deployment.rake
tasks/environment.rake
tasks/website.rake
-test/test_activerecord_dbslayer_adapter.rb
-test/test_helper.rb
+test/helper.rb
+test/localtest.rb
+test/test_dbslayer_adapter.rb
+test/test_dbslayer_column.rb
+test/test_dbslayer_connection.rb
+test/test_dbslayer_results.rb
website/index.html
website/index.txt
website/javascripts/rounded_corners_lite.inc.js
View
4 lib/active_record/connection_adapters/dbslayer_adapter.rb
@@ -7,8 +7,8 @@ class Base
# Establishes a connection to the database that's used by all Active Record objects.
def self.dbslayer_connection(config) # :nodoc:
config = config.symbolize_keys
- host = config[:host]
- port = config[:port]
+ host = config[:host]
+ port = config[:port]
connection = ConnectionAdapters::DbslayerConnection.new(host, port)
ConnectionAdapters::DbslayerAdapter.new(connection, logger, [host, port], config)
View
28 lib/active_record/connection_adapters/dbslayer_connection.rb
@@ -6,6 +6,9 @@
module ActiveRecord
module ConnectionAdapters
+ class DbslayerException < RuntimeError
+ end
+
class DbslayerResult
def initialize(results_hash)
@hash = results_hash
@@ -55,6 +58,8 @@ def each_hash
end
class DbslayerConnection
+ attr_reader :host, :port
+
def initialize(host='localhost', port=9090)
@host = host
@port = port
@@ -64,13 +69,19 @@ def initialize(host='localhost', port=9090)
# Executes a SQL query
def sql_query(sql)
dbslay_results = cmd_execute(:db, 'SQL' => sql)
-
+
case dbslay_results
when Hash
- DbslayerResult.new(dbslay_results['RESULT'])
+ # check for an error
+ if dbslay_results['MYSQL_ERROR']
+ raise DbslayerException, "MySQL Error #{dbslay_results['MYSQL_ERRNO']}: #{dbslay_results['MYSQL_ERROR']}"
+ else
+ DbslayerResult.new(dbslay_results['RESULT'])
+ end
when Array
dbslay_results.map { |r| DbslayerResult.new(r['RESULT']) }
else
+ raise DbslayerException, "Unknown format for SQL results from DBSlayer"
end
end
@@ -107,6 +118,19 @@ def server_version_num
@server_version
end
+ def escape_string(str)
+ str.gsub(/([\0\n\r\032\'\"\\])/) do
+ case $1
+ when "\0" then "\\0"
+ when "\n" then "\\n"
+ when "\r" then "\\r"
+ when "\032" then "\\Z"
+ else "\\"+$1
+ end
+ end
+ end
+ alias :quote :escape_string
+
private
def query_string(commands)
URI.encode commands.to_json
View
1  test/helper.rb
@@ -68,6 +68,7 @@
"TYPES"=>["MYSQL_TYPE_VAR_STRING", "MYSQL_TYPE_VAR_STRING"]},
"SERVER"=>"test-slave"}.freeze
+ERROR_REPLY = {"MYSQL_ERRNO"=>1045, "MYSQL_ERROR"=>"Access denied for user 'harrisj'@'localhost' (using password: NO)", "SERVER"=>"test"}
def sql_hash(sql)
{"SQL" => sql}.freeze
View
31 test/localtest.rb
@@ -0,0 +1,31 @@
+require 'rubygems'
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
+
+require 'active_record'
+require 'active_record/connection_adapters/dbslayer_adapter'
+
+ActiveRecord::Base.establish_connection({
+ :adapter => 'dbslayer',
+ :host => 'localhost',
+ :port => 9090
+})
+
+class Restaurant < ActiveRecord::Base
+ has_many :reviews
+end
+
+class Review < ActiveRecord::Base
+ belongs_to :restaurant
+end
+
+rests = Restaurant.find(:all, :limit => 10)
+
+puts rests[0].inspect
+
+size = Restaurant.count(:all)
+
+puts size
+
+caracas = Restaurant.find_by_name('Caracas Arepa Bar')
+
+puts caracas.reviews.first.inspect
View
5 test/test_dbslayer_connection.rb
@@ -37,4 +37,9 @@ def test_client_info
assert_equal CLIENT_INFO_REPLY['CLIENT_INFO'], reply
end
+
+ def test_server_error
+ @slayer.stubs(:cmd_execute).returns(ERROR_REPLY)
+ assert_raise(DbslayerException) { @slayer.sql_query("SELECT * FROM items") }
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.