zeybie / fastql

A small and fast library for building SQL queries from entity classes in a better way than regular string concatenation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

license NuGet NuGet


Here comes the query!

Overview

What is the ⚡Fastql?

A small and fast library for building SQL queries from entity classes in a better way than regular string concatenation.

Table of Contents

#
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

Get Started

Install Package

⚡ Add package to your existing project from NuGet Package Manager.
⚡ Or download the source code from GitHub.

Import the namespace

using Fastsql;

Sample Code

Check out the Fastql Sample Project with Unit of Work in Repository Pattern using Dapper.

How to use

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)

Prepare Your Entity

Fastql has 4 attributes to handle your queries.

Table Attribute

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.

IsPrimaryKey Attribute

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; }
    }

IsNotInsertable Attribute

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; }

IsNotUpdatable Attribute

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; }

APIs

⚡ TableName
⚡ InsertQuery
⚡ UpdateQuery

Dapper Example

⚡Fastql is a great extension for Dapper Micro ORM. You can handle all of your CRUD operations easily with ⚡Fastql.

CRUD Operations

Create (Insert)

InsertQuery() function returns you the insert query based on your decisions.

Connection.Execute(
                fastql.InsertQuery(),
                param: entity,
                transaction: Transaction
               );

Read (Select)

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
              );

Update

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
              );

Delete

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

About

A small and fast library for building SQL queries from entity classes in a better way than regular string concatenation.

License:MIT License


Languages

Language:C# 100.0%