From fb143d6dd3690e590647b3f9ac8f611b7299c5c3 Mon Sep 17 00:00:00 2001 From: Xc Date: Mon, 16 Dec 2024 20:58:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86exec=E5=AF=B9mysql?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=9C=E7=9A=84=E5=88=A4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/MySQL/connector.d.ts | 2 +- lib/MySQL/store.d.ts | 2 +- lib/MySQL/store.js | 16 +++--- lib/MySQL/translator.js | 20 ++++---- src/MySQL/connector.ts | 2 +- src/MySQL/store.ts | 19 ++++---- test/testMySQLStore.ts | 102 ++++++++++++++++++++++++++++++++++++++- 7 files changed, 132 insertions(+), 31 deletions(-) diff --git a/lib/MySQL/connector.d.ts b/lib/MySQL/connector.d.ts index 8416f6b..3f9168f 100644 --- a/lib/MySQL/connector.d.ts +++ b/lib/MySQL/connector.d.ts @@ -9,7 +9,7 @@ export declare class MySqlConnector { connect(): void; disconnect(): Promise; startTransaction(option?: TxnOption): Promise; - exec(sql: string, txn?: string): Promise; + exec(sql: string, txn?: string): Promise<[mysql.RowDataPacket[] | mysql.RowDataPacket[][] | mysql.OkPacket | mysql.OkPacket[] | mysql.ResultSetHeader, mysql.FieldPacket[]]>; commitTransaction(txn: string): Promise; rollbackTransaction(txn: string): Promise; } diff --git a/lib/MySQL/store.d.ts b/lib/MySQL/store.d.ts index 31f5120..1619e8c 100644 --- a/lib/MySQL/store.d.ts +++ b/lib/MySQL/store.d.ts @@ -12,7 +12,7 @@ export declare class MysqlStore>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): AggregationResult; protected selectAbjointRow(entity: T, selection: ED[T]['Selection'], context: SyncContext, option: OP): Partial[]; protected updateAbjointRow(entity: T, operation: ED[T]['Operation'], context: SyncContext, option: OP): number; - exec(script: string, txnId?: string): Promise; + exec(script: string, txnId?: string): Promise; connector: MySqlConnector; translator: MySqlTranslator; constructor(storageSchema: StorageSchema, configuration: MySQLConfiguration); diff --git a/lib/MySQL/store.js b/lib/MySQL/store.js index 06450be..dfdb7a6 100644 --- a/lib/MySQL/store.js +++ b/lib/MySQL/store.js @@ -33,8 +33,8 @@ class MysqlStore extends CascadeStore_1.CascadeStore { updateAbjointRow(entity, operation, context, option) { throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿'); } - exec(script, txnId) { - return this.connector.exec(script, txnId); + async exec(script, txnId) { + await this.connector.exec(script, txnId); } connector; translator; @@ -240,21 +240,21 @@ class MysqlStore extends CascadeStore_1.CascadeStore { case 'create': { const { data } = operation; const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]); - await connector.exec(sql, txn); - return data instanceof Array ? data.length : 1; + const result = await connector.exec(sql, txn); + return result[0].affectedRows; } case 'remove': { const sql = translator.translateRemove(entity, operation, option); - await connector.exec(sql, txn); + const result = await connector.exec(sql, txn); // todo 这里对sorter和indexfrom/count的支持不完整 - return 1; + return result[0].changedRows; } default: { (0, assert_1.default)(!['select', 'download', 'stat'].includes(action)); const sql = translator.translateUpdate(entity, operation, option); - await connector.exec(sql, txn); + const result = await connector.exec(sql, txn); // todo 这里对sorter和indexfrom/count的支持不完整 - return 1; + return result[0].changedRows; } } } diff --git a/lib/MySQL/translator.js b/lib/MySQL/translator.js index 5546a28..9e52c58 100644 --- a/lib/MySQL/translator.js +++ b/lib/MySQL/translator.js @@ -119,21 +119,21 @@ class MySqlTranslator extends sqlTranslator_1.SqlTranslator { // numeric types "bit", "int", - "integer", // synonym for int + "integer", "tinyint", "smallint", "mediumint", "bigint", "float", "double", - "double precision", // synonym for double - "real", // synonym for double + "double precision", + "real", "decimal", - "dec", // synonym for decimal - "numeric", // synonym for decimal - "fixed", // synonym for decimal - "bool", // synonym for tinyint - "boolean", // synonym for tinyint + "dec", + "numeric", + "fixed", + "bool", + "boolean", // date and time types "date", "datetime", @@ -142,10 +142,10 @@ class MySqlTranslator extends sqlTranslator_1.SqlTranslator { "year", // string types "char", - "nchar", // synonym for national char + "nchar", "national char", "varchar", - "nvarchar", // synonym for national varchar + "nvarchar", "national varchar", "blob", "text", diff --git a/src/MySQL/connector.ts b/src/MySQL/connector.ts index fa018fa..eaff420 100644 --- a/src/MySQL/connector.ts +++ b/src/MySQL/connector.ts @@ -51,7 +51,7 @@ export class MySqlConnector { return startInner(); } - async exec(sql: string, txn?: string): Promise { + async exec(sql: string, txn?: string) { if (process.env.NODE_ENV === 'development') { // console.log(sql); } diff --git a/src/MySQL/store.ts b/src/MySQL/store.ts index 1d40bb0..749df34 100644 --- a/src/MySQL/store.ts +++ b/src/MySQL/store.ts @@ -10,6 +10,7 @@ import { judgeRelation } from 'oak-domain/lib/store/relation'; import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore'; import { SyncContext } from 'oak-domain/lib/store/SyncRowStore'; import { CreateEntityOption } from '../types/Translator'; +import { FieldPacket, ResultSetHeader, RowDataPacket } from 'mysql2'; function convertGeoTextToObject(geoText: string): object { @@ -41,8 +42,8 @@ export class MysqlStore(entity: T, operation: ED[T]['Operation'], context: SyncContext, option: OP): number { throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿'); } - exec(script: string, txnId?: string) { - return this.connector.exec(script, txnId); + async exec(script: string, txnId?: string) { + await this.connector.exec(script, txnId); } connector: MySqlConnector; translator: MySqlTranslator; @@ -269,21 +270,21 @@ export class MysqlStore(entity: T, selection: Pick, context: AsyncContext, option: SelectOption): Promise { const sql = this.translator.translateCount(entity, selection, option); - const result = await this.connector.exec(sql, context.getCurrentTxnId()); + const result = await this.connector.exec(sql, context.getCurrentTxnId()) as [RowDataPacket[], FieldPacket[]]; return result[0][0].cnt as number; } async count(entity: T, selection: Pick, context: Cxt, option: SelectOption) { diff --git a/test/testMySQLStore.ts b/test/testMySQLStore.ts index 5488a2d..5726845 100644 --- a/test/testMySQLStore.ts +++ b/test/testMySQLStore.ts @@ -17,7 +17,7 @@ describe('test mysqlstore', function () { host: 'localhost', database: 'oakdb', user: 'root', - password: '', + password: 'root', charset: 'utf8mb4_general_ci', connectionLimit: 20, }); @@ -461,6 +461,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, system: { id: v4(), @@ -482,6 +487,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, system: { id: v4(), @@ -559,6 +569,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, system: { id: v4(), @@ -582,6 +597,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, system: { id: v4(), @@ -701,6 +721,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, system: { id: v4(), @@ -724,6 +749,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, system: { id: v4(), @@ -844,6 +874,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, @@ -934,6 +969,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, { @@ -947,6 +987,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }] @@ -1053,6 +1098,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, @@ -1169,6 +1219,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, @@ -1183,6 +1238,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }] @@ -1207,6 +1267,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, @@ -1221,6 +1286,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }] @@ -1279,6 +1349,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: ['email', 'mobile'], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, {}); @@ -1317,6 +1392,11 @@ describe('test mysqlstore', function () { appSecret: '', }, passport: ['email', 'mobile'], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, {}); @@ -1773,6 +1853,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, @@ -1787,6 +1872,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }] @@ -1811,6 +1901,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }, @@ -1825,6 +1920,11 @@ describe('test mysqlstore', function () { config: { type: 'web', passport: [], + location: { + protocol: "http:", + hostname: '', + port: '', + }, }, } }]