102 lines
2.7 KiB
Markdown
102 lines
2.7 KiB
Markdown
# Quick Start
|
|
|
|
## Install and Reference
|
|
|
|
Reference the `DynamORM` project or package from your application.
|
|
|
|
## Basic Setup
|
|
|
|
```csharp
|
|
using System.Data.SQLite;
|
|
using DynamORM;
|
|
|
|
var options =
|
|
DynamicDatabaseOptions.SingleConnection |
|
|
DynamicDatabaseOptions.SingleTransaction |
|
|
DynamicDatabaseOptions.SupportLimitOffset |
|
|
DynamicDatabaseOptions.SupportSchema;
|
|
|
|
using (var db = new DynamicDatabase(
|
|
SQLiteFactory.Instance,
|
|
"Data Source=app.db;",
|
|
options))
|
|
{
|
|
db.DumpCommands = true;
|
|
|
|
var users = db.Table("users");
|
|
var total = users.Count(columns: "id");
|
|
var first = users.First(columns: "id,first,last");
|
|
}
|
|
```
|
|
|
|
This setup mirrors `DynamORM.Tests/TestsBase.cs`.
|
|
|
|
If you do not want `SingleConnection`, but still want managed reuse of open connections, enable `DynamicDatabaseOptions.ConnectionPooling` and configure:
|
|
|
|
- `db.ConnectionPoolingKeepOpenCount`
|
|
- `db.ConnectionPoolingMaximumOpenCount`
|
|
- `db.ConnectionPoolingConnectionLifetime`
|
|
|
|
Full details are documented in [Transactions and Disposal](transactions-and-disposal.md).
|
|
|
|
## First Query (Dynamic API)
|
|
|
|
```csharp
|
|
using (var db = new DynamicDatabase(SQLiteFactory.Instance, "Data Source=app.db;", options))
|
|
{
|
|
var row = db.Table("users").Single(id: 19);
|
|
Console.WriteLine(row.first);
|
|
}
|
|
```
|
|
|
|
## First Query (Fluent Builder API)
|
|
|
|
```csharp
|
|
using (var db = new DynamicDatabase(SQLiteFactory.Instance, "Data Source=app.db;", options))
|
|
using (var query = db.From("users").Where("id", 19).SelectColumn("first"))
|
|
{
|
|
var first = query.ScalarAs<string>();
|
|
Console.WriteLine(first);
|
|
}
|
|
```
|
|
|
|
## First Query (Typed Fluent Syntax)
|
|
|
|
```csharp
|
|
using (var db = new DynamicDatabase(SQLiteFactory.Instance, "Data Source=app.db;", options))
|
|
{
|
|
var cmd = db.FromTyped<User>("u")
|
|
.SelectSql(u => u.Col(x => x.Code))
|
|
.WhereSql(u => u.Col(x => x.Id).Eq(19));
|
|
|
|
var value = cmd.ScalarAs<string>();
|
|
Console.WriteLine(value);
|
|
}
|
|
```
|
|
|
|
For multi-join typed queries, prefer `FromTypedScope(...)`. Full details are documented in [Typed Fluent Syntax](typed-fluent-syntax.md).
|
|
|
|
## Insert, Update, Delete
|
|
|
|
```csharp
|
|
var table = db.Table("users");
|
|
|
|
table.Insert(code: "201", first: "Juri", last: "Gagarin");
|
|
table.Update(values: new { first = "Yuri" }, where: new { code = "201" });
|
|
table.Delete(code: "201");
|
|
```
|
|
|
|
These forms are validated in `DynamORM.Tests/Modify/DynamicModificationTests.cs`.
|
|
|
|
## Stored Procedures
|
|
|
|
Three common entry points:
|
|
|
|
```csharp
|
|
var legacy = db.Procedures.sp_DoWork(id: 10, ret_code: 0);
|
|
var typed = db.Procedure<MyProcedure>(new MyProcedureArgs());
|
|
var typedResult = db.TypedProcedure<MyProcedure, MyProcedureResult>().Exec(new MyProcedureArgs());
|
|
```
|
|
|
|
Full details are documented in [Stored Procedures](stored-procedures.md).
|