All Projects → xin9le → Declarativesql

xin9le / Declarativesql

Licence: mit
Attribute-based database access

Projects that are alternatives of or similar to Declarativesql

Sql Streams
Painless low level jdbc abstraction using the java 8 stream api.
Stars: ✭ 17 (-58.54%)
Mutual labels:  sql, database
Node Pg Migrate
Node.js database migration management for Postgresql
Stars: ✭ 838 (+1943.9%)
Mutual labels:  sql, database
Hana sql exporter
SAP Hana SQL Exporter for Prometheus
Stars: ✭ 18 (-56.1%)
Mutual labels:  sql, database
Sqlz
SQL Query Builder for Go
Stars: ✭ 36 (-12.2%)
Mutual labels:  sql, database
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (+2209.76%)
Mutual labels:  sql, database
Ray.aurasqlmodule
Aura.Sql module for Ray.Di
Stars: ✭ 5 (-87.8%)
Mutual labels:  sql, database
Databases workshop
RCS Intro to Databases workshop materials
Stars: ✭ 25 (-39.02%)
Mutual labels:  sql, database
Db Dumper
Dump the contents of a database
Stars: ✭ 744 (+1714.63%)
Mutual labels:  sql, database
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+2300%)
Mutual labels:  sql, database
Express Knex Objection
A simple API system on a pg database, using knex and objection to simplify connection and management
Stars: ✭ 20 (-51.22%)
Mutual labels:  sql, database
Imdbpy
IMDbPY is a Python package useful to retrieve and manage the data of the IMDb movie database about movies, people, characters and companies
Stars: ✭ 792 (+1831.71%)
Mutual labels:  sql, database
Siodb
The simplicity of REST and the power of SQL combined in a database that automatized security and performance. Forget the database, develop faster and safer!
Stars: ✭ 31 (-24.39%)
Mutual labels:  sql, database
Eralchemy
Entity Relation Diagrams generation tool
Stars: ✭ 767 (+1770.73%)
Mutual labels:  sql, database
Efcore.pg
Entity Framework Core provider for PostgreSQL
Stars: ✭ 838 (+1943.9%)
Mutual labels:  sql, database
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+18709.76%)
Mutual labels:  sql, database
Reiner
萊納 - A MySQL wrapper which might be better than the ORMs and written in Golang
Stars: ✭ 19 (-53.66%)
Mutual labels:  sql, database
Baikaldb
BaikalDB, A Distributed HTAP Database.
Stars: ✭ 707 (+1624.39%)
Mutual labels:  sql, database
Nano Sql
Universal database layer for the client, server & mobile devices. It's like Lego for databases.
Stars: ✭ 717 (+1648.78%)
Mutual labels:  sql, database
Phoenix
Mirror of Apache Phoenix
Stars: ✭ 867 (+2014.63%)
Mutual labels:  sql, database
Avsqldebugger
A Simple Core Data Debugger that will look inside your apps DB
Stars: ✭ 30 (-26.83%)
Mutual labels:  sql, database

DeclarativeSql

This library provides attribute-based table mapping and simple database access. It mainly contains following features.

  • Unified connection to the some databases (SQL Server / Oracle / MySQL / SQLite etc.)
  • Attribute-based simple SQL generation
  • Super easy CRUD access based Dapper
  • Automatically set CreatedAt / ModifiedAt column.
  • High availability connection support by master/slave approach

Releases

Support Platform

  • .NET Framework 4.6.1+
  • .NET Standard 2.0+
  • .NET 5.0+

Attribute-based O/R mapping information

DeclarativeSql that is also inspired by Entity Framework provides attribute-based database mapping. Following code is its sample. Generates SQL and performs O/R mapping that based on these attributes and types.

using System;
using DeclarativeSql.Annotations;

namespace SampleApp
{
    [Table(DbKind.SqlServer, "T_Person", Schema = "dbo")]  // Customize table name per database
    public class Person
    {
        [PrimaryKey]  // Primary key constraint
        [AutoIncrement]  // Automatically numbering
        public int Id { get; set; }

        [Unique(0)]  // Unique constraint by index
        public string Email { get; set; }

        [Column(DbKind.SqlServer, "名前")]  // Customize column name per database
        public string Name { get; set; }

        [AllowNull]  // Nullable
        public int? Age { get; set; }

        [CreatedAt]  // Set datetime when row is inserted
        [DefaultValue(DbKind.SqlServer, "SYSDATETIME()")]
        public DateTimeOffset CreatedOn { get; set; }

        [ModifiedAt]  // Set datetime when row is updated
        [DefaultValue(DbKind.SqlServer, "SYSDATETIME()")]
        public DateTimeOffset UpdatedOn { get; set; }
    }
}

SQL generation

This library also provides automatic sql generation feature using above meta data. You can get very simple and typical sql using QueryBuilder class. Of course it's completely type-safe.

//--- Query records with specified columns that matched specified condition
var sql
    = QueryBuilder.Select<Person>
    (
        this.DbProvider,
        x => x.Age >= 30,
        x => new { x.Name, x.Age }
    )
    .Statement;

/*
select
    [名前] as Name,
    [Age] as Age
from [dbo].[T_Person]
where
    [Age] >= @p1
*/
//--- If wants OrderBy / ThenBy, allows you to write like following
using (var builder = new QueryBuilder<Person>(DbProvider.SqlServer))
{
    builder.Select(x => new { x.Id, x.Name });
    builder.Where(x => x.Name == "xin9le");
    builder.OrderByDescending(x => x.Name);
    builder.ThenBy(x => x.CreatedOn);
    var sql = builder.Build().Statement;
}

/*
select
    [Id] as Id,
    [名前] as Name
from [dbo].[T_Person]
where
    [名前] = @p1
order by
    [名前] desc,
    [CreatedOn]
*/
//--- Insert record to SQL Server
var sql = QueryBuilder.Insert<Person>(DbProvider.SqlServer).Statement;

/*
insert into [dbo].[T_Person]
(
    [Email],
    [名前],
    [Age],
    [CreatedOn],
    [UpdatedOn]
)
values
(
    @Email,
    @Name,
    @Age,
    SYSDATETIME(),
    SYSDATETIME()
)
*/
//--- Update records with specified columns that matched specified condition
var sql
    = QueryBuilder.Update<Person>
    (
        DbProvider.SqlServer,
        x => x.Age < 35 || x.Name == "xin9le",
        x => new { x.Name, x.Age }
    )
    .Statement;

/*
update [dbo].[T_Person]
set
    [名前] = @Name,
    [Age] = @Age,
    [UpdatedOn] = SYSDATETIME()
where
    [Age] < @p1 or [名前] = @p2
*/

QueryBuilder class also provides some other overload functions and Count / Delete / Truncate methods, and so on.

Dapper integration

This library automates typical CRUD operations completely using above sql generation feature and Dapper. By using expression tree, you can specify target column and filter records. Provided method names are directly described the CRUD operations, so you can understand and use them easily.

//--- Query all records
var p1 = connection.Select<Person>();

//--- Query all records with specified columns
var p2 = connection.Select<Person>(x => new { x.Id, x.Name });

//--- Query 'ID = 3' records only
var p3 = connection.Select<Person>(x => x.Id == 3);

//--- Query 'ID = 3' records with specified columns
var p4 = connection.Select<Person>
(
    x => x.Id == 3,
    x => new { x.Id, x.Name }
);
//--- Insert specified data
var p5 = connection.Insert(new Person { Name = "xin9le", Age = 30 });

//--- Insert collection
var p6 = connection.InsertMulti(new []
{
    new Person { Name = "yoshiki", Age= 49, },
    new Person { Name = "suzuki",  Age= 30, },
    new Person { Name = "anders",  Age= 54, },
});

//--- Super easy bulk insert
var p7 = connection.BulkInsert(new []
{
    new Person { Id = 1, Name = "yoshiki", Age= 49, },
    new Person { Id = 2, Name = "suzuki",  Age= 30, },
    new Person { Id = 3, Name = "anders",  Age= 54, },
});

//--- Insert and get generated auto incremented id
var p8 = connection.InsertAndGetId(new Person { Name = "xin9le", Age = 30 });
//--- Update records which is matched specified condition
var p9 = connection.Update
(
    new Person { Name = "test", Age = 23 },
    x => x.Age == 30
);
//--- Delete all records
var p10 = connection.Delete<Person>();

//--- Delete records which is matched specified condition
var p11 = connection.Delete<Person>(x => x.Age != 30);
//--- Truncate table
var p12 = connection.Truncate<Person>();
//--- Count all records
var p13 = connection.Count<Person>();

//--- Count records which is matched specified condition
var p14 = connection.Count<Person>(x => x.Name == "xin9le");

These CRUD methods are provided not only synchronous but also asynchronous.

Enable environment specific feature

BulkInsert / InsertAndGetId (etc.) methods are environment specific feature. If you want to use them, please call follows once.

MicrosoftSqlClientInitializer.Initialize();
SystemSqlClientInitializer.Initialize();

High availability connection

If you want to create a highly available database configuration, you can use HighAvailabilityConnection. This provides the simple master/slave pattern. High availability can be achieved simply by writing to the master database and reading from the slave database.

public class FooConnection : HighAvailabilityConnection
{
    public FooConnection()
        : base("ConnectionString-ToMasterServer", "ConnectionString-ToSlaveServer")
    {}

    protected override IDbConnection CreateConnection(string connectionString, AvailabilityTarget target)
        => new SqlConnection(connectionString);
}
using (var connection = new FooConnection())
{
    //--- Read from slave database
    var p = connection.Slave.Select<Person>();

    //--- Write to master database
    connection.Master.Insert(new Person { Name = "xin9le" });
}

Of course, by using the same connection string for the master database and for the slave database, a single database environment can be also supported.

Installation

Getting started from downloading NuGet package.

PM> Install-Package DeclarativeSql
PM> Install-Package DeclarativeSql.MicrosoftSqlClient
PM> Install-Package DeclarativeSql.SystemSqlClient

License

This library is provided under MIT License.

Author

Takaaki Suzuki (a.k.a @xin9le) is software developer in Japan who awarded Microsoft MVP for Developer Technologies (C#) since July 2012.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].