DatabaseWrapper 6.1.0
dotnet add package DatabaseWrapper --version 6.1.0
NuGet\Install-Package DatabaseWrapper -Version 6.1.0
<PackageReference Include="DatabaseWrapper" Version="6.1.0" />
paket add DatabaseWrapper --version 6.1.0
#r "nuget: DatabaseWrapper, 6.1.0"
// Install DatabaseWrapper as a Cake Addin #addin nuget:?package=DatabaseWrapper&version=6.1.0 // Install DatabaseWrapper as a Cake Tool #tool nuget:?package=DatabaseWrapper&version=6.1.0
DatabaseWrapper
DatabaseWrapper is the EASIEST and FASTEST way to get a data-driven application up and running using SQL Server, MySQL, PostgreSQL, or Sqlite.
For a sample app exercising this library, refer to the numerous Test
projects contained within the solution.
Core features:
- Dynamic query building
- Hierarchical Boolean logic using Expression objects
- Support for SQL server native vs Windows authentication
- Support for SELECT, INSERT, UPDATE, DELETE, TRUNCATE, CREATE, DROP or raw queries
- Programmatic table creation and removal (drop)
- Built-in sanitization
- Support for .NET Standard, .NET Core, and .NET Framework
- Support for SQL Server, Sqlite, PostgreSQL, MySQL, MariaDB, both on-premises and in the cloud
- Both synchronous and asynchronous APIs
New in v6.0.x
- Minor breaking changes (API name changes)
- Asynchronous APIs
- Updated dependencies
- Bugfixes
Special Thanks
Special thanks to those who have helped contribute or otherwise improve the library!
@shawty @constantinje @thedarklort @l-404-l @igrgurina @Vaelek @treyhay31 @daoye @twobytescy @rinkusahu1
A Note on Sanitization
Use of parameterized queries vs building queries dynamically is a sensitive subject. Proponents of parameterized queries have data on their side - that parameterization does the right thing to prevent SQL injection and other issues. I do not disagree with them. However, it is worth noting that with proper care, you CAN build systems that allow you to dynamically build queries, and you SHOULD do so as long as you build in the appropriate safeguards.
If you find an injection attack that will defeat the sanitization layer built into this project, please let me know!
Simple Example
Refer to the test project for a more complete example with sample table setup scripts.
using DatabaseWrapper;
using DatabaseWrapper.Core;
using ExpressionTree;
DatabaseClient client = null;
// Sqlite
client = new DatabaseClient("[databasefilename]");
// SQL Server, MySQL, or PostgreSQL
client = new DatabaseClient(DbTypes.SqlServer, "[hostname]", [port], "[user]", "[password]", "[databasename]");
client = new DatabaseClient(DbTypes.Mysql, "[hostname]", [port], "[user]", "[password]", "[databasename]");
client = new DatabaseClient(DbTypes.Postgresql, "[hostname]", [port], "[user]", "[password]", "[databasename]");
// SQL Express
client = new DatabaseClient(DbTypes.SqlServer, "[hostname]", [port], "[user]", "[password]", "[instance]", "[databasename]");
// some variables we'll be using
Dictionary<string, object> d;
Expr e;
List<string> fields;
DataTable result;
// add a record
d = new Dictionary<string, object>();
d.Add("firstName", "Joel");
d.Add("lastName", "Christner");
d.Add("notes", "Author");
result = client.Insert("person", d);
result = await client.InsertAsync("person", d);
// update a record
d = new Dictionary<string, object>();
d.Add("notes", "The author :)");
e = new Expr("firstName", OperatorEnum.Equals, "Joel");
client.Update("person", d, e);
await client.UpdateAsync("person", d, e);
// retrieve 10 records
fields = new List<string> { "firstName", "lastName" }; // leave null for *
e = new Expr("lastName", OperatorEnum.Equals, "Christner");
ResultOrder[] order = new ResultOrder[1];
order = new ResultOrder("firstName", OrderDirectionEnum.Ascending)
result = client.Select("person", 0, 10, fields, e, order);
result = await client.SelectAsync("person", 0, 10, fields, e, order);
// delete a record
e = new Expr("firstName", Operators.Equals, "Joel");
client.Delete("person", e);
await client.DeleteAsync("person", e);
// execute a raw query
result = client.Query("SELECT customer_id FROM customer WHERE customer_id > 10");
result = await client.QueryAsync("SELECT customer_id FROM customer WHERE customer_id > 10");
Sample Compound Expression
Expressions, i.e. the Expr
class from ExpressionTree
, can be nested in either the Left
or Right
properties. Conversion from Expr
to a WHERE clause uses recursion, so you have a good degree of flexibility in building your expressions in terms of depth.
Expr e = new Expr {
Left = new Expr("age", OperatorEnum.GreaterThan, 30),
Operator = Operators.And,
Right = new Expr("height", OperatorEnum.LessThan, 74)
};
Select with Pagination
Use IndexStart
, MaxResults
, and ResultOrder[]
to retrieve paginated results. The query will retrieve maxResults records starting at row number indexStart using an ordering based on orderByClause. See the example in the DatabaseWrapperTest project.
IMPORTANT: When doing pagination with SQL Server, you MUST specify an ResultOrder[]
.
ResultOrder[] order = new ResultOrder[1];
order = new ResultOrder("firstName", OrderDirectionEnum.Ascending);
DataTable result = client.Select("person", 5, 10, null, e, order);
Need a Timestamp?
We added a simple static method for this which you can use when building expressions (or elsewhere). An object method exists as well.
string ts = client.Timestamp(DateTime.Now);
// 08/23/2016 05:34:32.4349034 PM
string tso = client.Timestamp(DateTime.Now);
// 2016-08-23 17:34:32.446913
Other Notes
General
When using database-specific classes DatabaseWrapper.Mysql
, DatabaseWrapper.Postgresql
, DatabaseWrapper.SqlServer
, or DatabaseWrapper.Sqlite
, the constructor is simplified from what is shown above.
For Sqlite:
DatabaseClient client = new DatabaseClient("[databasefilename]");
For SQL Server, MySQL, or PostgreSQL:
DatabaseClient client = new DatabaseClient(DbTypes.SqlServer, "[hostname]", [port], "[user]", "[password]", "[databasename]");
DatabaseClient client = new DatabaseClient(DbTypes.Mysql, "[hostname]", [port], "[user]", "[password]", "[databasename]");
DatabaseClient client = new DatabaseClient(DbTypes.Postgresql, "[hostname]", [port], "[user]", "[password]", "[databasename]");
For SQL Server Express:
DatabaseClient client = new DatabaseClient(DbTypes.SqlServer, "[hostname]", [port], "[user]", "[password]", "[instance]", "[databasename]");
MySQL
- MySQL does not like to return updated rows. I thought about making the UPDATE clause require that you supply the ID field and the ID value so that I could retrieve it after the fact, but that approach is just too limiting.
MariaDB
- Use the MySQL constructor. MySQL constraints remain.
PostgreSQL
- Cleansing of strings in PostgreSQL uses the dollar-quote style. Fieldnames are always encapsulated in double-quotes for PostgreSQL.
SQL Server
- Pagination where
IndexStart
andMaxResults
are supplied demands use ofResultOrder[]
.
Sqlite
- Sqlite may not work out of the box with .NET Framework. In order to use Sqlite with .NET Framework, you'll need to manually copy the
runtimes
folder into your project output directory. This directory is automatically created when building for .NET Core. To get this folder, build theTest.Sqlite
project and navigate to thebin/debug/netcoreapp*
directory. Then copy theruntimes
folder into the project output directory of your .NET Framework application.
Version history
Refer to CHANGELOG.md.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Framework | net48 is compatible. net481 was computed. |
-
.NETFramework 4.8
- DatabaseWrapper.Mysql (>= 6.1.0)
- DatabaseWrapper.Oracle (>= 6.1.0)
- DatabaseWrapper.Postgresql (>= 6.1.0)
- DatabaseWrapper.Sqlite (>= 6.1.0)
- DatabaseWrapper.SqlServer (>= 6.1.0)
-
net6.0
- DatabaseWrapper.Mysql (>= 6.1.0)
- DatabaseWrapper.Oracle (>= 6.1.0)
- DatabaseWrapper.Postgresql (>= 6.1.0)
- DatabaseWrapper.Sqlite (>= 6.1.0)
- DatabaseWrapper.SqlServer (>= 6.1.0)
-
net7.0
- DatabaseWrapper.Mysql (>= 6.1.0)
- DatabaseWrapper.Oracle (>= 6.1.0)
- DatabaseWrapper.Postgresql (>= 6.1.0)
- DatabaseWrapper.Sqlite (>= 6.1.0)
- DatabaseWrapper.SqlServer (>= 6.1.0)
-
net8.0
- DatabaseWrapper.Mysql (>= 6.1.0)
- DatabaseWrapper.Oracle (>= 6.1.0)
- DatabaseWrapper.Postgresql (>= 6.1.0)
- DatabaseWrapper.Sqlite (>= 6.1.0)
- DatabaseWrapper.SqlServer (>= 6.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DatabaseWrapper:
Package | Downloads |
---|---|
RestDb
RestDb is a platform that enables a RESTful API interface in front of Microsoft SQL Server, MySQL, PostgreSQL, and Sqlite databases. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.1.0 | 183 | 9/19/2024 |
6.0.7 | 418 | 3/27/2024 |
6.0.6 | 1,137 | 10/4/2023 |
6.0.5 | 1,705 | 8/29/2023 |
6.0.3 | 3,385 | 7/11/2023 |
6.0.2 | 995 | 7/11/2023 |
6.0.1 | 900 | 7/11/2023 |
6.0.0 | 927 | 7/11/2023 |
5.0.2 | 2,912 | 2/2/2023 |
5.0.0 | 3,110 | 10/25/2022 |
4.1.5 | 2,983 | 8/29/2022 |
4.1.3 | 1,459 | 6/21/2022 |
4.1.2 | 1,244 | 5/27/2022 |
4.0.0.1 | 2,013 | 12/31/2021 |
4.0.0 | 1,065 | 12/31/2021 |
3.3.2.3 | 1,062 | 12/22/2021 |
3.3.2.1 | 1,431 | 11/20/2021 |
3.3.2 | 2,030 | 11/19/2021 |
3.3.1.5 | 1,038 | 11/12/2021 |
3.3.1.2 | 5,441 | 6/7/2021 |
3.3.1.1 | 2,861 | 4/14/2021 |
3.3.1 | 1,149 | 4/14/2021 |
3.3.0 | 2,062 | 3/2/2021 |
3.2.2.18 | 2,354 | 2/9/2021 |
3.2.2.17 | 4,387 | 12/26/2020 |
3.2.2.16 | 2,619 | 11/28/2020 |
3.2.2.15 | 1,323 | 11/26/2020 |
3.2.2.13 | 1,371 | 11/25/2020 |
3.2.2.12 | 1,338 | 11/25/2020 |
3.2.2.11 | 1,575 | 11/24/2020 |
3.2.2.10 | 1,767 | 11/15/2020 |
3.2.2.9 | 1,624 | 11/10/2020 |
3.2.2.8 | 1,647 | 11/9/2020 |
3.2.2.7 | 1,605 | 10/19/2020 |
3.2.2.6 | 1,595 | 10/15/2020 |
3.2.2.5 | 1,666 | 10/5/2020 |
3.2.2.4 | 1,672 | 10/5/2020 |
3.2.2.3 | 1,761 | 9/18/2020 |
3.2.2.2 | 1,663 | 9/18/2020 |
3.2.2.1 | 1,734 | 9/18/2020 |
3.2.2 | 1,657 | 9/18/2020 |
3.2.0.7 | 1,807 | 9/16/2020 |
3.2.0.6 | 1,707 | 9/16/2020 |
3.2.0.5 | 1,594 | 9/10/2020 |
3.2.0.4 | 1,700 | 9/8/2020 |
3.2.0.3 | 1,978 | 9/8/2020 |
3.2.0.2 | 4,594 | 8/17/2020 |
3.2.0.1 | 4,639 | 7/28/2020 |
3.2.0 | 2,202 | 7/10/2020 |
3.1.0.3 | 2,231 | 6/20/2020 |
3.1.0.2 | 1,608 | 6/20/2020 |
3.1.0.1 | 2,157 | 6/11/2020 |
3.1.0 | 1,566 | 6/11/2020 |
3.0.1.2 | 1,841 | 6/10/2020 |
3.0.1.1 | 2,431 | 6/4/2020 |
3.0.1 | 1,851 | 6/3/2020 |
3.0.0.3 | 3,600 | 5/21/2020 |
3.0.0.2 | 2,194 | 5/20/2020 |
3.0.0 | 1,646 | 5/20/2020 |
2.0.5 | 5,383 | 5/12/2020 |
2.0.4 | 18,754 | 3/23/2020 |
2.0.3 | 1,627 | 3/23/2020 |
2.0.2 | 11,514 | 3/9/2020 |
2.0.1 | 1,833 | 3/9/2020 |
2.0.0 | 2,103 | 3/9/2020 |
1.5.1 | 38,836 | 12/18/2019 |
1.5.0 | 1,360 | 12/15/2019 |
1.4.7 | 17,621 | 10/1/2019 |
1.4.5 | 1,977 | 9/21/2019 |
1.4.4 | 1,643 | 8/22/2019 |
1.4.3 | 1,535 | 8/21/2019 |
1.4.2 | 1,558 | 8/21/2019 |
1.4.1 | 1,549 | 8/20/2019 |
1.4.0 | 1,496 | 8/19/2019 |
1.3.2 | 1,623 | 6/13/2019 |
1.3.1 | 2,074 | 4/25/2019 |
1.2.7 | 2,258 | 3/10/2019 |
1.2.6 | 2,115 | 3/20/2018 |
1.2.5 | 2,052 | 1/12/2018 |
1.2.4 | 1,986 | 12/28/2017 |
1.2.3 | 1,888 | 11/1/2017 |
1.2.2 | 1,916 | 9/25/2017 |
1.2.1 | 2,060 | 5/17/2017 |
1.1.9 | 1,905 | 5/3/2017 |
1.1.8 | 1,950 | 5/3/2017 |
1.1.7 | 1,998 | 5/3/2017 |
1.1.6 | 1,839 | 5/2/2017 |
1.1.5 | 1,975 | 3/29/2017 |
1.1.4 | 1,980 | 1/25/2017 |
Oracle support (thank you @Skimmenthal13!), bugfixes, dependency updates.