Search code examples
c#.netado.netpetapocomicro-orm

What is the use of sql builder in PetaPoco?


I have this code in petapoco

public List<T> Fetch<T>(Sql sql)
{
            return Fetch<T>(sql.SQL, sql.Arguments);
}

Which inherently calling Fetch method which takes a string as parameter. Then why do we need sql builder in petapoco?


Solution

  • Sql.Builder is a fluent API and gives the ability to conditionally build SQL. Which makes formatting SQL strings easy and provides a mechanism to use proper parameter replacements to protect from SQL injection.

    Example not tested

    var sql = PetaPoco.Sql.Builder()
        .Select("*")
        .From("Orders.Product")
        .Where("OrderID = @0", id);
    

    From PetaPoco documentation:

    Fetch returns a List<> of POCO's