From dd3289be9426f675c1c2b3aa929d5c8bc0a5e262 Mon Sep 17 00:00:00 2001 From: Alexandre Tiertant Date: Fri, 28 Apr 2017 11:47:24 +0100 Subject: [PATCH] Add fetchAsString optional parameter to oracledb dialect (#1998) * Add optional parameter fetchAsString to oracledb * Add some checks to oracle fetchAsString * use const instead of let * add fetchAsString test * change const to var in tests * move oracle driver specific test to bottom * fix fetchAsSting multiple instance * Fix tests * replace const to var in test * Rework Oracle fetchAsString tests * remove const * remove done and timeout --- src/dialects/oracledb/index.js | 16 +++++ test/index.js | 14 ++--- test/unit/dialects/oracledb.js | 108 ++++++++++++++++++++++++++------- 3 files changed, 109 insertions(+), 29 deletions(-) diff --git a/src/dialects/oracledb/index.js b/src/dialects/oracledb/index.js index ba0a90f5ae..dd68a6f7d1 100644 --- a/src/dialects/oracledb/index.js +++ b/src/dialects/oracledb/index.js @@ -28,7 +28,21 @@ inherits(Client_Oracledb, Client_Oracle); Client_Oracledb.prototype.driverName = 'oracledb'; Client_Oracledb.prototype._driver = function() { + const client = this; const oracledb = require('oracledb'); + client.fetchAsString = []; + if (this.config.fetchAsString && _.isArray(this.config.fetchAsString)) { + this.config.fetchAsString.forEach(function(type) { + if (!_.isString(type)) return; + type = type.toUpperCase(); + if (oracledb[type]) { + if (type !== 'NUMBER' && type !== 'DATE' && type !== 'CLOB') { + helpers.warn('Only "date", "number" and "clob" are supported for fetchAsString'); + } + client.fetchAsString.push(oracledb[type]); + } + }); + } return oracledb; }; @@ -86,6 +100,8 @@ Client_Oracledb.prototype.acquireRawConnection = function() { oracleDbConfig.stmtCacheSize = client.connectionSettings.stmtCacheSize; } + client.driver.fetchAsString = client.fetchAsString; + client.driver.getConnection(oracleDbConfig, function(err, connection) { if (err) { return rejecter(err); diff --git a/test/index.js b/test/index.js index 27de3ecdc2..401c759863 100644 --- a/test/index.js +++ b/test/index.js @@ -31,15 +31,15 @@ describe('Query Building Tests', function() { require('./unit/schema/oracledb') }) +describe('Integration Tests', function() { + this.timeout(process.env.KNEX_TEST_TIMEOUT || 5000); + require('./integration') +}) + var config = require('./knexfile'); -if (config.oracle) { - describe('ExternalAuth ORACLE Tests', function() { +if (config.oracledb) { + describe('Oracledb driver tests', function() { this.timeout(process.env.KNEX_TEST_TIMEOUT || 5000); require('./unit/dialects/oracledb'); }); } - -describe('Integration Tests', function() { - this.timeout(process.env.KNEX_TEST_TIMEOUT || 5000); - require('./integration') -}) diff --git a/test/unit/dialects/oracledb.js b/test/unit/dialects/oracledb.js index 80875a832f..6e6fe9d0e8 100644 --- a/test/unit/dialects/oracledb.js +++ b/test/unit/dialects/oracledb.js @@ -1,32 +1,29 @@ -/*global it, describe, expect*/ +// global it, describe, expect 'use strict'; +var _ = require('lodash'); var expect = require('chai').expect; var knex = require('../../../knex'); -var knexInstance = knex( - { - client: 'oracledb', - connection: { - user : "user", - password : "password", - connectString : 'connect-string', - externalAuth : true, - host : "host", - database : "database" - } - } - ); -var spy; +var config = require('../../knexfile'); -beforeEach(function() { - spy = sinon.spy(knexInstance.client.driver, "getConnection"); -}); +describe("OracleDb externalAuth", function() { + var knexInstance = knex({ + client: 'oracledb', + connection: { + user : "user", + password : "password", + connectString : 'connect-string', + externalAuth : true, + host : "host", + database : "database" + } + }); + var spy; -afterEach(function() { - sinon.restore(knexInstance.client.driver.getConnection); -}); + before(function() { + spy = sinon.spy(knexInstance.client.driver, "getConnection"); + }); -describe("OracleDb externalAuth", function() { it('externalAuth and connectString should be sent to the getConnection', function() { var connectionWithExternalAuth = { connectString: "connect-string", @@ -39,4 +36,71 @@ describe("OracleDb externalAuth", function() { expect(spy).to.have.callCount(1); expect(spy).to.have.been.calledWith(connectionWithExternalAuth); }); + + after(function() { + sinon.restore(knexInstance.client.driver.getConnection); + }); + +}); + +describe("OracleDb parameters", function() { + + describe("with fetchAsString parameter", function() { + var knexClient; + + before(function() { + var conf = _.clone(config.oracledb); + conf.fetchAsString = [ 'number', 'DATE', 'cLOb']; + knexClient = knex(conf); + return knexClient; + }); + + it('on float', function() { + return knexClient.raw('select 7.329 as "field" from dual').then(function(result) { + expect(result[0]).to.be.ok; + expect(result[0].field).to.be.a('string'); + }) + }); + + it('on date', function() { + return knexClient.raw('select CURRENT_DATE as "field" from dual').then(function(result) { + expect(result[0]).to.be.ok; + expect(result[0].field).to.be.a('string'); + }) + }); + + after(function() { + return knexClient.destroy(); + }); + + }); + + describe("without fetchAsString parameter", function() { + var knexClient; + + before(function() { + knexClient = knex(config.oracledb); + return knexClient; + }); + + it('on float', function() { + return knexClient.raw('select 7.329 as "field" from dual').then(function(result) { + expect(result[0]).to.be.ok; + expect(result[0].field).to.not.be.a('string'); + }) + }); + + it('on date', function() { + return knexClient.raw('select CURRENT_DATE as "field" from dual').then(function(result) { + expect(result[0]).to.be.ok; + expect(result[0].field).to.not.be.a('string'); + }) + }); + + after(function() { + return knexClient.destroy(); + }); + + }); + });