Skip to content

Commit

Permalink
Resolve circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
dankochetov committed Oct 12, 2023
1 parent 33f4f10 commit 2881b4c
Show file tree
Hide file tree
Showing 37 changed files with 736 additions and 730 deletions.
3 changes: 2 additions & 1 deletion drizzle-orm/src/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { entityKind, is } from './entity.ts';
import type { Relation } from './relations.ts';
import { SQL, sql } from './sql/index.ts';
import { Table } from './table.ts';
import { type View, ViewBaseConfig } from './view.ts';
import { ViewBaseConfig } from './view-common.ts';
import type { View } from './view.ts';

export class ColumnAliasProxyHandler<TColumn extends Column> implements ProxyHandler<TColumn> {
static readonly [entityKind]: string = 'ColumnAliasProxyHandler';
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export * from './sql/index.ts';
export * from './subquery.ts';
export * from './table.ts';
export * from './utils.ts';
export * from './view-common.ts';
export * from './view.ts';
4 changes: 2 additions & 2 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { and, eq, Param, type QueryWithTypings, SQL, sql, type SQLChunk } from '
import { Subquery, SubqueryConfig } from '~/subquery.ts';
import { getTableName, Table } from '~/table.ts';
import { orderSelectedFields, type UpdateSet } from '~/utils.ts';
import { View, ViewBaseConfig } from '~/view.ts';
import { DrizzleError } from '../index.ts';
import { View } from '~/view.ts';
import { DrizzleError, ViewBaseConfig } from '../index.ts';
import { MySqlColumn } from './columns/common.ts';
import type { MySqlDeleteConfig } from './query-builders/delete.ts';
import type { MySqlInsertConfig } from './query-builders/insert.ts';
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/mysql-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export * from './subquery.ts';
export * from './table.ts';
export * from './unique-constraint.ts';
export * from './utils.ts';
export * from './view-common.ts';
export * from './view.ts';
16 changes: 5 additions & 11 deletions drizzle-orm/src/mysql-core/query-builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ import {
type TableRelationalConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import { type Query, type QueryWithTypings, type SQL } from '~/sql/index.ts';
import { type KnownKeysOnly } from '~/utils.ts';
import { type MySqlDialect } from '../dialect.ts';
import {
type Mode,
type MySqlSession,
type PreparedQueryConfig,
type PreparedQueryHKTBase,
type PreparedQueryKind,
} from '../session.ts';
import { type MySqlTable } from '../table.ts';
import type { Query, QueryWithTypings, SQL } from '~/sql/index.ts';
import type { KnownKeysOnly } from '~/utils.ts';
import type { MySqlDialect } from '../dialect.ts';
import type { Mode, MySqlSession, PreparedQueryConfig, PreparedQueryHKTBase, PreparedQueryKind } from '../session.ts';
import type { MySqlTable } from '../table.ts';

export class RelationalQueryBuilder<
TPreparedQueryHKT extends PreparedQueryHKTBase,
Expand Down
3 changes: 2 additions & 1 deletion drizzle-orm/src/mysql-core/query-builders/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { SelectionProxyHandler, Subquery, SubqueryConfig } from '~/subquery.ts';
import { Table } from '~/table.ts';
import { applyMixins, getTableColumns, getTableLikeName, type ValueOrArray } from '~/utils.ts';
import { orderSelectedFields } from '~/utils.ts';
import { type ColumnsSelection, View, ViewBaseConfig } from '~/view.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import { type ColumnsSelection, View } from '~/view.ts';
import type {
CreateMySqlSelectFromBuilderMode,
LockConfig,
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/mysql-core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { is } from '~/entity.ts';
import { Table } from '~/table.ts';
import { ViewBaseConfig } from '~/view.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import type { Check } from './checks.ts';
import { CheckBuilder } from './checks.ts';
import type { ForeignKey } from './foreign-keys.ts';
Expand All @@ -11,8 +11,8 @@ import type { PrimaryKey } from './primary-keys.ts';
import { PrimaryKeyBuilder } from './primary-keys.ts';
import { MySqlTable } from './table.ts';
import { type UniqueConstraint, UniqueConstraintBuilder } from './unique-constraint.ts';
import { MySqlViewConfig } from './view-common.ts';
import type { MySqlView } from './view.ts';
import { MySqlViewConfig } from './view.ts';

export function getTableConfig(table: MySqlTable) {
const columns = Object.values(table[MySqlTable.Symbol.Columns]);
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/mysql-core/view-common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MySqlViewConfig = Symbol.for('drizzle:MySqlViewConfig');
3 changes: 1 addition & 2 deletions drizzle-orm/src/mysql-core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { type MySqlColumn, type MySqlColumnBuilderBase } from './columns/index.t
import { QueryBuilder } from './query-builders/index.ts';
import type { SelectedFields } from './query-builders/select.types.ts';
import { mysqlTable } from './table.ts';
import { MySqlViewConfig } from './view-common.ts';

export interface ViewBuilderConfig {
algorithm?: 'undefined' | 'merge' | 'temptable';
Expand Down Expand Up @@ -163,8 +164,6 @@ export abstract class MySqlViewBase<
};
}

export const MySqlViewConfig = Symbol.for('drizzle:MySqlViewConfig');

export class MySqlView<
TName extends string = string,
TExisting extends boolean = boolean,
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/pg-core/columns/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import { makePgArray, parsePgArray } from '../utils.ts';
import { makePgArray, parsePgArray } from '../utils/array.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

export class PgArrayBuilder<
Expand Down
3 changes: 2 additions & 1 deletion drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
import { Subquery, SubqueryConfig } from '~/subquery.ts';
import { getTableName, Table } from '~/table.ts';
import { orderSelectedFields, type UpdateSet } from '~/utils.ts';
import { View, ViewBaseConfig } from '~/view.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import { View } from '~/view.ts';
import type { PgSession } from './session.ts';
import { type PgMaterializedView, PgViewBase } from './view.ts';

Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/src/pg-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export * from './subquery.ts';
export * from './table.ts';
export * from './unique-constraint.ts';
export * from './utils.ts';
export * from './utils/index.ts';
export * from './view-common.ts';
export * from './view.ts';
3 changes: 2 additions & 1 deletion drizzle-orm/src/pg-core/query-builders/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import { Table } from '~/table.ts';
import { tracer } from '~/tracing.ts';
import { applyMixins, getTableColumns, getTableLikeName, type ValueOrArray } from '~/utils.ts';
import { orderSelectedFields } from '~/utils.ts';
import { type ColumnsSelection, View, ViewBaseConfig } from '~/view.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import { type ColumnsSelection, View } from '~/view.ts';
import type {
CreatePgSelectFromBuilderMode,
LockConfig,
Expand Down
101 changes: 3 additions & 98 deletions drizzle-orm/src/pg-core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { is } from '~/entity.ts';
import { PgTable } from '~/pg-core/table.ts';
import { Table } from '~/table.ts';
import { ViewBaseConfig } from '~/view.ts';
import { ViewBaseConfig } from '~/view-common.ts';
import { type Check, CheckBuilder } from './checks.ts';
import type { AnyPgColumn } from './columns/index.ts';
import { type ForeignKey, ForeignKeyBuilder } from './foreign-keys.ts';
import { type Index, IndexBuilder } from './indexes.ts';
import { type PrimaryKey, PrimaryKeyBuilder } from './primary-keys.ts';
import { type UniqueConstraint, UniqueConstraintBuilder } from './unique-constraint.ts';
import { type PgMaterializedView, PgMaterializedViewConfig, type PgView, PgViewConfig } from './view.ts';
import { PgViewConfig } from './view-common.ts';
import { type PgMaterializedView, PgMaterializedViewConfig, type PgView } from './view.ts';

export function getTableConfig<TTable extends PgTable>(table: TTable) {
const columns = Object.values(table[Table.Symbol.Columns]);
Expand Down Expand Up @@ -71,102 +72,6 @@ export function getMaterializedViewConfig<
};
}

function parsePgArrayValue(arrayString: string, startFrom: number, inQuotes: boolean): [string, number] {
for (let i = startFrom; i < arrayString.length; i++) {
const char = arrayString[i];

if (char === '\\') {
i++;
continue;
}

if (char === '"') {
return [arrayString.slice(startFrom, i).replace(/\\/g, ''), i + 1];
}

if (inQuotes) {
continue;
}

if (char === ',' || char === '}') {
return [arrayString.slice(startFrom, i).replace(/\\/g, ''), i];
}
}

return [arrayString.slice(startFrom).replace(/\\/g, ''), arrayString.length];
}

export function parsePgNestedArray(arrayString: string, startFrom = 0): [any[], number] {
const result: any[] = [];
let i = startFrom;
let lastCharIsComma = false;

while (i < arrayString.length) {
const char = arrayString[i];

if (char === ',') {
if (lastCharIsComma || i === startFrom) {
result.push('');
}
lastCharIsComma = true;
i++;
continue;
}

lastCharIsComma = false;

if (char === '\\') {
i += 2;
continue;
}

if (char === '"') {
const [value, startFrom] = parsePgArrayValue(arrayString, i + 1, true);
result.push(value);
i = startFrom;
continue;
}

if (char === '}') {
return [result, i + 1];
}

if (char === '{') {
const [value, startFrom] = parsePgNestedArray(arrayString, i + 1);
result.push(value);
i = startFrom;
continue;
}

const [value, newStartFrom] = parsePgArrayValue(arrayString, i, false);
result.push(value);
i = newStartFrom;
}

return [result, i];
}

export function parsePgArray(arrayString: string): any[] {
const [result] = parsePgNestedArray(arrayString, 1);
return result;
}

export function makePgArray(array: any[]): string {
return `{${
array.map((item) => {
if (Array.isArray(item)) {
return makePgArray(item);
}
if (typeof item === 'string' && item.includes(',')) {
return `"${item.replace(/"/g, '\\"')}"`;
}
return `${item}`;
}).join(',')
}}`;
}

export type ColumnsWithTable<
TTableName extends string,
TForeignTableName extends string,
Expand Down
95 changes: 95 additions & 0 deletions drizzle-orm/src/pg-core/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
function parsePgArrayValue(arrayString: string, startFrom: number, inQuotes: boolean): [string, number] {
for (let i = startFrom; i < arrayString.length; i++) {
const char = arrayString[i];

if (char === '\\') {
i++;
continue;
}

if (char === '"') {
return [arrayString.slice(startFrom, i).replace(/\\/g, ''), i + 1];
}

if (inQuotes) {
continue;
}

if (char === ',' || char === '}') {
return [arrayString.slice(startFrom, i).replace(/\\/g, ''), i];
}
}

return [arrayString.slice(startFrom).replace(/\\/g, ''), arrayString.length];
}

export function parsePgNestedArray(arrayString: string, startFrom = 0): [any[], number] {
const result: any[] = [];
let i = startFrom;
let lastCharIsComma = false;

while (i < arrayString.length) {
const char = arrayString[i];

if (char === ',') {
if (lastCharIsComma || i === startFrom) {
result.push('');
}
lastCharIsComma = true;
i++;
continue;
}

lastCharIsComma = false;

if (char === '\\') {
i += 2;
continue;
}

if (char === '"') {
const [value, startFrom] = parsePgArrayValue(arrayString, i + 1, true);
result.push(value);
i = startFrom;
continue;
}

if (char === '}') {
return [result, i + 1];
}

if (char === '{') {
const [value, startFrom] = parsePgNestedArray(arrayString, i + 1);
result.push(value);
i = startFrom;
continue;
}

const [value, newStartFrom] = parsePgArrayValue(arrayString, i, false);
result.push(value);
i = newStartFrom;
}

return [result, i];
}

export function parsePgArray(arrayString: string): any[] {
const [result] = parsePgNestedArray(arrayString, 1);
return result;
}

export function makePgArray(array: any[]): string {
return `{${
array.map((item) => {
if (Array.isArray(item)) {
return makePgArray(item);
}
if (typeof item === 'string' && item.includes(',')) {
return `"${item.replace(/"/g, '\\"')}"`;
}
return `${item}`;
}).join(',')
}}`;
}
1 change: 1 addition & 0 deletions drizzle-orm/src/pg-core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './array.ts';
1 change: 1 addition & 0 deletions drizzle-orm/src/pg-core/view-common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PgViewConfig = Symbol.for('drizzle:PgViewConfig');
3 changes: 1 addition & 2 deletions drizzle-orm/src/pg-core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { PgColumn, PgColumnBuilderBase } from './columns/common.ts';
import { QueryBuilder } from './query-builders/index.ts';
import type { SelectedFields } from './query-builders/select.types.ts';
import { pgTable } from './table.ts';
import { PgViewConfig } from './view-common.ts';

export interface ViewWithConfig {
checkOption: 'local' | 'cascaded';
Expand Down Expand Up @@ -282,8 +283,6 @@ export abstract class PgViewBase<
};
}

export const PgViewConfig = Symbol.for('drizzle:PgViewConfig');

export class PgView<
TName extends string = string,
TExisting extends boolean = boolean,
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/sql/expressions/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type SQLChunk,
type SQLWrapper,
StringChunk,
} from '../index.ts';
} from '../sql.ts';

export function bindIfParam(value: unknown, column: SQLWrapper): SQLChunk {
if (
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/sql/expressions/select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnyColumn } from '../../column.ts';
import type { SQL, SQLWrapper } from '../index.ts';
import { sql } from '../index.ts';
import type { SQL, SQLWrapper } from '../sql.ts';
import { sql } from '../sql.ts';

/**
* Used in sorting, this specifies that the given
Expand Down
Loading

0 comments on commit 2881b4c

Please sign in to comment.