mm_mysql 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +49 -8
  2. package/package.json +1 -1
  3. package/sql.js +89 -0
package/index.js CHANGED
@@ -626,8 +626,12 @@ Mysql.prototype.exec = async function (sql, params = [], timeout = null) {
626
626
 
627
627
  const [rows] = await Promise.race([queryPromise, timeoutPromise]);
628
628
 
629
- // 对于增删改操作,返回影响的行数
630
- return rows.affectedRows || rows.insertId || rows.changedRows || rows;
629
+ // 返回与mm_sqlite兼容的格式
630
+ return {
631
+ affectedRows: rows.affectedRows || 0,
632
+ insertId: rows.insertId || 0,
633
+ changedRows: rows.changedRows || 0
634
+ };
631
635
  } catch (err) {
632
636
  $.log.error('[Mysql] [exec]', 'SQL执行失败', {
633
637
  error: err.message,
@@ -700,8 +704,8 @@ Mysql.prototype.read = async function (table, condition = {}, options = {}) {
700
704
  $.log.debug(`[${this.constructor.name}] [read] 查询数据`, { sql, params });
701
705
  }
702
706
 
703
- // 执行查询
704
- const results = await this.exec(sql, params);
707
+ // 执行查询,使用run方法获取数组格式的结果
708
+ const results = await this.run(sql, params);
705
709
 
706
710
  if (this.config.debug) {
707
711
  $.log.info(`[${this.constructor.name}] [read] 查询成功`, { count: results.length });
@@ -931,10 +935,6 @@ Mysql.prototype._splitSqlStatements = function(sqlContent) {
931
935
  * @returns {Object} 包含exec、add、set、get、del等方法的数据库操作对象
932
936
  */
933
937
  Mysql.prototype.db = function () {
934
- if (this._status !== 'connected') {
935
- throw new Error('数据库连接未建立');
936
- }
937
-
938
938
  return new DB(this);
939
939
  };
940
940
 
@@ -991,6 +991,47 @@ Mysql.prototype._sleep = function (ms) {
991
991
  return new Promise(resolve => setTimeout(resolve, ms));
992
992
  };
993
993
 
994
+ /**
995
+ * SQL语法转换方法(与mm_sqlite保持兼容)
996
+ * @param {String} sql - 原始SQL语句
997
+ * @returns {String} 转换后的SQL语句
998
+ */
999
+ Mysql.prototype._convertSqlSyntax = function (sql) {
1000
+ // MySQL不需要特殊的语法转换,直接返回原SQL
1001
+ // 此方法主要用于保持与mm_sqlite的接口兼容性
1002
+ return sql;
1003
+ };
1004
+
1005
+ /**
1006
+ * 获取数据库路径(与mm_sqlite保持兼容)
1007
+ * @returns {String} 数据库连接信息
1008
+ */
1009
+ Mysql.prototype._getDatabasePath = function () {
1010
+ // MySQL不需要文件路径,返回连接信息用于兼容性
1011
+ return `${this.config.host}:${this.config.port}/${this.config.database}`;
1012
+ };
1013
+
1014
+ /**
1015
+ * 初始化MySQL服务
1016
+ * @async
1017
+ * @returns {Promise<void>}
1018
+ */
1019
+ Mysql.prototype.init = async function () {
1020
+ if (this._isInited) {
1021
+ $.log.warn('MySQL服务已初始化');
1022
+ return;
1023
+ }
1024
+
1025
+ try {
1026
+ $.log.debug('[Mysql] [init]', '初始化MySQL服务');
1027
+ await this._initService();
1028
+ await this.open();
1029
+ $.log.debug('[Mysql] [init]', 'MySQL服务初始化完成');
1030
+ } catch (error) {
1031
+ $.log.error('[Mysql] [init]', 'MySQL服务初始化失败', { error: error.message });
1032
+ throw error;
1033
+ }
1034
+ };
994
1035
 
995
1036
  /**
996
1037
  * 导出模块
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_mysql",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/sql.js CHANGED
@@ -1625,4 +1625,93 @@ Sql.prototype.model = function (model) {
1625
1625
  });
1626
1626
  };
1627
1627
 
1628
+ /**
1629
+ * 添加或修改数据(存在则修改,不存在则添加)
1630
+ * @param {Object|String} where 查询条件
1631
+ * @param {Object|String} set 要设置的数据
1632
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
1633
+ * @return {Promise<Object>} 执行结果
1634
+ */
1635
+ Sql.prototype.addOrSet = async function(where, set, like) {
1636
+ if (!this.table || !where || !set) {
1637
+ throw new Error('表名、条件或数据未设置');
1638
+ }
1639
+ try {
1640
+ let query = where;
1641
+ let body = set;
1642
+ let whereStr;
1643
+
1644
+ if (typeof where === "object") {
1645
+ whereStr = await this.toWhere(where, like);
1646
+ } else {
1647
+ whereStr = where;
1648
+ }
1649
+
1650
+ const count = await this.countSql(whereStr);
1651
+
1652
+ if (count === 0) {
1653
+ let key = "";
1654
+ let value = "";
1655
+
1656
+ if (typeof set === "string") {
1657
+ const arr = set.split(",");
1658
+ for (const o of arr) {
1659
+ const ar = o.split('=');
1660
+ if (ar.length === 2) {
1661
+ key += "," + ar[0];
1662
+ value += "," + ar[1];
1663
+ }
1664
+ }
1665
+ } else {
1666
+ // 触发前置事件
1667
+ if (typeof $.eventer === 'object' && typeof $.eventer.run === 'function' && typeof body === "object") {
1668
+ await $.eventer.run("mysql_add_before:" + this.table, { body });
1669
+ }
1670
+
1671
+ for (const k in set) {
1672
+ if (!Object.prototype.hasOwnProperty.call(set, k)) continue;
1673
+
1674
+ key += "," + escapeId(k);
1675
+ let val = set[k];
1676
+ if (typeof val === "string") {
1677
+ val = val.trim("'");
1678
+ }
1679
+ value += "," + escape(val);
1680
+ }
1681
+ }
1682
+
1683
+ const bl = await this.addSql(key.replace(",", ""), value.replace(",", ""));
1684
+
1685
+ // 触发后置事件
1686
+ if (typeof $.eventer === 'object' && typeof $.eventer.run === 'function' && typeof body === "object") {
1687
+ await $.eventer.run("mysql_add_after:" + this.table, { body, sql: this.sql, error: this.error, bl });
1688
+ }
1689
+
1690
+ return bl;
1691
+ } else {
1692
+ // 触发前置事件
1693
+ if (typeof $.eventer === 'object' && typeof $.eventer.run === 'function' && typeof set === "object") {
1694
+ await $.eventer.run("mysql_set_before:" + this.table, { query, body, like, page: this.page, size: this.size, sql: this.sql });
1695
+ }
1696
+
1697
+ if (typeof set === "object") {
1698
+ set = await this.toSet(set);
1699
+ }
1700
+
1701
+ const bl1 = await this.setSql(whereStr, set);
1702
+
1703
+ // 触发后置事件
1704
+ if (typeof $.eventer === 'object' && typeof $.eventer.run === 'function' && typeof body === "object") {
1705
+ await $.eventer.run("mysql_set_after:" + this.table, { query, body, like, page: this.page, size: this.size, sql: this.sql, error: this.error, bl: bl1 });
1706
+ }
1707
+
1708
+ return bl1;
1709
+ }
1710
+ } catch (err) {
1711
+ this.error = err.message;
1712
+ $.log.error(`添加或修改数据失败: ${err.message}`);
1713
+ throw err;
1714
+ }
1715
+ };
1716
+
1628
1717
  module.exports = Sql;