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

Handling of bigint and decimal types #42

Closed
uki00a opened this issue May 10, 2020 · 2 comments · Fixed by #46
Closed

Handling of bigint and decimal types #42

uki00a opened this issue May 10, 2020 · 2 comments · Fixed by #46

Comments

@uki00a
Copy link
Member

uki00a commented May 10, 2020

Summary

deno_mysql currently treats MySQL's bigint and decimal as the JavaScript's Number.

However, these types cannot be accurately represented by the Number.

The following code reproduces this problem:

mod.ts:

import { Client } from "https://deno.land/x/mysql@1.9.1/mod.ts";

const client = await new Client().connect({
  hostname: "localhost",
  port: 3306,
  username: "test",
  db: "test",
  password: "test",
});

await client.query(`DROP TABLE IF EXISTS test`);
await client.query(`CREATE TABLE test (
  id int(11) NOT NULL AUTO_INCREMENT,
  bigint_column bigint NOT NULL,
  decimal_column decimal(65,30) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4`);

const bigint = "9223372036854775807";
const decimal = "0.012345678901234567890123456789";
await client.execute(`
  INSERT INTO test (bigint_column, decimal_column)
  VALUES (${bigint}, ${decimal})`);

console.log(await client.query(`SELECT * FROM test`));
$ deno run --allow-net ./mod.ts
INFO connecting localhost:3306
INFO connected to localhost
[
 { id: 1, bigint_column: 9223372036854776000, decimal_column: 0.012345678901234568 }
]
  • deno@v0.42.0
  • deno_mysql@v1.9.1

Possible solutions

  • Always treat bigint and decimal as String (probably the simplest solution).
  • Provide options that change the handling of these types (like node-mysql).
    • node-mysql provides supportBigNumbers and bigNumberStrings options.

These are just ideas, so any feedback would be appreciated 😄

Ref

@magichim
Copy link
Collaborator

magichim commented May 10, 2020

I think, it is necessary point! 😀

@magichim
Copy link
Collaborator

magichim commented May 14, 2020

In sql-builder, BigInt number is possible to solve it as follow.

if (val >= Number.MAX_SAFE_INTEGER) {
  return `${BigInt(val)}`;
}

However, '0.012345678901234567890123456789' Long decimal number is a little complicated...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants