# @salmonui/query-builder/sql API reference

> 12 exported symbols of @salmonui/query-builder/sql: 2 functions, 1 class, 6 interfaces and 3 types.

Canonical: https://salmonui.dev/docs/reference/query-builder/sql

```ts
import { … } from '@salmonui/query-builder/sql';
```

## Functions

### fromSql

Kind: function

Page: https://salmonui.dev/docs/reference/query-builder/sql/fromSql

```ts
function fromSql(sql: string, options?: FromSqlOptions): QueryGroup;
```

A SQL WHERE expression written by `toSql` as a Query Builder value.

### toSql

Kind: function

Page: https://salmonui.dev/docs/reference/query-builder/sql/toSql

```ts
function toSql(value: QueryGroup, options?: ToSqlOptions): SqlResult;
```

A Query Builder value as a SQL WHERE expression.

## Classes

### SqlParseError

Kind: class

Page: https://salmonui.dev/docs/reference/query-builder/sql/SqlParseError

```ts
class SqlParseError extends SyntaxError {
    readonly position: number;
    constructor(message: string, position: number);
}
```

Thrown for SQL outside the subset `toSql` writes.

Members (1):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `position` | `readonly position: number;` |  | Offset of the offending token in the SQL text. |

## Interfaces

### FromSqlOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/query-builder/sql/FromSqlOptions

```ts
interface FromSqlOptions {
    readonly params?: readonly Json[] | Readonly<Record<string, Json>>;
    readonly named?: boolean;
    readonly pathSeparator?: string;
}
```

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `params` | `readonly params?: readonly Json[] \| Readonly<Record<string, Json>>;` | optional | Values of the placeholders: an array for positional ones (`?`, `$1`, `@p1`), a record for named ones (`:p1`). |
| `named` | `readonly named?: boolean;` | optional | Named placeholders (`:name`, `@name`) read from `params` by name. Default false. |
| `pathSeparator` | `readonly pathSeparator?: string;` | optional | Separator written between path segments (`"a"."b"` becomes `a.b`). Default `'.'`. |

### QueryGroup

Kind: interface

Page: https://salmonui.dev/docs/reference/query-builder/sql/QueryGroup

```ts
interface QueryGroup {
    readonly id?: string;
    readonly logic: 'and' | 'or';
    readonly not?: boolean;
    readonly locked?: boolean;
    readonly filters: readonly QueryNode[];
}
```

AND / OR group of rules and groups, optionally negated.

Members (5):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `id` | `readonly id?: string;` | optional |  |
| `logic` | `readonly logic: 'and' \| 'or';` |  |  |
| `not` | `readonly not?: boolean;` | optional | NOT (members). |
| `locked` | `readonly locked?: boolean;` | optional | A locked group and everything in it cannot be edited from the UI. |
| `filters` | `readonly filters: readonly QueryNode[];` |  |  |

### QueryRule

Kind: interface

Page: https://salmonui.dev/docs/reference/query-builder/sql/QueryRule

```ts
interface QueryRule {
    readonly id?: string;
    readonly field: string;
    readonly operator: string;
    readonly value?: Json;
    readonly matchCase?: boolean;
    readonly ignoreAccents?: boolean;
    readonly locked?: boolean;
}
```

One condition: `field operator value`.

Members (7):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `id` | `readonly id?: string;` | optional | Stable node id; assigned by QueryBuilder when missing. |
| `field` | `readonly field: string;` |  | Field path (`address.city` with the default `pathSeparator`). |
| `operator` | `readonly operator: string;` |  | A Data Grid filter operator name (`equal`, `contains`, `between`, ...) or a custom operator. |
| `value` | `readonly value?: Json;` | optional | `between`: `[from, to]`; `in` / `notIn`: an array; value-less operators omit it. |
| `matchCase` | `readonly matchCase?: boolean;` | optional |  |
| `ignoreAccents` | `readonly ignoreAccents?: boolean;` | optional |  |
| `locked` | `readonly locked?: boolean;` | optional | A locked rule cannot be edited, moved or deleted from the UI. |

### SqlOperatorContext

Kind: interface

Page: https://salmonui.dev/docs/reference/query-builder/sql/SqlOperatorContext

```ts
interface SqlOperatorContext {
    readonly field: string;
    readonly rule: QueryRule;
    readonly value: Json | undefined;
    readonly dialect: SqlDialect;
    param(value: Json): string;
}
```

Members (5):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `field` | `readonly field: string;` |  | The column expression (quoted when `quoteIdentifier`). |
| `rule` | `readonly rule: QueryRule;` |  |  |
| `value` | `readonly value: Json \| undefined;` |  |  |
| `dialect` | `readonly dialect: SqlDialect;` |  |  |
| `param` | `param(value: Json): string;` |  | Adds `value` as a parameter (or an inlined literal) and returns its SQL text. |

### SqlResult

Kind: interface

Page: https://salmonui.dev/docs/reference/query-builder/sql/SqlResult

```ts
interface SqlResult {
    readonly sql: string;
    readonly params: Json[] | Record<string, Json>;
}
```

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `sql` | `readonly sql: string;` |  | The WHERE expression without `WHERE`; '' for an empty query. |
| `params` | `readonly params: Json[] \| Record<string, Json>;` |  | Positional values in placeholder order, or a record for `named`. |

### ToSqlOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/query-builder/sql/ToSqlOptions

```ts
interface ToSqlOptions {
    readonly named?: boolean;
    readonly inline?: boolean;
    readonly quoteIdentifier?: boolean | ((name: string) => string);
    readonly dialect?: SqlDialect;
    readonly operators?: Readonly<Record<string, (ctx: SqlOperatorContext) => string>>;
    readonly pathSeparator?: string;
    readonly paramPrefix?: string;
}
```

Members (7):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `named` | `readonly named?: boolean;` | optional | Named placeholders (`:p1`, `@p1` on mssql) with `params` as a record. Default false. |
| `inline` | `readonly inline?: boolean;` | optional | Escaped literals instead of placeholders (`params` is empty). Default false. |
| `quoteIdentifier` | `readonly quoteIdentifier?: boolean \| ((name: string) => string);` | optional | Quotes identifiers for the dialect (each path segment), or quotes with a function. Default false. |
| `dialect` | `readonly dialect?: SqlDialect;` | optional | Placeholder and quoting style. Default `'ansi'` (`?`, `"x"`); postgres `$1`, mssql `@p1` and `[x]`, mysql `` `x` ``. |
| `operators` | `readonly operators?: Readonly<Record<string, (ctx: SqlOperatorContext) => string>>;` | optional | SQL of custom operators, keyed by operator name; they also override built-ins. |
| `pathSeparator` | `readonly pathSeparator?: string;` | optional | Separator of nested field paths. Default `'.'`. |
| `paramPrefix` | `readonly paramPrefix?: string;` | optional | Prefix of named parameters. Default `'p'`. |

## Types

### Json

Kind: type

Page: https://salmonui.dev/docs/reference/query-builder/sql/Json

```ts
type Json = null | boolean | number | string | readonly Json[] | {
    readonly [key: string]: Json;
};
```

### QueryNode

Kind: type

Page: https://salmonui.dev/docs/reference/query-builder/sql/QueryNode

```ts
type QueryNode = QueryRule | QueryGroup;
```

### SqlDialect

Kind: type

Page: https://salmonui.dev/docs/reference/query-builder/sql/SqlDialect

```ts
type SqlDialect = 'ansi' | 'postgres' | 'mysql' | 'sqlite' | 'mssql';
```
