AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
6.97k stars 649 forks source link

Implement LET clause (like in OrientDB) #210

Open agershun opened 9 years ago

agershun commented 9 years ago

Like:

SELECT FROM Profile
LET $city = address.city
WHERE $city.name like '%Saint%"' and
    ( $city.country.name = 'Italy' or $city.country.name = 'France' )

Source

This cluse will be calculated right before wherefn() and selectfn() ( or groupfn()).

agershun commented 9 years ago

One more crazy idea to extend SQL: I am thinking about two different operators: LET and BEGIN (like in the question from StackOverflow).

LET will work before WHERE (wherefn()) and BEGIN will work right before or right after SELECT(selectfn()/groupfn()) like:

    SET @c = 0;
    SELECT a, @b, @c 
        FROM one 
        LET @b = a*a          // internal variable only for current line
        WHERE @b > 10 
        BEGIN SET @c = @c + a END;  // own accumulator for selected lines

or call external procedure for each selected line:

    alasql.fn.myfn = function (r) {
          alasql.vars.b = r.default.a*r.default.a
    };
    alasql('SELECT a, @b FROM one LET myfn() WHERE @b > 10 ');