# .NET

QuillSQL.

```sh
dotnet add package QuillSQL
```

```csharp
using System.Text.Json;
using Quill;
using Quill.Models;
using Quill.Tenants;

var quill = new Quill.Quill(new QuillOptions
{
    PrivateKey = Environment.GetEnvironmentVariable("QUILL_PRIVATE_KEY")!,
    DatabaseConnectionString = Environment.GetEnvironmentVariable("POSTGRES_READ"),
    DatabaseType = "postgresql",
});

// RequireAuthorization is your own auth; "org_id" is a claim of the signed-in user
app.MapPost("/quill", async (HttpContext context, JsonElement body) =>
{
    var orgId = context.User.FindFirst("org_id")!.Value;
    var result = await quill.Query(new QueryParams
    {
        Tenants = [new Tenant<string>("organization_id", [orgId])],
        Metadata = JsonSerializer.Deserialize<IDictionary<string, object>>(body.GetProperty("metadata").GetRawText()),
    });
    return Results.Json(result);
}).RequireAuthorization();
```

## new Quill

```csharp
new Quill.Quill(QuillOptions options)
```

| Property                   | Default                |                                                                                                                                                             |
| -------------------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `PrivateKey`               | required               | `string`: the private key of your Quill environment                                                                                                         |
| `DatabaseType`             | required               | `"postgresql"`, `"redshift"`, `"snowflake"`, `"bigquery"`, `"mysql"`, `"clickhouse"`, `"mssql"` or `"databricks"`                                           |
| `DatabaseConnectionString` |                        | `string?`; used over `DatabaseConfig`                                                                                                                       |
| `DatabaseConfig`           |                        | `IDatabaseConfig?`: the connection, by `DatabaseType`, below                                                                                                |
| `Cache`                    |                        | `CacheCredentials?` `{ CacheType = "redis" \| "rediss", Host, Port, Username, Password, Ttl }`: caches results in Redis for `Ttl` seconds, 86400 by default |
| `MetadataServerUrl`        | `https://api.quill.co` | `string?`                                                                                                                                                   |
| `IsPooled`                 | `false`                | `bool`: pools database connections                                                                                                                          |

| `DatabaseType`               | `DatabaseConfig`                                                                                               |
| ---------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `"postgresql"`, `"redshift"` | `Quill.Postgres.PostgresConnectionConfig { ConnectionString, Ssl, IsPooled }`                                  |
| `"snowflake"`                | `Quill.Snowflake.SnowflakeConnectionConfig { Account, Username, Password, Database, Warehouse, Schema, Role }` |
| `"bigquery"`                 | `Quill.BigQuery.BigQueryConnectionConfig { ProjectId, DatasetName, CredentialsJson }`                          |
| `"mysql"`                    | `Quill.Mysql.MysqlConnectionConfig { Host, Port, User, Password, Database, SslMode }`                          |
| `"clickhouse"`               | `Quill.ClickHouse.ClickHouseConnectionConfig { Host, Port, User, Password, Database, Secure, Protocol }`       |
| `"mssql"`                    | `Quill.Mssql.MssqlConnectionConfig { ConnectionString, IsPooled }`                                             |
| `"databricks"`               | `Quill.Databricks.DatabricksConnectionConfig { Host, Path, Token, Catalog }`                                   |

## Query

```csharp
Task<IDictionary<string, object>> Query(QueryParams parameters)
```

| Property       | Default       |                                                                                             |
| -------------- | ------------- | ------------------------------------------------------------------------------------------- |
| `Tenants`      | required      | `IEnumerable<dynamic>`: `Quill.Tenants.Tenant<T>` records                                   |
| `Metadata`     | required      | `IDictionary<string, object>`: the `metadata` of the request body                           |
| `Flags`        |               | `IEnumerable<dynamic>`: strings, or `Quill.Tenants.TenantFlags`                             |
| `Filters`      |               | `List<QueryFilter>`                                                                         |
| `AdminEnabled` | `null`, false | `bool?`: allows admin tasks, such as editing dashboards; set it from your own authorization |

| Returns     |                                    |
| ----------- | ---------------------------------- |
| `"status"`  | `"success"` or `"error"`           |
| `"data"`    | `object`                           |
| `"queries"` | `{ "queryResults": [] }`           |
| `"error"`   | `string`, with `"status": "error"` |

## GenerateClientToken

```csharp
Task<string> GenerateClientToken(string publicKey, IEnumerable<object> tenants, long expiration, IEnumerable<object>? flags = null)
```

| Param        |                                                 |
| ------------ | ----------------------------------------------- |
| `publicKey`  |                                                 |
| `tenants`    | `Quill.Tenants.Tenant<T>` records               |
| `expiration` | milliseconds since the epoch, in the future     |
| `flags`      | strings, or `Quill.Tenants.TenantFlags` records |

## Close

```csharp
Task Close()
```
