Class: Mxrb::Runtime::SqlServerDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/runtime/sql_server_database.rb

Overview

Read-only query-plan connection to an explicitly configured SQL Server deployment. Credentials are passed through SQLCMDPASSWORD, never argv.

Constant Summary collapse

MUTATING_SQL =

rubocop:disable Metrics/ClassLength

/\b(?:ALTER|CREATE|DELETE|DROP|EXEC(?:UTE)?|INSERT|INTO|MERGE|TRUNCATE|UPDATE)\b/i

Instance Method Summary collapse

Constructor Details

#initialize(server:, database:, user:, password: ENV['MXRB_SQLSERVER_PASSWORD'], sqlcmd: 'sqlcmd', runner: nil) ⇒ SqlServerDatabase

rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
22
# File 'lib/mxrb/runtime/sql_server_database.rb', line 14

def initialize(server:, database:, user:, password: ENV['MXRB_SQLSERVER_PASSWORD'],
               sqlcmd: 'sqlcmd', runner: nil)
  @server = required(server, 'SQL Server host')
  @database = required(database, 'SQL Server database')
  @user = required(user, 'SQL Server user')
  @password = required(password, 'MXRB_SQLSERVER_PASSWORD')
  @sqlcmd = sqlcmd
  @runner = runner || method(:capture)
end

Instance Method Details

#explain(sql, analyze: false) ⇒ Object

rubocop:enable Metrics/ParameterLists



25
26
27
28
29
30
# File 'lib/mxrb/runtime/sql_server_database.rb', line 25

def explain(sql, analyze: false)
  statement = read_only_statement(sql)
  mode = analyze ? 'STATISTICS XML' : 'SHOWPLAN_XML'
  output = run!("SET #{mode} ON; #{statement}; SET #{mode} OFF;")
  Oql::SqlServerPlanAnalyzer.new.analyze(extract_showplan(output))
end

#workload(limit: 20) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/mxrb/runtime/sql_server_database.rb', line 32

def workload(limit: 20)
  maximum = Integer(limit)
  raise ArgumentError, 'workload limit must be between 1 and 1000' unless (1..1000).cover?(maximum)

  Oql::SqlServerWorkloadAnalyzer.new.analyze(
    query_rows: query_workload(maximum), table_rows: table_workload,
    index_rows: index_workload
  )
end