Skip to content

Commit ca24c1c

Browse files
committed
(maint) Refactor connection string building
- Note that Persist Security Info = False was removed from the Connection String since it's already the default.
1 parent 0e63dde commit ca24c1c

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/puppet_x/sqlserver/sql_connection.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ def open(config)
2828
end
2929

3030
def get_connection_string(config)
31-
config = {'database' => 'master'}.merge(config)
32-
# Open ADO connection to the SQL Server database
33-
connection_string = "Provider=SQLOLEDB.1;"
34-
connection_string << "Persist Security Info=False;"
35-
connection_string << "User ID=#{config['admin']};"
36-
connection_string << "password=#{config['pass']};"
37-
connection_string << "Initial Catalog=#{config['database']};"
38-
connection_string << "Application Name=Puppet;"
31+
params = {
32+
'Provider' => 'SQLOLEDB.1',
33+
'User ID' => config['admin'],
34+
'Password' => config['pass'],
35+
'Initial Catalog' => config['database'] || 'master',
36+
'Application Name' => 'Puppet',
37+
'Data Source' => 'localhost'
38+
}
3939
if config['instance'] !~ /^MSSQLSERVER$/
40-
connection_string << "Data Source=localhost\\#{config['instance']};"
41-
else
42-
connection_string << "Data Source=localhost;"
40+
params['Data Source'] = "localhost\\#{config['instance']};"
4341
end
42+
43+
params.map { |k, v| "#{k}=#{v}" }.join(';')
4444
end
4545

4646
def command(sql)

spec/unit/puppet_x/sql_connection_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def stub_no_errors
2222
before :each do
2323
stub_connection
2424
@connection.stubs(:State).returns(0)
25-
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;')
25+
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost')
2626
end
2727
it 'should not raise an error but populate has_errors with message' do
2828
subject.stubs(:win32_exception).returns(Exception)
@@ -48,11 +48,11 @@ def stub_no_errors
4848
@connection.stubs(:State).returns(0)
4949
end
5050
it 'should not add MSSQLSERVER to connection string' do
51-
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;')
51+
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost')
5252
subject.open_and_run_command('query', {'admin' => 'sa', 'pass' => 'Pupp3t1@', 'instance' => 'MSSQLSERVER'})
5353
end
5454
it 'should add a non default instance to connection string' do
55-
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=superuser;password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING;')
55+
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=superuser;Password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING')
5656
subject.open_and_run_command('query', {'admin' => 'superuser', 'pass' => 'puppetTested', 'instance' => 'LOGGING'})
5757
end
5858
end

0 commit comments

Comments
 (0)