module PLSQL::SQLStatements

Public Instance Methods

commit() click to toggle source

Execute COMMIT in current database session. Use beforehand

plsql.connection.autocommit = false

to turn off automatic commits after each statement.

# File lib/plsql/sql_statements.rb, line 50
def commit
  @connection.commit
end
execute(*args) click to toggle source

Execute SQL statement. Example:

plsql.execute "DROP TABLE employees"
# File lib/plsql/sql_statements.rb, line 40
def execute(*args)
  @connection.exec(*args)
end
rollback() click to toggle source

Execute ROLLBACK in current database session. Use beforehand

plsql.connection.autocommit = false

to turn off automatic commits after each statement.

# File lib/plsql/sql_statements.rb, line 60
def rollback
  @connection.rollback
end
rollback_to(name) click to toggle source

Roll back changes to specified savepoint (that was created using savepoint method) Use beforehand

plsql.connection.autocommit = false

to turn off automatic commits after each statement.

# File lib/plsql/sql_statements.rb, line 81
def rollback_to(name)
  execute "ROLLBACK TO #{name}"
end
savepoint(name) click to toggle source

Create SAVEPOINT with specified name. Later use rollback_to method to roll changes back to specified savepoint. Use beforehand

plsql.connection.autocommit = false

to turn off automatic commits after each statement.

# File lib/plsql/sql_statements.rb, line 71
def savepoint(name)
  execute "SAVEPOINT #{name}"
end
select(*args) click to toggle source

Select :first or :all values. Examples:

plsql.select :first, "SELECT * FROM employees WHERE employee_id = :1", 1
plsql.select :all, "SELECT * FROM employees ORDER BY employee_id"
# File lib/plsql/sql_statements.rb, line 22
def select(*args)
  case args[0]
  when nil
    raise ArgumentError, "Not enough arguments"
  when :first
    args.shift
    @connection.select_hash_first(*args)
  when :all
    args.shift
    @connection.select_hash_all(*args)
  else
    @connection.select_hash_all(*args)
  end
end
select_all(sql, *bindvars, &block) click to toggle source

Select all rows as array or values (without column names)

# File lib/plsql/sql_statements.rb, line 9
def select_all(sql, *bindvars, &block)
  @connection.select_all(sql, *bindvars, &block)
end
select_first(sql, *bindvars) click to toggle source

Select first row as array or values (without column names)

# File lib/plsql/sql_statements.rb, line 4
def select_first(sql, *bindvars)
  @connection.select_first(sql, *bindvars)
end
select_one(sql, *bindvars) click to toggle source

Select one value (use if only one row with one value is selected)

# File lib/plsql/sql_statements.rb, line 14
def select_one(sql, *bindvars)
  (row = @connection.select_first(sql, *bindvars)) && row[0]
end