Skip to content

Commit

Permalink
Merge 1380102 into 1c6d191
Browse files Browse the repository at this point in the history
  • Loading branch information
kimung committed Jul 11, 2018
2 parents 1c6d191 + 1380102 commit fa301f7
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 135 deletions.
3 changes: 2 additions & 1 deletion lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class BaseDataProvider extends BaseProvider {
let query = this.section.section.query || {}
let visitor = load(query.visitor, 'sql/visitor/expression')
let translator = load(query.translator, 'sql/translator/expression')
let mixins = load(query.mixins, 'sql/mixins')
let QueryProvider = load(query.provider, 'query/default')

this.query = new QueryProvider(visitor, translator)
this.query = new QueryProvider(visitor, translator, mixins)
}
execute (command, parameters) {
errors.throwNotImplementedError('execute')
Expand Down
5 changes: 2 additions & 3 deletions lib/query/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ const data = require('../')
const BaseQueryProvider = require('./base')

class DefaultQueryProvider extends BaseQueryProvider {
constructor (ExpressionVisitor, ExpressionTranslator) {
constructor (ExpressionVisitor, ExpressionTranslator, mixins) {
super()
this.visitor = new ExpressionVisitor(this)
this.translator = new ExpressionTranslator(this)
this.mixins = mixins ? mixins(this) : {}
logger.debug(`QueryProvider initialized with visitor ${ExpressionVisitor.name} and translator ${ExpressionTranslator.name}`)
}
get InsertExpression () {
Expand All @@ -58,8 +59,6 @@ class DefaultQueryProvider extends BaseQueryProvider {
return '?'
})

console.log(command, parameters)

return data.getInstance().execute(command, parameters)
}
}
Expand Down
35 changes: 10 additions & 25 deletions lib/queryable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,30 @@ class Queryable {
this.expression = expression
}
filter (predicate) {
let expression = new MethodExpression(
this.expression,
'filter', [Expression.lambda.parse(predicate)])
return new Queryable(expression)
return this._method('filter', predicate)
}
select (selector) {
let expression = new MethodExpression(
this.expression,
'select', [Expression.lambda.parse(selector)])
return new Queryable(expression)
return this._method('select', selector)
}
join (queryable, predicate) {
let expression = new MethodExpression(
this.expression,
'join', [queryable.expression, Expression.lambda.parse(predicate)])
return new Queryable(expression)
return this._method('join', queryable.expression, predicate)
}
orderBy (selector) {
let expression = new MethodExpression(
this.expression,
'orderBy', [Expression.lambda.parse(selector)])
return new Queryable(expression)
return this._method('orderBy', selector)
}
groupBy (selector) {
let expression = new MethodExpression(
this.expression,
'groupBy', [Expression.lambda.parse(selector)])
return new Queryable(expression)
return this._method('groupBy', selector)
}
take (count) {
let expression = new MethodExpression(
this.expression,
'take', [count])
return new Queryable(expression)
return this._method('limit', count)
}
skip (count) {
return this._method('offset', count)
}
_method (type, ...args) {
let expression = new MethodExpression(
this.expression,
'skip', [count])
type, args.map(arg => typeof arg === 'function' ? Expression.lambda.parse(arg) : arg))
return new Queryable(expression)
}
first (context) {
Expand Down
34 changes: 34 additions & 0 deletions lib/sql/mixins/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* MIT License
*
* Copyright (c) 2016 - 2018 RDUK <tech@rduk.fr>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict'

module.exports = query =>
BaseEntity => class extends BaseEntity {
_delete (pk) {
let expression = new query.DeleteExpression(this.source)
expression.where = this._generateWhere(pk)
return query.execute(expression, this)
}
}
35 changes: 35 additions & 0 deletions lib/sql/mixins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* MIT License
*
* Copyright (c) 2016 - 2018 RDUK <tech@rduk.fr>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict'

const deleteMixin = require('./delete')
const insertMixin = require('./insert')
const updateMixin = require('./update')

module.exports = queryProvider => ({
delete: deleteMixin(queryProvider),
insert: insertMixin(queryProvider),
update: updateMixin(queryProvider)
})
43 changes: 43 additions & 0 deletions lib/sql/mixins/insert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* MIT License
*
* Copyright (c) 2016 - 2018 RDUK <tech@rduk.fr>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict'

const FieldExpression = require('@rduk/expression/lib/parser/expression/field')
const LambdaExpression = require('@rduk/expression/lib/parser/expression/lambda')
const NameExpression = require('@rduk/expression/lib/parser/expression/name')
const ObjectLiteralExpression = require('@rduk/expression/lib/parser/expression/object')
const PropertyExpression = require('@rduk/expression/lib/parser/expression/property')

module.exports = query =>
BaseEntity => class extends BaseEntity {
_create () {
let expression = new query.InsertExpression(this.source)
let obj = new ObjectLiteralExpression(
this._properties.filter(prop => (!!this[prop])).map(prop => (
new FieldExpression(this._info.properties[prop].name || prop, new PropertyExpression(new NameExpression('this'), prop)))))
expression.assignments.push(new LambdaExpression(obj, []))
return query.execute(expression, this)
}
}
44 changes: 44 additions & 0 deletions lib/sql/mixins/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* MIT License
*
* Copyright (c) 2016 - 2018 RDUK <tech@rduk.fr>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict'

const FieldExpression = require('@rduk/expression/lib/parser/expression/field')
const LambdaExpression = require('@rduk/expression/lib/parser/expression/lambda')
const NameExpression = require('@rduk/expression/lib/parser/expression/name')
const ObjectLiteralExpression = require('@rduk/expression/lib/parser/expression/object')
const PropertyExpression = require('@rduk/expression/lib/parser/expression/property')

module.exports = query =>
BaseEntity => class extends BaseEntity {
_update (pk) {
let expression = new query.UpdateExpression(this.source)
let obj = new ObjectLiteralExpression(
this._properties.filter(prop => (!!this[prop] && !!pk.filter(p => p.name !== prop).length)).map(prop => (
new FieldExpression(this._info.properties[prop].name || prop, new PropertyExpression(new NameExpression('this'), prop)))))
expression.where = this._generateWhere(pk)
expression.assignments.push(new LambdaExpression(obj, []))
return query.execute(expression, this)
}
}
89 changes: 89 additions & 0 deletions lib/sql/schema/entity/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* MIT License
*
* Copyright (c) 2016 - 2018 RDUK <tech@rduk.fr>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

'use strict'

const errors = require('@rduk/errors')

const BinaryExpression = require('@rduk/expression/lib/parser/expression/binary')
const LambdaExpression = require('@rduk/expression/lib/parser/expression/lambda')
const NameExpression = require('@rduk/expression/lib/parser/expression/name')
const PropertyExpression = require('@rduk/expression/lib/parser/expression/property')
const TokenType = require('@rduk/expression/lib/token/type')

const SourceExpression = require('../../../expression/source')

class BaseEntity {
constructor (info, properties, mapping) {
this._info = info
this._properties = properties
this._mapping = mapping
}
get pk () {
let props = this._properties.filter(key => (this._info.properties[key].pk))
return props.map(prop => ({
name: prop,
value: this[prop]
}))
}
save () {
let promise
let pk = this.pk
if (pk && !!pk.filter(prop => !!prop.value).length) {
promise = this._update(pk)
} else {
promise = this._create()
}

return promise
}
drop () {
return this._delete(this.pk)
}
_generateWhere (pk) {
let binary = pk.map(prop => (
new BinaryExpression(
new PropertyExpression(new NameExpression('item'), prop.name),
new PropertyExpression(new NameExpression('this'), prop.name),
TokenType.EQEQEQ)
)).reduce((acc, cur) => {
return new BinaryExpression(cur, acc, TokenType.AND)
})
return new LambdaExpression(binary, [new NameExpression('item')])
}
_create () {
errors.throwNotImplementedError('_create')
}
_update (pk) {
errors.throwNotImplementedError('_update')
}
_delete (pk) {
errors.throwNotImplementedError('_delete')
}
get source () {
return new SourceExpression(this._info.table)
}
}

module.exports = BaseEntity

0 comments on commit fa301f7

Please sign in to comment.