Class rick_db.sql.Delete¶
Delete.__init__(dialect: SqlDialect = None)¶
Initialize a Delete() object, using a database dialect. If no dialect is provided, a default dialect will be used. Check SqlDialect for more details.
Delete.from_(table, schema=None):¶
Defines the target table name and schema for deletion. table can be a Record object.
Example:
# simple DELETE example
qry = Delete(PgSqlDialect()).from_('table')
# output: ('DELETE FROM "table"', [])
print(qry.assemble())
# DELETE w/ Record object
record = Publisher(name='some publisher name')
qry = Delete(PgSqlDialect()).from_(record)
# output: ('DELETE FROM "publisher"', [])
print(qry.assemble())
Delete.where(field, operator=None, value=None)¶
Adds a WHERE clause to the DELETE clause. It requires a mandatory field name and an operator, and allows an optional value. Clauses generated by multiple calls to this method are concatenated with AND.
Example:
# DELETE WHERE... common usage
qry = Delete(PgSqlDialect()).from_("table").where("id", "=", 7)
# output: ('DELETE FROM "table" WHERE "id" = %s', [7])
print(qry.assemble())
# DELETE WHERE... no value
qry = Delete(PgSqlDialect()).from_("table").where("id", "IS NOT NULL")
# output: ('DELETE FROM "table" WHERE "id" IS NOT NULL', [])
print(qry.assemble())
# DELETE WHERE... with multiple clauses
qry = (
Delete(PgSqlDialect())
.from_("table")
.where("id", "=", 7)
.where("name", "ILIKE", "john%")
)
# output: ('DELETE FROM "table" WHERE "id" = %s AND "name" ILIKE %s', [7, 'john%'])
print(qry.assemble())
Delete.orwhere(field, operator=None, value=None)¶
Adds a WHERE clause to the DELETE clause. It requires a mandatory field name and an operator, and allows an optional value. Clauses generated by multiple calls to this method are concatenated with OR.
Example:
qry = (
Delete(PgSqlDialect())
.from_("table")
.where("id", "=", 7)
.orwhere("name", "ILIKE", "john%")
)
# output: ('DELETE FROM "table" WHERE "id" = %s OR "name" ILIKE %s', [7, 'john%'])
print(qry.assemble())
Delete.assemble()¶
Assembles DELETE SQL string and returns a tuple with (sql_string, list_of_values). If an error occurs, SqlError is raised.
Example:
# simple DELETE example
qry = Delete(PgSqlDialect()).from_('table')
# output: ('DELETE FROM "table"', [])
print(qry.assemble())