A small and fast library for building SQL queries from entity classes in a better way than regular string concatenation.
# | |
---|---|
1 | Get Started |
1.1 | Install Package |
1.2 | Import the namespace |
1.3 | Sample Code |
2 | How to use |
2.1 | Prepare your Entity |
2.1.1 | Table Attribute |
2.1.2 | IsPrimaryKey Attribute |
2.1.3 | IsNotInsertable Attribute |
2.1.4 | IsNotUpdatable Attribute |
2.2 | APIs |
3 | Dapper Example |
3.1 | Crud Operations |
3.1.1 | Create (Insert) |
3.1.1 | Read (Select) |
3.1.1 | Update |
3.1.1 | Delete |
⚡ Add package to your existing project from NuGet Package Manager.
⚡ Or download the source code from GitHub.
using Fastsql;
Check out the Fastql Sample Project with Unit of Work in Repository Pattern using Dapper.
Define a FastqlBuilder variable in your database related class. TEntity should be your poco to generate Insert and Update queries.
⚡ This class fits perfect to Repository Pattern with Dapper Micro ORM.
private FastqlBuilder<TEntity> fastql;
Constructor needs an object to handle attributes of the demanded class at runtime. If you don't have any at design time, you can initialize your fastql like this:
fastql = new FastqlBuilder<TEntity>((TEntity)Activator.CreateInstance(typeof(TEntity)));
(Activator is in namespace System in assembly System.Runtime.dll)
Fastql has 4 attributes to handle your queries.
Define your table and schema name. If you don't set your schema, it'll use dbo as default.
[Table("Customer", "Sales")]
public class Customer
{
It will be rendered like [Sales].[Customer] for your query.
If your database table has PK and auto increment, you should define this attribute to your PK field. If your field has this key, your field won't be used in Insert or Update query.
[Table("Customer", "Sales")]
public class Customer
{
[IsPrimaryKey]
public int Id { get; set; }
}
Fields have this attribute won't be placed in your Insert query. You don't need to define this key for your PK field.
[IsNotInsertable]
public DateTime UpdatedOn { get; set; }
Fields have this attribute won't be placed in your Update query. You don't need to define this key for your PK field.
[IsNotUpdatable]
public DateTime CreatedOn { get; set; }
⚡ TableName
⚡ InsertQuery
⚡ UpdateQuery
⚡Fastql is a great extension for Dapper Micro ORM. You can handle all of your CRUD operations easily with ⚡Fastql.
InsertQuery() function returns you the insert query based on your decisions.
Connection.Execute(
fastql.InsertQuery(),
param: entity,
transaction: Transaction
);
TableName() function returns you the schema and table for ready-to-use in select query.
Connection.Query<TEntity>(
$"SELECT * FROM {fastql.TableName()} WHERE Id=@Id",
param: new { Id = id },
transaction: Transaction
);
UpdateQuery(TEntity,string) returns you the update query with parameter tags added. You can bind your entity to query as parameter. Where string can include data or you can set your param tag to use it. (For.ex.) "Id=@Id" or $"Id={id}"
Connection.Execute(
fastql.UpdateQuery(entity, where),
param: entity,
transaction: Transaction
);
TableName() function can be used for delete operation too. You can handle your where condition with your way.
Connection.Execute(
$"DELETE FROM {fastql.TableName()} WHERE {where}",
// param: entity,
transaction: Transaction