Skip to content

Commit

Permalink
Interim
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed May 7, 2024
1 parent 9814b18 commit 47d3f51
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 137 deletions.
4 changes: 2 additions & 2 deletions src/core/Column.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {input, type Input} from './Expr.ts'
import type {Field, FieldData} from './Field.ts'
import type {HasColumn, HasSql} from './Internal.ts'
import {getField, internalColumn} from './Internal.ts'
import type {Sql} from './Sql.ts'
import type {Field, FieldData} from './expr/Field.ts'
import {input, type Input} from './expr/Input.ts'

export class ColumnData {
type!: Sql
Expand Down
2 changes: 1 addition & 1 deletion src/core/Constraint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {Field, FieldData} from './Field.ts'
import {
getData,
getField,
Expand All @@ -8,6 +7,7 @@ import {
type HasData
} from './Internal.ts'
import {sql} from './Sql.ts'
import type {Field, FieldData} from './expr/Field.ts'

export interface UniqueConstraintData {
fields: Array<FieldData>
Expand Down
2 changes: 1 addition & 1 deletion src/core/Emitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {ColumnData} from './Column.ts'
import type {FieldData} from './Field.ts'
import {
getData,
getQuery,
Expand All @@ -11,6 +10,7 @@ import {
} from './Internal.ts'
import {ValueParam, type Param} from './Param.ts'
import {sql} from './Sql.ts'
import type {FieldData} from './expr/Field.ts'
import type {Create} from './query/CreateTable.ts'
import type {Delete} from './query/Delete.ts'
import type {Drop} from './query/DropTable.ts'
Expand Down
15 changes: 0 additions & 15 deletions src/core/Functions.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/Index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {Field, FieldData} from './Field.ts'
import {
getData,
getField,
internalData,
type HasData,
type HasSql
} from './Internal.ts'
import type {Field, FieldData} from './expr/Field.ts'

export interface IndexData {
fields: Array<FieldData>
Expand Down
2 changes: 1 addition & 1 deletion src/core/Internal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {ColumnData} from './Column.ts'
import type {FieldData} from './Field.ts'
import type {QueryMeta} from './MetaData.ts'
import type {Resolver} from './Resolver.ts'
import type {Selection} from './Selection.ts'
import type {Sql} from './Sql.ts'
import type {TableApi, TableDefinition} from './Table.ts'
import type {FieldData} from './expr/Field.ts'

export const internalData = Symbol()
export const internalSql = Symbol()
Expand Down
3 changes: 2 additions & 1 deletion src/core/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export class Selection implements HasSql {
const expr = this.#exprOf(input)
if (expr) {
const value = values.shift()
if (expr.mapFromDriverValue) return expr.mapFromDriverValue(value)
if (value !== null && expr.mapFromDriverValue)
return expr.mapFromDriverValue(value)
return value
}
const result: Record<string, unknown> = {}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Sql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Emitter} from './Emitter.ts'
import type {FieldData} from './Field.ts'
import {getSql, internalSql, type HasSql} from './Internal.ts'
import type {FieldData} from './expr/Field.ts'

type EmitMethods = {
[K in keyof Emitter as K extends `emit${string}` ? K : never]: Emitter[K]
Expand Down
5 changes: 3 additions & 2 deletions src/core/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type {
PrimaryKeyConstraint,
UniqueConstraint
} from './Constraint.ts'
import {jsonExpr, type Input, type JsonExpr} from './Expr.ts'
import {Field} from './Field.ts'
import type {Index} from './Index.ts'
import {
getColumn,
Expand All @@ -20,6 +18,9 @@ import {
type HasTarget
} from './Internal.ts'
import {sql, type Sql} from './Sql.ts'
import {jsonExpr, type JsonExpr} from './expr/Conditions.ts'
import {Field} from './expr/Field.ts'
import type {Input} from './expr/Input.ts'

const {assign, fromEntries, entries} = Object

Expand Down
2 changes: 1 addition & 1 deletion src/core/Virtual.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Field} from './Field.ts'
import {getSql, hasSql} from './Internal.ts'
import type {SelectionInput} from './Selection.ts'
import {Field} from './expr/Field.ts'

export function virtual<Input extends SelectionInput>(
alias: string,
Expand Down
36 changes: 36 additions & 0 deletions src/core/expr/Aggregate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {getSql, type HasSql} from '../Internal.ts'
import {sql, type Sql} from '../Sql.ts'
import {distinct} from './Conditions.ts'
import {Functions} from './Functions.ts'

export function count(input?: HasSql): Sql<number> {
return Functions.count(input ?? sql`*`).mapWith(Number)
}

export function countDistinct(input: HasSql): Sql<number> {
return Functions.count(distinct(input)).mapWith(Number)
}

export function avg(input: HasSql): Sql<string | null> {
return Functions.avg(input).mapWith(String)
}

export function avgDistinct(input: HasSql): Sql<string | null> {
return Functions.avg(distinct(input)).mapWith(String)
}

export function sum(input: HasSql): HasSql<string | null> {
return Functions.sum(input).mapWith(String)
}

export function sumDistinct(input: HasSql): HasSql<string | null> {
return Functions.sum(distinct(input)).mapWith(String)
}

export function max<T>(input: HasSql<T>) {
return Functions.max(input).mapWith(getSql(input))
}

export function min<T>(input: HasSql<T>) {
return Functions.min(input).mapWith(getSql(input))
}
26 changes: 11 additions & 15 deletions src/core/Expr.ts → src/core/expr/Conditions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import {getSql, getTable, hasSql, hasTable, type HasSql} from './Internal.ts'
import {sql, type Sql} from './Sql.ts'

export type Input<T = unknown> = HasSql<T> | T

export function input<T>(value: Input<T>): HasSql<T> {
if (typeof value !== 'object' || value === null) return sql.value(value)
if (hasTable(value)) return sql.identifier(getTable(value).name)
if (hasSql(value)) return getSql(value) as Sql<T>
return sql.value(value)
}
import type {HasSql} from '../Internal.ts'
import {sql, type Sql} from '../Sql.ts'
import {type Input, input} from './Input.ts'

export function eq<T>(left: Input<T>, right: Input<T>): HasSql<boolean> {
return sql`${input(left)} = ${input(right)}`
Expand Down Expand Up @@ -148,12 +140,16 @@ export function arrayOverlaps<T>(
return sql`${input(left)} && ${input(right)}`
}

export function asc<T>(column: HasSql<T>): Sql {
return sql`${column} asc`
export function asc<T>(input: HasSql<T>): Sql {
return sql`${input} asc`
}

export function desc<T>(input: HasSql<T>): Sql {
return sql`${input} desc`
}

export function desc<T>(column: HasSql<T>): Sql {
return sql`${column} desc`
export function distinct<T>(input: HasSql<T>): Sql {
return sql`distinct ${input}`
}

export interface JsonArrayHasSql<Value> extends HasSql<Value> {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Field.ts → src/core/expr/Field.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {internalField, internalSql, type HasSql} from './Internal.ts'
import {sql, type Sql} from './Sql.ts'
import {internalField, internalSql, type HasSql} from '../Internal.ts'
import {sql, type Sql} from '../Sql.ts'

export interface FieldData {
targetName: string
Expand Down
14 changes: 14 additions & 0 deletions src/core/expr/Functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {sql, type Sql} from '../Sql.ts'
import {input, type Input} from './Input.ts'

function get(target: Record<string, Function>, method: string) {
return (target[method] ??= (...args: Array<Input<unknown>>) => {
return sql`${sql.identifier(method)}(${sql.join(args.map(input), sql`, `)})`
})
}

export const Functions = <Functions>new Proxy(Object.create(null), {get})

export type Functions = {
[key: string]: (...args: Array<Input<any>>) => Sql<any>
}
11 changes: 11 additions & 0 deletions src/core/expr/Input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {getSql, getTable, hasSql, hasTable, type HasSql} from '../Internal.ts'
import {sql, type Sql} from '../Sql.ts'

export type Input<T = unknown> = HasSql<T> | T

export function input<T>(value: Input<T>): HasSql<T> {
if (typeof value !== 'object' || value === null) return sql.value(value)
if (hasTable(value)) return sql.identifier(getTable(value).name)
if (hasSql(value)) return getSql(value) as Sql<T>
return sql.value(value)
}
2 changes: 1 addition & 1 deletion src/core/query/Insert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {input, type Input} from '../Expr.ts'
import {
getColumn,
getData,
Expand All @@ -24,6 +23,7 @@ import type {
TableRow,
TableUpdate
} from '../Table.ts'
import {input, type Input} from '../expr/Input.ts'

class InsertIntoData<Meta extends QueryMeta> extends QueryData<Meta> {
into!: HasTable
Expand Down
4 changes: 2 additions & 2 deletions src/core/query/Select.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {input, type Input as UserInput} from '../Expr.ts'
import type {Field} from '../Field.ts'
import {
getData,
getQuery,
Expand Down Expand Up @@ -29,6 +27,8 @@ import {
import {sql} from '../Sql.ts'
import type {Table, TableDefinition, TableFields} from '../Table.ts'
import type {Expand} from '../Types.ts'
import type {Field} from '../expr/Field.ts'
import {input, type Input as UserInput} from '../expr/Input.ts'
import {Union} from './Union.ts'

export type SelectionType = 'selection' | 'allFrom' | 'joinTables'
Expand Down
2 changes: 1 addition & 1 deletion src/core/query/Update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {input} from '../Expr.ts'
import {
getData,
internalData,
Expand All @@ -17,6 +16,7 @@ import {
} from '../Selection.ts'
import {sql, type Sql} from '../Sql.ts'
import type {TableDefinition, TableUpdate} from '../Table.ts'
import {input} from '../expr/Input.ts'

export class UpdateData<
Meta extends QueryMeta = QueryMeta
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ export * from './core/Column.ts'
export * from './core/Constraint.ts'
export * from './core/Database.ts'
export * from './core/Driver.ts'
export * from './core/Expr.ts'
export * from './core/Index.ts'
export * from './core/Query.ts'
export * from './core/Selection.ts'
export * from './core/Sql.ts'
export * from './core/Table.ts'
export * from './core/expr/Conditions.ts'
export * from './core/query/CreateTable.ts'
export * from './core/query/Delete.ts'
export * from './core/query/Insert.ts'
Expand Down
Loading

0 comments on commit 47d3f51

Please sign in to comment.