|
1 |
| -module PuppetX::Sqlserver |
2 |
| - CONNECTION_CLOSED = 0 |
3 |
| - |
4 |
| - class SqlConnection # rubocop:disable Style/Documentation |
5 |
| - def open_and_run_command(query, config) |
6 |
| - begin |
7 |
| - open(config) |
8 |
| - execute(query) |
9 |
| - rescue win32_exception => e |
10 |
| - return ResultOutput.new(true, e.message, @connection) |
11 |
| - ensure |
12 |
| - close |
| 1 | +module PuppetX # rubocop:disable Style/ClassAndModuleChildren |
| 2 | + module Sqlserver |
| 3 | + CONNECTION_CLOSED = 0 |
| 4 | + |
| 5 | + class SqlConnection # rubocop:disable Style/Documentation |
| 6 | + def open_and_run_command(query, config) |
| 7 | + begin |
| 8 | + open(config) |
| 9 | + execute(query) |
| 10 | + rescue win32_exception => e |
| 11 | + return ResultOutput.new(true, e.message, @connection) |
| 12 | + ensure |
| 13 | + close |
| 14 | + end |
| 15 | + |
| 16 | + ResultOutput.new(false, nil, @connection) |
13 | 17 | end
|
14 | 18 |
|
15 |
| - ResultOutput.new(false, nil, @connection) |
16 |
| - end |
17 |
| - |
18 |
| - private |
19 |
| - |
20 |
| - def connection |
21 |
| - @connection ||= create_connection |
22 |
| - end |
| 19 | + private |
23 | 20 |
|
24 |
| - def open(config) |
25 |
| - connection_string = get_connection_string(config) |
26 |
| - connection.Open(connection_string) if connection_closed? |
27 |
| - end |
28 |
| - |
29 |
| - def get_connection_string(config) |
30 |
| - params = { |
31 |
| - 'Provider' => 'SQLNCLI11', |
32 |
| - 'Initial Catalog' => config[:database] || 'master', |
33 |
| - 'Application Name' => 'Puppet', |
34 |
| - 'Data Source' => '.', |
35 |
| - 'DataTypeComptibility' => 80, |
36 |
| - } |
37 |
| - |
38 |
| - admin_user = config[:admin_user] || '' |
39 |
| - admin_pass = config[:admin_pass] || '' |
40 |
| - |
41 |
| - if config[:admin_login_type] == 'WINDOWS_LOGIN' |
42 |
| - # Windows based authentication |
43 |
| - raise ArgumentError, _('admin_user must be empty or nil') unless admin_user == '' |
44 |
| - raise ArgumentError, _('admin_pass must be empty or nil') unless admin_pass == '' |
45 |
| - params.store('Integrated Security', 'SSPI') |
46 |
| - else |
47 |
| - # SQL Server based authentication |
48 |
| - raise ArgumentError, _('admin_user must not be empty or nil') unless admin_user != '' |
49 |
| - raise ArgumentError, _('admin_pass must not be empty or nil') unless admin_pass != '' |
50 |
| - params.store('User ID', admin_user) |
51 |
| - params.store('Password', admin_pass) |
| 21 | + def connection |
| 22 | + @connection ||= create_connection |
52 | 23 | end
|
53 | 24 |
|
54 |
| - if !config[:instance_name].nil? && config[:instance_name] !~ %r{^MSSQLSERVER$} |
55 |
| - params['Data Source'] = ".\\#{config[:instance_name]}" |
| 25 | + def open(config) |
| 26 | + connection_string = get_connection_string(config) |
| 27 | + connection.Open(connection_string) if connection_closed? |
56 | 28 | end
|
57 | 29 |
|
58 |
| - params.map { |k, v| "#{k}=#{v}" }.join(';') |
59 |
| - end |
| 30 | + def get_connection_string(config) |
| 31 | + params = { |
| 32 | + 'Provider' => 'SQLNCLI11', |
| 33 | + 'Initial Catalog' => config[:database] || 'master', |
| 34 | + 'Application Name' => 'Puppet', |
| 35 | + 'Data Source' => '.', |
| 36 | + 'DataTypeComptibility' => 80, |
| 37 | + } |
| 38 | + |
| 39 | + admin_user = config[:admin_user] || '' |
| 40 | + admin_pass = config[:admin_pass] || '' |
| 41 | + |
| 42 | + if config[:admin_login_type] == 'WINDOWS_LOGIN' |
| 43 | + # Windows based authentication |
| 44 | + raise ArgumentError, _('admin_user must be empty or nil') unless admin_user == '' |
| 45 | + raise ArgumentError, _('admin_pass must be empty or nil') unless admin_pass == '' |
| 46 | + params.store('Integrated Security', 'SSPI') |
| 47 | + else |
| 48 | + # SQL Server based authentication |
| 49 | + raise ArgumentError, _('admin_user must not be empty or nil') unless admin_user != '' |
| 50 | + raise ArgumentError, _('admin_pass must not be empty or nil') unless admin_pass != '' |
| 51 | + params.store('User ID', admin_user) |
| 52 | + params.store('Password', admin_pass) |
| 53 | + end |
| 54 | + |
| 55 | + if !config[:instance_name].nil? && config[:instance_name] !~ %r{^MSSQLSERVER$} |
| 56 | + params['Data Source'] = ".\\#{config[:instance_name]}" |
| 57 | + end |
| 58 | + |
| 59 | + params.map { |k, v| "#{k}=#{v}" }.join(';') |
| 60 | + end |
60 | 61 |
|
61 |
| - def close |
62 |
| - connection.Close unless connection_closed? |
63 |
| - rescue win32_exception # rubocop:disable Lint/HandleExceptions |
64 |
| - end |
| 62 | + def close |
| 63 | + connection.Close unless connection_closed? |
| 64 | + rescue win32_exception # rubocop:disable Lint/HandleExceptions |
| 65 | + end |
65 | 66 |
|
66 |
| - def connection_closed? |
67 |
| - connection.State == CONNECTION_CLOSED |
68 |
| - end |
| 67 | + def connection_closed? |
| 68 | + connection.State == CONNECTION_CLOSED |
| 69 | + end |
69 | 70 |
|
70 |
| - def create_connection |
71 |
| - require 'win32ole' |
72 |
| - WIN32OLE.new('ADODB.Connection') |
73 |
| - end |
| 71 | + def create_connection |
| 72 | + require 'win32ole' |
| 73 | + WIN32OLE.new('ADODB.Connection') |
| 74 | + end |
74 | 75 |
|
75 |
| - def execute(sql) |
76 |
| - connection.Execute(sql, nil, nil) |
77 |
| - end |
| 76 | + def execute(sql) |
| 77 | + connection.Execute(sql, nil, nil) |
| 78 | + end |
78 | 79 |
|
79 |
| - def parse_column_names(result) |
80 |
| - result.Fields.extend(Enumerable).map(&:Name) |
81 |
| - end |
| 80 | + def parse_column_names(result) |
| 81 | + result.Fields.extend(Enumerable).map(&:Name) |
| 82 | + end |
82 | 83 |
|
83 |
| - # having as a method instead of hard coded allows us to stub and test outside of Windows |
84 |
| - def win32_exception |
85 |
| - ::WIN32OLERuntimeError |
| 84 | + # having as a method instead of hard coded allows us to stub and test outside of Windows |
| 85 | + def win32_exception |
| 86 | + ::WIN32OLERuntimeError |
| 87 | + end |
86 | 88 | end
|
87 |
| - end |
88 | 89 |
|
89 |
| - class ResultOutput # rubocop:disable Style/Documentation |
90 |
| - attr_reader :exitstatus, :error_message |
| 90 | + class ResultOutput # rubocop:disable Style/Documentation |
| 91 | + attr_reader :exitstatus, :error_message |
91 | 92 |
|
92 |
| - def initialize(has_errors, error_message, connection) |
93 |
| - @exitstatus = has_errors ? 1 : 0 |
| 93 | + def initialize(has_errors, error_message, connection) |
| 94 | + @exitstatus = has_errors ? 1 : 0 |
94 | 95 |
|
95 |
| - @error_message = extract_messages(connection) || error_message |
96 |
| - end |
| 96 | + @error_message = extract_messages(connection) || error_message |
| 97 | + end |
97 | 98 |
|
98 |
| - def extract_messages(connection) |
99 |
| - return nil if connection.nil? || connection.Errors.count.zero? |
| 99 | + def extract_messages(connection) |
| 100 | + return nil if connection.nil? || connection.Errors.count.zero? |
100 | 101 |
|
101 |
| - error_count = connection.Errors.count - 1 |
| 102 | + error_count = connection.Errors.count - 1 |
102 | 103 |
|
103 |
| - ((0..error_count).map { |i| connection.Errors(i).Description.to_s }).join("\n") |
104 |
| - end |
| 104 | + ((0..error_count).map { |i| connection.Errors(i).Description.to_s }).join("\n") |
| 105 | + end |
105 | 106 |
|
106 |
| - def has_errors # rubocop:disable Style/PredicateName |
107 |
| - @exitstatus != 0 |
| 107 | + def has_errors # rubocop:disable Style/PredicateName |
| 108 | + @exitstatus != 0 |
| 109 | + end |
108 | 110 | end
|
109 | 111 | end
|
110 | 112 | end
|
0 commit comments