Skip to content

added functionality #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
287 changes: 262 additions & 25 deletions dist/deparser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/deparser.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 12 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"homepage": "http://github.com/zhm/pg-query-parser",
"main": "dist/index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --reporter dot test/setup.js test/test.js",
"simpletest": "node_modules/.bin/mocha ./test/setup.js ./test/test.js ./test/elements/*.js",
"test":
"./node_modules/.bin/mocha --reporter dot test/setup.js test/test.js",
"simpletest":
"node_modules/.bin/mocha ./test/setup.js ./test/test.js ./test/elements/*.js",
"prepublish": "npm run lint && npm run clean && npm run build",
"lint": "node_modules/.bin/eslint src/* src/**/*",
"build": "node_modules/.bin/babel src --out-dir dist --source-maps",
Expand All @@ -25,19 +27,19 @@
"extends": "fulcrum"
},
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.9.1",
"babel-eslint": "^6.0.4",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-preset-es2015-node5": "^1.2.0",
"babel-preset-stage-1": "^6.5.0",
"browserify": "^13.0.1",
"chai": "^3.5.0",
"coffee-script": "^1.10.0",
"coffeeify": "^2.0.1",
"eslint": "^2.11.1",
"eslint-config-fulcrum": "^1.0.2",
"eslint-plugin-babel": "^3.3.0",
"eslint-plugin-react": "^5.1.1",
"eslint": "^4.18.1",
"eslint-config-fulcrum": "^1.5.4",
"eslint-plugin-babel": "^4.1.2",
"eslint-plugin-react": "^7.7.0",
"glob": "^7.0.3",
"mocha": "^2.5.3",
"npm-check-updates": "^2.6.5",
Expand All @@ -50,12 +52,5 @@
"lodash": "^4.13.1",
"pg-query-native": "^1.1.0"
},
"keywords": [
"sql",
"postgres",
"postgresql",
"pg",
"query",
"database"
]
"keywords": ["sql", "postgres", "postgresql", "pg", "query", "database"]
}
269 changes: 260 additions & 9 deletions src/deparser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import _ from 'lodash';
import { format } from 'util';

const CONSTRAINT_TYPES = [
'NULL',
'NOT NULL',
'DEFAULT',
'CHECK',
'PRIMARY KEY',
'UNIQUE',
'EXCLUDE',
'REFERENCES'
];

const { keys } = _;

const compact = o => {
Expand Down Expand Up @@ -624,8 +635,7 @@ export default class Deparser {
}

const wrapped =
(node.rarg.JoinExpr != null) || node.alias ? '(' + output.join(' ') + ')'
: output.join(' ');
(node.rarg.JoinExpr != null) || node.alias ? '(' + output.join(' ') + ')' : output.join(' ');

if (node.alias) {
return wrapped + ' ' + this.deparse(node.alias);
Expand Down Expand Up @@ -961,6 +971,247 @@ export default class Deparser {
return output.join(' ');
}

['CreateStmt'](node) {
const output = [];
output.push('CREATE TABLE');
output.push(this.deparse(node.relation));
output.push('(');
output.push(this.list(node.tableElts));
output.push(')');
output.push(';');
return output.join(' ');
}

['ConstraintStmt'](node) {
const output = [];
const constraint = CONSTRAINT_TYPES[node.contype];

if (node.conname) {
output.push(`CONSTRAINT ${node.conname} ${constraint}`);
} else {
output.push(constraint);
}

return output.join(' ');
}

['ReferenceConstraint'](node) {
const output = [];
if (node.pk_attrs && node.fk_attrs) {
output.push('FOREIGN KEY');
output.push('(');
output.push(this.list(node.fk_attrs));
output.push(')');
output.push('REFERENCES');
output.push(this.deparse(node.pktable));
output.push('(');
output.push(this.list(node.pk_attrs));
output.push(')');
} else if (node.pk_attrs) {
output.push(this.ConstraintStmt(node));
output.push(this.deparse(node.pktable));
output.push('(');
output.push(this.list(node.pk_attrs));
output.push(')');
} else {
output.push(this.ConstraintStmt(node));
output.push(this.deparse(node.pktable));
}
return output.join(' ');
}

['ExclusionConstraint'](node) {
const output = [];
function getExclusionGroup(nde) {
const out = [];
const a = nde.exclusions.map(excl => {
if (excl[0].IndexElem.name) {
return excl[0].IndexElem.name;
}
return excl[0].IndexElem.expr ? this.deparse(excl[0].IndexElem.expr) : null;
});

const b = nde.exclusions.map(excl => this.deparse(excl[1][0]));

for (let i = 0; i < a.length; i++) {
out.push(`${a[i]} WITH ${b[i]}`);
if (i !== a.length - 1) {
out.push(',');
}
}

return out.join(' ');
}

if (node.exclusions && node.access_method) {
output.push('USING');
output.push(node.access_method);
output.push('(');
output.push(getExclusionGroup.call(this, node));
output.push(')');
}

return output.join(' ');
}

['Constraint'](node) {
const output = [];

const constraint = CONSTRAINT_TYPES[node.contype];
if (!constraint) {
throw new Error('type not implemented: ' + node.contype);
}

if (constraint === 'REFERENCES') {
output.push(this.ReferenceConstraint(node));
} else {
output.push(this.ConstraintStmt(node));
}

if (node.keys) {
output.push('(');
output.push(this.list(node.keys));
output.push(')');
}

if (node.raw_expr) {
output.push(this.deparse(node.raw_expr));
}

if (node.fk_del_action) {
switch (node.fk_del_action) {
case 'r':
output.push('ON DELETE RESTRICT');
break;
case 'c':
output.push('ON DELETE CASCADE');
break;
default:
}
}

if (node.fk_upd_action) {
switch (node.fk_upd_action) {
case 'r':
output.push('ON UPDATE RESTRICT');
break;
case 'c':
output.push('ON UPDATE CASCADE');
break;
default:
}
}

if (constraint === 'EXCLUDE') {
output.push(this.ExclusionConstraint(node));
}

return output.join(' ');
}

['FunctionParameter'](node) {
const output = [];

output.push(node.name);
output.push(this.deparse(node.argType));

return output.join(' ');
}

['CreateFunctionStmt'](node) {
const output = [];

output.push('CREATE');
if (node.replace) {
output.push('OR REPLACE');
}
output.push('FUNCTION');

output.push(node.funcname.map(name => this.deparse(name)).join('.'));
output.push('(');
output.push(
node.parameters
.filter(({ FunctionParameter }) => FunctionParameter.mode === 105)
.map(param => this.deparse(param))
.join(', ')
);
output.push(')');

const returns = node.parameters.filter(
({ FunctionParameter }) => FunctionParameter.mode === 116
);
// var setof = node.parameters.filter(
// ({ FunctionParameter }) => FunctionParameter.mode === 109
// );

output.push('RETURNS');
if (returns.length) {
output.push('TABLE');
output.push('(');
output.push(
node.parameters
.filter(({ FunctionParameter }) => FunctionParameter.mode === 116)
.map(param => this.deparse(param))
.join(', ')
);
output.push(')');
} else {
output.push(this.deparse(node.returnType));
}

const elems = {};

node.options.forEach(option => {
if (option && option.DefElem) {
switch (option.DefElem.defname) {
case 'as':
elems.as = option;
break;

case 'language':
elems.language = option;
break;

case 'volatility':
elems.volatility = option;
break;
default:
}
}
});

output.push(`
AS $$${this.deparse(elems.as.DefElem.arg[0])}$$
LANGUAGE '${this.deparse(elems.language.DefElem.arg)}' ${this.deparse(elems.volatility.DefElem.arg).toUpperCase()};
`);

return output.join(' ');
}
['CreateSchemaStmt'](node) {
const output = [];

output.push('CREATE');
if (node.replace) {
output.push('OR REPLACE');
}
output.push('SCHEMA');
output.push(node.schemaname);
return output.join(' ');
}

['TransactionStmt'](node) {
switch (node.kind) {
case 0:
return 'BEGIN';
case 1:
break;
case 2:
return 'COMMIT';
default:
}
return '';
}

['SortBy'](node) {
const output = [];

Expand Down Expand Up @@ -1299,13 +1550,13 @@ export default class Deparser {
this.INTERVALS[(1 << this.BITS.HOUR)] = [ 'hour' ];
this.INTERVALS[(1 << this.BITS.MINUTE)] = [ 'minute' ];
this.INTERVALS[(1 << this.BITS.SECOND)] = [ 'second' ];
this.INTERVALS[(1 << this.BITS.YEAR | 1 << this.BITS.MONTH)] = [ 'year', 'month' ];
this.INTERVALS[(1 << this.BITS.DAY | 1 << this.BITS.HOUR)] = [ 'day', 'hour' ];
this.INTERVALS[(1 << this.BITS.DAY | 1 << this.BITS.HOUR | 1 << this.BITS.MINUTE)] = [ 'day', 'minute' ];
this.INTERVALS[(1 << this.BITS.DAY | 1 << this.BITS.HOUR | 1 << this.BITS.MINUTE | 1 << this.BITS.SECOND)] = [ 'day', 'second' ];
this.INTERVALS[(1 << this.BITS.HOUR | 1 << this.BITS.MINUTE)] = [ 'hour', 'minute' ];
this.INTERVALS[(1 << this.BITS.HOUR | 1 << this.BITS.MINUTE | 1 << this.BITS.SECOND)] = [ 'hour', 'second' ];
this.INTERVALS[(1 << this.BITS.MINUTE | 1 << this.BITS.SECOND)] = [ 'minute', 'second' ];
this.INTERVALS[((1 << this.BITS.YEAR) | (1 << this.BITS.MONTH))] = [ 'year', 'month' ];
this.INTERVALS[((1 << this.BITS.DAY) | (1 << this.BITS.HOUR))] = [ 'day', 'hour' ];
this.INTERVALS[((1 << this.BITS.DAY) | (1 << this.BITS.HOUR) | (1 << this.BITS.MINUTE))] = [ 'day', 'minute' ];
this.INTERVALS[((1 << this.BITS.DAY) | (1 << this.BITS.HOUR) | (1 << this.BITS.MINUTE) | (1 << this.BITS.SECOND))] = [ 'day', 'second' ];
this.INTERVALS[((1 << this.BITS.HOUR) | (1 << this.BITS.MINUTE))] = [ 'hour', 'minute' ];
this.INTERVALS[((1 << this.BITS.HOUR) | (1 << this.BITS.MINUTE) | (1 << this.BITS.SECOND))] = [ 'hour', 'second' ];
this.INTERVALS[((1 << this.BITS.MINUTE) | (1 << this.BITS.SECOND))] = [ 'minute', 'second' ];

// utils/timestamp.h
// #define INTERVAL_FULL_RANGE (0x7FFF)
Expand Down
Loading