Skip to content

Commit

Permalink
support string bignumbers in mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
yshrsmz committed Mar 3, 2024
1 parent 570f4e3 commit d7474a6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
12 changes: 6 additions & 6 deletions examples/node-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ SELECT id, name, bio FROM authors
WHERE id = ? LIMIT 1`;

export interface GetAuthorArgs {
id: number;
id: string;
}

export interface GetAuthorRow {
id: number;
id: string;
name: string;
bio: string | null;
}
Expand All @@ -40,7 +40,7 @@ SELECT id, name, bio FROM authors
ORDER BY name`;

export interface ListAuthorsRow {
id: number;
id: string;
name: string;
bio: string | null;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ DELETE FROM authors
WHERE id = ?`;

export interface DeleteAuthorArgs {
id: number;
id: string;
}

export async function deleteAuthor(client: Client, args: DeleteAuthorArgs): Promise<void> {
Expand All @@ -127,8 +127,8 @@ export interface TestRow {
cMediumint: number | null;
cInt: number | null;
cInteger: number | null;
cBigint: number | null;
cSerial: number;
cBigint: string | null;
cSerial: string;
cDecimal: string | null;
cDec: string | null;
cNumeric: string | null;
Expand Down
2 changes: 2 additions & 0 deletions examples/node-mysql2/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ async function main() {
user: url.username,
password: url.password,
database: url.pathname.substring(1),
supportBigNumbers: true,
bigNumberStrings: true,
ssl: {
// TODO: FIXME
rejectUnauthorized: false,
Expand Down
3 changes: 3 additions & 0 deletions examples/sqlc.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ sql:
options:
runtime: node
driver: mysql2
mysql2:
big_number_strings: true
support_big_numbers: true
- schema: "authors/mysql/schema.sql"
queries: "authors/mysql/query.sql"
engine: "mysql"
Expand Down
27 changes: 13 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { argName, colName } from "./drivers/utlis";
import { Driver as Sqlite3Driver } from "./drivers/better-sqlite3";
import { Driver as PgDriver } from "./drivers/pg";
import { Driver as PostgresDriver } from "./drivers/postgres";
import { Driver as MysqlDriver } from "./drivers/mysql2";
import { Mysql2Options, Driver as MysqlDriver } from "./drivers/mysql2";

// Read input from stdin
const input = readInput();
Expand All @@ -43,6 +43,7 @@ writeOutput(result);
interface Options {
runtime?: string;
driver?: string;
mysql2?: Mysql2Options
}

interface Driver {
Expand Down Expand Up @@ -78,10 +79,10 @@ interface Driver {
) => Node;
}

function createNodeGenerator(driver?: string): Driver {
switch (driver) {
function createNodeGenerator(options: Options): Driver {
switch (options.driver) {
case "mysql2": {
return new MysqlDriver();
return new MysqlDriver(options.mysql2);
}
case "pg": {
return new PgDriver();
Expand All @@ -93,7 +94,7 @@ function createNodeGenerator(driver?: string): Driver {
return new Sqlite3Driver();
}
}
throw new Error(`unknown driver: ${driver}`);
throw new Error(`unknown driver: ${options.driver}`);
}

function codegen(input: GenerateRequest): GenerateResponse {
Expand All @@ -105,7 +106,7 @@ function codegen(input: GenerateRequest): GenerateResponse {
options = JSON.parse(text) as Options;
}

const driver = createNodeGenerator(options.driver);
const driver = createNodeGenerator(options);

// TODO: Verify options, parse them from protobuf honestly

Expand Down Expand Up @@ -146,17 +147,15 @@ ${query.text}`
)
);

const ctype = driver.columnType;

let argIface = undefined;
let returnIface = undefined;
if (query.params.length > 0) {
argIface = `${query.name}Args`;
nodes.push(argsDecl(argIface, ctype, query.params));
nodes.push(argsDecl(argIface, driver, query.params));
}
if (query.columns.length > 0) {
returnIface = `${query.name}Row`;
nodes.push(rowDecl(returnIface, ctype, query.columns));
nodes.push(rowDecl(returnIface, driver, query.columns));
}

switch (query.cmd) {
Expand Down Expand Up @@ -240,7 +239,7 @@ function queryDecl(name: string, sql: string) {

function argsDecl(
name: string,
ctype: (c?: Column) => TypeNode,
driver: Driver,
params: Parameter[]
) {
return factory.createInterfaceDeclaration(
Expand All @@ -253,15 +252,15 @@ function argsDecl(
undefined,
factory.createIdentifier(argName(i, param.column)),
undefined,
ctype(param.column)
driver.columnType(param.column)
)
)
);
}

function rowDecl(
name: string,
ctype: (c?: Column) => TypeNode,
driver: Driver,
columns: Column[]
) {
return factory.createInterfaceDeclaration(
Expand All @@ -274,7 +273,7 @@ function rowDecl(
undefined,
factory.createIdentifier(colName(i, column)),
undefined,
ctype(column)
driver.columnType(column)
)
)
);
Expand Down
23 changes: 23 additions & 0 deletions src/drivers/mysql2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { SyntaxKind, NodeFlags, TypeNode, factory } from "typescript";
import { Parameter, Column, Query } from "../gen/plugin/codegen_pb";
import { argName, colName } from "./utlis";

export interface Mysql2Options {
support_big_numbers?: boolean;
big_number_strings?: boolean;
}

function funcParamsDecl(iface: string | undefined, params: Parameter[]) {
let funcParams = [
factory.createParameterDeclaration(
Expand Down Expand Up @@ -40,6 +45,12 @@ function funcParamsDecl(iface: string | undefined, params: Parameter[]) {
}

export class Driver {
private readonly options: Mysql2Options

constructor(options?: Mysql2Options) {
this.options = options ?? {}
}

columnType(column?: Column): TypeNode {
if (column === undefined || column.type === undefined) {
return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
Expand All @@ -49,6 +60,18 @@ export class Driver {
switch (column.type.name) {
case "bigint": {
typ = factory.createKeywordTypeNode(SyntaxKind.NumberKeyword);

if (this.options.support_big_numbers) {
if (this.options.big_number_strings) {
typ = factory.createKeywordTypeNode(SyntaxKind.StringKeyword)
} else {
typ = factory.createUnionTypeNode([
factory.createKeywordTypeNode(SyntaxKind.NumberKeyword),
factory.createKeywordTypeNode(SyntaxKind.StringKeyword)
])
}
}

break;
}
case "binary": {
Expand Down

0 comments on commit d7474a6

Please sign in to comment.