更新了exec对mysql返回结果的判定
This commit is contained in:
parent
d132eea664
commit
fb143d6dd3
|
|
@ -9,7 +9,7 @@ export declare class MySqlConnector {
|
||||||
connect(): void;
|
connect(): void;
|
||||||
disconnect(): Promise<void>;
|
disconnect(): Promise<void>;
|
||||||
startTransaction(option?: TxnOption): Promise<string>;
|
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>;
|
commitTransaction(txn: string): Promise<void>;
|
||||||
rollbackTransaction(txn: string): Promise<void>;
|
rollbackTransaction(txn: string): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 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 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;
|
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;
|
connector: MySqlConnector;
|
||||||
translator: MySqlTranslator<ED>;
|
translator: MySqlTranslator<ED>;
|
||||||
constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
|
constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
|
||||||
|
|
|
||||||
|
|
@ -33,8 +33,8 @@ class MysqlStore extends CascadeStore_1.CascadeStore {
|
||||||
updateAbjointRow(entity, operation, context, option) {
|
updateAbjointRow(entity, operation, context, option) {
|
||||||
throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
|
throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
|
||||||
}
|
}
|
||||||
exec(script, txnId) {
|
async exec(script, txnId) {
|
||||||
return this.connector.exec(script, txnId);
|
await this.connector.exec(script, txnId);
|
||||||
}
|
}
|
||||||
connector;
|
connector;
|
||||||
translator;
|
translator;
|
||||||
|
|
@ -240,21 +240,21 @@ class MysqlStore extends CascadeStore_1.CascadeStore {
|
||||||
case 'create': {
|
case 'create': {
|
||||||
const { data } = operation;
|
const { data } = operation;
|
||||||
const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
|
const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
|
||||||
await connector.exec(sql, txn);
|
const result = await connector.exec(sql, txn);
|
||||||
return data instanceof Array ? data.length : 1;
|
return result[0].affectedRows;
|
||||||
}
|
}
|
||||||
case 'remove': {
|
case 'remove': {
|
||||||
const sql = translator.translateRemove(entity, operation, option);
|
const sql = translator.translateRemove(entity, operation, option);
|
||||||
await connector.exec(sql, txn);
|
const result = await connector.exec(sql, txn);
|
||||||
// todo 这里对sorter和indexfrom/count的支持不完整
|
// todo 这里对sorter和indexfrom/count的支持不完整
|
||||||
return 1;
|
return result[0].changedRows;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
(0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
|
(0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
|
||||||
const sql = translator.translateUpdate(entity, operation, option);
|
const sql = translator.translateUpdate(entity, operation, option);
|
||||||
await connector.exec(sql, txn);
|
const result = await connector.exec(sql, txn);
|
||||||
// todo 这里对sorter和indexfrom/count的支持不完整
|
// todo 这里对sorter和indexfrom/count的支持不完整
|
||||||
return 1;
|
return result[0].changedRows;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -119,21 +119,21 @@ class MySqlTranslator extends sqlTranslator_1.SqlTranslator {
|
||||||
// numeric types
|
// numeric types
|
||||||
"bit",
|
"bit",
|
||||||
"int",
|
"int",
|
||||||
"integer", // synonym for int
|
"integer",
|
||||||
"tinyint",
|
"tinyint",
|
||||||
"smallint",
|
"smallint",
|
||||||
"mediumint",
|
"mediumint",
|
||||||
"bigint",
|
"bigint",
|
||||||
"float",
|
"float",
|
||||||
"double",
|
"double",
|
||||||
"double precision", // synonym for double
|
"double precision",
|
||||||
"real", // synonym for double
|
"real",
|
||||||
"decimal",
|
"decimal",
|
||||||
"dec", // synonym for decimal
|
"dec",
|
||||||
"numeric", // synonym for decimal
|
"numeric",
|
||||||
"fixed", // synonym for decimal
|
"fixed",
|
||||||
"bool", // synonym for tinyint
|
"bool",
|
||||||
"boolean", // synonym for tinyint
|
"boolean",
|
||||||
// date and time types
|
// date and time types
|
||||||
"date",
|
"date",
|
||||||
"datetime",
|
"datetime",
|
||||||
|
|
@ -142,10 +142,10 @@ class MySqlTranslator extends sqlTranslator_1.SqlTranslator {
|
||||||
"year",
|
"year",
|
||||||
// string types
|
// string types
|
||||||
"char",
|
"char",
|
||||||
"nchar", // synonym for national char
|
"nchar",
|
||||||
"national char",
|
"national char",
|
||||||
"varchar",
|
"varchar",
|
||||||
"nvarchar", // synonym for national varchar
|
"nvarchar",
|
||||||
"national varchar",
|
"national varchar",
|
||||||
"blob",
|
"blob",
|
||||||
"text",
|
"text",
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ export class MySqlConnector {
|
||||||
return startInner();
|
return startInner();
|
||||||
}
|
}
|
||||||
|
|
||||||
async exec(sql: string, txn?: string): Promise<any> {
|
async exec(sql: string, txn?: string) {
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
// console.log(sql);
|
// console.log(sql);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import { judgeRelation } from 'oak-domain/lib/store/relation';
|
||||||
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
||||||
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
||||||
import { CreateEntityOption } from '../types/Translator';
|
import { CreateEntityOption } from '../types/Translator';
|
||||||
|
import { FieldPacket, ResultSetHeader, RowDataPacket } from 'mysql2';
|
||||||
|
|
||||||
|
|
||||||
function convertGeoTextToObject(geoText: string): object {
|
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 {
|
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不支持同步更新数据,不应该跑到这儿');
|
throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
|
||||||
}
|
}
|
||||||
exec(script: string, txnId?: string) {
|
async exec(script: string, txnId?: string) {
|
||||||
return this.connector.exec(script, txnId);
|
await this.connector.exec(script, txnId);
|
||||||
}
|
}
|
||||||
connector: MySqlConnector;
|
connector: MySqlConnector;
|
||||||
translator: MySqlTranslator<ED>;
|
translator: MySqlTranslator<ED>;
|
||||||
|
|
@ -269,21 +270,21 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
|
||||||
case 'create': {
|
case 'create': {
|
||||||
const { data } = operation as ED[T]['Create'];
|
const { data } = operation as ED[T]['Create'];
|
||||||
const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
|
const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
|
||||||
await connector.exec(sql, txn);
|
const result = await connector.exec(sql, txn) as [ResultSetHeader, FieldPacket[]];
|
||||||
return data instanceof Array ? data.length : 1;
|
return result[0].affectedRows;
|
||||||
}
|
}
|
||||||
case 'remove': {
|
case 'remove': {
|
||||||
const sql = translator.translateRemove(entity, operation as ED[T]['Remove'], option);
|
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的支持不完整
|
// todo 这里对sorter和indexfrom/count的支持不完整
|
||||||
return 1;
|
return result[0].changedRows!;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
assert(!['select', 'download', 'stat'].includes(action));
|
assert(!['select', 'download', 'stat'].includes(action));
|
||||||
const sql = translator.translateUpdate(entity, operation as ED[T]['Update'], option);
|
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的支持不完整
|
// 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> {
|
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 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;
|
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) {
|
async count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption) {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ describe('test mysqlstore', function () {
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
database: 'oakdb',
|
database: 'oakdb',
|
||||||
user: 'root',
|
user: 'root',
|
||||||
password: '',
|
password: 'root',
|
||||||
charset: 'utf8mb4_general_ci',
|
charset: 'utf8mb4_general_ci',
|
||||||
connectionLimit: 20,
|
connectionLimit: 20,
|
||||||
});
|
});
|
||||||
|
|
@ -461,6 +461,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
system: {
|
system: {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
|
|
@ -482,6 +487,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
system: {
|
system: {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
|
|
@ -559,6 +569,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
system: {
|
system: {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
|
|
@ -582,6 +597,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
system: {
|
system: {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
|
|
@ -701,6 +721,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
system: {
|
system: {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
|
|
@ -724,6 +749,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
system: {
|
system: {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
|
|
@ -844,6 +874,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -934,6 +969,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
|
|
@ -947,6 +987,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|
@ -1053,6 +1098,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -1169,6 +1219,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -1183,6 +1238,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|
@ -1207,6 +1267,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -1221,6 +1286,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|
@ -1279,6 +1349,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: ['email', 'mobile'],
|
passport: ['email', 'mobile'],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}, {});
|
}, {});
|
||||||
|
|
@ -1317,6 +1392,11 @@ describe('test mysqlstore', function () {
|
||||||
appSecret: '',
|
appSecret: '',
|
||||||
},
|
},
|
||||||
passport: ['email', 'mobile'],
|
passport: ['email', 'mobile'],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}, {});
|
}, {});
|
||||||
|
|
@ -1773,6 +1853,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -1787,6 +1872,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|
@ -1811,6 +1901,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -1825,6 +1920,11 @@ describe('test mysqlstore', function () {
|
||||||
config: {
|
config: {
|
||||||
type: 'web',
|
type: 'web',
|
||||||
passport: [],
|
passport: [],
|
||||||
|
location: {
|
||||||
|
protocol: "http:",
|
||||||
|
hostname: '',
|
||||||
|
port: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue