File tree Expand file tree Collapse file tree 3 files changed +15
-4
lines changed Expand file tree Collapse file tree 3 files changed +15
-4
lines changed Original file line number Diff line number Diff line change 3
3
class Puppet ::Property ::SqlserverTsql < Puppet ::Property
4
4
desc 'TSQL property that we are going to wrap with a try catch'
5
5
munge do |value |
6
+ quoted_value = value . gsub ( '\'' , '\'\'' )
6
7
erb_template = <<-TEMPLATE
7
8
BEGIN TRY
8
- #{ value }
9
+ DECLARE @sql_text as NVARCHAR(max);
10
+ SET @sql_text = N'#{ quoted_value } '
11
+ EXECUTE sp_executesql @sql_text;
9
12
END TRY
10
13
BEGIN CATCH
11
14
DECLARE @msg as VARCHAR(max);
Original file line number Diff line number Diff line change 14
14
it 'should munge value to have begin and end try' do
15
15
@node [ :command ] = 'function foo'
16
16
@node [ :onlyif ] = 'exec bar'
17
- expect ( @node [ :onlyif ] ) . to match ( /BEGIN TRY\n \s +exec bar\n END TRY/ )
18
- expect ( @node [ :command ] ) . to match ( /BEGIN TRY\n \s +function foo\n END TRY/ )
17
+ 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;\n END TRY/ )
18
+ 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;\n END TRY/ )
19
+ end
20
+
21
+ it 'should properly escape single quotes in queries' do
22
+ @node [ :command ] = 'SELECT \'FOO\''
23
+ expect ( @node [ :command ] ) . to match ( /SET @sql_text = N'SELECT \' \' FOO\' \' / )
19
24
end
20
25
21
26
end
Original file line number Diff line number Diff line change @@ -22,9 +22,12 @@ def stub_get_instance_config(config)
22
22
end
23
23
24
24
def gen_query ( query )
25
+ quoted_query = query . gsub ( '\'' , '\'\'' )
25
26
<<-PP
26
27
BEGIN TRY
27
- #{ query }
28
+ DECLARE @sql_text as NVARCHAR(max);
29
+ SET @sql_text = N'#{ quoted_query } '
30
+ EXECUTE sp_executesql @sql_text;
28
31
END TRY
29
32
BEGIN CATCH
30
33
DECLARE @msg as VARCHAR(max);
You can’t perform that action at this time.
0 commit comments