-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
873 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Connection | ||
|
||
## Connection String | ||
|
||
Set the **Connection String** | ||
|
||
```cs | ||
var connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\path\to\file.mdf;Integrated Security=True;Connect Timeout=30"; | ||
var providerName = "System.Data.SqlClient"; | ||
|
||
EasyDb.Default.SetConnectionStringSettings(connectionString,providerName); | ||
``` | ||
|
||
With a **Configuration File** | ||
|
||
```cs | ||
EasyDb.Default.SetConnectionStringSettings(); // "Default" connection | ||
// or | ||
EasyDb.Default.SetConnectionStringSettings("MyConnection"); | ||
``` | ||
_Example_ | ||
|
||
```xml | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
|
||
<connectionStrings> | ||
<add name="Default" | ||
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\path\to\mydb.mdf;Integrated Security=True;Connect Timeout=20" | ||
providerName="System.Data.SqlClient" /> | ||
<add name="MyConnection" | ||
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\path\to\mydb2.mdf;Integrated Security=True;Connect Timeout=20" | ||
providerName="System.Data.SqlClient" /> | ||
</connectionStrings> | ||
|
||
</configuration> | ||
``` | ||
|
||
# Connection Strategy | ||
|
||
> EasyDb use System.Data.Common (DbConnection, DbCommand, etc.). | ||
By **default** the connection is **opened** and **closed** for **each request**. | ||
|
||
```cs | ||
EasyDb.Default.SetConnectionStringSettings(connectionString, providerName, ConnectionStrategy.Manual); | ||
|
||
var result = await EasyDb.Default.CreateCommand("select * from users where [id]=@id") | ||
.AddParameter("@id", 1) | ||
.ReadOneAsync<User>(); | ||
``` | ||
|
||
**Controlling** the **connection**. | ||
|
||
```cs | ||
EasyDb.Default.SetConnectionStringSettings(connectionString,providerName,ConnectionStrategy.Manual); | ||
``` | ||
.. Or | ||
```cs | ||
EasyDb.Default.SetConnectionStrategy(ConnectionStrategy.Manual); // "Default" or "Manual" | ||
``` | ||
_Then_ | ||
```cs | ||
EasyDb.Default.SetConnectionStringSettings(connectionString, providerName, ConnectionStrategy.Manual); | ||
|
||
await EasyDb.Default.OpenAsync(); | ||
|
||
var result = await EasyDb.Default.CreateCommand("select * from users where [id]=@id") | ||
.AddParameter("@id", 1) | ||
.ReadOneAsync<User>(); | ||
|
||
EasyDb.Default.Close(); | ||
``` | ||
|
||
## Error handler | ||
|
||
```cs | ||
var db = new EasyDb(); | ||
|
||
db.OnError += (sender, e) => | ||
{ | ||
//... intercepted | ||
}; | ||
|
||
db.SetConnectionStringSettings("invalid", "invalid"); // exception ... | ||
``` | ||
|
||
## Create an EasyDb instance or use Default Static instance | ||
|
||
Use the default **Static** instance | ||
|
||
```cs | ||
EasyDb.Default.SetConnectionStringSettings("MyConnection"); | ||
|
||
var user = await EasyDb.Default | ||
.CreateCommand("select * from [users] where [id]=@id") | ||
.AddParameter("@id", 1) | ||
.ReadOneAsync<User>(); | ||
``` | ||
|
||
... Or **create an instance** | ||
|
||
```cs | ||
IEasyDb db = new EasyDb(); | ||
|
||
db.SetConnectionStringSettings("MyConnection"); | ||
|
||
var user = await db.CreateCommand("select * from [users] where [id]=@id") | ||
.AddParameter("@id", 1) | ||
.ReadOneAsync<User>(); | ||
``` | ||
|
||
## Other usefull Connection methods | ||
|
||
* OpenAsync | ||
* Close | ||
* IsOpen | ||
* IsClosed | ||
* GetState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# EasyDb (version 3.x) | ||
|
||
> EasyDb is a Library for **.NET** Projects. | ||
* **Micro ORM** | ||
* **Simple commands** with **SQL** | ||
* **Queries** with **Relations** Zero One, One to many and Many to Many | ||
* **Mapping** | ||
* **No dependencies**. _Do not require to update the Model._ | ||
* **Testable** | ||
* **Repository** _Pattern recommended_ | ||
|
||
## Installation | ||
|
||
``` | ||
PM> Install-Package EasyDb | ||
``` | ||
|
||
<p align="center"> | ||
<img src="http://res.cloudinary.com/romagny13/image/upload/v1475077310/easyDb_logo_lrcq7m.png"/> | ||
</p> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# Insert, Update and Delete Queries | ||
|
||
## Insert Query | ||
|
||
### Get the Query | ||
|
||
```cs | ||
var db = new EasyDb(); | ||
db.SetConnectionStringSettings(connectionString, providerName); | ||
|
||
var user = new User | ||
{ | ||
FirstName = "Marie", | ||
LastName = "Bellin", | ||
Age = 20, | ||
Email ="[email protected]" | ||
}; | ||
|
||
var result = db.InsertInto("users") | ||
.Values("firstname", user.FirstName) | ||
.Values("lastname", user.LastName) | ||
.Values("age", user.Age) | ||
.Values("email", user.Email) | ||
.GetQuery(); | ||
// insert into [users] ([firstname],[lastname],[age],[email]) output inserted.id values (@firstname,@lastname,@age,@email) | ||
``` | ||
|
||
### Execute and get the Last Inserted Id | ||
|
||
```cs | ||
var db = new EasyDb(); | ||
db.SetConnectionStringSettings(connectionString, providerName); | ||
|
||
var user = new User | ||
{ | ||
FirstName = "new firstname", | ||
LastName = "new lastname", | ||
Age = 20, | ||
Email = "[email protected]" | ||
}; | ||
|
||
var result = await db.InsertInto("users") | ||
.Values("firstname", user.FirstName) | ||
.Values("lastname", user.LastName) | ||
.Values("age", user.Age) | ||
.Values("email", user.Email) | ||
.LastInsertedId<int>() | ||
``` | ||
_For Guid replace "int" by "Guid"_ | ||
|
||
|
||
## Fetch | ||
|
||
Returns the Model result | ||
|
||
```cs | ||
var db = this.GetDb(); | ||
|
||
Mapping.SetTable("users").SetPrimaryKeyColumn("id", "id"); | ||
|
||
var user = new User | ||
{ | ||
FirstName = "new firstname", | ||
LastName = "new lastname", | ||
Age = 20, | ||
Email = "[email protected]" | ||
}; | ||
|
||
var result = await db.InsertInto("users") | ||
.Values("firstname", user.FirstName) | ||
.Values("lastname", user.LastName) | ||
.Values("age", user.Age) | ||
.Values("email", user.Email) | ||
.Fetch<User>(Mapping.GetTable("users")) | ||
``` | ||
|
||
With **Mapping** | ||
|
||
```cs | ||
var db = new EasyDb(); | ||
db.SetConnectionStringSettings(connectionString, providerName); | ||
|
||
Mapping.SetTable("users") | ||
.SetPrimaryKeyColumn("id", "Id") | ||
.SetColumn("firstname","FirstName") | ||
.SetColumn("lastname", "LastName") | ||
.SetColumn("age", "Age") | ||
.SetColumn("email", "Email"); | ||
|
||
var user = new User | ||
{ | ||
FirstName = "new firstname", | ||
LastName = "new lastname", | ||
Age = 20, | ||
Email = "[email protected]" | ||
}; | ||
|
||
var result = await db.InsertInto("users") | ||
.Values("firstname", user.FirstName) | ||
.Values("lastname", user.LastName) | ||
.Values("age", user.Age) | ||
.Values("email", user.Email) | ||
.Fetch<User>(Mapping.GetTable("users")); | ||
``` | ||
|
||
## Update Query | ||
|
||
```cs | ||
var db = new EasyDb(); | ||
db.SetConnectionStringSettings(connectionString, providerName); | ||
|
||
var user = new User | ||
{ | ||
Id = 1, | ||
FirstName = "Updated firstname", | ||
LastName = "Updated lastname" | ||
}; | ||
|
||
var rowAffected = await db | ||
.Update("users") | ||
.Set("firstname", user.FirstName) | ||
.Set("lastname", user.LastName) | ||
.Set("age", user.Age) | ||
.Set("email", user.Email) | ||
.Where(Check.Op("id", user.Id)) | ||
.NonQueryAsync(); | ||
``` | ||
|
||
_We could get the Query with the function GetQuery. Example:_ | ||
|
||
```sql | ||
update [users] set [firstname]=@firstname,[lastname]=@lastname,[age]=@age,[email]=@email where [id]=@id | ||
``` | ||
|
||
## Delete Query | ||
|
||
```cs | ||
var db = new EasyDb(); | ||
db.SetConnectionStringSettings(connectionString, providerName); | ||
|
||
var result = await db | ||
.DeleteFrom("posts") | ||
.Where(Check.Op("id", 2)) | ||
.NonQueryAsync(); | ||
``` | ||
|
||
_We could get the Query with the function GetQuery_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Mapping | ||
|
||
> Its recommended to define the mapping at the application bootstrap | ||
**Mapping columns** is only **required** : | ||
* **with relations** | ||
* or **if** the **column name** is **not equal** to the **property name** _(example: "id" and "Id", "user_id" and "UserId")_ | ||
|
||
**Create** the Mapping for a **table** | ||
|
||
```cs | ||
Mapping | ||
.SetTable("posts") // table name | ||
.SetPrimaryKeyColumn("id", "Id") // primary key | ||
.SetColumn("title", "Title") // column name => property name | ||
.SetForeignKeyColumn("user_id", "UserId", "users", "id"); // foreign key, property, table, primary key | ||
``` | ||
|
||
**Ignore a column** (do not fill the property) | ||
|
||
```cs | ||
Mapping | ||
.SetTable("posts") | ||
.SetPrimaryKeyColumn("id", "Id",true) // ignored | ||
.SetColumn("title", "Title") | ||
.SetForeignKeyColumn("user_id", "UserId", "users", "id"); | ||
``` | ||
|
||
Set the **DbType** | ||
|
||
```cs | ||
Mapping | ||
.SetTable("posts") | ||
.SetPrimaryKeyColumn("id", "Id", DbType.Int16) | ||
.SetColumn("title", "Title") | ||
.SetForeignKeyColumn("user_id", "UserId", "users", "id"); | ||
``` | ||
|
||
Create an **intermediate table** (for **Many to Many relations**) | ||
|
||
```cs | ||
Mapping.SetIntermediateTable("users_permissions") // table name | ||
.SetPrimaryKeyColumn("user_id", "id", "users") // primary key, target table pk, target table name | ||
.SetPrimaryKeyColumn("permission_id", "id", "permissions"); | ||
```` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Query Services | ||
|
||
EasyDb support by default 3 Providers: | ||
* "_System.Data.SqlClient_" (Sql Server) | ||
* "_System.Data.OleDb_" (Access) | ||
* "_MySql.Data.MySqlClient_" (MySQL with [MySQLConnector for .NET](https://dev.mysql.com/downloads/connector/net/)) | ||
|
||
For the other providers we have to create a custom Query Service. | ||
|
||
## Change/ Add a Query Service | ||
|
||
By **default** the Query Service targets **Sql Server** and wrap tables and columns with **quotes**. | ||
|
||
```sql | ||
select [permissions].* | ||
from [users_permissions],[permissions] | ||
where [users_permissions].[permission_id]=[permissions].[id] and [users_permissions].[user_id]=@user_id | ||
``` | ||
|
||
Create a _custom service_ for example. | ||
|
||
```cs | ||
public class MyCustomQueryService : QueryService | ||
{ | ||
public MyCustomQueryService() | ||
: base("`", "`") | ||
{ } | ||
|
||
// override methods ... | ||
} | ||
``` | ||
|
||
_Change the service_ | ||
|
||
```cs | ||
EasyDb.Default.SetQueryService("MySql.Data.MySqlClient", new MyCustomQueryService()); | ||
``` | ||
|
||
|
Oops, something went wrong.