从 node.js multipleStatements 查询中获取结果?
Getting Result From node.js multipleStatements query?
我有以下 node.js
代码通过 mysql
模块执行它下面的 SQL
,但是它甚至将 null
打印到 console
当 SQL
本身在同一个 MySQL
用户帐户下手动执行时 returns 的预期值。在执行此操作以检索适当的结果时我是否遗漏了什么?
let wipeSQL = fs.readFileSync('./inc/sql/wipedb.sql', 'utf8').replace('%%DATABASE_NAME%%', conf['databasedatabase']);
conn.query(wipeSQL, (err, results, fields) => {
if (err) {
conn.destroy();
reject (false);
return;
}
console.log(results[results.length - 1][0]['@procedures'], fields[fields.length - 1]);
resolve();
});
USE `%%DATABASE_NAME%%`;
SET FOREIGN_KEY_CHECKS = 0;
SET @procedures = '';
SELECT
GROUP_CONCAT(CONCAT('DROP PROCEDURE IF EXISTS `', routine_schema, '`.`', routine_name, '`') SEPARATOR ';')
INTO
@procedures
FROM
information_schema.ROUTINES R
WHERE
R.ROUTINE_TYPE = "PROCEDURE" AND
R.ROUTINE_SCHEMA = '%%DATABASE_NAME%%';
SET @procedures = CONCAT(@procedures, ';');
SET @functions = '';
SELECT
GROUP_CONCAT(CONCAT('DROP FUNCTION IF EXISTS `', routine_schema, '`.`', routine_name, '`') SEPARATOR ';')
INTO
@functions
FROM
information_schema.ROUTINES R
WHERE
R.ROUTINE_TYPE = "FUNCTION" AND
R.ROUTINE_SCHEMA = '%%DATABASE_NAME%%';
SET @functions = CONCAT(@functions, ';');
SET @procedures = CONCAT(@procedures, @functions);
SET @tables = '';
SELECT
GROUP_CONCAT(CONCAT('`', table_schema, '`.`', table_name, '`'))
INTO
@tables
FROM
information_schema.tables
WHERE
table_schema = '%%DATABASE_NAME%%';
SET @tables = IF(@tables IS NULL, 'SET @tables = NULL;', CONCAT('DROP TABLE ', @tables, ';'));
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
SELECT @procedures;
目的是解决 MySQL
中的一个问题,该问题阻止从动态 SQL
和另一个 stored procedure
中删除 functions
和 stored procedures
,要求我 return 要删除的东西,然后将它们作为单独的语句执行。删除 functions
和 stored procedures
的解决方案同样有效,但我不希望它存在(这是来自 PHP
的端口,其中多重语句 return 效果很好。控制台输出,如果有帮助:
null [ FieldPacket {
catalog: 'def',
db: '',
table: '',
orgTable: '',
name: '@procedures',
orgName: '',
charsetNr: 33,
length: 50331645,
type: 250,
flags: 0,
decimals: 31,
default: undefined,
zeroFill: false,
protocol41: true } ]
在 conn.query 之前执行 console.log(wipeSQL) 并检查正在构建并传递给 conn.query 的查询。然后在 mysql 查询浏览器上手动触发此查询以查看结果。
我有以下 node.js
代码通过 mysql
模块执行它下面的 SQL
,但是它甚至将 null
打印到 console
当 SQL
本身在同一个 MySQL
用户帐户下手动执行时 returns 的预期值。在执行此操作以检索适当的结果时我是否遗漏了什么?
let wipeSQL = fs.readFileSync('./inc/sql/wipedb.sql', 'utf8').replace('%%DATABASE_NAME%%', conf['databasedatabase']);
conn.query(wipeSQL, (err, results, fields) => {
if (err) {
conn.destroy();
reject (false);
return;
}
console.log(results[results.length - 1][0]['@procedures'], fields[fields.length - 1]);
resolve();
});
USE `%%DATABASE_NAME%%`;
SET FOREIGN_KEY_CHECKS = 0;
SET @procedures = '';
SELECT
GROUP_CONCAT(CONCAT('DROP PROCEDURE IF EXISTS `', routine_schema, '`.`', routine_name, '`') SEPARATOR ';')
INTO
@procedures
FROM
information_schema.ROUTINES R
WHERE
R.ROUTINE_TYPE = "PROCEDURE" AND
R.ROUTINE_SCHEMA = '%%DATABASE_NAME%%';
SET @procedures = CONCAT(@procedures, ';');
SET @functions = '';
SELECT
GROUP_CONCAT(CONCAT('DROP FUNCTION IF EXISTS `', routine_schema, '`.`', routine_name, '`') SEPARATOR ';')
INTO
@functions
FROM
information_schema.ROUTINES R
WHERE
R.ROUTINE_TYPE = "FUNCTION" AND
R.ROUTINE_SCHEMA = '%%DATABASE_NAME%%';
SET @functions = CONCAT(@functions, ';');
SET @procedures = CONCAT(@procedures, @functions);
SET @tables = '';
SELECT
GROUP_CONCAT(CONCAT('`', table_schema, '`.`', table_name, '`'))
INTO
@tables
FROM
information_schema.tables
WHERE
table_schema = '%%DATABASE_NAME%%';
SET @tables = IF(@tables IS NULL, 'SET @tables = NULL;', CONCAT('DROP TABLE ', @tables, ';'));
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
SELECT @procedures;
目的是解决 MySQL
中的一个问题,该问题阻止从动态 SQL
和另一个 stored procedure
中删除 functions
和 stored procedures
,要求我 return 要删除的东西,然后将它们作为单独的语句执行。删除 functions
和 stored procedures
的解决方案同样有效,但我不希望它存在(这是来自 PHP
的端口,其中多重语句 return 效果很好。控制台输出,如果有帮助:
null [ FieldPacket {
catalog: 'def',
db: '',
table: '',
orgTable: '',
name: '@procedures',
orgName: '',
charsetNr: 33,
length: 50331645,
type: 250,
flags: 0,
decimals: 31,
default: undefined,
zeroFill: false,
protocol41: true } ]
在 conn.query 之前执行 console.log(wipeSQL) 并检查正在构建并传递给 conn.query 的查询。然后在 mysql 查询浏览器上手动触发此查询以查看结果。