@ticatec/node-common-library
Version:
A comprehensive Node.js database access framework providing robust abstractions for database connection management, SQL execution, transaction handling, pagination, and dynamic query building.
113 lines (112 loc) • 3.52 kB
TypeScript
import DBConnection from './DBConnection';
import PaginationList from "./PaginationList";
import { Logger } from 'log4js';
export default abstract class CommonSearchCriteria {
protected readonly logger: Logger;
protected sql: string;
protected orderBy: string;
protected params: Array<any>;
private readonly page;
private readonly rows;
protected criteria: any;
protected constructor(criteria?: any);
/**
* 构建动态查询条件,子类应重写此方法
* @protected
* @abstract
*/
protected buildDynamicQuery(): void;
/**
* 查询符合条件的记录数量
* @param conn - 数据库连接对象
* @param sql - SQL查询语句
* @param params - SQL参数数组
* @private
* @returns Promise返回记录总数
*/
private queryCount;
/**
* 在将行记录转换成对象后的后处理回调函数
* @protected
* @returns 后处理函数或null
*/
protected getPostConstructor(): any;
/**
* 判断值是否不为空
* @param s - 要检查的值
* @protected
* @returns 如果不为空则返回true
*/
protected isNotEmpty(s: any): boolean;
/**
* 转义字符串中的%字符
* @param s - 要转义的字符串
* @protected
* @returns 转义后的字符串
*/
protected escapePercentage(s: string): string;
/**
* 判断字符串是否包含通配符*
* @param s - 要检查的字符串
* @protected
* @returns 如果包含*则返回true
*/
protected includeStar(s: string): boolean;
/**
* 将字符串中的*通配符替换成SQL的%通配符
* @param s - 要转换的字符串
* @protected
* @returns 转换后的字符串
*/
protected toWildSQL(s: string): string;
/**
* 替换所有的*为%通配符,先转义原有的%字符
* @param s - 要处理的字符串
* @protected
* @returns 处理后的字符串
*/
protected replaceWildStar(s: string): string;
/**
* 构建范围查询条件(from/to范围)
* @param fromValue - 起始值
* @param toValue - 结束值
* @param field - 字段名
* @protected
* @returns 下一个参数的索引
*/
protected buildRangeCriteria(fromValue: any, toValue: any, field: string): number;
/**
* 构建带通配符的查询条件(有*用LIKE,无*用等于)
* @param text - 搜索文本
* @param field - 字段名
* @protected
* @returns 下一个参数的索引
*/
protected buildStarCriteria(text: string, field: string): number;
/**
* 构建等于条件查询
* @param value - 查询值
* @param field - 字段名
* @protected
* @returns 下一个参数的索引
*/
protected buildCriteria(value: any, field: string): number;
/**
* 封装LIKE查询值,在字符串两端添加%通配符
* @param s - 要封装的字符串
* @protected
* @returns 封装后的字符串 (%字符串%)
*/
protected wrapLikeMatch(s: string): string;
/**
* 执行分页查询,返回分页结果
* @param conn - 数据库连接对象
*/
paginationQuery(conn: DBConnection): Promise<PaginationList>;
/**
* 不分页查询,返回所有符合条件的记录
* @param conn - 数据库连接对象
* @returns Promise返回数据对象数组
*/
query(conn: DBConnection): Promise<Array<any>>;
}