0
点赞
收藏
分享

微信扫一扫

封装webSQL(六)封装 统计和查询

下一步要做什么呢?要为分页做准备,首先实现统计和查询的功能。

sql

select count(1) as count from table where xxx= xxx

封装


/**
 * 分页获取数据,可以查询
 * @param { MySQLHelp } help 访问数据库的实例
 * @param { Object } meta 表、字段
 * @param { Object } query 查询条件
 * @returns 添加记录的ID
 * * meta 结构:
 * * * tableName: '', 表名
 * * * cols:{colName: '类型'}, 需要显示的字段
 * * query 结构(查询条件):
 * * * { colName: [401, '查询关键字'] } 字段名称,查询方式,查询关键字
 */
export default function getCount (help, meta, query) {
  return new Promise((resolve, reject) => {
    // 查询条件和查询参数
    const { whereQuery, whereValue } = help._getWhereQuery(query)
    // 统计总数
    const sql = `SELECT count(1) as count FROM ${meta.tableName}  ${whereQuery} `
    console.log('count-sql:', sql, whereValue)
    help.query(sql, whereValue).then((re) => {
      resolve(re.rows[0].count) // 返回总数
    }).catch((err) => {
      // 出错了
      console.log('统计总记录数失败了:', err)
      reject(err)
    })
  })
}

统计功能比较简单,只是需要处理一下查询条件。
关于查询,这里设置了一种特定的结构,由查询控件提供查询条件,然后再写个函数解析一下即可。

查询条件

  /**
   * 内部函数,根据查询条件,拼接查询用的SQL语句,where 后面的部分。
   * @param {object} query 查询条件
   * @returns where 后面的查询语句
   */
  _getWhereQuery (query) {
    // 查询条件
    const findKind = {
      // 字符串
      401: ' {col} = ? ',
      402: ' {col} <> ? ',
      403: ' {col} like ? ',
      404: ' {col} not like ? ',
      405: ' {col} like ? ', // 起始于
      406: ' {col} like ? ', // 结束于
      // 数字
      411: ' {col} = ? ',
      412: ' {col} <> ? ',
      413: ' {col} > ? ',
      414: ' {col} >= ? ',
      415: ' {col} < ? ',
      416: ' {col} <= ? ',
      417: ' {col} between ? and ? ',
      // 日期
      421: ' {col} = ? ',
      422: ' {col} <> ? ',
      423: ' {col} > ? ',
      424: ' {col} >= ? ',
      425: ' {col} < ? ',
      426: ' {col} <= ? ',
      427: ' {col} between ? and ? ',
      // 范围
      441: ' {col} in (?)'
    }
    const _whereCol = [] // 查询字段
    const _whereValue = [] // 查询参数
    // 设置查询条件
    for (const key in query) {
      const val = query[key]
      _whereCol.push(findKind[val[0]].replace('{col}', key))
      switch (val[0]) {
        case 403: // like
        case 404: // not like
          _whereValue.push('%' + val[1] + '%')
          break
        case 405: // like a%
          _whereValue.push(val[1] + '%')
          break
        case 406: // like %a
          _whereValue.push('%' + val[1])
          break
        case 417: // between 数字
        case 427: // between 日期
          _whereValue.push(...val[1])
          break
        case 441: // in
          _whereCol[_whereCol.length - 1] =
            _whereCol[_whereCol.length - 1]
              .replace('?', val[1].map(a => '?').join(','))
          _whereValue.push(...val[1])
          break
        default:
          _whereValue.push(val[1])
          break
      }
    }
    // 如果没有查询添加,设置 1=1 占位
    if (_whereCol.length === 0) {
      return {
        whereQuery: '',
        whereValue: []
      }
    } else {
      return {
        whereQuery: ' WHERE ' + _whereCol.join(' and '),
        whereValue: _whereValue
      }
    }
  }

这个是按照SQL的相关标准来做的,只是由于 webSQL 的字段类型似乎比较随意,所以有些查询方式得到的结果似乎会和想的不一致。

使用方式

一般会在分页的时候使用,当然也可以单独使用

     // 需要统计总记录数
      getCount(help, info, query).then((count) => {
        console.log('外部获取总记录数:', count)
        // 设置总记录数
        re.pager.pagerTotal = count
      }).catch((err) => {
        reject(err) // '获取总记录数出错!'
      })

js 有一个比较烦人的地方,就是不知道参数的结构到底是啥,调用函数的时候,参数咋整?

所以ts开始流行,因为可以查询参数类型。

基本就是这样:

举报

相关推荐

0 条评论