mirror of
https://github.com/knex/knex.git
synced 2025-07-04 07:26:21 +00:00
82 lines
1.7 KiB
JavaScript
82 lines
1.7 KiB
JavaScript
![]() |
const helpers = require('../util/helpers');
|
||
|
const extend = require('lodash/extend');
|
||
|
|
||
|
class ViewBuilder {
|
||
|
constructor(client, method, viewName, fn) {
|
||
|
this.client = client;
|
||
|
this._method = method;
|
||
|
this._schemaName = undefined;
|
||
|
this._columns = undefined;
|
||
|
this._fn = fn;
|
||
|
this._viewName = viewName;
|
||
|
this._statements = [];
|
||
|
this._single = {};
|
||
|
}
|
||
|
|
||
|
setSchema(schemaName) {
|
||
|
this._schemaName = schemaName;
|
||
|
}
|
||
|
|
||
|
columns(columns) {
|
||
|
this._columns = columns;
|
||
|
}
|
||
|
|
||
|
as(selectQuery) {
|
||
|
this._selectQuery = selectQuery;
|
||
|
}
|
||
|
|
||
|
checkOption() {
|
||
|
throw new Error(
|
||
|
'check option definition is not supported by this dialect.'
|
||
|
);
|
||
|
}
|
||
|
|
||
|
localCheckOption() {
|
||
|
throw new Error(
|
||
|
'check option definition is not supported by this dialect.'
|
||
|
);
|
||
|
}
|
||
|
|
||
|
cascadedCheckOption() {
|
||
|
throw new Error(
|
||
|
'check option definition is not supported by this dialect.'
|
||
|
);
|
||
|
}
|
||
|
|
||
|
toSQL() {
|
||
|
if (this._method === 'alter') {
|
||
|
extend(this, AlterMethods);
|
||
|
}
|
||
|
this._fn.call(this, this);
|
||
|
return this.client.viewCompiler(this).toSQL();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const AlterMethods = {
|
||
|
column(column) {
|
||
|
const self = this;
|
||
|
return {
|
||
|
rename: function (newName) {
|
||
|
self._statements.push({
|
||
|
grouping: 'alterView',
|
||
|
method: 'renameColumn',
|
||
|
args: [column, newName],
|
||
|
});
|
||
|
return this;
|
||
|
},
|
||
|
defaultTo: function (defaultValue) {
|
||
|
self._statements.push({
|
||
|
grouping: 'alterView',
|
||
|
method: 'defaultTo',
|
||
|
args: [column, defaultValue],
|
||
|
});
|
||
|
return this;
|
||
|
},
|
||
|
};
|
||
|
},
|
||
|
};
|
||
|
|
||
|
helpers.addQueryContext(ViewBuilder);
|
||
|
|
||
|
module.exports = ViewBuilder;
|