Permalink
Browse files

Moved handling of options hash to DO Adapter.

  • Loading branch information...
david committed Apr 8, 2008
1 parent acc896b commit 7edfb1915187b89ceaccb375a5a930982d9a9c89
View
@@ -1,22 +1,22 @@
-Copyright (c) 2007 Sam Smoot
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+Copyright (c) 2007 Sam Smoot
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
@@ -405,6 +405,23 @@ def quote_column_name(column_name)
"\"#{column_name.gsub('"', '""')}\""
end
+ def rewrite_uri(uri, options)
+ new_uri = uri.dup
+
+ new_uri.host = options.delete(:host) || uri.host
+ new_uri.user = options.delete(:user) || uri.user
+ new_uri.password = options.delete(:password) || uri.password
+ new_uri.path = (options[:database] && "/" << options.delete(:database)) || uri.path
+ new_uri.port = options.delete(:port) || uri.port
+
+ stropt = options.inject({}) { |h, (k, v)| h[k.to_s] = v; h }
+ all_options = Hash[ *(uri.query ? uri.query.split(/[&=]/) : []) ].merge!(stropt)
+ query = all_options.to_a.map { |pair| pair.join('=') }.join('&')
+
+ new_uri.query = query unless query.empty?
+
+ new_uri
+ end
end # class DoAdapter
end # module Adapters
@@ -17,18 +17,6 @@ def quote_table_name(table_name)
def quote_column_name(column_name)
"`#{column_name}`"
end
-
- def rewrite_uri(uri, options)
- new_uri = uri.dup
- new_uri.host = options[:host] || uri.host
- new_uri.user = options[:user] || uri.user
- new_uri.password = options[:password] || uri.password
- new_uri.path = (options[:database] && "/" << options[:database]) || uri.path
- new_uri.port = options[:port] || uri.port
- new_uri.query = (options[:socket] && "socket=#{options[:socket]}") || uri.query
-
- new_uri
- end
end # class MysqlAdapter
end # module Adapters
@@ -20,13 +20,6 @@ def create_connection
return connnection
end
- def rewrite_uri(uri, options)
- new_uri = uri.dup
- new_uri.path = options[:path] || uri.path
-
- new_uri
- end
-
end # class Sqlite3Adapter
end # module Adapters
@@ -43,7 +43,8 @@ def self.setup(name, uri, options = {})
end
end
- adapter = Adapters::const_get(DataMapper::Inflection.classify(uri.scheme) + "Adapter").new(name, uri)
+ adapter = Adapters::const_get(DataMapper::Inflection.classify(uri.scheme) + "Adapter").
+ new(name, uri, options)
Repository.adapters[name] = adapter
end
@@ -2,8 +2,8 @@
describe "a DataMapper Adapter", :shared => true do
it "should initialize the connection uri" do
- new_adapter = @adapter.class.new(:default, 'some://uri/string')
- new_adapter.instance_variable_get('@uri').should == 'some://uri/string'
+ new_adapter = @adapter.class.new(:default, URI.parse('some://uri/string'))
+ new_adapter.instance_variable_get('@uri').to_s.should == URI.parse('some://uri/string').to_s
end
%w{create read update delete read_one read_set delete_set} .each do |meth|
@@ -7,7 +7,7 @@
describe DataMapper::Adapters::DataObjectsAdapter do
before do
- @adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, 'mock::/localhost')
+ @adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, URI.parse('mock://localhost'))
end
it_should_behave_like 'a DataMapper Adapter'
@@ -55,8 +55,8 @@
describe '#query' do
before do
@mock_reader = mock('Reader', :fields => ['id', 'UserName', 'AGE'],
- :values => [1, 'rando', 27],
- :close => true)
+ :values => [1, 'rando', 27],
+ :close => true)
@mock_command = mock('Command', :execute_reader => @mock_reader)
@mock_db = mock('DB Connection', :create_command => @mock_command, :close => true)
@@ -139,7 +139,7 @@
describe DataMapper::Adapters::DataObjectsAdapter::SQL, "creating, reading, updating, deleting statements" do
before do
- @adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, 'mock::/localhost')
+ @adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, URI.parse('mock://localhost'))
class Cheese
include DataMapper::Resource
@@ -246,7 +246,6 @@ class LittleBox
end
describe "#read_statement (without lazy attributes)" do
-
it 'should generate a SQL statement for a serial Key' do
@adapter.read_statement(Cheese, [1]).should == <<-EOS.compress_lines
SELECT "id", "name", "color" FROM "cheeses" WHERE "id" = ?
@@ -259,4 +258,33 @@ class LittleBox
EOS
end
end
+
+ describe 'URI Rewriting' do
+ before do
+ @uri = URI.parse("mysql://localhost/over_nine_thousand?socket=/tmp/da.sock")
+ end
+
+ it 'should override the path when the option is passed' do
+ options = {
+ :host => 'davidleal.com',
+ :user => 'me',
+ :password => 'mypass',
+ :port => 5000,
+ :database => 'you_can_call_me_al',
+ :socket => 'nosock',
+ :opt => 'whoa'
+ }
+
+ adapter = DataMapper::Adapters::DataObjectsAdapter.allocate
+ adapter.rewrite_uri(@uri, options).should ==
+ URI.parse("mysql://me:mypass@davidleal.com:5000/you_can_call_me_al?socket=nosock&opt=whoa")
+ end
+
+ it 'should accept the uri when no overrides exist' do
+ adapter = DataMapper::Adapters::DataObjectsAdapter.allocate
+ uri = @uri.dup
+ adapter.rewrite_uri(uri, {}).should == @uri
+ end
+ end
end
+
@@ -8,30 +8,6 @@
DataMapper.setup(:mysql, "mysql://localhost/dm_integration_test")
- describe DataMapper::Adapters::MysqlAdapter do
- before do
- @uri = URI.parse("mysql://localhost/over_nine_thousand&socket=/tmp/da.sock")
- end
-
- it 'should override the path when the option is passed' do
-
- options = {
- :host => 'davidleal.com',
- :user => 'me',
- :password => 'mypass',
- :port => 5000,
- :database => 'you_can_call_me_all'
- }
-
- adapter = DataMapper::Adapters::MysqlAdapter.new(:mock, @uri, options)
- adapter.instance_variable_get("@uri").should == URI.parse("mysql://me:mypass@davidleal.com:5000/you_can_call_me_all")
- end
-
- it 'should accept the uri when no overrides exist' do
- adapter = DataMapper::Adapters::MysqlAdapter.new(:mock, @uri)
- adapter.instance_variable_get("@uri").should == @uri
- end
- end
rescue LoadError => e
describe 'do_mysql' do
it 'should be required' do
@@ -8,22 +8,6 @@
DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
- describe DataMapper::Adapters::Sqlite3Adapter do
- before do
- @uri = URI.parse("sqlite3:///test.db")
- end
-
- it 'should override the path when the option is passed' do
- adapter = DataMapper::Adapters::Sqlite3Adapter.new(:mock, @uri, { :path => '/test2.db' })
- adapter.instance_variable_get("@uri").should == URI.parse("sqlite3:///test2.db")
- end
-
- it 'should accept the uri when no overrides exist' do
- adapter = DataMapper::Adapters::Sqlite3Adapter.new(:mock, @uri)
- adapter.instance_variable_get("@uri").should == @uri
- end
- end
-
describe DataMapper::Adapters::DataObjectsAdapter do
describe "reading & writing a database" do

0 comments on commit 7edfb19

Please sign in to comment.