Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Create View #49

Merged
merged 1 commit into from

1 participant

@jrf0110
Owner
CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW name [ ( column_name [, ...] ) ]
    [ WITH ( view_option_name [= view_option_value] [, ... ] ) ]
    AS query
@jrf0110
Owner

wow, great work, John! Just waiting on Mr. T. to ensure all tests pass. I pity the fool who submits a pull with broken tests!

@jrf0110 jrf0110 merged commit aeda6b3 into from
@jrf0110 jrf0110 deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 11, 2013
  1. @jrf0110

    made create view statement

    jrf0110 authored
This page is out of date. Refresh to see the latest.
View
5 helpers/query-types.js
@@ -47,6 +47,11 @@ define(function(require, exports, module){
, 'alter table {ifExists} {only} {table} {action}'
);
+ queryTypes.add(
+ 'create-view'
+ , 'create {orReplace} {temporary} view {view} {columns} as {expression}'
+ );
+
[
'array_to_json'
, 'row_to_json'
View
23 helpers/query/boolean-helpers.js
@@ -0,0 +1,23 @@
+if (typeof module === 'object' && typeof define !== 'function') {
+ var define = function(factory) {
+ module.exports = factory(require, exports, module);
+ };
+}
+
+define(function(require, exports, module){
+ var helpers = require('../../lib/query-helpers');
+ var bools = {
+ orReplace: 'or replace'
+ , temporary: 'temporary'
+ };
+
+ Object.keys( bools ).forEach( function( key ){
+ helpers.register( key, function( bool, values ){
+ return bool ? bools[ key ] : '';
+ });
+ });
+
+
+
+ return module.exports;
+});
View
2  helpers/query/columns.js
@@ -12,7 +12,7 @@ define(function(require, exports, module){
helpers.register('columns', function(columns, values, query){
if (typeof columns != 'object') throw new Error('Invalid columns input in query properties');
- if (query.type == 'insert'){
+ if (['insert', 'create-view'].indexOf(query.type) > -1){
return '(' + columns.map(function(col){
return utils.quoteColumn( col );
}).join(', ') + ')';
View
15 helpers/query/view.js
@@ -0,0 +1,15 @@
+if (typeof module === 'object' && typeof define !== 'function') {
+ var define = function(factory) {
+ module.exports = factory(require, exports, module);
+ };
+}
+
+define(function(require, exports, module){
+ var helpers = require('../../lib/query-helpers');
+
+ helpers.register('view', function(view, values, query){
+ return '"' + view + '"';
+ });
+
+ return module.exports;
+});
View
2  index.js
@@ -40,6 +40,8 @@ define(function(require, exports, module){
require('./helpers/query/expression');
require('./helpers/query/from');
require('./helpers/query/distinct');
+ require('./helpers/query/boolean-helpers');
+ require('./helpers/query/view');
// Register conditional helpers
require('./helpers/conditional');
View
111 test/create-view.js
@@ -0,0 +1,111 @@
+var assert = require('assert');
+var builder = require('../');
+
+describe('Built-In Query Types', function(){
+
+ describe('Type: create view', function(){
+
+ it('it should create a view', function(){
+ var query = builder.sql({
+ type: 'create-view'
+ , view: 'jobs_gt_10'
+ , expression: {
+ type: 'select'
+ , table: 'jobs'
+ , where: { id: { $gt: 10 } }
+ }
+ });
+
+ assert.equal(
+ query.toString()
+ , [ 'create view "jobs_gt_10" as '
+ , 'select "jobs".* from "jobs" '
+ , 'where "jobs"."id" > $1'
+ ].join('')
+ );
+
+ assert.deepEqual(
+ query.values
+ , [10]
+ );
+ });
+
+ it('it should create or replace a view', function(){
+ var query = builder.sql({
+ type: 'create-view'
+ , view: 'jobs_gt_10'
+ , orReplace: true
+ , expression: {
+ type: 'select'
+ , table: 'jobs'
+ , where: { id: { $gt: 10 } }
+ }
+ });
+
+ assert.equal(
+ query.toString()
+ , [ 'create or replace view "jobs_gt_10" as '
+ , 'select "jobs".* from "jobs" '
+ , 'where "jobs"."id" > $1'
+ ].join('')
+ );
+
+ assert.deepEqual(
+ query.values
+ , [10]
+ );
+ });
+
+ it('it should create a temporary view', function(){
+ var query = builder.sql({
+ type: 'create-view'
+ , view: 'jobs_gt_10'
+ , temporary: true
+ , expression: {
+ type: 'select'
+ , table: 'jobs'
+ , where: { id: { $gt: 10 } }
+ }
+ });
+
+ assert.equal(
+ query.toString()
+ , [ 'create temporary view "jobs_gt_10" as '
+ , 'select "jobs".* from "jobs" '
+ , 'where "jobs"."id" > $1'
+ ].join('')
+ );
+
+ assert.deepEqual(
+ query.values
+ , [10]
+ );
+ });
+
+ it('it should create view with a specific column list', function(){
+ var query = builder.sql({
+ type: 'create-view'
+ , view: 'jobs_gt_10'
+ , columns: ['id', 'name']
+ , expression: {
+ type: 'select'
+ , table: 'jobs'
+ , where: { id: { $gt: 10 } }
+ }
+ });
+
+ assert.equal(
+ query.toString()
+ , [ 'create view "jobs_gt_10" ("id", "name") as '
+ , 'select "jobs".* from "jobs" '
+ , 'where "jobs"."id" > $1'
+ ].join('')
+ );
+
+ assert.deepEqual(
+ query.values
+ , [10]
+ );
+ });
+ });
+});
Something went wrong with that request. Please try again.