Module: PLSQL::SQLStatements
- Included in:
- Schema
- Defined in:
- lib/plsql/sql_statements.rb
Instance Method Summary collapse
-
#commit ⇒ Object
Execute COMMIT in current database session.
-
#execute(*args) ⇒ Object
Execute SQL statement.
-
#rollback ⇒ Object
Execute ROLLBACK in current database session.
-
#rollback_to(name) ⇒ Object
Roll back changes to specified savepoint (that was created using
savepoint
method) Use beforehand. -
#savepoint(name) ⇒ Object
Create SAVEPOINT with specified name.
-
#select(*args) ⇒ Object
Select :first or :all values.
-
#select_all(sql, *bindvars, &block) ⇒ Object
Select all rows as array or values (without column names).
-
#select_first(sql, *bindvars) ⇒ Object
Select first row as array or values (without column names).
-
#select_one(sql, *bindvars) ⇒ Object
Select one value (use if only one row with one value is selected).
Instance Method Details
#commit ⇒ Object
Execute COMMIT in current database session. Use beforehand
plsql.connection.autocommit = false
to turn off automatic commits after each statement.
50 51 52 |
# File 'lib/plsql/sql_statements.rb', line 50 def commit @connection.commit end |
#execute(*args) ⇒ Object
Execute SQL statement. Example:
plsql.execute "DROP TABLE employees"
40 41 42 |
# File 'lib/plsql/sql_statements.rb', line 40 def execute(*args) @connection.exec(*args) end |
#rollback ⇒ Object
Execute ROLLBACK in current database session. Use beforehand
plsql.connection.autocommit = false
to turn off automatic commits after each statement.
60 61 62 |
# File 'lib/plsql/sql_statements.rb', line 60 def rollback @connection.rollback end |
#rollback_to(name) ⇒ Object
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.
81 82 83 |
# File 'lib/plsql/sql_statements.rb', line 81 def rollback_to(name) execute "ROLLBACK TO #{name}" end |
#savepoint(name) ⇒ Object
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.
71 72 73 |
# File 'lib/plsql/sql_statements.rb', line 71 def savepoint(name) execute "SAVEPOINT #{name}" end |
#select(*args) ⇒ Object
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"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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) ⇒ Object
Select all rows as array or values (without column names)
9 10 11 |
# 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) ⇒ Object
Select first row as array or values (without column names)
4 5 6 |
# File 'lib/plsql/sql_statements.rb', line 4 def select_first(sql, *bindvars) @connection.select_first(sql, *bindvars) end |
#select_one(sql, *bindvars) ⇒ Object
Select one value (use if only one row with one value is selected)
14 15 16 |
# File 'lib/plsql/sql_statements.rb', line 14 def select_one(sql, *bindvars) (row = @connection.select_first(sql, *bindvars)) && row[0] end |