node-oracledb

nodeを使ってoracleにアクセスしてみた。


list: test_oracledb.js

#!/usr/bin/env node

var oracledb = require('oracledb');

oracledb.getConnection(
  {
    user          : "hr",
    password      : "*****",
    connectString : "localhost/XE"
  },
  function(err, connection)
  {
    if (err) {
      console.error(err.message);
      return;
    }
    connection.execute(
      "SELECT * FROM jobs",
      function(err, result)
      {
        if (err) {
          console.error(err.message);
          return;
        }
        console.log(result.rows);
      });
  });


実行結果

$ ./test_oracledb.js 
[ [ 'AD_PRES', 'President', 20080, 40000 ],
  [ 'AD_VP', 'Administration Vice President', 15000, 30000 ],
  [ 'AD_ASST', 'Administration Assistant', 3000, 6000 ],
  [ 'FI_MGR', 'Finance Manager', 8200, 16000 ],
  [ 'FI_ACCOUNT', 'Accountant', 4200, 9000 ],
  [ 'AC_MGR', 'Accounting Manager', 8200, 16000 ],
  [ 'AC_ACCOUNT', 'Public Accountant', 4200, 9000 ],
  [ 'SA_MAN', 'Sales Manager', 10000, 20080 ],
  [ 'SA_REP', 'Sales Representative', 6000, 12008 ],
  [ 'PU_MAN', 'Purchasing Manager', 8000, 15000 ],
  [ 'PU_CLERK', 'Purchasing Clerk', 2500, 5500 ],
  [ 'ST_MAN', 'Stock Manager', 5500, 8500 ],
  [ 'ST_CLERK', 'Stock Clerk', 2008, 5000 ],
  [ 'SH_CLERK', 'Shipping Clerk', 2500, 5500 ],
  [ 'IT_PROG', 'Programmer', 4000, 10000 ],
  [ 'MK_MAN', 'Marketing Manager', 9000, 15000 ],
  [ 'MK_REP', 'Marketing Representative', 4000, 9000 ],
  [ 'HR_REP', 'Human Resources Representative', 4000, 9000 ],
  [ 'PR_REP', 'Public Relations Representative', 4500, 10500 ] ]

参照したURL:
https://www.npmjs.com/package/oracledb
https://github.com/oracle/node-oracledb/blob/master/INSTALL.md#instoh
https://blogs.oracle.com/opal/entry/introducing_node_oracledb_a_node

nodeのバージョンを0.10から6.7.0にあげたら動かんようになった(2016/10/1)

sudo n latestでnodeのバージョンをあげたら、0.10から6.7.0になった。。バージョンのあがりかたがえぐい。

$ ./test_oracledb.js 
/home/xxx/test/node_modules/oracledb/lib/oracledb.js:28
    throw err;
    ^

Error: Module did not self-register.
    at Error (native)
    at Object.Module._extensions..node (module.js:583:18)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at Object. (/home/xxx/test/node_modules/oracledb/lib/oracledb.js:23:15)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)


もう一度インストールする(自動でrebuildになってる?)

$ npm install oracledb

> oracledb@1.11.0 install /home/xxx/test/node_modules/oracledb
> node-gyp rebuild

make: Entering directory '/home/xxx/test/node_modules/oracledb/build'
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsOracle.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsPool.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsConnection.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsResultSet.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsMessages.o
  CXX(target) Release/obj.target/oracledb/src/njs/src/njsIntLob.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiEnv.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiEnvImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiException.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiExceptionImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiConnImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiDateTimeArrayImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiPoolImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiStmtImpl.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiUtils.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiLob.o
  CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiCommon.o
  SOLINK_MODULE(target) Release/obj.target/oracledb.node
  COPY Release/oracledb.node
make: Leaving directory '/home/xxx/test/node_modules/oracledb/build'
  • nan@1.9.0 node_modules/oracledb/node_modules/nan
/home/xxx/test └─┬ oracledb@1.11.0 └── nan@2.3.5 npm WARN enoent ENOENT: no such file or directory, open '/home/xxx/test/package.json' npm WARN test No description npm WARN test No repository field. npm WARN test No README data npm WARN test No license field.


動いた!

$ ./test_oracledb.js 
[ [ 'AD_PRES', 'President', 20080, 40000 ],
  [ 'AD_VP', 'Administration Vice President', 15000, 30000 ],
  [ 'AD_ASST', 'Administration Assistant', 3000, 6000 ],
...


参考:
http://gihyo.jp/dev/serial/01/nodejs/0002