Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(DO NOT MERGE)(MODULES-2312) Use sp_executesql to execute T-SQL #127

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/puppet/property/sqlserver_tsql.rb
Expand Up @@ -3,9 +3,12 @@
class Puppet::Property::SqlserverTsql < Puppet::Property
desc 'TSQL property that we are going to wrap with a try catch'
munge do |value|
quoted_value = value.gsub('\'', '\'\'')
erb_template = <<-TEMPLATE
BEGIN TRY
#{value}
DECLARE @sql_text as NVARCHAR(max);
SET @sql_text = N'#{quoted_value}'
EXECUTE sp_executesql @sql_text;
END TRY
BEGIN CATCH
DECLARE @msg as VARCHAR(max);
Expand Down
9 changes: 7 additions & 2 deletions spec/unit/puppet/property/tsql_spec.rb
Expand Up @@ -14,8 +14,13 @@
it 'should munge value to have begin and end try' do
@node[:command] = 'function foo'
@node[:onlyif] = 'exec bar'
expect(@node[:onlyif]).to match(/BEGIN TRY\n\s+exec bar\nEND TRY/)
expect(@node[:command]).to match(/BEGIN TRY\n\s+function foo\nEND TRY/)
expect(@node[:onlyif]).to match(/BEGIN TRY\n\s+DECLARE @sql_text as NVARCHAR\(max\);\n\s+SET @sql_text = N'exec bar'\n\s+EXECUTE sp_executesql @sql_text;\nEND TRY/)
expect(@node[:command]).to match(/BEGIN TRY\n\s+DECLARE @sql_text as NVARCHAR\(max\);\n\s+SET @sql_text = N'function foo'\n\s+EXECUTE sp_executesql @sql_text;\nEND TRY/)
end

it 'should properly escape single quotes in queries' do
@node[:command] = 'SELECT \'FOO\''
expect(@node[:command]).to match(/SET @sql_text = N'SELECT \'\'FOO\'\'/)
end

end
5 changes: 4 additions & 1 deletion spec/unit/puppet/provider/sqlserver__tsql_spec.rb
Expand Up @@ -22,9 +22,12 @@ def stub_get_instance_config(config)
end

def gen_query(query)
quoted_query = query.gsub('\'', '\'\'')
<<-PP
BEGIN TRY
#{query}
DECLARE @sql_text as NVARCHAR(max);
SET @sql_text = N'#{quoted_query}'
EXECUTE sp_executesql @sql_text;
END TRY
BEGIN CATCH
DECLARE @msg as VARCHAR(max);
Expand Down