Git Product home page Git Product logo

apis's Introduction

Build Status codecov CodeFactor NPM version License

By default, Entity Framework does not support the ability to dynamically select by passing a string with the table name to get a DbSet. This is how this library came about. This library allows you to perform a SELECT query by passing Request object.

Report Bug

You can send bug report: Report Bug

Note

If you would like to submit a bug report. Please use a template

Install

Package Manager

Install-Package ArdenHide.Utils.QuickSQL

.NET CLI

dotnet add package ArdenHide.Utils.QuickSQL

You also need to install a package with your provider or implement your provider. Example for Microsoft Sql Server provider:

Package Manager

Install-Package ArdenHide.Utils.QuickSQL.MicrosoftSqlServer

.NET CLI

dotnet add package ArdenHide.Utils.QuickSQL.MicrosoftSqlServer

Example usage:

Providers currently supported: SupportedProviders

Also you can add your provider, see below.

Warning starting with QuickSQL 1.3.3, Poolz does not support the ArdenHide.Utils.QuickSQL.MySql package.

The first step is to create the desired request.

using QuickSQL;

Request tokenBalances = new Request(
    tableName: "TokenBalances",
    selectedColumns: new Collection<string>
    {
        { "Token" }, { "Owner" }, { "Amount" }
    },
    whereConditions: new Collection<Condition>
    {
        new Condition { ParamName = "Id", Operator = OperatorName.Equals, ParamValue = "1" }
    },
    orderRules: new Collection<OrderRule>
    {
        new OrderRule("Amount", SortBy.DESC)
    });

Request fields

  • TableName - This is a required parameter. Pass a table name from which to take data.
  • SelectedColumns - This is a required parameter. Pass columns from which to take data.
  • WhereConditions - Not required parameter. Enter condition for search tables. If it is a string parameter, you need to pass the condition parameter in single quotes, like ParamValue = "'Alex'
  • OrderRules - Not required parameter. Enter condition for sorting. Default SortBy value SortBy.ASC

The second step, invoke request.

using QuickSQL;
using QuickSQL.MicrosoftSqlServer;

object result = QuickSql.InvokeRequest(
    request: tokenBalances,
    connectionString: ConnectionString,
    // your custom or supported provider
    dataReader: new SqlDataReader(),
    queryCreator: new SqlQueryCreator()
);
System.Console.WriteLine(JsonSerializer.Serialize(result));

Security

This library does not have SQL injection checks. Best security practice is to create a read-only user. It's also a good idea to limit the user's visibility to tables that they shouldn't see.

I didn't find my provider. Instructions for adding your provider

The first step is to create a DataReader for your SQL provider. It is easier than it might seem, to implement your DateReader inherit the abstract class BaseDataReader. This abstract class have core logic for read SQL data. You need to define CreateConnection() and CreateReader() for your provider.

Example for MySql provider

using QuickSQL.Datareader;

public class MySqlDataReader : BaseDataReader
{
    public override DbConnection CreateConnection(string connectionString)
        => new MySqlConnection(connectionString);

    public override DbDataReader CreateReader(string commandQuery, DbConnection connection)
        => new MySqlCommand(commandQuery, (MySqlConnection)connection).ExecuteReader();
}

Example for MicrosoftSqlServer provider

using QuickSQL.Datareader;

public class SqlDataReader : BaseDataReader
{
    public override DbConnection CreateConnection(string connectionString)
        => new SqlConnection(connectionString);

    public override DbDataReader CreateReader(string commandQuery, DbConnection connection)
        => new SqlCommand(commandQuery, (SqlConnection)connection).ExecuteReader();
}

The second step is to create a QueryCreator for your SQL provider. You need to define OnCreateCommandQuery() for your provider. This function should create a SQL query string returning data in JSON format. You can use the CreateWhereCondition() internal function to create the condition string and CreateOrderByRules() for create order rules string.

Example for MicrosoftSqlServer provider

using QuickSQL.QueryCreator;

public class SqlQueryCreator : BaseQueryCreator
{
    protected override string OnCreateCommandQuery(Request request)
    {
        string commandQuery = $"SELECT {string.Join(", ", request.SelectedColumns)} FROM {request.TableName}";
        commandQuery += request.WhereConditions != null ? $" {CreateWhereCondition(request.WhereConditions)}" : string.Empty;
        commandQuery += request.OrderRules != null ? $" {CreateOrderByRules(request.OrderRules)}" : string.Empty;
        commandQuery += " FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER";
        return commandQuery;
    }
}

apis's People

Contributors

ardenhide avatar lomet avatar nikitabuschan avatar poolzadmin avatar

Stargazers

Ashwin Arora avatar  avatar  avatar

Watchers

 avatar

apis's Issues

Add logging

For programmer exception, remove throw and add context logging
For user exception, remove throw and add return null

Create Menu table

Make SQL table that will hold all the Api's information (what tables need to take in order to make result)

Update to version 1.3.2

  • updated XML
  • added OrderRules constructor by default for JSON serialization (System.Text.Json)

Update Request model

Update Request model to netcore6.0. with backwards compatibility. This is necessary to be able to serialize the object to JSON.

Update result

Return array only if need
If single object, dont return array

Pass context to method, not object

Now UniverslAPI asks for a context when initializing an object. Change the code so that the context is passed to the method GetTable()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.