Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break down analyzeQuery for more generation flexibility #885

Merged
merged 2 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/driver/src/baseConn.ts
Expand Up @@ -97,12 +97,12 @@ const OLD_ERROR_CODES = new Map([
]);

export type ParseResult = [
Cardinality,
ICodec,
ICodec,
number,
Uint8Array | null,
Uint8Array | null
cardinality: Cardinality,
inCodec: ICodec,
outCodec: ICodec,
capabilities: number,
inCodecBuffer: Uint8Array | null,
outCodecBuffer: Uint8Array | null
];

export type connConstructor = new (
Expand Down
44 changes: 23 additions & 21 deletions packages/driver/src/reflection/analyzeQuery.ts
@@ -1,4 +1,3 @@
import type { ParseResult } from "../baseConn";
import { ArrayCodec } from "../codecs/array";
import { AT_LEAST_ONE, AT_MOST_ONE, MANY, ONE } from "../codecs/consts";
import { EnumCodec } from "../codecs/enum";
Expand All @@ -25,25 +24,8 @@ export async function analyzeQuery(
client: Client,
query: string
): Promise<QueryType> {
let parseResult: ParseResult;
const pool: BaseClientPool = (client as any).pool;
const [cardinality, inCodec, outCodec] = await parseQuery(client, query);

const holder = await pool.acquireHolder(Options.defaults());
try {
const cxn = await holder._getConnection();
parseResult = await cxn._parse(
query,
OutputFormat.BINARY,
Cardinality.MANY,
Session.defaults()
);
} finally {
await holder.release();
}

const cardinality = parseResult[0];
const inCodec = parseResult[1];
const outCodec = parseResult[2];
const imports = new Set<string>();
const args = walkCodec(inCodec, {
indent: "",
Expand All @@ -69,7 +51,27 @@ export async function analyzeQuery(
};
}

function generateSetType(type: string, cardinality: Cardinality): string {
export async function parseQuery(client: Client, query: string) {
const pool: BaseClientPool = (client as any).pool;

const holder = await pool.acquireHolder(Options.defaults());
try {
const cxn = await holder._getConnection();
return await cxn._parse(
query,
OutputFormat.BINARY,
Cardinality.MANY,
Session.defaults()
);
} finally {
await holder.release();
}
}

export function generateSetType(
type: string,
cardinality: Cardinality
): string {
switch (cardinality) {
case Cardinality.MANY:
return `${type}[]`;
Expand All @@ -85,7 +87,7 @@ function generateSetType(type: string, cardinality: Cardinality): string {

// type AtLeastOne<T> = [T, ...T[]];

function walkCodec(
export function walkCodec(
codec: ICodec,
ctx: { indent: string; optionalNulls: boolean; imports: Set<string> }
): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/reflection/index.ts
Expand Up @@ -30,4 +30,4 @@ export * from "./reservedKeywords";
// export * from "../syntax/funcops";
// export * from "./syntax";
export * as introspect from "./queries";
export * from "./analyzeQuery";
export { analyzeQuery } from "./analyzeQuery";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change necessary or are you being defensive here to avoid leaking the new exports?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defensive. But I forgot until now that reflection/index isn't exported at top level. So many exporting these functions just to reflection is ok. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion on this, but I guess it's safest to be restrictive now and add more to the export if we want them in the future, so good to keep what you have here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I adjusted names to better fit a more generalized export/import.

The more I look at this the more I wonder why this is in the driver package and not the generation one...