Deleting Records
The CQL::Delete class provides a structured and flexible way to build and execute SQL DELETE queries in your Crystal applications. This guide will help you understand how to create delete queries, apply conditions, and execute them to remove records from your database.
Key Features
Delete records from any table in a straightforward manner.
Filter records to delete using flexible
WHEREconditions.Return columns after deletion if needed.
Chainable syntax for clean and maintainable query building.
Real-World Example: Deleting a User Record
Let’s start with a simple example of deleting a user from the users table where the id is 1.
delete = CQL::Delete.new(schema)
.from(:users)
.where(id: 1)
.commitThis query deletes the record in the users table where id = 1.
Core Methods
The following section provides a breakdown of the key methods available in the CQL::Delete class and how to use them effectively.
1. from(table : Symbol)
from(table : Symbol)Purpose: Specifies the table from which records will be deleted.
Parameters:
table— A symbol representing the table name.Returns:
Deleteobject (for chaining).
Real-World Example: Specifying the Table
delete.from(:users)This sets the users table as the target for the delete operation.
2. where(**fields)
where(**fields)Purpose: Adds a WHERE clause to filter the records to be deleted.
Parameters:
fields— A key-value hash where keys represent column names and values represent the conditions to match.Returns:
Deleteobject (for chaining).
Real-World Example: Filtering by Conditions
delete
.from(:users)
.where(id: 1)This filters the query to only delete the user where id = 1.
3. where(&block)
where(&block)Purpose: Adds a WHERE clause using a block for more complex filtering conditions.
Parameters: A block that defines the filtering logic using a filter builder.
Returns:
Deleteobject (for chaining).
Real-World Example: Using a Block for Conditions
delete
.from(:users)
.where { |w| w.age < 30 }This deletes all users where the age is less than 30.
4. commit
commitPurpose: Executes the delete query and commits the changes to the database.
Returns: A
DB::Resultobject representing the result of the query execution.
Real-World Example: Committing the Delete
delete = CQL::Delete.new(schema)
.from(:users)
.where(id: 1)
.commitThis deletes the user from the users table where id = 1 and commits the change.
5. using(table : Symbol)
using(table : Symbol)Purpose: Adds a USING clause to the delete query, useful when deleting records based on conditions from another table.
Parameters:
table— A symbol representing the name of the table to use in theUSINGclause.Returns:
Deleteobject (for chaining).
Real-World Example: Using Another Table for Deletion
delete
.from(:users)
.using(:posts)
.where { |w| w.posts.user_id == w.users.id }This example deletes users where they are linked to posts based on the condition posts.user_id = users.id.
6. back(*columns : Symbol)
back(*columns : Symbol)Purpose: Specifies the columns to return after the delete operation.
Parameters:
columns— An array of symbols representing the columns to return.Returns:
Deleteobject (for chaining).
Real-World Example: Returning Columns After Deletion
delete
.from(:users)
.where(id: 1)
.back(:name, :email)
.commitThis deletes the user with id = 1 and returns the name and email of the deleted record.
7. to_sql(gen = @schema.gen)
to_sql(gen = @schema.gen)Purpose: Generates the SQL query and parameters required for the delete operation.
Parameters:
gen— The generator used for SQL generation (default: schema generator).Returns: A tuple containing the SQL query string and the parameters.
Real-World Example: Generating SQL for Deletion
delete = CQL::Delete.new(schema)
.from(:users)
.where(id: 1)
sql, params = delete.to_sql
puts sql # "DELETE FROM users WHERE id = $1"
puts params # [1]This generates the raw SQL query and its associated parameters without executing it.
Putting It All Together
Let’s combine multiple methods to handle a more advanced use case. Suppose you want to delete a user from the users table where they have no associated posts, and you want to return the deleted user’s name and email:
delete = CQL::Delete.new(schema)
result = delete
.from(:users)
.using(:posts)
.where { |w| w.posts.user_id == w.users.id && w.posts.id.nil? }
.back(:name, :email)
.commit
puts result # This returns the name and email of the deleted user(s).In this query:
We specify the
userstable as the target for deletion.We use the
poststable to filter users without any posts.We return the
nameandemailof the deleted user(s).
Conclusion
The CQL::Delete class provides an intuitive and powerful interface for deleting records in your Crystal applications. With chainable methods for setting conditions, joining tables, and selecting return columns, you can easily construct and execute delete queries with precision and clarity.
Whether you need to delete specific records or perform complex, condition-based deletions, the CQL::Delete class ensures that your queries are efficient and maintainable.
Last updated
Was this helpful?