Class: SqlPostgres::Delete
- Inherits:
-
Object
- Object
- SqlPostgres::Delete
- Defined in:
- lib/sqlpostgres/Delete.rb
Overview
This class creates and executes an SQL delete statement.
Instance Method Summary collapse
-
#exec(connection = @connection) ⇒ Object
Execute the delete statement.
-
#initialize(table, connection = Connection.default) ⇒ Delete
constructor
Create a delete statement.
-
#statement ⇒ Object
Return the SQL statement.
-
#where(expression) ⇒ Object
Add a “where” condition to this statement.
Constructor Details
#initialize(table, connection = Connection.default) ⇒ Delete
Create a delete statement
- table
-
The table name
- connection
-
If supplied, the connection to use. If not supplied, use the default.
15 16 17 18 19 |
# File 'lib/sqlpostgres/Delete.rb', line 15 def initialize(table, connection = Connection.default) @table = table @connection = connection @where = [] end |
Instance Method Details
#exec(connection = @connection) ⇒ Object
Execute the delete statement
- connection
-
If present, the connection to use. If nil, uses the connection passed to new, or if no connection was passed to new, uses the default connection.
45 46 47 |
# File 'lib/sqlpostgres/Delete.rb', line 45 def exec(connection = @connection) connection.exec(statement) end |
#statement ⇒ Object
Return the SQL statement
34 35 36 |
# File 'lib/sqlpostgres/Delete.rb', line 34 def statement "delete from #{@table}#{where_clause}" end |
#where(expression) ⇒ Object
Add a “where” condition to this statement.
- expression
-
The condition. Should be one of:
- A string
-
The expression
- An array
-
An expression converted using #substitute_values
28 29 30 |
# File 'lib/sqlpostgres/Delete.rb', line 28 def where(expression) @where << Translate.substitute_values(expression) end |