Class: SQLCmd

Inherits:
Object
  • Object
show all
Includes:
Albacore::RunCommand, Albacore::Task
Defined in:
lib/albacore/sqlcmd.rb

Instance Attribute Summary collapse

Attributes included from Albacore::RunCommand

#command, #working_directory

Attributes included from Logging

#current_log_device, #logger

Instance Method Summary collapse

Methods included from Albacore::RunCommand

#get_command, #run_command

Methods included from AttrMethods

#attr_array, #attr_hash

Methods included from Albacore::Task

clean_dirname, create_rake_task, include_config, included

Methods included from UpdateAttributes

#<<, #update_attributes

Methods included from YAMLConfig

#configure, #load_config_by_task_name

Methods included from Logging

#create_logger, #log_device=, #log_level, #log_level=

Methods included from Failure

#fail_with_message

Constructor Details

#initializeSQLCmd

Returns a new instance of SQLCmd.



11
12
13
14
15
16
17
18
19
20
# File 'lib/albacore/sqlcmd.rb', line 11

def initialize
  @require_valid_command = false
  @scripts=[]
  @variables={}
  @trusted_connection = true
  @batch_abort = true
  @severity = nil
  super()
  update_attributes Albacore.configuration.sqlcmd.to_hash
end

Instance Attribute Details

#batch_abortObject

Returns the value of attribute batch_abort.



7
8
9
# File 'lib/albacore/sqlcmd.rb', line 7

def batch_abort
  @batch_abort
end

#databaseObject

Returns the value of attribute database.



7
8
9
# File 'lib/albacore/sqlcmd.rb', line 7

def database
  @database
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/albacore/sqlcmd.rb', line 7

def password
  @password
end

#serverObject

Returns the value of attribute server.



7
8
9
# File 'lib/albacore/sqlcmd.rb', line 7

def server
  @server
end

#severityObject

Returns the value of attribute severity.



7
8
9
# File 'lib/albacore/sqlcmd.rb', line 7

def severity
  @severity
end

#trusted_connectionObject

Returns the value of attribute trusted_connection.



7
8
9
# File 'lib/albacore/sqlcmd.rb', line 7

def trusted_connection
  @trusted_connection
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/albacore/sqlcmd.rb', line 7

def username
  @username
end

Instance Method Details

#build_parameter(param_name, param_value) ⇒ Object



70
71
72
# File 'lib/albacore/sqlcmd.rb', line 70

def build_parameter(param_name, param_value)
  "-#{param_name} \"#{param_value}\""
end

#build_script_listObject



66
67
68
# File 'lib/albacore/sqlcmd.rb', line 66

def build_script_list
  @scripts.map{|s| "-i \"#{s.strip}\""}.join(" ")
end

#build_variable_listObject



74
75
76
77
78
79
80
# File 'lib/albacore/sqlcmd.rb', line 74

def build_variable_list
  vars = []
  @variables.each do |k,v| 
    vars << "-v #{k}=#{v}"
  end
  vars.join(" ")
end

#check_commandObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/albacore/sqlcmd.rb', line 53

def check_command
  sql2012cmdPath = File.join(ENV['SystemDrive'],'program files','microsoft sql server','110','tools','binn', 'sqlcmd.exe')
  sql2008cmdPath = File.join(ENV['SystemDrive'],'program files','microsoft sql server','100','tools','binn', 'sqlcmd.exe')
  sql2005cmdPath = File.join(ENV['SystemDrive'],'program files','microsoft sql server','90','tools','binn', 'sqlcmd.exe')
  sql2012cmdPathx86 = File.join(ENV['SystemDrive'],'program files (x86)','microsoft sql server','110','tools','binn', 'sqlcmd.exe')
  sql2008cmdPathx86 = File.join(ENV['SystemDrive'],'program files (x86)','microsoft sql server','100','tools','binn', 'sqlcmd.exe')
  sql2005cmdPathx86 = File.join(ENV['SystemDrive'],'program files (x86)','microsoft sql server','90','tools','binn', 'sqlcmd.exe')
  @command = [sql2012cmdPath, sql2008cmdPath, sql2005cmdPath, sql2012cmdPathx86, sql2008cmdPathx86, sql2005cmdPathx86].select { |p| File.exist?(p) }.first
  return true if @command != nil
  fail_with_message 'SQLCmd.command cannot be nil.'
  return false
end

#executeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/albacore/sqlcmd.rb', line 22

def execute
  return unless check_command
  
  cmd_params=[]
  serverParam = @server.nil? ? build_parameter("S", ".") : build_parameter("S", @server)
  cmd_params << serverParam
  cmd_params << build_parameter("d", @database) unless @database.nil?
  cmd_params << get_authentication_params
  cmd_params << build_variable_list if @variables.length > 0
  cmd_params << get_batch_abort_param
  cmd_params << build_script_list if @scripts.length > 0
  cmd_params << build_parameter("V", @severity) unless @severity.nil?
  
  result = run_command "SQLCmd", cmd_params.join(" ")
  
  failure_msg = 'SQLCmd Failed. See Build Log For Detail.'
  fail_with_message failure_msg if !result
end

#get_authentication_paramsObject



45
46
47
48
49
50
51
# File 'lib/albacore/sqlcmd.rb', line 45

def get_authentication_params
  integratedParam = "-E" if @trusted_connection
  if ((!(@username.nil?)) and (!(@password.nil?)))
    integratedParam = build_parameter("U", @username) + " " + build_parameter("P", @password)
  end
  integratedParam
end

#get_batch_abort_paramObject



41
42
43
# File 'lib/albacore/sqlcmd.rb', line 41

def get_batch_abort_param
  "-b" if (@scripts.length > 1 && @batch_abort)
end