zoukankan      html  css  js  c++  java
  • nodejs 针对 mysql 设计的原型库,支持事务/共享多次/单次查询

    //通过this访问内置流程对象, 在每个流程中都能使用
    //this.conn => mysql-connection
    //this.results => 整个流程数已经返回的值
    //this.next => 函数, 可以进入下一个流程, 第一个函数为错误信息, 后面的为该流程返回值
    
    //每个流程必须为 function() { } 或者 function(next, thisarg) {}
    //next 等同于 this.next
    //thisarg 等同于 this
    //注意: 箭头函数中使用 this 得到的是外层 scope 的 this, 所以添加流程不能偷懒
    
    //this 为 sql 构造器函数, 最后使用 query(done) 或者 adminQuery(done) 执行, 与 lib.dal 使用方法相同
    lib.dal('select * from users where 1 = 1')
      ('and username=?', username).query(done);
    //相当于
    var sql = 'select * from users where 1 = 1';
    if (usersname === null || usersname === undefined)
      sql += 'and username=?'; //添加一个参数 usernname
    conn.query(done);
    
    //整个事务使用同一个mysql-connection
    lib.dal.trans(
      //创建mysql-connection并且开启事务, 
      function () { //一个流程
        this('select * from users').query(this.next);
      },
      function () { //一个流程
        this('select * from users').query(this.next);
      },
      function () { //一个流程
        this('select * from users').query(this.next);
        this.next('设置任何错误信息,将回滚事务');
      },
      //在done方法前内置提交/回滚事务,并释放mysql-connection
      (err, r) => {
        //console.log(JSON.stringify(r));
      }
    );
    
    
    lib.dal.map( //使用lib.dal.map.call(流程对象 或者 mysql-connection
      [11, 13, 16],
      (v, k) => { //三个流程
        this.next(v+2);
      },
      (err, r) { //done
        console.log(r); // => [13, 15, 18]
      }
    );
    
    lib.dal.map( //同上
      流程1, 流程2, 流程3, done
    );
  • 相关阅读:
    团队冲刺--第二阶段(五)
    团队冲刺--第二阶段(四)
    团队冲刺--第二阶段(三)
    团队冲刺--第二阶段(二)
    团队冲刺--第二阶段(一)
    第一阶段意见评论
    人月神话阅读笔记02
    基础-定位
    基础-颜色
    标准文档流
  • 原文地址:https://www.cnblogs.com/kellynic/p/5295156.html
Copyright © 2011-2022 走看看