Skip to content

Commit

Permalink
CREATE and DROP MILESTONE
Browse files Browse the repository at this point in the history
  • Loading branch information
drodrigues4 committed Jul 24, 2024
1 parent 3f844c4 commit feffcfd
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 0 deletions.
14 changes: 14 additions & 0 deletions drizzle-orm/src/singlestore-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import type {
} from './session.ts';
import type { WithSubqueryWithSelection } from './subquery.ts';
import type { SingleStoreTable } from './table.ts';
import { SingleStoreCreateMilestoneBase } from './query-builders/createMilestone.ts';
import { SingleStoreDropMilestoneBase } from './query-builders/dropMilestone.ts';

export class SingleStoreDatabase<
TQueryResult extends SingleStoreQueryResultHKT,
Expand Down Expand Up @@ -495,6 +497,18 @@ export class SingleStoreDatabase<
): SingleStoreBranchBase<TDatabase, TQueryResult, TPreparedQueryHKT> {
return new SingleStoreBranchBase(database, branchName, this.session, this.dialect);
}

createMilestone<TMilestone extends string>(
milestone: TMilestone,
): SingleStoreCreateMilestoneBase<TMilestone, TQueryResult, TPreparedQueryHKT> {
return new SingleStoreCreateMilestoneBase(milestone, this.session, this.dialect);
}

dropMilestone<TMilestone extends string>(
milestone: TMilestone,
): SingleStoreDropMilestoneBase<TMilestone, TQueryResult, TPreparedQueryHKT> {
return new SingleStoreDropMilestoneBase(milestone, this.session, this.dialect);
}
}

export type SingleStoreWithReplicas<Q> = Q & { $primary: Q };
Expand Down
14 changes: 14 additions & 0 deletions drizzle-orm/src/singlestore-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import type { SingleStoreUpdateConfig } from './query-builders/update.ts';
import type { SingleStoreSession } from './session.ts';
import { SingleStoreTable } from './table.ts';
import { SingleStoreViewBase } from './view-base.ts';
import type { SingleStoreCreateMilestoneConfig } from './query-builders/createMilestone.ts';
import type { SingleStoreDropMilestoneConfig } from './query-builders/dropMilestone.ts';

export class SingleStoreDialect {
static readonly [entityKind]: string = 'SingleStoreDialect';
Expand Down Expand Up @@ -150,6 +152,18 @@ export class SingleStoreDialect {
}${fromWorkspaceGroupSql}${readOnlySql}${asSql}${milestoneSql}${timeSql}`;
}

buildCreateMilestoneQuery({ database, milestone }: SingleStoreCreateMilestoneConfig): SQL {
const forSql = database ? sql` for ${sql.identifier(database)}` : undefined;

return sql`create milestone ${milestone}${forSql}`;
}

buildDropMilestoneQuery({ database, milestone }: SingleStoreDropMilestoneConfig): SQL {
const forSql = database ? sql` for ${sql.identifier(database)}` : undefined;

return sql`drop milestone ${milestone}${forSql}`;
}

buildUpdateSet(table: SingleStoreTable, set: UpdateSet): SQL {
const tableColumns = table[Table.Symbol.Columns];

Expand Down
136 changes: 136 additions & 0 deletions drizzle-orm/src/singlestore-core/query-builders/createMilestone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { entityKind } from '~/entity.ts';
import { QueryPromise } from '~/query-promise.ts';
import type { SingleStoreDialect } from '~/singlestore-core/dialect.ts';
import type {
AnySingleStoreQueryResultHKT,
PreparedQueryHKTBase,
PreparedQueryKind,
SingleStorePreparedQueryConfig,
SingleStoreQueryResultHKT,
SingleStoreQueryResultKind,
SingleStoreSession,
} from '~/singlestore-core/session.ts';
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';

export type SingleStoreCreateMilestoneWithout<
T extends AnySingleStoreCreateMilestoneBase,
TDynamic extends boolean,
K extends keyof T & string,
> = TDynamic extends true ? T
: Omit<
SingleStoreCreateMilestoneBase<
T['_']['milestone'],
T['_']['queryResult'],
T['_']['preparedQueryHKT'],
TDynamic,
T['_']['excludedMethods'] | K
>,
T['_']['excludedMethods'] | K
>;

export type SingleStoreCreateMilestone<
TDatabase extends string = string,
TQueryResult extends SingleStoreQueryResultHKT = AnySingleStoreQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase = PreparedQueryHKTBase,
> = SingleStoreCreateMilestoneBase<TDatabase, TQueryResult, TPreparedQueryHKT, true, never>;

export interface SingleStoreCreateMilestoneConfig {
milestone: string;
database?: string | undefined;
}

export type SingleStoreCreateMilestonePrepare<T extends AnySingleStoreCreateMilestoneBase> = PreparedQueryKind<
T['_']['preparedQueryHKT'],
SingleStorePreparedQueryConfig & {
execute: SingleStoreQueryResultKind<T['_']['queryResult'], never>;
iterator: never;
},
true
>;

type SingleStoreCreateMilestoneDynamic<T extends AnySingleStoreCreateMilestoneBase> = SingleStoreCreateMilestone<
T['_']['milestone'],
T['_']['queryResult'],
T['_']['preparedQueryHKT']
>;

type AnySingleStoreCreateMilestoneBase = SingleStoreCreateMilestoneBase<any, any, any, any, any>;

export interface SingleStoreCreateMilestoneBase<
TMilestone extends string,
TQueryResult extends SingleStoreQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase,
TDynamic extends boolean = false,
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> {
readonly _: {
readonly milestone: TMilestone;
readonly queryResult: TQueryResult;
readonly preparedQueryHKT: TPreparedQueryHKT;
readonly dynamic: TDynamic;
readonly excludedMethods: TExcludedMethods;
};
}

export class SingleStoreCreateMilestoneBase<
TMilestone extends string,
TQueryResult extends SingleStoreQueryResultHKT,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TPreparedQueryHKT extends PreparedQueryHKTBase,
TDynamic extends boolean = false,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> implements SQLWrapper {
static readonly [entityKind]: string = 'SingleStoreCreateMilestone';

private config: SingleStoreCreateMilestoneConfig;

constructor(
private milestone: TMilestone,
private session: SingleStoreSession,
private dialect: SingleStoreDialect,
) {
super();
this.config = { milestone };
}

// TODO(singlestore): docs
for(database: string): SingleStoreCreateMilestoneWithout<this, TDynamic, 'for'> {
this.config.database = database;
return this as any;
}

/** @internal */
getSQL(): SQL {
return this.dialect.buildCreateMilestoneQuery(this.config);
}

toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}

prepare(): SingleStoreCreateMilestonePrepare<this> {
return this.session.prepareQuery(
this.dialect.sqlToQuery(this.getSQL()),
undefined,
) as SingleStoreCreateMilestonePrepare<this>;
}

override execute: ReturnType<this['prepare']>['execute'] = (placeholderValues) => {
return this.prepare().execute(placeholderValues);
};

private createIterator = (): ReturnType<this['prepare']>['iterator'] => {
const self = this;
return async function*(placeholderValues) {
yield* self.prepare().iterator(placeholderValues);
};
};

iterator = this.createIterator();

$dynamic(): SingleStoreCreateMilestoneDynamic<this> {
return this as any;
}
}
136 changes: 136 additions & 0 deletions drizzle-orm/src/singlestore-core/query-builders/dropMilestone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { entityKind } from '~/entity.ts';
import { QueryPromise } from '~/query-promise.ts';
import type { SingleStoreDialect } from '~/singlestore-core/dialect.ts';
import type {
AnySingleStoreQueryResultHKT,
PreparedQueryHKTBase,
PreparedQueryKind,
SingleStorePreparedQueryConfig,
SingleStoreQueryResultHKT,
SingleStoreQueryResultKind,
SingleStoreSession,
} from '~/singlestore-core/session.ts';
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';

export type SingleStoreDropMilestoneWithout<
T extends AnySingleStoreDropMilestoneBase,
TDynamic extends boolean,
K extends keyof T & string,
> = TDynamic extends true ? T
: Omit<
SingleStoreDropMilestoneBase<
T['_']['milestone'],
T['_']['queryResult'],
T['_']['preparedQueryHKT'],
TDynamic,
T['_']['excludedMethods'] | K
>,
T['_']['excludedMethods'] | K
>;

export type SingleStoreDropMilestone<
TDatabase extends string = string,
TQueryResult extends SingleStoreQueryResultHKT = AnySingleStoreQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase = PreparedQueryHKTBase,
> = SingleStoreDropMilestoneBase<TDatabase, TQueryResult, TPreparedQueryHKT, true, never>;

export interface SingleStoreDropMilestoneConfig {
milestone: string;
database?: string | undefined;
}

export type SingleStoreDropMilestonePrepare<T extends AnySingleStoreDropMilestoneBase> = PreparedQueryKind<
T['_']['preparedQueryHKT'],
SingleStorePreparedQueryConfig & {
execute: SingleStoreQueryResultKind<T['_']['queryResult'], never>;
iterator: never;
},
true
>;

type SingleStoreDropMilestoneDynamic<T extends AnySingleStoreDropMilestoneBase> = SingleStoreDropMilestone<
T['_']['milestone'],
T['_']['queryResult'],
T['_']['preparedQueryHKT']
>;

type AnySingleStoreDropMilestoneBase = SingleStoreDropMilestoneBase<any, any, any, any, any>;

export interface SingleStoreDropMilestoneBase<
TMilestone extends string,
TQueryResult extends SingleStoreQueryResultHKT,
TPreparedQueryHKT extends PreparedQueryHKTBase,
TDynamic extends boolean = false,
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> {
readonly _: {
readonly milestone: TMilestone;
readonly queryResult: TQueryResult;
readonly preparedQueryHKT: TPreparedQueryHKT;
readonly dynamic: TDynamic;
readonly excludedMethods: TExcludedMethods;
};
}

export class SingleStoreDropMilestoneBase<
TMilestone extends string,
TQueryResult extends SingleStoreQueryResultHKT,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TPreparedQueryHKT extends PreparedQueryHKTBase,
TDynamic extends boolean = false,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TExcludedMethods extends string = never,
> extends QueryPromise<SingleStoreQueryResultKind<TQueryResult, never>> implements SQLWrapper {
static readonly [entityKind]: string = 'SingleStoreDropMilestone';

private config: SingleStoreDropMilestoneConfig;

constructor(
private milestone: TMilestone,
private session: SingleStoreSession,
private dialect: SingleStoreDialect,
) {
super();
this.config = { milestone };
}

// TODO(singlestore): docs
for(database: string): SingleStoreDropMilestoneWithout<this, TDynamic, 'for'> {
this.config.database = database;
return this as any;
}

/** @internal */
getSQL(): SQL {
return this.dialect.buildDropMilestoneQuery(this.config);
}

toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}

prepare(): SingleStoreDropMilestonePrepare<this> {
return this.session.prepareQuery(
this.dialect.sqlToQuery(this.getSQL()),
undefined,
) as SingleStoreDropMilestonePrepare<this>;
}

override execute: ReturnType<this['prepare']>['execute'] = (placeholderValues) => {
return this.prepare().execute(placeholderValues);
};

private createIterator = (): ReturnType<this['prepare']>['iterator'] => {
const self = this;
return async function*(placeholderValues) {
yield* self.prepare().iterator(placeholderValues);
};
};

iterator = this.createIterator();

$dynamic(): SingleStoreDropMilestoneDynamic<this> {
return this as any;
}
}

0 comments on commit feffcfd

Please sign in to comment.