sqb-serializer-oracle
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -14,2 +14,3 @@ /* sqb-serializer-oracle | ||
createSerializer: function(config) { | ||
/* istanbul ignore else */ | ||
if (config.dialect === 'oracle') { | ||
@@ -16,0 +17,0 @@ return new OracleSerializer(config); |
@@ -29,25 +29,39 @@ /* sqb-serializer-oracle | ||
proto.isReserved = function(s) { | ||
proto.isReserved = function(ctx, s) { | ||
return reservedWords.indexOf(String(s).toLowerCase()) >= 0; | ||
}; | ||
proto.serialize = function(ctx, type, o, defFn) { | ||
switch (type) { | ||
case 'select_query': | ||
return this.serializeSelect(ctx, o, defFn); | ||
case 'select_from': | ||
return this.serializeFrom(ctx, o, defFn); | ||
case 'operator_ne': | ||
case 'operator_eq': | ||
return this.serializeOperator(ctx, o, defFn); | ||
case 'date': | ||
return this.serializeDateValue(ctx, o, defFn); | ||
case 'returning': | ||
return this.serializeReturning(ctx, o, defFn); | ||
} | ||
}; | ||
/** | ||
* @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); | ||
proto.serializeSelect = function(ctx, o, defFn) { | ||
var out = defFn(ctx, o); | ||
const limit = o.limit || 0; | ||
const offset = Math.max((o.offset || 0), 0); | ||
if (limit || offset) { | ||
if (instance.config.serverVersion >= 12) { | ||
if (ctx.serverVersion >= 12) { | ||
if (offset) | ||
out += (offset ? '\nOFFSET ' + offset + ' ROWS' : '') + | ||
out += '\nOFFSET ' + offset + ' ROWS' + | ||
(limit ? ' FETCH NEXT ' + limit + ' ROWS ONLY' : ''); | ||
else out += (limit ? '\nFETCH FIRST ' + limit + ' ROWS ONLY' : ''); | ||
else out += '\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' + | ||
if (offset || o.orderBy) { | ||
out = 'select * from (\n\t' + | ||
'select /*+ first_rows(' + (limit || 100) + | ||
@@ -57,8 +71,7 @@ ') */ t.*, rownum row$number from (\n\t' + | ||
') t' + | ||
(limit ? ' where rownum <= ' + (limit + offset) : '') + | ||
'\n\b)' + (a ? ' ' + a : ''); | ||
(limit ? ' where rownum <= ' + (limit + offset) : '') + '\n\b)'; | ||
if (offset) | ||
out += ' where row$number >= ' + (offset + 1); | ||
} else { | ||
out = 'select ' + (a ? a + '.' : '') + '* from (\n\t' + | ||
out = 'select * from (\n\t' + | ||
out + '\n\b' + | ||
@@ -75,4 +88,4 @@ ') where rownum <= ' + limit; | ||
*/ | ||
proto.serializeFrom = function(instance, tables, inf) { | ||
return instance.serializeFrom(tables, inf) || 'from dual'; | ||
proto.serializeFrom = function(ctx, o, defFn) { | ||
return defFn(ctx, o) || 'from dual'; | ||
}; | ||
@@ -83,9 +96,9 @@ | ||
*/ | ||
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'); | ||
} | ||
proto.serializeOperator = function(ctx, o, defFn) { | ||
var s = defFn(ctx, o); | ||
s = s | ||
.replace(/not = *null/g, 'is not null') | ||
.replace(/!= *null/g, 'is not null') | ||
.replace(/= *null/g, 'is null') | ||
.replace(/<> *null/g, 'is not null'); | ||
return s; | ||
@@ -97,4 +110,4 @@ }; | ||
*/ | ||
proto.serializeDateValue = function(instance, date, inf) { | ||
const s = instance.serializeDateValue(date, inf); | ||
proto.serializeDateValue = function(ctx, o, defFn) { | ||
const s = defFn(ctx, o); | ||
return s.length <= 12 ? | ||
@@ -108,15 +121,12 @@ 'to_date(' + s + ', \'yyyy-mm-dd\')' : | ||
*/ | ||
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; | ||
proto.serializeReturning = function(ctx, arr, defFn) { | ||
var out = ''; | ||
arr.forEach(function(o, i) { | ||
out += (i ? ', ' : '') + | ||
(o.schema ? o.schema + '.' : '') + | ||
(o.table ? o.table + '.' : '') + | ||
(o.isReservedWord ? '"' + o.field + '"' : o.field) + | ||
' into :returning$' + (o.alias || o.field); | ||
}); | ||
return out; | ||
}; |
{ | ||
"name": "sqb-serializer-oracle", | ||
"description": "SQB serialization plugin for Oracle database", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"author": "Panates Ltd.", | ||
@@ -28,6 +28,6 @@ "contributors": [ | ||
"mocha": "^4.0.1", | ||
"sqb": "^1.0.0-beta.2" | ||
"sqb": "1.0.1-alpha.1" | ||
}, | ||
"peerDependencies": { | ||
"sqb": "^1.0.0-beta.2" | ||
"sqb": "1.0.1-alpha.1" | ||
}, | ||
@@ -34,0 +34,0 @@ "engines": { |
7436
132