Skip to content

Commit

Permalink
Support utf8mb4
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Apr 11, 2020
1 parent 9ba183e commit 5d8bf22
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ export interface ClientConfig {
debug?: boolean;
/** Connect timeout */
timeout?: number;
/** TODO: auto reconnect */
reconnect?: boolean;
/** Number of retries that failed in the link process */
retry?: number;
/** Connection pool size default 1 */
poolSize?: number;
/** charset */
charset?: string;
}

/** Transaction processor */
Expand Down
4 changes: 4 additions & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class Connection {
log.info(`connected to ${this.client.config.hostname}`);
this.state = ConnectionState.CONNECTED;
}

if (this.client.config.charset) {
await this.execute(`SET NAMES ${this.client.config.charset}`);
}
}

/** Connect to database */
Expand Down
8 changes: 4 additions & 4 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ testWithClient(async function testCreateTable(client) {
is_top tinyint(1) default 0,
created_at timestamp not null default current_timestamp,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`);
});

Expand All @@ -37,7 +37,7 @@ testWithClient(async function testInsert(client) {
testWithClient(async function testUpdate(client) {
let result = await client.execute(
`update users set ?? = ?, ?? = ? WHERE id = ?`,
["name", "MYR", "created_at", new Date(), 1]
["name", "MYR🦕", "created_at", new Date(), 1]
);
assertEquals(result, { affectedRows: 1, lastInsertId: 0 });
});
Expand All @@ -47,7 +47,7 @@ testWithClient(async function testQuery(client) {
"select ??,`is_top`,`name` from ?? where id = ?",
["id", "users", 1]
);
assertEquals(result, [{ id: 1, name: "MYR", is_top: false }]);
assertEquals(result, [{ id: 1, name: "MYR🦕", is_top: false }]);
});

testWithClient(async function testQueryErrorOccurred(client) {
Expand All @@ -72,7 +72,7 @@ testWithClient(async function testQueryList(client) {
const sql = "select ??,?? from ??";
let result = await client.query(sql, ["id", "name", "users"]);
assertEquals(result, [
{ id: 1, name: "MYR" },
{ id: 1, name: "MYR🦕" },
{ id: 2, name: "MySQL" },
]);
});
Expand Down
1 change: 1 addition & 0 deletions test.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const config = {
username,
port,
db,
charset: "utf8mb4",
password,
};

Expand Down

0 comments on commit 5d8bf22

Please sign in to comment.