更新了exec对mysql返回结果的判定

This commit is contained in:
Xu Chang 2024-12-16 20:58:00 +08:00
parent d132eea664
commit fb143d6dd3
7 changed files with 132 additions and 31 deletions

View File

@ -9,7 +9,7 @@ export declare class MySqlConnector {
connect(): void;
disconnect(): Promise<void>;
startTransaction(option?: TxnOption): Promise<string>;
exec(sql: string, txn?: string): Promise<any>;
exec(sql: string, txn?: string): Promise<[mysql.RowDataPacket[] | mysql.RowDataPacket[][] | mysql.OkPacket | mysql.OkPacket[] | mysql.ResultSetHeader, mysql.FieldPacket[]]>;
commitTransaction(txn: string): Promise<void>;
rollbackTransaction(txn: string): Promise<void>;
}

View File

@ -12,7 +12,7 @@ export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt exte
protected aggregateAbjointRowSync<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): AggregationResult<ED[T]['Schema']>;
protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[];
protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number;
exec(script: string, txnId?: string): Promise<any>;
exec(script: string, txnId?: string): Promise<void>;
connector: MySqlConnector;
translator: MySqlTranslator<ED>;
constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);

View File

@ -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;
}
}
}

View File

@ -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",

View File

@ -51,7 +51,7 @@ export class MySqlConnector {
return startInner();
}
async exec(sql: string, txn?: string): Promise<any> {
async exec(sql: string, txn?: string) {
if (process.env.NODE_ENV === 'development') {
// console.log(sql);
}

View File

@ -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<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, 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<ED>;
@ -269,21 +270,21 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
case 'create': {
const { data } = operation as ED[T]['Create'];
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) as [ResultSetHeader, FieldPacket[]];
return result[0].affectedRows;
}
case 'remove': {
const sql = translator.translateRemove(entity, operation as ED[T]['Remove'], option);
await connector.exec(sql, txn);
const result = await connector.exec(sql, txn) as [ResultSetHeader, FieldPacket[]];
// todo 这里对sorter和indexfrom/count的支持不完整
return 1;
return result[0].changedRows!;
}
default: {
assert(!['select', 'download', 'stat'].includes(action));
const sql = translator.translateUpdate(entity, operation as ED[T]['Update'], option);
await connector.exec(sql, txn);
const result = await connector.exec(sql, txn) as [ResultSetHeader, FieldPacket[]];
// todo 这里对sorter和indexfrom/count的支持不完整
return 1;
return result[0].changedRows!;
}
}
}
@ -299,7 +300,7 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
protected async countAbjointRowAsync<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: AsyncContext<ED>, option: SelectOption): Promise<number> {
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<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption) {

View File

@ -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: '',
},
},
}
}]