Class: Sneaql::Core::SneaqlCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/sneaql_lib/base.rb

Overview

Base class for SneaQL command tags

Instance Method Summary collapse

Constructor Details

#initialize(jdbc_connection, expression_handler, exception_manager, recordset_manager, statement, logger = nil) ⇒ SneaqlCommand

this is the base object for a sneaql command subclass this and override the action method

Parameters:

  • jdbc_connection (Object)

    JDBC connection object to database

  • expression_handler (Sneaql::Core::ExpressionHandler)
  • recordset_manager (Sneaql::Core::RecordsetManager)
  • statement (String)

    SQL statement provided in body, with all variables resolved

  • logger (Logger) (defaults to: nil)

    object otherwise will default to new Logger



77
78
79
80
81
82
83
84
85
# File 'lib/sneaql_lib/base.rb', line 77

def initialize(jdbc_connection, expression_handler, exception_manager, recordset_manager, statement, logger = nil)
  @logger = logger ? logger : Logger.new(STDOUT)

  @jdbc_connection = jdbc_connection
  @expression_handler = expression_handler
  @exception_manager = exception_manager
  @statement = statement
  @recordset_manager = recordset_manager
end

Instance Method Details

#actionObject

override this method with the actual code for your command



88
89
90
# File 'lib/sneaql_lib/base.rb', line 88

def action
  nil
end

#arg_definitionObject

override with an array in the form [:expression, :operator]



93
94
95
# File 'lib/sneaql_lib/base.rb', line 93

def arg_definition
  []
end

#valid_expression?(a) ⇒ Boolean

validates that the value is a valid expression

Parameters:

  • a (String, Float, Fixnum)

    value to test

Returns:

  • (Boolean)


140
141
142
143
144
145
146
# File 'lib/sneaql_lib/base.rb', line 140

def valid_expression?(a)
  @expression_handler.valid_expression_reference?(a.to_s.strip)
rescue => e
  @logger.error(e.message)
  e.backtrace { |r| @logger.error(r) }
  raise e
end

#valid_operator?(a) ⇒ Boolean

validates that the value is a valid operator

Parameters:

  • a (String)

    value to test

Returns:

  • (Boolean)


151
152
153
154
155
156
157
# File 'lib/sneaql_lib/base.rb', line 151

def valid_operator?(a)
  @expression_handler.valid_operators.include?(a.to_s.strip)
rescue => e
  @logger.error(e.message)
  e.backtrace { |r| @logger.error(r) }
  raise e
end

#valid_recordset?(a) ⇒ Boolean

validates that the value is a valid recordset name

Parameters:

  • a (String)

    value to test

Returns:

  • (Boolean)


162
163
164
165
166
167
168
# File 'lib/sneaql_lib/base.rb', line 162

def valid_recordset?(a)
  @recordset_manager.valid_recordset_name?(a.to_s.strip)
rescue => e
  @logger.error(e.message)
  e.backtrace { |r| @logger.error(r) }
  raise e
end

#valid_symbol?(a) ⇒ Boolean

note that this is not a ruby symbol, but a sneaql symbol should be a contigiuous string consisting of only letters, digits and underscores

Parameters:

  • a (String)

    value to test

Returns:

  • (Boolean)


175
176
177
178
179
180
181
182
# File 'lib/sneaql_lib/base.rb', line 175

def valid_symbol?(a)
  return true if a.match(/^\w+$/)
  false
rescue => e
  @logger.error(e.message)
  e.backtrace { |r| @logger.error(r) }
  raise e
end

#valid_variable?(a) ⇒ Boolean

validates that the value is a valid variable name

Parameters:

  • a (String)

    value to test

Returns:

  • (Boolean)


129
130
131
132
133
134
135
# File 'lib/sneaql_lib/base.rb', line 129

def valid_variable?(a)
  @expression_handler.valid_session_variable_name?(a.to_s.strip)
rescue => e
  @logger.error(e.message)
  e.backtrace { |r| @logger.error(r) }
  raise e
end

#validate_args(args) ⇒ Boolean

override this if you have a complex tag structure

Parameters:

  • args (Array)

    argument array to validate

Returns:

  • (Boolean)

    true if all arguments are valid



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sneaql_lib/base.rb', line 100

def validate_args(args)
  return false if args.length != arg_definition.length
  return true if (arg_definition == []) and (args == [])
  valid = []
  args.each_with_index do |a, i|
    case
    when arg_definition[i] == :variable then
      valid << valid_variable?(a)
    when arg_definition[i] == :expression then
      valid << valid_expression?(a)
    when arg_definition[i] == :operator then
      valid << valid_operator?(a)
    when arg_definition[i] == :recordset then
      valid << valid_recordset?(a)
    when arg_definition[i] == :symbol then
      valid << valid_symbol?(a)
    else valid << false end
  end
  @logger.debug("arg validation results: #{valid}")
  !valid.include?(false)
rescue => e
  @logger.error(e.message)
  e.backtrace { |r| @logger.error(r) }
  raise e
end