Skip to content

Commit

Permalink
Simplify formatter use
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Sep 12, 2016
1 parent f05b12f commit 30342c3
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 130 deletions.
4 changes: 1 addition & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ inherits(Client, EventEmitter)

assign(Client.prototype, {

Formatter,

formatter() {
return new this.Formatter(this)
return new Formatter(this)
},

QueryBuilder,
Expand Down
27 changes: 0 additions & 27 deletions src/dialects/mssql/formatter.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/dialects/mssql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { assign, map, flatten, values } from 'lodash'
import inherits from 'inherits';

import Formatter from './formatter';
import Client from '../../client';
import Promise from 'bluebird';
import * as helpers from '../../helpers';
Expand Down Expand Up @@ -44,8 +43,6 @@ assign(Client_MSSQL.prototype, {

Transaction,

Formatter,

QueryCompiler,

SchemaCompiler,
Expand Down
4 changes: 2 additions & 2 deletions src/dialects/mssql/query/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ assign(QueryCompiler_MSSQL.prototype, {
case 'update':
case 'insert':
return value
? `output ${this.formatter.columnizeWithPrefix('inserted.', value)}`
? `output inserted.${this.formatter.columnize(value)}`
: '';
case 'del':
return value
? `output ${this.formatter.columnizeWithPrefix('deleted.', value)}`
? `output deleted.${this.formatter.columnize(value)}`
: '';
case 'rowcount':
return value
Expand Down
32 changes: 0 additions & 32 deletions src/dialects/oracle/formatter.js

This file was deleted.

27 changes: 23 additions & 4 deletions src/dialects/oracle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import { assign, map, flatten, values } from 'lodash'

import inherits from 'inherits';
import Formatter from './formatter';
import Client from '../../client';
import Promise from 'bluebird';
import * as helpers from '../../helpers';
import {bufferToString} from '../../query/string';
import Formatter from '../../formatter';

import Transaction from './transaction';
import QueryCompiler from './query/compiler';
Expand All @@ -22,7 +22,7 @@ import { ReturningHelper } from './utils';
// Always initialize with the "QueryBuilder" and "QueryCompiler"
// objects, which extend the base 'lib/query/builder' and
// 'lib/query/compiler', respectively.
function Client_Oracle(config) {
export default function Client_Oracle(config) {
Client.call(this, config)
}
inherits(Client_Oracle, Client)
Expand All @@ -39,7 +39,9 @@ assign(Client_Oracle.prototype, {

Transaction,

Formatter,
formatter() {
return new Oracle_Formatter(this)
},

QueryCompiler,

Expand Down Expand Up @@ -171,4 +173,21 @@ assign(Client_Oracle.prototype, {

})

export default Client_Oracle
export class Oracle_Formatter extends Formatter {

alias(first, second) {
return first + ' ' + second;
}

parameter(value, notSetValue) {
// Returning helper uses always ROWID as string
if (value instanceof ReturningHelper && this.client.driver) {
value = new this.client.driver.OutParam(this.client.driver.OCCISTRING)
}
else if (typeof value === 'boolean') {
value = value ? 1 : 0
}
return super.parameter(value, notSetValue)
}

}
27 changes: 0 additions & 27 deletions src/dialects/oracledb/formatter.js

This file was deleted.

25 changes: 22 additions & 3 deletions src/dialects/oracledb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
// -------
const _ = require('lodash');
const inherits = require('inherits');
const Client_Oracle = require('../oracle');
const QueryCompiler = require('./query/compiler');
const ColumnCompiler = require('./schema/columncompiler');
const Formatter = require('./formatter');
const BlobHelper = require('./utils').BlobHelper;
const ReturningHelper = require('./utils').ReturningHelper;
const Promise = require('bluebird');
const stream = require('stream');
const helpers = require('../../helpers');
const Transaction = require('./transaction');
const Oracle_Formatter = require('../oracle/formatter');
const BlobHelper = require('./utils').BlobHelper;

import Client_Oracle, {Oracle_Formatter} from '../oracle';

function Client_Oracledb() {
Client_Oracle.apply(this, arguments);
Expand All @@ -33,7 +35,9 @@ Client_Oracledb.prototype._driver = function() {

Client_Oracledb.prototype.QueryCompiler = QueryCompiler;
Client_Oracledb.prototype.ColumnCompiler = ColumnCompiler;
Client_Oracledb.prototype.Formatter = Formatter;
Client_Oracledb.prototype.formatter = function() {
return new Oracledb_Formatter(this)
};
Client_Oracledb.prototype.Transaction = Transaction;

Client_Oracledb.prototype.prepBindings = function(bindings) {
Expand Down Expand Up @@ -345,4 +349,19 @@ Client_Oracledb.prototype.processResponse = function(obj, runner) {
}
};

class Oracledb_Formatter extends Oracle_Formatter {

// Checks whether a value is a function... if it is, we compile it
// otherwise we check whether it's a raw
parameter(value) {
if (typeof value === 'function') {
return this.outputQuery(this.compileCallback(value), true);
} else if (value instanceof BlobHelper) {
return 'EMPTY_BLOB()';
}
return this.unwrapRaw(value, true) || '?';
}

}

module.exports = Client_Oracledb;
41 changes: 19 additions & 22 deletions src/formatter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import QueryBuilder from './query/builder';
import Raw from './raw';

import { assign, transform } from 'lodash'
import { transform } from 'lodash'

// Valid values for the `order by` clause generation.
const orderBys = ['asc', 'desc'];
Expand All @@ -17,12 +16,12 @@ const operators = transform([
result[key] = true
}, {});

function Formatter(client) {
this.client = client
this.bindings = []
}
export default class Formatter {

assign(Formatter.prototype, {
constructor(client) {
this.client = client
this.bindings = []
}

// Accepts a string or array of columns to wrap as appropriate.
columnize(target) {
Expand All @@ -33,7 +32,7 @@ assign(Formatter.prototype, {
str += this.wrap(columns[i])
}
return str
},
}

// Turns a list of values into a list of ?'s, joining them with commas unless
// a "joining" value is specified (e.g. ' and ')
Expand All @@ -46,7 +45,7 @@ assign(Formatter.prototype, {
str += this.parameter(values[i] === undefined ? notSetValue : values[i])
}
return str;
},
}

// Checks whether a value is a function... if it is, we compile it
// otherwise we check whether it's a raw
Expand All @@ -55,7 +54,7 @@ assign(Formatter.prototype, {
return this.outputQuery(this.compileCallback(value), true);
}
return this.unwrapRaw(value, true) || '?';
},
}

unwrapRaw(value, isParameter) {
let query;
Expand All @@ -77,14 +76,14 @@ assign(Formatter.prototype, {
if (isParameter) {
this.bindings.push(value);
}
},
}

rawOrFn(value, method) {
if (typeof value === 'function') {
return this.outputQuery(this.compileCallback(value, method));
}
return this.unwrapRaw(value) || '';
},
}

// Puts the appropriate wrapper around a value depending on the database
// engine, unless it's a knex.raw value, in which case it's left alone.
Expand All @@ -96,15 +95,15 @@ assign(Formatter.prototype, {
if (raw) return raw;
if (typeof value === 'number') return value;
return this._wrapString(value + '');
},
}

wrapAsIdentifier(value) {
return this.client.wrapIdentifier((value || '').trim());
},
}

alias(first, second) {
return first + ' as ' + second;
},
}

// The operator method takes a value and returns something or other.
operator(value) {
Expand All @@ -114,14 +113,14 @@ assign(Formatter.prototype, {
throw new TypeError(`The operator "${value}" is not permitted`);
}
return value;
},
}

// Specify the direction of the ordering.
direction(value) {
const raw = this.unwrapRaw(value);
if (raw) return raw;
return orderBys.indexOf((value || '').toLowerCase()) !== -1 ? value : 'asc';
},
}

// Compiles a callback using the query builder.
compileCallback(callback, method) {
Expand All @@ -137,7 +136,7 @@ assign(Formatter.prototype, {

// Return the compiled & parameterized sql.
return compiler.toSQL(method || 'select');
},
}

// Ensures the query is aliased if necessary.
outputQuery(compiled, isParameter) {
Expand All @@ -149,7 +148,7 @@ assign(Formatter.prototype, {
}
}
return sql;
},
}

// Coerce to string to prevent strange errors when it's not a string.
_wrapString(value) {
Expand All @@ -173,6 +172,4 @@ assign(Formatter.prototype, {
return wrapped.join('.');
}

});

export default Formatter;
}
6 changes: 0 additions & 6 deletions src/util/make-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export default function makeClient(ParentClient) {
}
inherits(Client, ParentClient)

function Formatter(client) {
Formatter.super_.call(this, client)
}
inherits(Formatter, ParentClient.prototype.Formatter)

function QueryBuilder(client) {
QueryBuilder.super_.call(this, client)
}
Expand Down Expand Up @@ -57,7 +52,6 @@ export default function makeClient(ParentClient) {
inherits(ColumnCompiler, ParentClient.prototype.ColumnCompiler)

assign(Client.prototype, {
Formatter,
QueryBuilder,
SchemaBuilder,
SchemaCompiler,
Expand Down
Loading

0 comments on commit 30342c3

Please sign in to comment.