From 8ad8021d4c89d8a36fcba38a2da154c4540f239e Mon Sep 17 00:00:00 2001 From: David Sferruzza Date: Tue, 29 Dec 2015 01:35:46 +0100 Subject: [PATCH] Fix issue 2 in #22 --- dist/simpleSqlParser.js | 20 ++++++++++++++++++++ dist/simpleSqlParser.min.js | 2 +- dist/simpleSqlParser.withoutDeps.js | 20 ++++++++++++++++++++ dist/simpleSqlParser.withoutDeps.min.js | 2 +- src/sql2ast.js | 20 ++++++++++++++++++++ tests/tests-select.js | 21 +++++++++++++++++++++ 6 files changed, 83 insertions(+), 2 deletions(-) diff --git a/dist/simpleSqlParser.js b/dist/simpleSqlParser.js index ec50b9e..08bc255 100644 --- a/dist/simpleSqlParser.js +++ b/dist/simpleSqlParser.js @@ -836,6 +836,19 @@ var operator = alt( // A number var number = regex(/[-]?\d+\.?\d*/); +// A duration +var duration = seq( + number, + alt( + string('u'), + string('s'), + string('m'), + string('h'), + string('d'), + string('w') + ) +); + /******************************************************************************************** @@ -896,6 +909,13 @@ var expression = seq( column: null, }; }), + duration.map(function(node) { + return { + expression: mkString(node), + table: null, + column: null, + }; + }), number.map(function(node) { return { expression: node, diff --git a/dist/simpleSqlParser.min.js b/dist/simpleSqlParser.min.js index 9250258..fb15dc5 100644 --- a/dist/simpleSqlParser.min.js +++ b/dist/simpleSqlParser.min.js @@ -1 +1 @@ -!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.simpleSqlParser=n()}}(function(){return function n(e,t,r){function i(s,o){if(!t[s]){if(!e[s]){var a="function"==typeof require&&require;if(!o&&a)return a(s,!0);if(u)return u(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var c=t[s]={exports:{}};e[s][0].call(c.exports,function(n){var t=e[s][1][n];return i(t?t:n)},c,c.exports,n,e,t,r)}return t[s].exports}for(var u="function"==typeof require&&require,s=0;se.furthest)return n;var t=n.furthest===e.furthest?n.expected.concat(e.expected):e.expected;return{status:n.status,index:n.index,value:n.value,furthest:e.furthest,expected:t}}function u(e){if(!(e instanceof n))throw new Error("not a parser: "+e)}function s(n){return 1===n.length?n[0]:"one of "+n.join(", ")}function o(n,e){var t=e.index;if(t===n.length)return", got the end of the stream";var r=t>0?"'...":"'",i=n.length-t>12?"...'":"'";return" at character "+t+", got "+r+n.slice(t,t+12)+i}var a=n.prototype;r.formatError=function(n,e){return"expected "+s(e.expected)+o(n,e)};a.parse=function(n){var e=this.skip(d)._(n,0);return e.status?{status:!0,value:e.value}:{status:!1,index:e.furthest,expected:e.expected}};var l=r.seq=function(){var t=[].slice.call(arguments),r=t.length;return n(function(n,u){for(var s,o=new Array(r),a=0;r>a;a+=1){if(s=i(t[a]._(n,u),s),!s.status)return s;o[a]=s.value,u=s.index}return i(e(u,o),s)})},c=r.seqMap=function(){var n=[].slice.call(arguments),e=n.pop();return l.apply(null,n).map(function(n){return e.apply(null,n)})},p=(r.custom=function(r){return n(r(e,t))},r.alt=function(){var e=[].slice.call(arguments),t=e.length;return 0===t?h("zero alternates"):n(function(n,t){for(var r,u=0;uc;c+=1){if(o=u._(n,s),a=i(o,a),!o.status)return a;s=o.index,l.push(o.value)}for(;r>c&&(o=u._(n,s),a=i(o,a),o.status);c+=1)s=o.index,l.push(o.value);return i(e(s,l),a)})},a.result=function(n){return this.map(function(e){return n})},a.atMost=function(n){return this.times(0,n)},a.atLeast=function(n){return c(this.times(n),this.many(),function(n,e){return n.concat(e)})},a.map=function(t){var r=this;return n(function(n,u){var s=r._(n,u);return s.status?i(e(s.index,t(s.value)),s):s})},a.skip=function(n){return l(this,n).map(function(n){return n[0]})},a.mark=function(){return c(v,this,v,function(n,e,t){return{start:n,value:e,end:t}})},a.desc=function(e){var t=this;return n(function(n,r){var i=t._(n,r);return i.status||(i.expected=[e]),i})};var f=(r.string=function(r){var i=r.length,u="'"+r+"'";return n(function(n,s){var o=n.slice(s,s+i);return o===r?e(s+i,o):t(s,u)})},r.regex=function(r,i){var u=RegExp("^(?:"+r.source+")",(""+r).slice((""+r).lastIndexOf("/")+1)),s=""+r;return null==i&&(i=0),n(function(n,r){var o=u.exec(n.slice(r));if(o){var a=o[0],l=o[i];if(null!=l)return e(r+a.length,l)}return t(r,s)})}),m=r.succeed=function(t){return n(function(n,r){return e(r,t)})},h=r.fail=function(e){return n(function(n,r){return t(r,e)})},d=(r.letter=f(/[a-z]/i).desc("a letter"),r.letters=f(/[a-z]*/i),r.digit=f(/[0-9]/).desc("a digit"),r.digits=f(/[0-9]*/),r.whitespace=f(/\s+/).desc("whitespace"),r.optWhitespace=f(/\s*/),r.any=n(function(n,r){return r>=n.length?t(r,"any character"):e(r+1,n.charAt(r))}),r.all=n(function(n,t){return e(n.length,n.slice(t))}),r.eof=n(function(n,r){return r=0})},r.noneOf=function(n){return x(function(e){return n.indexOf(e)<0})},r.takeWhile=function(t){return n(function(n,r){for(var i=r;i0&&(e+="GROUP BY ",e+=n.group.map(function(n){return n.expression}).join(", ")),e}function s(n){var e="";return n.order.length>0&&(e+="ORDER BY ",e+=n.order.map(function(n){return n.expression}).join(", ")),e}function o(n){var e="";return null!==n.limit&&(e+="LIMIT ",null!==n.limit.from&&(e+=n.limit.from+", "),e+=n.limit.nb),e}function a(n){return"INSERT INTO "+n.into.expression}function l(n){var e="",t=n.values.filter(function(n){return null!==n.target});return t.length>0&&(e+="(",e+=t.map(function(n){return n.target.expression}).join(", "),e+=") "),e+="VALUES (",e+=n.values.map(function(n){return n.value}).join(", "),e+=")"}function c(n){return"UPDATE "+n.table.expression}function p(n){var e="SET ";return e+=n.values.map(function(n){return n.target.expression+" = "+n.value}).join(", ")}if("object"!=typeof n||n.status!==!0)return!1;var f=n.value,m=[];if("select"===f.type)m.push(e(f)),m.push(t(f)),m.push(r(f)),m.push(i(f)),m.push(u(f)),m.push(s(f)),m.push(o(f));else if("insert"===f.type)m.push(a(f)),m.push(l(f));else if("update"===f.type)m.push(c(f)),m.push(p(f)),m.push(i(f));else{if("delete"!==f.type)return!1;m.push("DELETE"),m.push(t(f)),m.push(i(f))}return m.filter(function(n){return""!==n}).join(" ")}},{}],6:[function(n,e,t){"use strict";function r(n,e){return"undefined"==typeof e?n.or(l.succeed([])):n.or(l.succeed(e))}function i(n){return n.join("")}function u(n){return n[0].push(n[1]),n[0]}function s(n){return c(n.skip(h).skip(m(",")).skip(h).many(),n.skip(h)).map(u)}function o(n){return n.replace(/^([`'"])(.*)\1$/,"$2")}function a(n){return c(l.index,n,l.index).map(function(n){var e={start:n[0],end:n[2]};if("object"==typeof n[1]){var t=n[1];return t.position=e,t}return e.out=n[1],e})}var l=n("parsimmon"),c=l.seq,p=l.alt,f=l.regex,m=l.string,h=l.optWhitespace,d=l.whitespace,x=l.lazy,v=p(f(/(?!(FROM|WHERE|GROUP BY|ORDER BY|LIMIT|INNER|LEFT|RIGHT|JOIN|ON|VALUES|SET)\s)[a-z*][a-z0-9_]*/i),f(/`[^`\\]*(?:\\.[^`\\]*)*`/)),g=p(f(/"[^"\\]*(?:\\.[^"\\]*)*"/),f(/'[^'\\]*(?:\\.[^'\\]*)*'/)),j=c(p(f(/[a-zA-Z0-9_]+\(/),m("(")),r(x(function(){return q})).map(i),m(")")).map(i),E=c(v,m("."),v),y=p(m("+"),m("-"),m("*"),m("/"),m("&&"),m("&"),m("~"),m("||"),m("|"),m("^"),f(/XOR/i),m("<=>"),m("="),m("!="),m(">="),m(">>"),m(">"),m("<="),m("<<"),m("<"),f(/IS NULL/i),f(/IS NOT/i),f(/IS NOT NULL/i),f(/IS/i),f(/LIKE/i),f(/NOT LIKE/i),m("%"),f(/MOD/i),f(/NOT/i),f(/OR\s/i),f(/AND/i),f(/IN/i)),b=f(/[-]?\d+\.?\d*/),O=c(m("("),h,c(p(b,g),h,r(m(",")),h,r(p(b,g))).map(i),h,m(")")).map(i),_=c(p(E.map(function(n){return{expression:n.join(""),table:o(n[0]),column:o(n[2])}}),j.map(function(n){return{expression:n,table:null,column:null}}),v.map(function(n){return{expression:n,table:null,column:o(n)}}),g.map(function(n){return{expression:n,table:null,column:null}}),b.map(function(n){return{expression:n,table:null,column:null}}),O.map(function(n){return{expression:n,table:null,column:null}})),r(c(h,y,r(c(h,x(function(){return _}).map(function(n){return n.expression})).map(i),null)).map(i),null)).map(function(n){return null!==n[1]?(n[0]=n[0].expression,{expression:n.join(""),table:null,column:null}):n[0]}),k=c(_,r(c(h,r(f(/AS\s/i)),p(v,g)).map(function(n){var e={};return e.alias=o(n[2]),e.expression=n.join(""),e}),null)).map(function(n){var e=n[0];return e.alias=null!==n[1]?n[1].alias:null,e.expression+=null!==n[1]?n[1].expression:"",e}),R=_.map(function(n){return n.expression}),I=c(p(E.map(i),v,f(/"([^"\\]*(?:\\.[^"\\]*)*)"/,1).map(function(n){return{table:n,expression:'"'+n+'"'}})),r(c(h,r(f(/AS\s/i)),p(v,g)).map(function(n){return{alias:o(n[2]),expression:n.join("")}}),null)).map(function(n){var e={};return"object"==typeof n[0]?(e.table=n[0].table,e.expression=n[0].expression):(e.table=n[0],e.expression=n[0]+(null!==n[1]?n[1].expression:"")),e.alias=null!==n[1]?n[1].alias:null,e}),N=c(r(c(f(/INNER|LEFT|RIGHT/i),d).map(function(n){return n[0].toLowerCase()}),null),f(/JOIN/i),h,a(I),h,f(/ON/i),h,a(_)).map(function(n){var e={};return e.type=n[0]||"inner",e.table=n[3].table,e.alias=n[3].alias,e.position=n[3].position,e.condition={expression:n[7].expression,position:n[7].position},e}),w=a(_).map(function(n){return{expression:n.expression,position:n.position}}),T=c(_,r(c(h,f(/ASC|DESC/i)),null)).map(function(n){return{expression:n[0].expression+(null!==n[1]?n[1].join(""):""),order:null!==n[1]?n[1][1]:"ASC",table:n[0].table,column:n[0].column}}),L=c(b,r(c(h,m(","),h,b),null)).map(function(n){return null===n[1]?{from:null,nb:parseInt(n[0],10)}:{from:parseInt(n[0],10),nb:parseInt(n[1][3],10)}}),S=p(E.map(function(n){return{expression:n.join(""),column:o(n[2])}}),v.map(function(n){return{expression:n,column:o(n)}})),D=_.map(function(n){return n.expression}),A=c(S,h,m("="),h,_).map(function(n){return{target:n[0],value:n[4].expression}}),q=c(c(R,h,m(","),h).map(i).many(),R.skip(h)).map(u),U=s(a(k)),M=s(a(I)),z=s(a(_)),C=s(a(T)),F=h.then(N).skip(h).many(),P=s(S),H=s(D),W=s(A),B=c(f(/SELECT/i).skip(h).then(r(U)),f(/FROM/i).skip(h).then(r(M)),r(F),r(f(/WHERE/i).skip(h).then(r(w)),null),r(f(/\s?GROUP BY/i).skip(h).then(r(z))),r(f(/\s?ORDER BY/i).skip(h).then(r(C))),r(f(/\s?LIMIT/i).skip(h).then(r(L)),null)).map(function(n){return{type:"select",select:n[0],from:n[1],join:n[2],where:n[3],group:n[4],order:n[5],limit:n[6]}}),G=c(f(/INSERT INTO/i).skip(h).then(I),h,r(c(m("("),P,m(")")).map(function(n){return n[1]})),h,f(/VALUES\s?\(/i).skip(h).then(H),m(")")).map(function(n){for(var e=[],t=Math.max(n[2].length,n[4].length),r=0;t>r;++r)e[r]={target:n[2][r]||null,value:n[4][r]||null};return{type:"insert",into:n[0],values:e}}),J=c(f(/UPDATE/i).skip(h).then(I),h,f(/SET/i).skip(h).then(W),h,r(f(/WHERE/i).skip(h).then(r(w)),null)).map(function(n){return{type:"update",table:n[0],values:n[2],where:n[4]}}),Y=c(f(/DELETE FROM/i).skip(h).then(r(M)),r(f(/WHERE/i).skip(h).then(r(w)),null)).map(function(n){return{type:"delete",from:n[0],where:n[1]}}),V=p(B,G,J,Y);e.exports=function(n){var e=V.parse(n);return e.status===!1&&(e.error=l.formatError(n,e)),e}},{parsimmon:3}]},{},[1])(1)}); \ No newline at end of file +!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.simpleSqlParser=n()}}(function(){return function n(e,t,r){function i(s,o){if(!t[s]){if(!e[s]){var a="function"==typeof require&&require;if(!o&&a)return a(s,!0);if(u)return u(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var c=t[s]={exports:{}};e[s][0].call(c.exports,function(n){var t=e[s][1][n];return i(t?t:n)},c,c.exports,n,e,t,r)}return t[s].exports}for(var u="function"==typeof require&&require,s=0;se.furthest)return n;var t=n.furthest===e.furthest?n.expected.concat(e.expected):e.expected;return{status:n.status,index:n.index,value:n.value,furthest:e.furthest,expected:t}}function u(e){if(!(e instanceof n))throw new Error("not a parser: "+e)}function s(n){return 1===n.length?n[0]:"one of "+n.join(", ")}function o(n,e){var t=e.index;if(t===n.length)return", got the end of the stream";var r=t>0?"'...":"'",i=n.length-t>12?"...'":"'";return" at character "+t+", got "+r+n.slice(t,t+12)+i}var a=n.prototype;r.formatError=function(n,e){return"expected "+s(e.expected)+o(n,e)};a.parse=function(n){var e=this.skip(d)._(n,0);return e.status?{status:!0,value:e.value}:{status:!1,index:e.furthest,expected:e.expected}};var l=r.seq=function(){var t=[].slice.call(arguments),r=t.length;return n(function(n,u){for(var s,o=new Array(r),a=0;r>a;a+=1){if(s=i(t[a]._(n,u),s),!s.status)return s;o[a]=s.value,u=s.index}return i(e(u,o),s)})},c=r.seqMap=function(){var n=[].slice.call(arguments),e=n.pop();return l.apply(null,n).map(function(n){return e.apply(null,n)})},p=(r.custom=function(r){return n(r(e,t))},r.alt=function(){var e=[].slice.call(arguments),t=e.length;return 0===t?h("zero alternates"):n(function(n,t){for(var r,u=0;uc;c+=1){if(o=u._(n,s),a=i(o,a),!o.status)return a;s=o.index,l.push(o.value)}for(;r>c&&(o=u._(n,s),a=i(o,a),o.status);c+=1)s=o.index,l.push(o.value);return i(e(s,l),a)})},a.result=function(n){return this.map(function(e){return n})},a.atMost=function(n){return this.times(0,n)},a.atLeast=function(n){return c(this.times(n),this.many(),function(n,e){return n.concat(e)})},a.map=function(t){var r=this;return n(function(n,u){var s=r._(n,u);return s.status?i(e(s.index,t(s.value)),s):s})},a.skip=function(n){return l(this,n).map(function(n){return n[0]})},a.mark=function(){return c(v,this,v,function(n,e,t){return{start:n,value:e,end:t}})},a.desc=function(e){var t=this;return n(function(n,r){var i=t._(n,r);return i.status||(i.expected=[e]),i})};var f=(r.string=function(r){var i=r.length,u="'"+r+"'";return n(function(n,s){var o=n.slice(s,s+i);return o===r?e(s+i,o):t(s,u)})},r.regex=function(r,i){var u=RegExp("^(?:"+r.source+")",(""+r).slice((""+r).lastIndexOf("/")+1)),s=""+r;return null==i&&(i=0),n(function(n,r){var o=u.exec(n.slice(r));if(o){var a=o[0],l=o[i];if(null!=l)return e(r+a.length,l)}return t(r,s)})}),m=r.succeed=function(t){return n(function(n,r){return e(r,t)})},h=r.fail=function(e){return n(function(n,r){return t(r,e)})},d=(r.letter=f(/[a-z]/i).desc("a letter"),r.letters=f(/[a-z]*/i),r.digit=f(/[0-9]/).desc("a digit"),r.digits=f(/[0-9]*/),r.whitespace=f(/\s+/).desc("whitespace"),r.optWhitespace=f(/\s*/),r.any=n(function(n,r){return r>=n.length?t(r,"any character"):e(r+1,n.charAt(r))}),r.all=n(function(n,t){return e(n.length,n.slice(t))}),r.eof=n(function(n,r){return r=0})},r.noneOf=function(n){return x(function(e){return n.indexOf(e)<0})},r.takeWhile=function(t){return n(function(n,r){for(var i=r;i0&&(e+="GROUP BY ",e+=n.group.map(function(n){return n.expression}).join(", ")),e}function s(n){var e="";return n.order.length>0&&(e+="ORDER BY ",e+=n.order.map(function(n){return n.expression}).join(", ")),e}function o(n){var e="";return null!==n.limit&&(e+="LIMIT ",null!==n.limit.from&&(e+=n.limit.from+", "),e+=n.limit.nb),e}function a(n){return"INSERT INTO "+n.into.expression}function l(n){var e="",t=n.values.filter(function(n){return null!==n.target});return t.length>0&&(e+="(",e+=t.map(function(n){return n.target.expression}).join(", "),e+=") "),e+="VALUES (",e+=n.values.map(function(n){return n.value}).join(", "),e+=")"}function c(n){return"UPDATE "+n.table.expression}function p(n){var e="SET ";return e+=n.values.map(function(n){return n.target.expression+" = "+n.value}).join(", ")}if("object"!=typeof n||n.status!==!0)return!1;var f=n.value,m=[];if("select"===f.type)m.push(e(f)),m.push(t(f)),m.push(r(f)),m.push(i(f)),m.push(u(f)),m.push(s(f)),m.push(o(f));else if("insert"===f.type)m.push(a(f)),m.push(l(f));else if("update"===f.type)m.push(c(f)),m.push(p(f)),m.push(i(f));else{if("delete"!==f.type)return!1;m.push("DELETE"),m.push(t(f)),m.push(i(f))}return m.filter(function(n){return""!==n}).join(" ")}},{}],6:[function(n,e,t){"use strict";function r(n,e){return"undefined"==typeof e?n.or(l.succeed([])):n.or(l.succeed(e))}function i(n){return n.join("")}function u(n){return n[0].push(n[1]),n[0]}function s(n){return c(n.skip(h).skip(m(",")).skip(h).many(),n.skip(h)).map(u)}function o(n){return n.replace(/^([`'"])(.*)\1$/,"$2")}function a(n){return c(l.index,n,l.index).map(function(n){var e={start:n[0],end:n[2]};if("object"==typeof n[1]){var t=n[1];return t.position=e,t}return e.out=n[1],e})}var l=n("parsimmon"),c=l.seq,p=l.alt,f=l.regex,m=l.string,h=l.optWhitespace,d=l.whitespace,x=l.lazy,v=p(f(/(?!(FROM|WHERE|GROUP BY|ORDER BY|LIMIT|INNER|LEFT|RIGHT|JOIN|ON|VALUES|SET)\s)[a-z*][a-z0-9_]*/i),f(/`[^`\\]*(?:\\.[^`\\]*)*`/)),g=p(f(/"[^"\\]*(?:\\.[^"\\]*)*"/),f(/'[^'\\]*(?:\\.[^'\\]*)*'/)),j=c(p(f(/[a-zA-Z0-9_]+\(/),m("(")),r(x(function(){return U})).map(i),m(")")).map(i),E=c(v,m("."),v),y=p(m("+"),m("-"),m("*"),m("/"),m("&&"),m("&"),m("~"),m("||"),m("|"),m("^"),f(/XOR/i),m("<=>"),m("="),m("!="),m(">="),m(">>"),m(">"),m("<="),m("<<"),m("<"),f(/IS NULL/i),f(/IS NOT/i),f(/IS NOT NULL/i),f(/IS/i),f(/LIKE/i),f(/NOT LIKE/i),m("%"),f(/MOD/i),f(/NOT/i),f(/OR\s/i),f(/AND/i),f(/IN/i)),b=f(/[-]?\d+\.?\d*/),O=c(b,p(m("u"),m("s"),m("m"),m("h"),m("d"),m("w"))),_=c(m("("),h,c(p(b,g),h,r(m(",")),h,r(p(b,g))).map(i),h,m(")")).map(i),k=c(p(E.map(function(n){return{expression:n.join(""),table:o(n[0]),column:o(n[2])}}),j.map(function(n){return{expression:n,table:null,column:null}}),v.map(function(n){return{expression:n,table:null,column:o(n)}}),g.map(function(n){return{expression:n,table:null,column:null}}),O.map(function(n){return{expression:i(n),table:null,column:null}}),b.map(function(n){return{expression:n,table:null,column:null}}),_.map(function(n){return{expression:n,table:null,column:null}})),r(c(h,y,r(c(h,x(function(){return k}).map(function(n){return n.expression})).map(i),null)).map(i),null)).map(function(n){return null!==n[1]?(n[0]=n[0].expression,{expression:n.join(""),table:null,column:null}):n[0]}),R=c(k,r(c(h,r(f(/AS\s/i)),p(v,g)).map(function(n){var e={};return e.alias=o(n[2]),e.expression=n.join(""),e}),null)).map(function(n){var e=n[0];return e.alias=null!==n[1]?n[1].alias:null,e.expression+=null!==n[1]?n[1].expression:"",e}),I=k.map(function(n){return n.expression}),w=c(p(E.map(i),v,f(/"([^"\\]*(?:\\.[^"\\]*)*)"/,1).map(function(n){return{table:n,expression:'"'+n+'"'}})),r(c(h,r(f(/AS\s/i)),p(v,g)).map(function(n){return{alias:o(n[2]),expression:n.join("")}}),null)).map(function(n){var e={};return"object"==typeof n[0]?(e.table=n[0].table,e.expression=n[0].expression):(e.table=n[0],e.expression=n[0]+(null!==n[1]?n[1].expression:"")),e.alias=null!==n[1]?n[1].alias:null,e}),N=c(r(c(f(/INNER|LEFT|RIGHT/i),d).map(function(n){return n[0].toLowerCase()}),null),f(/JOIN/i),h,a(w),h,f(/ON/i),h,a(k)).map(function(n){var e={};return e.type=n[0]||"inner",e.table=n[3].table,e.alias=n[3].alias,e.position=n[3].position,e.condition={expression:n[7].expression,position:n[7].position},e}),T=a(k).map(function(n){return{expression:n.expression,position:n.position}}),L=c(k,r(c(h,f(/ASC|DESC/i)),null)).map(function(n){return{expression:n[0].expression+(null!==n[1]?n[1].join(""):""),order:null!==n[1]?n[1][1]:"ASC",table:n[0].table,column:n[0].column}}),S=c(b,r(c(h,m(","),h,b),null)).map(function(n){return null===n[1]?{from:null,nb:parseInt(n[0],10)}:{from:parseInt(n[0],10),nb:parseInt(n[1][3],10)}}),D=p(E.map(function(n){return{expression:n.join(""),column:o(n[2])}}),v.map(function(n){return{expression:n,column:o(n)}})),A=k.map(function(n){return n.expression}),q=c(D,h,m("="),h,k).map(function(n){return{target:n[0],value:n[4].expression}}),U=c(c(I,h,m(","),h).map(i).many(),I.skip(h)).map(u),M=s(a(R)),z=s(a(w)),C=s(a(k)),F=s(a(L)),P=h.then(N).skip(h).many(),H=s(D),W=s(A),B=s(q),G=c(f(/SELECT/i).skip(h).then(r(M)),f(/FROM/i).skip(h).then(r(z)),r(P),r(f(/WHERE/i).skip(h).then(r(T)),null),r(f(/\s?GROUP BY/i).skip(h).then(r(C))),r(f(/\s?ORDER BY/i).skip(h).then(r(F))),r(f(/\s?LIMIT/i).skip(h).then(r(S)),null)).map(function(n){return{type:"select",select:n[0],from:n[1],join:n[2],where:n[3],group:n[4],order:n[5],limit:n[6]}}),J=c(f(/INSERT INTO/i).skip(h).then(w),h,r(c(m("("),H,m(")")).map(function(n){return n[1]})),h,f(/VALUES\s?\(/i).skip(h).then(W),m(")")).map(function(n){for(var e=[],t=Math.max(n[2].length,n[4].length),r=0;t>r;++r)e[r]={target:n[2][r]||null,value:n[4][r]||null};return{type:"insert",into:n[0],values:e}}),Y=c(f(/UPDATE/i).skip(h).then(w),h,f(/SET/i).skip(h).then(B),h,r(f(/WHERE/i).skip(h).then(r(T)),null)).map(function(n){return{type:"update",table:n[0],values:n[2],where:n[4]}}),V=c(f(/DELETE FROM/i).skip(h).then(r(z)),r(f(/WHERE/i).skip(h).then(r(T)),null)).map(function(n){return{type:"delete",from:n[0],where:n[1]}}),K=p(G,J,Y,V);e.exports=function(n){var e=K.parse(n);return e.status===!1&&(e.error=l.formatError(n,e)),e}},{parsimmon:3}]},{},[1])(1)}); \ No newline at end of file diff --git a/dist/simpleSqlParser.withoutDeps.js b/dist/simpleSqlParser.withoutDeps.js index e4c0e94..dfde4c4 100644 --- a/dist/simpleSqlParser.withoutDeps.js +++ b/dist/simpleSqlParser.withoutDeps.js @@ -304,6 +304,19 @@ var operator = alt( // A number var number = regex(/[-]?\d+\.?\d*/); +// A duration +var duration = seq( + number, + alt( + string('u'), + string('s'), + string('m'), + string('h'), + string('d'), + string('w') + ) +); + /******************************************************************************************** @@ -364,6 +377,13 @@ var expression = seq( column: null, }; }), + duration.map(function(node) { + return { + expression: mkString(node), + table: null, + column: null, + }; + }), number.map(function(node) { return { expression: node, diff --git a/dist/simpleSqlParser.withoutDeps.min.js b/dist/simpleSqlParser.withoutDeps.min.js index f3a46fe..460e071 100644 --- a/dist/simpleSqlParser.withoutDeps.min.js +++ b/dist/simpleSqlParser.withoutDeps.min.js @@ -1 +1 @@ -!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.simpleSqlParser=n()}}(function(){return function n(e,t,r){function i(o,s){if(!t[o]){if(!e[o]){var l="function"==typeof require&&require;if(!s&&l)return l(o,!0);if(u)return u(o,!0);var p=new Error("Cannot find module '"+o+"'");throw p.code="MODULE_NOT_FOUND",p}var a=t[o]={exports:{}};e[o][0].call(a.exports,function(n){var t=e[o][1][n];return i(t?t:n)},a,a.exports,n,e,t,r)}return t[o].exports}for(var u="function"==typeof require&&require,o=0;o0&&(e+="GROUP BY ",e+=n.group.map(function(n){return n.expression}).join(", ")),e}function o(n){var e="";return n.order.length>0&&(e+="ORDER BY ",e+=n.order.map(function(n){return n.expression}).join(", ")),e}function s(n){var e="";return null!==n.limit&&(e+="LIMIT ",null!==n.limit.from&&(e+=n.limit.from+", "),e+=n.limit.nb),e}function l(n){return"INSERT INTO "+n.into.expression}function p(n){var e="",t=n.values.filter(function(n){return null!==n.target});return t.length>0&&(e+="(",e+=t.map(function(n){return n.target.expression}).join(", "),e+=") "),e+="VALUES (",e+=n.values.map(function(n){return n.value}).join(", "),e+=")"}function a(n){return"UPDATE "+n.table.expression}function f(n){var e="SET ";return e+=n.values.map(function(n){return n.target.expression+" = "+n.value}).join(", ")}if("object"!=typeof n||n.status!==!0)return!1;var c=n.value,m=[];if("select"===c.type)m.push(e(c)),m.push(t(c)),m.push(r(c)),m.push(i(c)),m.push(u(c)),m.push(o(c)),m.push(s(c));else if("insert"===c.type)m.push(l(c)),m.push(p(c));else if("update"===c.type)m.push(a(c)),m.push(f(c)),m.push(i(c));else{if("delete"!==c.type)return!1;m.push("DELETE"),m.push(t(c)),m.push(i(c))}return m.filter(function(n){return""!==n}).join(" ")}},{}],3:[function(n,e,t){(function(n){"use strict";function t(n,e){return"undefined"==typeof e?n.or(l.succeed([])):n.or(l.succeed(e))}function r(n){return n.join("")}function i(n){return n[0].push(n[1]),n[0]}function u(n){return p(n.skip(m).skip(c(",")).skip(m).many(),n.skip(m)).map(i)}function o(n){return n.replace(/^([`'"])(.*)\1$/,"$2")}function s(n){return p(l.index,n,l.index).map(function(n){var e={start:n[0],end:n[2]};if("object"==typeof n[1]){var t=n[1];return t.position=e,t}return e.out=n[1],e})}var l="undefined"!=typeof window?window.parsimmon:"undefined"!=typeof n?n.parsimmon:null,p=l.seq,a=l.alt,f=l.regex,c=l.string,m=l.optWhitespace,x=l.whitespace,d=l.lazy,h=a(f(/(?!(FROM|WHERE|GROUP BY|ORDER BY|LIMIT|INNER|LEFT|RIGHT|JOIN|ON|VALUES|SET)\s)[a-z*][a-z0-9_]*/i),f(/`[^`\\]*(?:\\.[^`\\]*)*`/)),E=a(f(/"[^"\\]*(?:\\.[^"\\]*)*"/),f(/'[^'\\]*(?:\\.[^'\\]*)*'/)),v=p(a(f(/[a-zA-Z0-9_]+\(/),c("(")),t(d(function(){return D})).map(r),c(")")).map(r),y=p(h,c("."),h),I=a(c("+"),c("-"),c("*"),c("/"),c("&&"),c("&"),c("~"),c("||"),c("|"),c("^"),f(/XOR/i),c("<=>"),c("="),c("!="),c(">="),c(">>"),c(">"),c("<="),c("<<"),c("<"),f(/IS NULL/i),f(/IS NOT/i),f(/IS NOT NULL/i),f(/IS/i),f(/LIKE/i),f(/NOT LIKE/i),c("%"),f(/MOD/i),f(/NOT/i),f(/OR\s/i),f(/AND/i),f(/IN/i)),O=f(/[-]?\d+\.?\d*/),b=p(c("("),m,p(a(O,E),m,t(c(",")),m,t(a(O,E))).map(r),m,c(")")).map(r),N=p(a(y.map(function(n){return{expression:n.join(""),table:o(n[0]),column:o(n[2])}}),v.map(function(n){return{expression:n,table:null,column:null}}),h.map(function(n){return{expression:n,table:null,column:o(n)}}),E.map(function(n){return{expression:n,table:null,column:null}}),O.map(function(n){return{expression:n,table:null,column:null}}),b.map(function(n){return{expression:n,table:null,column:null}})),t(p(m,I,t(p(m,d(function(){return N}).map(function(n){return n.expression})).map(r),null)).map(r),null)).map(function(n){return null!==n[1]?(n[0]=n[0].expression,{expression:n.join(""),table:null,column:null}):n[0]}),R=p(N,t(p(m,t(f(/AS\s/i)),a(h,E)).map(function(n){var e={};return e.alias=o(n[2]),e.expression=n.join(""),e}),null)).map(function(n){var e=n[0];return e.alias=null!==n[1]?n[1].alias:null,e.expression+=null!==n[1]?n[1].expression:"",e}),T=N.map(function(n){return n.expression}),j=p(a(y.map(r),h,f(/"([^"\\]*(?:\\.[^"\\]*)*)"/,1).map(function(n){return{table:n,expression:'"'+n+'"'}})),t(p(m,t(f(/AS\s/i)),a(h,E)).map(function(n){return{alias:o(n[2]),expression:n.join("")}}),null)).map(function(n){var e={};return"object"==typeof n[0]?(e.table=n[0].table,e.expression=n[0].expression):(e.table=n[0],e.expression=n[0]+(null!==n[1]?n[1].expression:"")),e.alias=null!==n[1]?n[1].alias:null,e}),g=p(t(p(f(/INNER|LEFT|RIGHT/i),x).map(function(n){return n[0].toLowerCase()}),null),f(/JOIN/i),m,s(j),m,f(/ON/i),m,s(N)).map(function(n){var e={};return e.type=n[0]||"inner",e.table=n[3].table,e.alias=n[3].alias,e.position=n[3].position,e.condition={expression:n[7].expression,position:n[7].position},e}),w=s(N).map(function(n){return{expression:n.expression,position:n.position}}),L=p(N,t(p(m,f(/ASC|DESC/i)),null)).map(function(n){return{expression:n[0].expression+(null!==n[1]?n[1].join(""):""),order:null!==n[1]?n[1][1]:"ASC",table:n[0].table,column:n[0].column}}),S=p(O,t(p(m,c(","),m,O),null)).map(function(n){return null===n[1]?{from:null,nb:parseInt(n[0],10)}:{from:parseInt(n[0],10),nb:parseInt(n[1][3],10)}}),k=a(y.map(function(n){return{expression:n.join(""),column:o(n[2])}}),h.map(function(n){return{expression:n,column:o(n)}})),q=N.map(function(n){return n.expression}),A=p(k,m,c("="),m,N).map(function(n){return{target:n[0],value:n[4].expression}}),D=p(p(T,m,c(","),m).map(r).many(),T.skip(m)).map(i),U=u(s(R)),M=u(s(j)),F=u(s(N)),H=u(s(L)),C=m.then(g).skip(m).many(),B=u(k),G=u(q),P=u(A),W=p(f(/SELECT/i).skip(m).then(t(U)),f(/FROM/i).skip(m).then(t(M)),t(C),t(f(/WHERE/i).skip(m).then(t(w)),null),t(f(/\s?GROUP BY/i).skip(m).then(t(F))),t(f(/\s?ORDER BY/i).skip(m).then(t(H))),t(f(/\s?LIMIT/i).skip(m).then(t(S)),null)).map(function(n){return{type:"select",select:n[0],from:n[1],join:n[2],where:n[3],group:n[4],order:n[5],limit:n[6]}}),Y=p(f(/INSERT INTO/i).skip(m).then(j),m,t(p(c("("),B,c(")")).map(function(n){return n[1]})),m,f(/VALUES\s?\(/i).skip(m).then(G),c(")")).map(function(n){for(var e=[],t=Math.max(n[2].length,n[4].length),r=0;t>r;++r)e[r]={target:n[2][r]||null,value:n[4][r]||null};return{type:"insert",into:n[0],values:e}}),J=p(f(/UPDATE/i).skip(m).then(j),m,f(/SET/i).skip(m).then(P),m,t(f(/WHERE/i).skip(m).then(t(w)),null)).map(function(n){return{type:"update",table:n[0],values:n[2],where:n[4]}}),z=p(f(/DELETE FROM/i).skip(m).then(t(M)),t(f(/WHERE/i).skip(m).then(t(w)),null)).map(function(n){return{type:"delete",from:n[0],where:n[1]}}),_=a(W,Y,J,z);e.exports=function(n){var e=_.parse(n);return e.status===!1&&(e.error=l.formatError(n,e)),e}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1])(1)}); \ No newline at end of file +!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.simpleSqlParser=n()}}(function(){return function n(e,t,r){function i(o,s){if(!t[o]){if(!e[o]){var l="function"==typeof require&&require;if(!s&&l)return l(o,!0);if(u)return u(o,!0);var p=new Error("Cannot find module '"+o+"'");throw p.code="MODULE_NOT_FOUND",p}var a=t[o]={exports:{}};e[o][0].call(a.exports,function(n){var t=e[o][1][n];return i(t?t:n)},a,a.exports,n,e,t,r)}return t[o].exports}for(var u="function"==typeof require&&require,o=0;o0&&(e+="GROUP BY ",e+=n.group.map(function(n){return n.expression}).join(", ")),e}function o(n){var e="";return n.order.length>0&&(e+="ORDER BY ",e+=n.order.map(function(n){return n.expression}).join(", ")),e}function s(n){var e="";return null!==n.limit&&(e+="LIMIT ",null!==n.limit.from&&(e+=n.limit.from+", "),e+=n.limit.nb),e}function l(n){return"INSERT INTO "+n.into.expression}function p(n){var e="",t=n.values.filter(function(n){return null!==n.target});return t.length>0&&(e+="(",e+=t.map(function(n){return n.target.expression}).join(", "),e+=") "),e+="VALUES (",e+=n.values.map(function(n){return n.value}).join(", "),e+=")"}function a(n){return"UPDATE "+n.table.expression}function f(n){var e="SET ";return e+=n.values.map(function(n){return n.target.expression+" = "+n.value}).join(", ")}if("object"!=typeof n||n.status!==!0)return!1;var c=n.value,m=[];if("select"===c.type)m.push(e(c)),m.push(t(c)),m.push(r(c)),m.push(i(c)),m.push(u(c)),m.push(o(c)),m.push(s(c));else if("insert"===c.type)m.push(l(c)),m.push(p(c));else if("update"===c.type)m.push(a(c)),m.push(f(c)),m.push(i(c));else{if("delete"!==c.type)return!1;m.push("DELETE"),m.push(t(c)),m.push(i(c))}return m.filter(function(n){return""!==n}).join(" ")}},{}],3:[function(n,e,t){(function(n){"use strict";function t(n,e){return"undefined"==typeof e?n.or(l.succeed([])):n.or(l.succeed(e))}function r(n){return n.join("")}function i(n){return n[0].push(n[1]),n[0]}function u(n){return p(n.skip(m).skip(c(",")).skip(m).many(),n.skip(m)).map(i)}function o(n){return n.replace(/^([`'"])(.*)\1$/,"$2")}function s(n){return p(l.index,n,l.index).map(function(n){var e={start:n[0],end:n[2]};if("object"==typeof n[1]){var t=n[1];return t.position=e,t}return e.out=n[1],e})}var l="undefined"!=typeof window?window.parsimmon:"undefined"!=typeof n?n.parsimmon:null,p=l.seq,a=l.alt,f=l.regex,c=l.string,m=l.optWhitespace,x=l.whitespace,d=l.lazy,h=a(f(/(?!(FROM|WHERE|GROUP BY|ORDER BY|LIMIT|INNER|LEFT|RIGHT|JOIN|ON|VALUES|SET)\s)[a-z*][a-z0-9_]*/i),f(/`[^`\\]*(?:\\.[^`\\]*)*`/)),E=a(f(/"[^"\\]*(?:\\.[^"\\]*)*"/),f(/'[^'\\]*(?:\\.[^'\\]*)*'/)),v=p(a(f(/[a-zA-Z0-9_]+\(/),c("(")),t(d(function(){return U})).map(r),c(")")).map(r),y=p(h,c("."),h),I=a(c("+"),c("-"),c("*"),c("/"),c("&&"),c("&"),c("~"),c("||"),c("|"),c("^"),f(/XOR/i),c("<=>"),c("="),c("!="),c(">="),c(">>"),c(">"),c("<="),c("<<"),c("<"),f(/IS NULL/i),f(/IS NOT/i),f(/IS NOT NULL/i),f(/IS/i),f(/LIKE/i),f(/NOT LIKE/i),c("%"),f(/MOD/i),f(/NOT/i),f(/OR\s/i),f(/AND/i),f(/IN/i)),b=f(/[-]?\d+\.?\d*/),O=p(b,a(c("u"),c("s"),c("m"),c("h"),c("d"),c("w"))),N=p(c("("),m,p(a(b,E),m,t(c(",")),m,t(a(b,E))).map(r),m,c(")")).map(r),R=p(a(y.map(function(n){return{expression:n.join(""),table:o(n[0]),column:o(n[2])}}),v.map(function(n){return{expression:n,table:null,column:null}}),h.map(function(n){return{expression:n,table:null,column:o(n)}}),E.map(function(n){return{expression:n,table:null,column:null}}),O.map(function(n){return{expression:r(n),table:null,column:null}}),b.map(function(n){return{expression:n,table:null,column:null}}),N.map(function(n){return{expression:n,table:null,column:null}})),t(p(m,I,t(p(m,d(function(){return R}).map(function(n){return n.expression})).map(r),null)).map(r),null)).map(function(n){return null!==n[1]?(n[0]=n[0].expression,{expression:n.join(""),table:null,column:null}):n[0]}),T=p(R,t(p(m,t(f(/AS\s/i)),a(h,E)).map(function(n){var e={};return e.alias=o(n[2]),e.expression=n.join(""),e}),null)).map(function(n){var e=n[0];return e.alias=null!==n[1]?n[1].alias:null,e.expression+=null!==n[1]?n[1].expression:"",e}),j=R.map(function(n){return n.expression}),w=p(a(y.map(r),h,f(/"([^"\\]*(?:\\.[^"\\]*)*)"/,1).map(function(n){return{table:n,expression:'"'+n+'"'}})),t(p(m,t(f(/AS\s/i)),a(h,E)).map(function(n){return{alias:o(n[2]),expression:n.join("")}}),null)).map(function(n){var e={};return"object"==typeof n[0]?(e.table=n[0].table,e.expression=n[0].expression):(e.table=n[0],e.expression=n[0]+(null!==n[1]?n[1].expression:"")),e.alias=null!==n[1]?n[1].alias:null,e}),g=p(t(p(f(/INNER|LEFT|RIGHT/i),x).map(function(n){return n[0].toLowerCase()}),null),f(/JOIN/i),m,s(w),m,f(/ON/i),m,s(R)).map(function(n){var e={};return e.type=n[0]||"inner",e.table=n[3].table,e.alias=n[3].alias,e.position=n[3].position,e.condition={expression:n[7].expression,position:n[7].position},e}),L=s(R).map(function(n){return{expression:n.expression,position:n.position}}),S=p(R,t(p(m,f(/ASC|DESC/i)),null)).map(function(n){return{expression:n[0].expression+(null!==n[1]?n[1].join(""):""),order:null!==n[1]?n[1][1]:"ASC",table:n[0].table,column:n[0].column}}),k=p(b,t(p(m,c(","),m,b),null)).map(function(n){return null===n[1]?{from:null,nb:parseInt(n[0],10)}:{from:parseInt(n[0],10),nb:parseInt(n[1][3],10)}}),q=a(y.map(function(n){return{expression:n.join(""),column:o(n[2])}}),h.map(function(n){return{expression:n,column:o(n)}})),A=R.map(function(n){return n.expression}),D=p(q,m,c("="),m,R).map(function(n){return{target:n[0],value:n[4].expression}}),U=p(p(j,m,c(","),m).map(r).many(),j.skip(m)).map(i),M=u(s(T)),F=u(s(w)),H=u(s(R)),C=u(s(S)),B=m.then(g).skip(m).many(),G=u(q),P=u(A),W=u(D),Y=p(f(/SELECT/i).skip(m).then(t(M)),f(/FROM/i).skip(m).then(t(F)),t(B),t(f(/WHERE/i).skip(m).then(t(L)),null),t(f(/\s?GROUP BY/i).skip(m).then(t(H))),t(f(/\s?ORDER BY/i).skip(m).then(t(C))),t(f(/\s?LIMIT/i).skip(m).then(t(k)),null)).map(function(n){return{type:"select",select:n[0],from:n[1],join:n[2],where:n[3],group:n[4],order:n[5],limit:n[6]}}),J=p(f(/INSERT INTO/i).skip(m).then(w),m,t(p(c("("),G,c(")")).map(function(n){return n[1]})),m,f(/VALUES\s?\(/i).skip(m).then(P),c(")")).map(function(n){for(var e=[],t=Math.max(n[2].length,n[4].length),r=0;t>r;++r)e[r]={target:n[2][r]||null,value:n[4][r]||null};return{type:"insert",into:n[0],values:e}}),z=p(f(/UPDATE/i).skip(m).then(w),m,f(/SET/i).skip(m).then(W),m,t(f(/WHERE/i).skip(m).then(t(L)),null)).map(function(n){return{type:"update",table:n[0],values:n[2],where:n[4]}}),_=p(f(/DELETE FROM/i).skip(m).then(t(F)),t(f(/WHERE/i).skip(m).then(t(L)),null)).map(function(n){return{type:"delete",from:n[0],where:n[1]}}),V=a(Y,J,z,_);e.exports=function(n){var e=V.parse(n);return e.status===!1&&(e.error=l.formatError(n,e)),e}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1])(1)}); \ No newline at end of file diff --git a/src/sql2ast.js b/src/sql2ast.js index 2b6ac85..da4f992 100644 --- a/src/sql2ast.js +++ b/src/sql2ast.js @@ -151,6 +151,19 @@ var operator = alt( // A number var number = regex(/[-]?\d+\.?\d*/); +// A duration +var duration = seq( + number, + alt( + string('u'), + string('s'), + string('m'), + string('h'), + string('d'), + string('w') + ) +); + /******************************************************************************************** @@ -211,6 +224,13 @@ var expression = seq( column: null, }; }), + duration.map(function(node) { + return { + expression: mkString(node), + table: null, + column: null, + }; + }), number.map(function(node) { return { expression: node, diff --git a/tests/tests-select.js b/tests/tests-select.js index e44ff61..023ba7c 100644 --- a/tests/tests-select.js +++ b/tests/tests-select.js @@ -362,6 +362,27 @@ var Select = [ limit: { from: null, nb: 1 }, }, }, + { + c: 'Durations', + q: 'SELECT * FROM table WHERE this >= 15m', + a: { + type: 'select', + select: [ + { expression: '*', column: '*', table: null, alias: null, position: { start: 7, end: 8 } }, + ], + from: [ + { expression: 'table', table: 'table', alias: null, position: { start: 14, end: 19 } }, + ], + join: [], + where: { + expression: 'this >= 15m', + position: { start: 26, end: 37 }, + }, + group: [], + order: [], + limit: null, + }, + }, { c: 'Group by', q: 'SELECT * FROM table GROUP BY col1, MONTH(col2), table.col3',