Class: Sneaql::Core::Commands::SneaqlIterateRecordset

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

Overview

iterates a recordset and runs the sql statement for each record

Instance Method Summary collapse

Methods inherited from SneaqlCommand

#arg_definition, #initialize, #valid_expression?, #valid_operator?, #valid_recordset?, #valid_symbol?, #valid_variable?

Constructor Details

This class inherits a constructor from Sneaql::Core::SneaqlCommand

Instance Method Details

#action(*args) ⇒ Object

Parameters:

  • args (*Array)

    parameters for recordset expression in the format



286
287
288
289
290
291
292
293
294
# File 'lib/sneaql_lib/core.rb', line 286

def action(*args)
  if args.size == 1
    iterate_all_records(*args)
  elsif ((args.size - 1) % 4) == 0
    iterate_records_conditionally(*args)
  end
rescue => e
  @exception_manager.pending_error = e
end

#iterate_all_records(recordset) ⇒ Object

Parameters:

  • recordset (String)

    recordset to iterate



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/sneaql_lib/core.rb', line 323

def iterate_all_records(recordset)
  raise Sneaql::Exceptions::RecordsetDoesNotExistError unless @recordset_manager.recordset.has_key?(recordset)
  @logger.info "iterating recordset #{recordset}..."
  @recordset_manager.recordset[recordset].each_with_index do |i, n|
    @logger.debug("#{n + 1} of #{recordset}: #{i}")
    tmp = @statement
    i.keys.each { |k| tmp = tmp.gsub(":#{recordset}.#{k}", i[k].to_s) }
    @expression_handler.set_session_variable(
      'last_statement_rows_affected',
      rows_affected_current_statement(tmp)
    )
  end
rescue => e
  @exception_manager.pending_error = e
  raise e
end

#iterate_records_conditionally(*args) ⇒ Object

Parameters:

  • args (*Array)

    all the arguments passed to the calling function



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/sneaql_lib/core.rb', line 341

def iterate_records_conditionally(*args)
  recordset = args.to_a[0]
  @logger.info "iterating recordset #{recordset}..."
  conditions = @recordset_manager.parse_recordset_expression(args.to_a)
  @recordset_manager.recordset[recordset].each_with_index do |i, n|
    @logger.debug("#{n + 1} of #{recordset}: #{i}")
    next unless @recordset_manager.evaluate_expression_against_record(i, conditions)
    tmp = @statement
    i.keys.each { |k| tmp = tmp.gsub(":#{recordset}.#{k}", i[k].to_s) }
    @expression_handler.set_session_variable(
      'last_statement_rows_affected',
      rows_affected_current_statement(tmp)
    )
  end
rescue => e
  @exception_manager.pending_error = e
  raise e
end

#rows_affected_current_statement(stmt) ⇒ Fixnum

Returns rows affected by the SQL statement.

Returns:

  • (Fixnum)

    rows affected by the SQL statement



361
362
363
364
365
366
367
# File 'lib/sneaql_lib/core.rb', line 361

def rows_affected_current_statement(stmt)
  JDBCHelpers::Execute.new(
    @jdbc_connection,
    stmt,
    @logger
  ).rows_affected
end

#validate_args(args) ⇒ Boolean

custom method for argument validation

Parameters:

  • args (Array)

Returns:

  • (Boolean)


299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/sneaql_lib/core.rb', line 299

def validate_args(args)
  if args.size == 1
    return valid_recordset?(args[0])
  elsif ((args.size - 1) % 4) == 0
    valid = []
    valid << valid_recordset?(args[0])
    args[1..args.length - 1].each_slice(4) do |s|
      if ['include', 'exclude'].include?(s[0])
        valid << true
      else
        valid << false
      end
      # field names have the same rules as recordset names for now
      valid << valid_recordset?(s[1])
      valid << valid_operator?(s[2])
      valid << valid_expression?(s[3])
    end
    !valid.include?(false)
  else
    return false
  end
end