简体   繁体   English

Node.js Firebird SQL 连接器 - 从 INSERT 获取结果......返回

[英]Node.js Firebird SQL connector - getting result from INSERT … RETURNING

I am trying to write a function that lets me insert a value into the firebird database.我正在尝试编写一个函数,让我在 firebird 数据库中插入一个值。 The query works well, only I get no callback to tell me that the insert went well.查询运行良好,只有我没有得到回调告诉我插入进行得很顺利。

It is the first time I am using a firebird connector.这是我第一次使用 firebird 连接器。 In the past, when using mySql connectors I can recall having some sort of callback when inserting new values.过去,当使用 mySql 连接器时,我可以回忆起在插入新值时有某种回调。 Right now I am using the node-firebird library by Henri Gourvest to accomplish this: https://github.com/hgourvest/node-firebird/现在我正在使用 Henri Gourvest 的 node-firebird 库来完成这个: https : //github.com/hgourvest/node-firebird/

I tried adding 'RETURNING FEATURE_ID' at the end, but an error "Cursor is not open" was thrown.我尝试在最后添加 'RETURNING FEATURE_ID',但抛出错误“光标未打开”。 The feature ID is generated by a trigger.功能 ID 由触发器生成。

Any advice would be very kind.任何建议都会非常友好。

pool.get(function(error, db) {
    if (error) {
        console.log(error)
        res.status(403)
        res.send()

    }
    else {
        var date = moment(req.body.date, "DD/MM/YYYY")
        var values = " VALUES ('" + date.format("MM/DD/YYYY") + "','Requested','" + req.body.type + "','" + req.body.description + "','" + req.body.memo +"')"
        var query = 'INSERT INTO "Features" (FEATURE_REQUESTDATE, FEATURE_STATUS, FEATURE_TYPE, FEATURE_DESCRIPTION, FEATURE_MEMO)' + values


        db.query( query , function(err, result) {
            if (result) { //why is there no result here?
                res.status(200)
                res.send('ok')
            }
            if (err) {
                console.log(err)
                res.status(403)
                res.send('error')
            }
        })


            db.detach();
        }
})

I tried adding 'RETURNING FEATURE_ID' at the end, but an error "Cursor is not open" was thrown.我尝试在最后添加 'RETURNING FEATURE_ID',但抛出错误“光标未打开”。

Sure, there can be no cursor.当然,不能有游标。 Cursors (AKA rowsets) are only created by queries - SELECT -type SQL statements.游标(AKA 行集)仅由查询创建 - SELECT类型的 SQL 语句。

As stated in Firebird documentation, statements with RETURNING clause are not of query type, they are of procedure call type.正如 Firebird 文档中所述,带有RETURNING子句的语句不是查询类型,而是过程调用类型。 You should execute them as you do with regular DELETE -type statements, then read the PARAMETERS of the statement executed.您应该像使用常规DELETE类型语句一样执行它们,然后读取所执行语句的参数。

Right now I am using the node-firebird library by Henri Gourvest to accomplish this: https://github.com/hgourvest/node-firebird/现在我正在使用 Henri Gourvest 的 node-firebird 库来完成这个: https : //github.com/hgourvest/node-firebird/
Any advice would be very kind.任何建议都会非常友好。

There are two advices.有两个建议。

  1. NEVER do splice your data values into SQL command text.永远不要将您的数据值拼接到 SQL命令文本中。 It makes your program very fragile.它使您的程序非常脆弱。 It would give you all the kinds of data conversion errors, and also it opens highways for your database corruption made by unexpected - erroneous or malicious - user input.它会给您带来各种数据转换错误,而且还会为您的数据库因意外(错误或恶意)用户输入造成的损坏打开高速公路。 See http://bobby-tables.com/ and http://en.wikipedia.org/wiki/SQL_injection请参阅http://bobby-tables.com/http://en.wikipedia.org/wiki/SQL_injection

  2. "Use the source Luke". “使用来源卢克”。 The library you mentioned is open-source.您提到的库是开源的。 So you have to check the examples in that library.因此,您必须检查该库中的示例。 Henri is known to be very laconic about documentation.众所周知,Henri 对文档非常简洁。 However he supplies his different libraries with vast sets of examples and/or tests.然而,他为他的不同库提供了大量示例和/或测试。 Both suit for you, as they do use the library , and so you can just read how the library was intended to be used by its creator.两者都适合您,因为它们确实使用了 library ,因此您可以阅读其创建者打算如何使用该库。 This particular library has tests.这个特定的库有测试。 And tests are always examples of intended use.测试始终是预期用途的示例。

So you go into test folder and you see there run.js file.所以你进入test文件夹,你会看到run.js文件。 Open it.打开它。

Now press Ctrl+F and search for "RETURNING" word.现在按 Ctrl+F 并搜索“返回”字样。 Not always first time, but one of its occurrences should be exactly test for the SQL feature you need.并非总是第一次,但其中一次应该完全测试您需要的 SQL 功能。

Here it is, the very FIRST occurrence of it in the library text you already have on your machine.在这里,它在您机器上已有的库文本中第一次出现。 Granted, the first occurrence adds complexity of working with BLOBs that you do not need right off.诚然,第一次出现会增加处理 BLOB 的复杂性,而您不需要立即使用。 So I would quote the THIRD example in the library you downloaded.所以我会引用你下载的库中的第三个例子。 But even the first example shows you how to properly execute queries with values and with RETURNING clauses.但即使是第一个示例也向您展示了如何使用值和 RETURNING 子句正确执行查询。

function test_insert(next) {

....skip.......

   // Insert record without blob 
         database.query('INSERT INTO test (ID, NAME, CREATED, PARENT) VALUES(?, ?, ?, ?) RETURNING ID', [3, 'Firebird 3', now, 862304020112911], function(err, r) { 
             assert.ok(!err, name + ': insert without blob (buffer) (1) ' + err); 
             assert.ok(r['id'] === 3, name + ': without blob (buffer) returning value'); 
             next(); 
         }); 

     // Insert record without blob (without returning value) 
         database.query('INSERT INTO test (ID, NAME, CREATED) VALUES(?, ?, ?)', [4, 'Firebird 4', '2014-12-12 13:59'], function(err, r) { 
             assert.ok(!err, name + ': insert without blob (buffer) (2) ' + err); 
             assert.ok(err === undefined, name + ': insert without blob + without returning value'); 
             next(); 
         }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM