Skip to content

Commit

Permalink
Add fetchAsString optional parameter to oracledb dialect (#1998)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
atiertant authored and elhigu committed Apr 28, 2017
1 parent 5c0b193 commit dd3289b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 29 deletions.
16 changes: 16 additions & 0 deletions src/dialects/oracledb/index.js
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions test/index.js
Expand Up @@ -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')
})
108 changes: 86 additions & 22 deletions 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",
Expand All @@ -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();
});

});

});

0 comments on commit dd3289b

Please sign in to comment.