Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetchAsString optional parameter to oracledb dialect #1998

Merged
merged 15 commits into from Apr 28, 2017
Merged
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')
})
109 changes: 87 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,72 @@ 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() {
this.timeout(10000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this timeout is here?


describe("with fetchAsString parameter", function() {
var knexClient;

before(function(done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necesssary done()

var conf = _.clone(config.oracledb);
conf.fetchAsString = [ 'number', 'DATE', 'cLOb'];
knexClient = knex(conf);
done();
});

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(done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary done parameter

knexClient = knex(config.oracledb);
done();
});

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();
});

});

});