2017-04-03 108 views
0

我有以下node.js代碼經由mysql模塊執行它下面的SQL,然而,打印nullconsole即使當同一MySQL用戶帳戶下手動執行的SQL本身返回預期的值。有沒有什麼我在執行這個檢索合適的結果時失蹤?獲取結果來自node.js multipleStatements查詢?

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這樣可防止內的另一stored procedure刪除從動態SQLfunctionsstored procedures,要求我歸還的東西被丟棄,然後執行那些作爲一個單獨的聲明。一種解決丟棄這些functionsstored 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 } ] 
+1

做的console.log( wipeSQL)之前conn.query並檢查什麼查詢獲取構建並傳遞給conn.query.Then在mysql查詢瀏覽器上手動觸發此查詢以查看結果。 – AJS

+0

@AJS那麼,這是愚蠢的。可能是時間傳遞給晚上,問題歸結爲用'.split('%% DATABASE_NAME %%')替換'.replace('%% DATABASE_NAME %%',conf ['databasedatabase'])'。join(conf ['databasedatabase'] )'謝謝,如果你發佈了這個效果的答案,我會接受它,因爲你找到了它。 – CoryG

+0

歡迎...添加爲答案... – AJS

回答

1

做conn.query之前的console.log(wipeSQL),並檢查什麼查詢得到建立和手動傳遞到conn.query.Then火這個查詢在MySQL查詢瀏覽器看到的結果。