class SqlPostgres::Delete

This class creates and executes an SQL delete statement.

Public Class Methods

new(table, connection = Connection.default) click to toggle source

Create a delete statement

table

The table name

connection

If supplied, the connection to use. If not supplied, use the default.

# File lib/sqlpostgres/Delete.rb, line 15
def initialize(table, connection = Connection.default)
  @table = table
  @connection = connection
  @where = []
end

Public Instance Methods

exec(connection = @connection) click to toggle source

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.

# File lib/sqlpostgres/Delete.rb, line 45
def exec(connection = @connection)
  connection.exec(statement)
end
statement() click to toggle source

Return the SQL statement

# File lib/sqlpostgres/Delete.rb, line 34
def statement
  "delete from #{@table}#{where_clause}"
end
where(expression) click to toggle source

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

# File lib/sqlpostgres/Delete.rb, line 28
def where(expression)
  @where << Translate.substitute_values(expression)
end

Private Instance Methods

where_clause() click to toggle source
# File lib/sqlpostgres/Delete.rb, line 51
def where_clause
  if @where.empty?
    ""
  else
    " where " + @where.join(' and ')
  end
end