sqb-serializer-oracle
Advanced tools
Comparing version 0.4.3 to 0.5.0
@@ -13,3 +13,3 @@ /* sqb-connect-oracledb | ||
module.exports = { | ||
createSerializer(config) { | ||
createSerializer: function(config) { | ||
if (config.dialect === 'oracle') { | ||
@@ -16,0 +16,0 @@ return new OracleSerializer(config); |
@@ -8,107 +8,116 @@ /* sqb-serializer-oracle | ||
*/ | ||
/** | ||
* Module variables. | ||
* @private | ||
*/ | ||
const reservedWords = ['comment', 'dual']; | ||
class OracleSerializer { | ||
/** | ||
* Expose `OracleSerializer`. | ||
*/ | ||
module.exports = OracleSerializer; | ||
constructor() { | ||
} | ||
/** | ||
* | ||
* @constructor | ||
*/ | ||
function OracleSerializer() { | ||
} | ||
// noinspection JSMethodCanBeStatic,JSUnusedGlobalSymbols | ||
isReserved(s) { | ||
return reservedWords.includes(String(s).toLowerCase()); | ||
} | ||
const proto = OracleSerializer.prototype = {}; | ||
proto.constructor = OracleSerializer; | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
serializeSelect(instance, obj, inf) { | ||
let out = instance.serializeSelect(obj, inf); | ||
const limit = instance.query._limit || 0; | ||
const offset = Math.max((obj._offset || 0), 0); | ||
// noinspection JSMethodCanBeStatic,JSUnusedGlobalSymbols | ||
proto.isReserved = function(s) { | ||
return reservedWords.indexOf(String(s).toLowerCase()) >= 0; | ||
}; | ||
if (limit || offset) { | ||
if (instance.config.serverVersion >= 12) { | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
proto.serializeSelect = function(instance, obj, inf) { | ||
var out = instance.serializeSelect(obj, inf); | ||
const limit = instance.query._limit || 0; | ||
const offset = Math.max((obj._offset || 0), 0); | ||
if (limit || offset) { | ||
if (instance.config.serverVersion >= 12) { | ||
if (offset) | ||
out += (offset ? '\nOFFSET ' + offset + ' ROWS' : '') + | ||
(limit ? ' FETCH NEXT ' + limit + ' ROWS ONLY' : ''); | ||
else out += (limit ? '\nFETCH FIRST ' + limit + ' ROWS ONLY' : ''); | ||
} else { | ||
const a = obj._alias; | ||
const order = instance.query._orderby; | ||
if (offset || (order && order.length)) { | ||
out = 'select ' + (a ? a + '.' : '') + '* from (\n\t' + | ||
'select /*+ first_rows(' + (limit || 100) + | ||
') */ t.*, rownum row$number from (\n\t' + | ||
out + '\n\b' + | ||
') t' + | ||
(limit ? ' where rownum <= ' + (limit + offset) : '') + | ||
'\n\b)' + (a ? ' ' + a : ''); | ||
if (offset) | ||
out += (offset ? '\nOFFSET ' + offset + ' ROWS' : '') + | ||
(limit ? ' FETCH NEXT ' + limit + ' ROWS ONLY' : ''); | ||
else out += (limit ? '\nFETCH FIRST ' + limit + ' ROWS ONLY' : ''); | ||
out += ' where row$number >= ' + (offset + 1); | ||
} else { | ||
const a = obj._alias; | ||
const order = instance.query._orderby; | ||
if (offset || (order && order.length)) { | ||
out = 'select ' + (a ? a + '.' : '') + '* from (\n\t' + | ||
'select /*+ first_rows(' + (limit || 100) + | ||
') */ t.*, rownum row$number from (\n\t' + | ||
out + '\n\b' + | ||
') t' + | ||
(limit ? ' where rownum <= ' + (limit + offset) : '') + | ||
'\n\b)' + (a ? ' ' + a : ''); | ||
if (offset) | ||
out += ' where row$number >= ' + (offset + 1); | ||
} else { | ||
out = 'select ' + (a ? a + '.' : '') + '* from (\n\t' + | ||
out + '\n\b' + | ||
') where rownum <= ' + limit; | ||
} | ||
out = 'select ' + (a ? a + '.' : '') + '* from (\n\t' + | ||
out + '\n\b' + | ||
') where rownum <= ' + limit; | ||
} | ||
} | ||
return out; | ||
} | ||
return out; | ||
}; | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
serializeFrom(instance, tables, inf) { | ||
return instance.serializeFrom(tables, inf) || 'from dual'; | ||
} | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
proto.serializeFrom = function(instance, tables, inf) { | ||
return instance.serializeFrom(tables, inf) || 'from dual'; | ||
}; | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
serializeCondition(instance, item, inf) { | ||
let s = instance.serializeCondition(item, inf); | ||
if (!item.isRaw) { | ||
s = s.replace(/!= ?null/g, 'is not null') | ||
.replace(/= ?null/g, 'is null') | ||
.replace(/<> ?null/g, 'is not null'); | ||
} | ||
return s; | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
proto.serializeCondition = function(instance, item, inf) { | ||
var s = instance.serializeCondition(item, inf); | ||
if (!item.isRaw) { | ||
s = s.replace(/!= ?null/g, 'is not null') | ||
.replace(/= ?null/g, 'is null') | ||
.replace(/<> ?null/g, 'is not null'); | ||
} | ||
return s; | ||
}; | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
serializeDateValue(instance, date, inf) { | ||
const s = instance.serializeDateValue(date, inf); | ||
return s.length <= 12 ? | ||
'to_date(' + s + ', \'yyyy-mm-dd\')' : | ||
'to_date(' + s + ', \'yyyy-mm-dd hh24:mi:ss\')'; | ||
} | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
proto.serializeDateValue = function(instance, date, inf) { | ||
const s = instance.serializeDateValue(date, inf); | ||
return s.length <= 12 ? | ||
'to_date(' + s + ', \'yyyy-mm-dd\')' : | ||
'to_date(' + s + ', \'yyyy-mm-dd hh24:mi:ss\')'; | ||
}; | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
serializeReturning(instance, bindings, inf) { | ||
let s = instance.serializeReturning(bindings, inf); | ||
if (s) { | ||
instance.returningParams = {}; | ||
const a = s.substring(10, s.length).split(/\s*,\s*/); | ||
s += ' into '; | ||
a.forEach((n, i) => { | ||
s += (i ? ', ' : '') + ':returning$' + n; | ||
instance.returningParams['returning$' + n] = bindings[n]; | ||
}); | ||
return s; | ||
} else | ||
instance.returningParams = undefined; | ||
} | ||
} | ||
module.exports = OracleSerializer; | ||
//noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic | ||
/** | ||
* @override | ||
*/ | ||
proto.serializeReturning = function(instance, bindings, inf) { | ||
var s = instance.serializeReturning(bindings, inf); | ||
if (s) { | ||
instance.returningParams = {}; | ||
const a = s.substring(10, s.length).split(/\s*,\s*/); | ||
s += ' into '; | ||
a.forEach(function(n, i) { | ||
s += (i ? ', ' : '') + ':returning$' + n; | ||
instance.returningParams['returning$' + n] = bindings[n]; | ||
}); | ||
return s; | ||
} else | ||
instance.returningParams = undefined; | ||
}; |
{ | ||
"name": "sqb-serializer-oracle", | ||
"description": "SQB serialization plugin for Oracle database", | ||
"version": "0.4.3", | ||
"version": "0.5.0", | ||
"author": "Panates Ltd.", | ||
@@ -23,13 +23,13 @@ "contributors": [ | ||
"devDependencies": { | ||
"babel-eslint": "^7.2.3", | ||
"eslint": "^4.6.1", | ||
"babel-eslint": "^8.0.1", | ||
"eslint": "^4.9.0", | ||
"eslint-config-google": "^0.9.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.5.0" | ||
"mocha": "^4.0.1" | ||
}, | ||
"peerDependencies": { | ||
"sqb": "^0.7.10" | ||
"sqb": "^0.8.2" | ||
}, | ||
"engines": { | ||
"node": ">= 6.0" | ||
"node": ">= 4.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "files": [ |
7695
128