Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:cesumilo/Denobjection into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yzalium committed Jul 11, 2024
2 parents 1c80d42 + 5458700 commit 5c0e939
Show file tree
Hide file tree
Showing 28 changed files with 3,568 additions and 1,577 deletions.
4 changes: 4 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"include": [
"./tests"
]
},
"compilerOptions": {
"experimentalDecorators": true,
"noImplicitOverride": true
}
}
34 changes: 0 additions & 34 deletions src/queryBuilder/FunctionBuilder.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/queryBuilder/FunctionBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { normalizeRawArgs, RawBuilder } from './RawBuilder.ts';
import { asSingle, isNumber } from '../utils/object.ts';
import { nany } from '../ninja.ts';

class FunctionBuilder extends RawBuilder {}

const keywords = [
'coalesce',
'concat',
'sum',
'avg',
'min',
'max',
'count',
'upper',
'lower',
// deno-lint-ignore no-explicit-any
].reduce((c: any, p: string) => {
c[p] = true;
return c;
}, {});

function fn(...argsIn: [string, ...nany[]]) {
const { sql, args } = normalizeRawArgs(argsIn);
return new FunctionBuilder(`${sql}(${args.map(() => '?').join(', ')})`, args);
}

export function createFunctionBuilder(
name: string,
): (...args: nany[]) => FunctionBuilder {
if (name === 'now') {
return (precision: string | string[]) => {
let p = parseInt(asSingle(precision), 10);

if (isNaN(p) || !isNumber(p)) {
p = 6;
}

// We need to use a literal precision instead of a binding here
// for the CURRENT_TIMESTAMP to work. This is okay here since we
// make sure `precision` is a number. There's no chance of SQL
// injection here.
return new FunctionBuilder(`CURRENT_TIMESTAMP(${p})`, []);
};
} else if (keywords[name]) {
return (...args: nany[]) => fn(name, args);
}
throw new Error(`Function ${name} not supported.`);
}
29 changes: 0 additions & 29 deletions src/queryBuilder/InternalOptions.js

This file was deleted.

27 changes: 0 additions & 27 deletions src/queryBuilder/QueryBuilderContext.js

This file was deleted.

21 changes: 12 additions & 9 deletions src/queryBuilder/QueryBuilderContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { nany } from '../ninja.ts';
import { QueryBuilderContextBase } from './QueryBuilderContextBase.ts';
import { IModel } from './QueryBuilderOperationSupport.ts';

export class QueryBuilderContext extends QueryBuilderContextBase {
export class QueryBuilderContext<T extends IModel>
extends QueryBuilderContextBase<T> {
runBefore: nany[];
runAfter: nany[];
onBuild: nany[];
Expand All @@ -14,14 +16,15 @@ export class QueryBuilderContext extends QueryBuilderContextBase {
this.onBuild = [];
}

clone() {
const ctx = new QueryBuilderContext();
super.cloneInto(ctx);

ctx.runBefore = this.runBefore.slice();
ctx.runAfter = this.runAfter.slice();
ctx.onBuild = this.onBuild.slice();
override clone(): QueryBuilderContext<T> {
return this.cloneInto(new QueryBuilderContext<T>());
}

return ctx;
override cloneInto(clone: QueryBuilderContext<T>): QueryBuilderContext<T> {
super.cloneInto(clone);
clone.runBefore = this.runBefore.slice();
clone.runAfter = this.runAfter.slice();
clone.onBuild = this.onBuild.slice();
return clone;
}
}
35 changes: 0 additions & 35 deletions src/queryBuilder/QueryBuilderContextBase.js

This file was deleted.

34 changes: 20 additions & 14 deletions src/queryBuilder/QueryBuilderContextBase.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Knex } from 'knex';
import { nany } from '../ninja.ts';
import { InternalOptions } from './InternalOptions.ts';
import { QueryBuilderUserContext } from './QueryBuilderUserContext.ts';
import {
IModel,
QueryBuilderOperationSupport,
} from './QueryBuilderOperationSupport.ts';

export class QueryBuilderContextBase {
userContext?: QueryBuilderUserContext;
export class QueryBuilderContextBase<T extends IModel> {
userContext?: QueryBuilderUserContext<T>;
options?: InternalOptions;
knex?: Knex;
aliasMap?: Map<nany, nany>;
tableMap?: Map<nany, nany>;
aliasMap?: Map<string, string>;
tableMap?: Map<string, string>;

constructor(builder?: nany) {
constructor(builder?: QueryBuilderOperationSupport<T>) {
this.userContext = builder
? new QueryBuilderUserContext(builder)
: undefined;
Expand All @@ -21,13 +24,16 @@ export class QueryBuilderContextBase {
return InternalOptions;
}

cloneInto(
newContext: QueryBuilderContextBase,
): void {
newContext.userContext = this.userContext;
newContext.options = this.options?.clone();
newContext.knex = this.knex;
newContext.aliasMap = this.aliasMap;
newContext.tableMap = this.tableMap;
clone(): QueryBuilderContextBase<T> {
return this.cloneInto(new QueryBuilderContextBase<T>());
}

cloneInto(context: QueryBuilderContextBase<T>): QueryBuilderContextBase<T> {
context.userContext = this.userContext;
context.options = this.options?.clone();
context.knex = this.knex;
context.aliasMap = this.aliasMap;
context.tableMap = this.tableMap;
return context;
}
}
Loading

0 comments on commit 5c0e939

Please sign in to comment.