I have the following node.js
code executing the SQL
below it via the mysql
module, however it is printing null
to the console
even when the SQL
itself executed manually under the same MySQL
user account returns the expected value. Is there something I'm missing in the execution of this to retrieve the appropriate result?
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;
The purpose is to work around an issue in MySQL
which prevents deleting functions
and stored procedures
from dynamic SQL
and from within another stored procedure
, requiring that I return the things to be dropped and then execute those as a separate statement. A solution to drop those functions
and stored procedures
would work just as well, but I don't expect it to exist (this is a port from PHP
, in which the multiple statement return works well. Console output, if it helps:
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 } ]
do console.log(wipeSQL) before conn.query and check what query is getting build and passing to conn.query.Then fire this query manually on mysql query browser to see results.