【问题标题】:sails.js run multiple command query in mysqlSails.js 在 mysql 中运行多个命令查询
【发布时间】:2016-01-14 22:29:48
【问题描述】:

我在sails.js 上执行多个sql 查询时遇到问题

我想从sails lift 上的文件运行脚本。

我在/config/bootstrap.js里面写了一个自定义处理

module.exports.bootstrap = function(cb) {

  fs.readFile('SQL\\StoredProcedures\\MyProcedure.sql', 'utf8', function (err,data) {
    if (err) {

      console.log(err);
    }
    console.log(data);
    MyModel.query(data, function(err, records){
        if(err){
          console.log(err);
        }
    });
  });
  // It's very important to trigger this callback method when you are finished
  // with the bootstrap!  (otherwise your server will never lift, since it's waiting on the bootstrap)
  cb();
};

问题是,.query() 函数内部不接受多个查询。我的意思是,它确实接受我的文件中有:

DROP PROCEDURE IF EXISTS `MyProcedure`;

但在我的文件中有我的文件时它不会接受:

DROP PROCEDURE IF EXISTS `MyProcedure`;
SELECT * FROM something;

有没有办法执行这个文件?

【问题讨论】:

    标签: mysql sails.js waterline


    【解决方案1】:

    这可以通过像这样设置您的config/datastores.js 来完成:

    module.exports = {
    
      default: {
        multipleStatements: true
      }
    }
    

    通过将其添加到您的配置中,您可以让 Sails 处理查询的解析和执行。

    问题在于,默认情况下,Node MySQL 驱动程序不允许同时运行多个查询。这是为了防止SQL注入。

    更完整的解释请看@sgress454的评论:https://github.com/balderdashy/sails/issues/4192

    【讨论】:

      【解决方案2】:

      您可以拆分文件中的行,并一一运行所有查询?

      var fs = require('fs');
      
      module.exports = function (cb) {
          fs.readFile('SQL\\StoredProcedures\\MyProcedure.sql', 'utf8', function (err,data) {
              if (err) {
                sails.log.error(err);
                return cb();  // you have no queries to run
              }
      
              sails.log.info(data);
      
              var queries = data.split('\n');
      
              // async is injected into the global scope by sails, so no need to require it
              // if you don't need your queries to run in order, then you can use async.each
              // instead of async.eachSeries
              // https://github.com/caolan/async#each
              async.eachSeries(
                  queries,
                  function (query, cb) {
                      MyModel.query(query, function (err, records) {
                          if (err) {
                              sails.log.error(err);
                              return cb();
                              // If you don't want other queries to execute in case of an error, then
                              // return cb(err);
                          }
                      });
                  },
                  function (err) {
                      if (err) { sails.log.error(err); }
                      return cb();
                  }
              );
          });
      };
      

      【讨论】:

      • 如果我的查询之一是多行怎么办?
      • 您可以使用; 或其他分隔符分割?
      猜你喜欢
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多