@@ -15,18 +15,18 @@ import type { PgViewBase } from '~/pg-core/view-base.ts';
1515import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts' ;
1616import type { AnyRelations , EmptyRelations } from '~/relations.ts' ;
1717import { SelectionProxyHandler } from '~/selection-proxy.ts' ;
18- import type { ColumnsSelection , SQL , SQLWrapper } from '~/sql/sql.ts' ;
18+ import { type ColumnsSelection , type SQL , sql , type SQLWrapper } from '~/sql/sql.ts' ;
1919import { WithSubquery } from '~/subquery.ts' ;
20- import type { DrizzleTypeError } from '~/utils.ts' ;
2120import type { PgColumn } from '../columns/common.ts' ;
2221import { QueryBuilder } from '../query-builders/query-builder.ts' ;
2322import type { SelectedFields } from '../query-builders/select.types.ts' ;
2423import { PgUpdateBuilder } from '../query-builders/update.ts' ;
25- import type { PgQueryResultHKT } from '../session.ts' ;
24+ import type { PgQueryResultHKT , PgQueryResultKind , PreparedQueryConfig } from '../session.ts' ;
2625import type { WithBuilder } from '../subquery.ts' ;
2726import type { PgMaterializedView } from '../view.ts' ;
2827import { PgEffectDeleteBase } from './delete.ts' ;
2928import { PgEffectRelationalQuery , type PgEffectRelationalQueryHKT } from './query.ts' ;
29+ import { PgEffectRaw } from './raw.ts' ;
3030import { PgEffectRefreshMaterializedView } from './refresh-materialized-view.ts' ;
3131import type { PgEffectSession } from './session.ts' ;
3232import { PgEffectUpdateBase , type PgEffectUpdateHKT } from './update.ts' ;
@@ -47,13 +47,6 @@ export class PgEffectDatabase<
4747 readonly session : PgEffectSession < TFullSchema , TRelations , TSchema > ;
4848 } ;
4949
50- /** @deprecated */
51- _query : TFullSchema extends Record < string , never >
52- ? DrizzleTypeError < 'Seems like the schema generic is missing - did you forget to add it to your DB type?' >
53- : {
54- [ K in keyof TSchema ] : _RelationalQueryBuilder < TSchema , TSchema [ K ] > ;
55- } ;
56-
5750 // TO-DO: Figure out how to pass DrizzleTypeError without breaking withReplicas
5851 query : {
5952 [ K in keyof TRelations ] : RelationalQueryBuilder <
@@ -87,21 +80,7 @@ export class PgEffectDatabase<
8780 relations : relations ,
8881 session,
8982 } ;
90- this . _query = { } as typeof this [ '_query' ] ;
91- // if (this._.schema) {
92- // for (const [tableName, columns] of Object.entries(this._.schema)) {
93- // (this._query as PgDatabase<TQueryResult, Record<string, any>>['_query'])[tableName] =
94- // new _RelationalQueryBuilder(
95- // schema!.fullSchema,
96- // this._.schema,
97- // this._.tableNamesMap,
98- // schema!.fullSchema[tableName] as PgTable,
99- // columns,
100- // dialect,
101- // session,
102- // );
103- // }
104- // }
83+
10584 this . query = { } as typeof this [ 'query' ] ;
10685 for ( const [ tableName , relation ] of Object . entries ( relations ) ) {
10786 ( this . query as PgEffectDatabase <
@@ -656,6 +635,27 @@ export class PgEffectDatabase<
656635 ) : PgEffectRefreshMaterializedView < TQueryResult > {
657636 return new PgEffectRefreshMaterializedView ( view , this . session , this . dialect ) ;
658637 }
638+
639+ execute < TRow extends Record < string , unknown > = Record < string , unknown > > (
640+ query : SQLWrapper | string ,
641+ ) : PgEffectRaw < PgQueryResultKind < TQueryResult , TRow > > {
642+ const sequel = typeof query === 'string' ? sql . raw ( query ) : query . getSQL ( ) ;
643+ const builtQuery = this . dialect . sqlToQuery ( sequel ) ;
644+ const prepared = this . session . prepareQuery <
645+ PreparedQueryConfig & { execute : PgQueryResultKind < TQueryResult , TRow > }
646+ > (
647+ builtQuery ,
648+ undefined ,
649+ undefined ,
650+ false ,
651+ ) ;
652+ return new PgEffectRaw (
653+ ( ) => prepared . execute ( ) ,
654+ sequel ,
655+ builtQuery ,
656+ ( result ) => prepared . mapResult ( result , true ) ,
657+ ) ;
658+ }
659659}
660660
661661export type PgEffectWithReplicas < Q > = Q & { $primary : Q ; $replicas : Q [ ] } ;
@@ -686,7 +686,7 @@ export const withReplicas = <
686686 const update : Q [ 'update' ] = ( ...args : [ any ] ) => primary . update ( ...args ) ;
687687 const insert : Q [ 'insert' ] = ( ...args : [ any ] ) => primary . insert ( ...args ) ;
688688 const $delete : Q [ 'delete' ] = ( ...args : [ any ] ) => primary . delete ( ...args ) ;
689- // const execute: Q['execute'] = (...args: [any]) => primary.execute(...args);
689+ const execute : Q [ 'execute' ] = ( ...args : [ any ] ) => primary . execute ( ...args ) ;
690690 // const transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);
691691 const refreshMaterializedView : Q [ 'refreshMaterializedView' ] = ( ...args : [ any ] ) =>
692692 primary . refreshMaterializedView ( ...args ) ;
@@ -696,7 +696,7 @@ export const withReplicas = <
696696 update,
697697 insert,
698698 delete : $delete ,
699- // execute,
699+ execute,
700700 // transaction,
701701 refreshMaterializedView,
702702 $primary : primary ,
@@ -707,9 +707,6 @@ export const withReplicas = <
707707 $count,
708708 $with,
709709 with : _with ,
710- get _query ( ) {
711- return getReplica ( replicas ) . _query ;
712- } ,
713710 get query ( ) {
714711 return getReplica ( replicas ) . query ;
715712 } ,
0 commit comments