SQL abstraction/generalization/protection layer.
Current connectors available:
- MySql
- MySql2
- MsSql
- Postgre (not tested too much)
- OleDb (requires .NET Framework).
This library allows you fluent SQL building, even for very complex queries.
It has a built-in capability of reading/writing records from classes - in a very different way than in Entity Framework.
It it less automated, which means you have to have your record class inherit from AbstractRecord and supply the definition of the schema.
But - that very definition of the schema allows to manage the whole database lifecycle from the code, including creating from scratch, migrations etc.
It also allows matching value types according to the schema when applicable.
The magic happens in the little SchemaGenerator addon for Visual Studio.
This little guys takes a "pseudo-script" and converts it to a full code of an AbstractRecord record.
You can override/extend almost anything in there, either in the script itself, or by defining a partial class and adding stuff.
This used to be dg.Sql library, renamed to SequelNet.
Migration notes - in the Releases section, for 2.0.0.
The use is pretty straight-forward. To supply a default connector, add a SequelNet.Connector key in the appSettings of web.config. Like this:
<add key="SequelNet.Connector" value="MySql2" />To supply a default connection string, add a SequelNet connection string in web.config,
or SequelNet::ConnectionString key in app config.
Another option is to add a SequelNet::ConnectionStringKey key in the app config, which specifies the connectionString's name to be used.
Query: Build the query, and executes in a number of different ways, and different way to retrieve the resultsConnectorBaseis the base class for the connector layer. Each connector has a subclass of this class, which supplies the basic functionalities, similar to theSqlConnectionclass. (I am considering a change this class's name in the future...)DataReaderBaseis the same story as withConnectorBase, providing the same functionality asSqlDataReaderAbstractRecordis a DAL class that represents your data, and contains aTableSchema, data accessors, and helper functions. UsingAbstractRecordis completely optional!TableSchemais a class representing the an actual db schema. This assists theQueryin converting values where necessary, bulding queries or even buildingCREATE TABLE,ALTER TABLEandCREATE INDEXqueries...- There's a namespace
Phraseswhich includes many objects that wrap native sql functions. You can add your own, based on theIPhraseclass.
Use ConnectorBase.GetDatabaseError(exception) to detect database errors without
referencing a driver exception type or checking numeric error codes:
using SequelNet.Connector;
try
{
query.Execute(connector);
}
catch (Exception ex) when (connector.GetDatabaseError(ex) == DatabaseError.UniqueViolation)
{
// Handle a primary-key or unique-key collision.
}UniqueViolation means duplicate values in a primary key or unique key.
The Duplicate* categories mean duplicate names or definitions; Undefined*
categories mean missing objects, including missing drop targets.
| Category | MySQL (both drivers) | SQL Server | PostgreSQL SQLSTATE | Jet/ACE SQLState |
|---|---|---|---|---|
UniqueViolation |
1062 | 2601, 2627 | 23505 | 3022 |
ForeignKeyViolation |
1452 | See ConstraintViolation |
23503 | 3200, 3201 |
ConstraintViolation |
— | 547 | — | — |
DuplicateColumn |
1060 | 2705 | 42701 | 3191, 3380 |
DuplicateTable |
1050 | — | 42P07 | 3010 |
DuplicateIndex |
1061 | — | — | 3284, 3375 |
DuplicateObject |
— | 1913, 2714 | 42710 | 3012, 3283 |
UndefinedColumn |
1054 | 207, 4924 | 42703 | 3381 |
UndefinedTable |
— | — | 42P01 | 3376 |
UndefinedObject |
1091 | 3728 | 42704 | 3372 |
Some provider conditions have broader meanings:
- PostgreSQL 42P07 maps to
DuplicateTable, matching its native condition name, but also covers duplicate indexes and other relations. 42710 maps separately toDuplicateObject. See PostgreSQL's error codes and index creation source. - SQL Server 547 covers foreign-key and check constraints, so it returns
ConstraintViolation. 1913 covers both indexes and statistics, and 2714 covers multiple object kinds, so both returnDuplicateObject. See Microsoft's error reference. - OleDb examines Jet/ACE engine error records and reads Access error numbers from
OleDbError.SQLState.NativeErrormay contain a different internal number. Error records from other OLE DB providers remainUnknown. Jet's generic missing-parameter errors also remainUnknown, since they do not uniquely identify a missing column. See the Access error catalogue.
SQL Server and OleDb return the first recognized category from their error collection. The Jet 4.0 mappings were exercised against a real temporary database; ACE was not installed on the validation machine.
Classification does not open a connection or change the exception. Null, unrecognized
errors, exceptions from other drivers, and connectors without a classification
implementation return DatabaseError.Unknown. Wrapped exceptions are not unwrapped;
pass the underlying provider exception explicitly if needed.
If you're using MySql, then there's a MySqlBackup class, which can create a full backup of a Database, including shcema and data. Yes, I know, it's cool.
There's a Visual Studio AddIn, called the "SchemaGenerator", which assists in creating the TableSchema and a matching AbstractRecord based on a simple language which resides in a comment.
This little tool makes rapid db prototyping easy, and gets you much further in development in significantly less time than when using any other tool I've used.
Full documentation of the syntax in Macro Structure.MD.
All the code here is under MIT license. Which means you could do virtually anything with the code. I will appreciate it very much if you keep an attribution where appropriate.
The MIT License (MIT)
Copyright (c) 2013 Daniel Cohen Gindi (danielgindi@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.