GithubHelp home page GithubHelp logo

alasql / alasql Goto Github PK

View Code? Open in Web Editor NEW
7.0K 7.0K 649.0 93 MB

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.

Home Page: http://alasql.org

License: MIT License

JavaScript 94.29% HTML 1.38% CSS 0.26% CoffeeScript 0.51% Yacc 3.23% TSQL 0.18% Shell 0.14%

alasql's Introduction

CI-test NPM downloads OPEN open source software Release Average time to resolve an issue Coverage OpenSSF Scorecard OpenSSF Best Practices Stars

AlaSQL

AlaSQL logo

AlaSQL - ( à la SQL ) [ælæ ɛskju:ɛl] - is an open source SQL database for JavaScript with a strong focus on query speed and data source flexibility for both relational data and schemaless data. It works in the web browser, Node.js, and mobile apps.

This library is perfect for:

  • Fast in-memory SQL data processing for BI and ERP applications on fat clients
  • Easy ETL and options for persistence by data import / manipulation / export of several formats
  • All major browsers, Node.js, and mobile applications

We focus on speed by taking advantage of the dynamic nature of JavaScript when building up queries. Real-world solutions demand flexibility regarding where data comes from and where it is to be stored. We focus on flexibility by making sure you can import/export and query directly on data stored in Excel (both .xls and .xlsx), CSV, JSON, TAB, IndexedDB, LocalStorage, and SQLite files.

The library adds the comfort of a full database engine to your JavaScript app. No, really - it's working towards a full database engine complying with most of the SQL-99 language, spiced up with additional syntax for NoSQL (schema-less) data and graph networks.

Traditional SQL Table

/* create SQL Table and add data */
alasql("CREATE TABLE cities (city string, pop number)");

alasql("INSERT INTO cities VALUES ('Paris',2249975),('Berlin',3517424),('Madrid',3041579)");

/* execute query */
var res = alasql("SELECT * FROM cities WHERE pop < 3500000 ORDER BY pop DESC");

// res = [ { "city": "Madrid", "pop": 3041579 }, { "city": "Paris", "pop": 2249975 } ]

Live Demo

Array of Objects

var data = [ {a: 1, b: 10}, {a: 2, b: 20}, {a: 1, b: 30} ];

var res = alasql('SELECT a, SUM(b) AS b FROM ? GROUP BY a',[data]);

// res = [ { "a": 1, "b": 40},{ "a": 2, "b": 20 } ]

Live Demo

Spreadsheet

// file is read asynchronously (Promise returned when SQL given as array)
alasql(['SELECT * FROM XLS("./data/mydata") WHERE lastname LIKE "A%" and city = "London" GROUP BY name '])
    .then(function(res){
        console.log(res); // output depends on mydata.xls
    }).catch(function(err){
        console.log('Does the file exist? There was an error:', err);
    });

Bulk Data Load

alasql("CREATE TABLE example1 (a INT, b INT)");

// alasql's data store for a table can be assigned directly
alasql.tables.example1.data = [
    {a:2,b:6},
    {a:3,b:4}
];

// ... or manipulated with normal SQL
alasql("INSERT INTO example1 VALUES (1,5)");

var res = alasql("SELECT * FROM example1 ORDER BY b DESC");

console.log(res); // [{a:2,b:6},{a:1,b:5},{a:3,b:4}]

If you are familiar with SQL, it should be no surprise that proper use of indexes on your tables is essential for good performance.

Options

AlaSQL has several configuration options which change the behavior. It can be set via SQL statements or via the options object before using alasql.

If you're using NOW() in queries often, setting alasql.options.dateAsString to false speed things up. It will just return a JS Date object instead of a string representation of a date.

Installation

yarn add alasql                # yarn

npm install alasql             # npm

npm install -g alasql          # global install of command line tool

For the browsers: include alasql.min.js

<script src="https://cdn.jsdelivr.net/npm/alasql@4"></script>

Getting started

See the "Getting started" section of the wiki

More advanced topics are covered in other wiki sections like "Data manipulation" and in questions on Stack Overflow

Other links:

Please note

All contributions are extremely welcome and greatly appreciated(!) - The project has never received any funding and is based on unpaid voluntary work: We really (really) love pull requests

The AlaSQL project depends on your contribution of code and may have bugs. So please, submit any bugs and suggestions as an issue.

Please check out the limitations of the library.

Performance

AlaSQL is designed for speed and includes some of the classic SQL engine optimizations:

  • Queries are cached as compiled functions
  • Joined tables are pre-indexed
  • WHERE expressions are pre-filtered for joins

See more performance-related info on the wiki

Features you might like

Traditional SQL

Use "good old" SQL on your data with multiple levels of: JOIN, VIEW, GROUP BY, UNION, PRIMARY KEY, ANY, ALL, IN, ROLLUP(), CUBE(), GROUPING SETS(), CROSS APPLY, OUTER APPLY, WITH SELECT, and subqueries. The wiki lists supported SQL statements and keywords.

User-Defined Functions in your SQL

You can use all benefits of SQL and JavaScript together by defining your own custom functions. Just add new functions to the alasql.fn object:

alasql.fn.myfn = function(a,b) {
    return a*b+1;
};
var res = alasql('SELECT myfn(a,b) FROM one');

You can also define your own aggregator functions (like your own SUM(...)). See more in the wiki

Compiled statements and functions

var ins = alasql.compile('INSERT INTO one VALUES (?,?)');
ins(1,10);
ins(2,20);

See more in the wiki

SELECT against your JavaScript data

Group your JavaScript array of objects by field and count number of records in each group:

var data = [{a:1,b:1,c:1},{a:1,b:2,c:1},{a:1,b:3,c:1}, {a:2,b:1,c:1}];
var res = alasql('SELECT a, COUNT(*) AS b FROM ? GROUP BY a', [data] );

See more ideas for creative data manipulation in the wiki

JavaScript Sugar

AlaSQL extends "good old" SQL to make it closer to JavaScript. The "sugar" includes:

  • Write Json objects - {a:'1',b:@['1','2','3']}

  • Access object properties - obj->property->subproperty

  • Access object and arrays elements - obj->(a*1)

  • Access JavaScript functions - obj->valueOf()

  • Format query output with SELECT VALUE, ROW, COLUMN, MATRIX

  • ES5 multiline SQL with var SQL = function(){/*SELECT 'MY MULTILINE SQL'*/} and pass instead of SQL string (will not work if you compress your code)

Read and write Excel and raw data files

You can import from and export to CSV, TAB, TXT, and JSON files. File extensions can be omitted. Calls to files will always be asynchronous so multi-file queries should be chained:

var tabFile = 'mydata.tab';

alasql.promise([
    "SELECT * FROM txt('MyFile.log') WHERE [0] LIKE 'M%'", // parameter-less query
    [ "SELECT * FROM tab(?) ORDER BY [1]", [tabFile] ],    // [query, array of params]
    "SELECT [3] AS city,[4] AS population FROM csv('./data/cities')",
    "SELECT * FROM json('../config/myJsonfile')"
]).then(function(results){
    console.log(results);
}).catch(console.error);

Read SQLite database files

AlaSQL can read (but not write) SQLite data files using SQL.js library:

<script src="alasql.js"></script>
<script src="sql.js"></script>
<script>
    alasql([
        'ATTACH SQLITE DATABASE Chinook("Chinook_Sqlite.sqlite")',
        'USE Chinook',
        'SELECT * FROM Genre'
    ]).then(function(res){
        console.log("Genres:",res.pop());
    });
</script>

sql.js calls will always be asynchronous.

AlaSQL works in the console - CLI

The node module ships with an alasql command-line tool:

$ npm install -g alasql ## install the module globally

$ alasql -h ## shows usage information

$ alasql "SET @data = @[{a:'1',b:?},{a:'2',b:?}]; SELECT a, b FROM @data;" 10 20
[ 1, [ { a: 1, b: 10 }, { a: 2, b: 20 } ] ]

$ alasql "VALUE OF SELECT COUNT(*) AS abc FROM TXT('README.md') WHERE LENGTH([0]) > ?" 140
// Number of lines with more than 140 characters in README.md

More examples are included in the wiki

Features you might love

AlaSQL ♥ D3.js

AlaSQL plays nice with d3.js and gives you a convenient way to integrate a specific subset of your data with the visual powers of D3. See more about D3.js and AlaSQL in the wiki

AlaSQL ♥ Excel

AlaSQL can export data to both Excel 2003 (.xls) and Excel 2007 (.xlsx) formats with coloring of cells and other Excel formatting functions.

AlaSQL ♥ Meteor

Meteor is amazing. You can query directly on your Meteor collections with SQL - simple and easy. See more about Meteor and AlaSQL in the wiki

AlaSQL ♥ Angular.js

Angular is great. In addition to normal data manipulation, AlaSQL works like a charm for exporting your present scope to Excel. See more about Angular and AlaSQL in the wiki

AlaSQL ♥ Google Maps

Pinpointing data on a map should be easy. AlaSQL is great to prepare source data for Google Maps from, for example, Excel or CSV, making it one unit of work for fetching and identifying what's relevant. See more about Google Maps and AlaSQL in the wiki

AlaSQL ♥ Google Spreadsheets

AlaSQL can query data directly from a Google spreadsheet. A good "partnership" for easy editing and powerful data manipulation. See more about Google Spreadsheets and AlaSQL in the wiki

Miss a feature?

Take charge and add your idea or vote for your favorite feature to be implemented:

Feature Requests

Limitations

Please be aware that AlaSQL has bugs. Beside having some bugs, there are a number of limitations:

  1. AlaSQL has a (long) list of keywords that must be escaped if used for column names. When selecting a field named key please write SELECT `key` FROM ... instead. This is also the case for words like `value`, `read`, `count`, `by`, `top`, `path`, `deleted`, `work` and `offset`. Please consult the full list of keywords.

  2. It is OK to SELECT 1000000 records or to JOIN two tables with 10000 records in each (You can use streaming functions to work with longer datasources - see test/test143.js) but be aware that the workload is multiplied so SELECTing from more than 8 tables with just 100 rows in each will show bad performance. This is one of our top priorities to make better.

  3. Limited functionality for transactions (supports only for localStorage) - Sorry, transactions are limited, because AlaSQL switched to more complex approach for handling PRIMARY KEYs / FOREIGN KEYs. Transactions will be fully turned on again in a future version.

  4. A (FULL) OUTER JOIN and RIGHT JOIN of more than 2 tables will not produce expected results. INNER JOIN and LEFT JOIN are OK.

  5. Please use aliases when you want fields with the same name from different tables (SELECT a.id AS a_id, b.id AS b_id FROM ?).

  6. At the moment AlaSQL does not work with JSZip 3.0.0 - please use version 2.x.

  7. JOINing a sub-SELECT does not work. Please use a with structure (Example here) or fetch the sub-SELECT to a variable and pass it as an argument (Example here).

  8. AlaSQL uses the FileSaver.js library for saving files locally from the browser. Please be aware that it does not save files in Safari 8.0.

There are probably many others. Please help us fix them by submitting an issue. Thank you!

How To

Use AlaSQL to convert data from CSV to Excel

ETL example:

alasql([
    'CREATE TABLE IF NOT EXISTS geo.country',
    'SELECT * INTO geo.country FROM CSV("country.csv",{headers:true})',
    'SELECT * INTO XLSX("asia") FROM geo.country WHERE continent_name = "Asia"'
]).then(function(res){
    // results from the file asia.xlsx
});

Use AlaSQL as a Web Worker

AlaSQL can run in a Web Worker. Please be aware that all interaction with AlaSQL when running must be async.

From the browser thread, the browser build alasql-worker.min.js automagically uses Web Workers:

<script src="alasql-worker.min.js"></script>
<script>
var arr = [{a:1},{a:2},{a:1}];

alasql([['SELECT * FROM ?',[arr]]]).then(function(data){
    console.log(data);
});
</script>

Live Demo.

The standard build alasql.min.js will use Web Workers if alasql.worker() is called:

<script src="alasql.min.js"></script>
<script>
alasql.worker();
alasql(['SELECT VALUE 10']).then(function(res){
    console.log(res);
}).catch(console.error);
</script>

Live Demo.

From a Web Worker, you can import alasql.min.js with importScripts:

importScripts('alasql.min.js');

Webpack, Browserify, Vue and React (Native)

When targeting the browser, several code bundlers like Webpack and Browserify will pick up modules you might not want.

Here's a list of modules that AlaSQL may require in certain environments or for certain features:

  • Node.js
    • fs
    • net
    • tls
    • request
    • path
  • React Native
    • react-native
    • react-native-fs
    • react-native-fetch-blob
  • Vertx
    • vertx
  • Agonostic
    • XLSX/XLS support
      • cptable
      • jszip
      • xlsx
      • cpexcel
    • es6-promise

Webpack

There are several ways to handle AlaSQL with Webpack:

IgnorePlugin

Ideal when you want to control which modules you want to import.

var IgnorePlugin =  require("webpack").IgnorePlugin;

module.exports = {
  ...
  // Will ignore the modules fs, path, xlsx, request, vertx, and react-native modules
  plugins:[new IgnorePlugin(/(^fs$|cptable|jszip|xlsx|^es6-promise$|^net$|^tls$|^forever-agent$|^tough-cookie$|cpexcel|^path$|^request$|react-native|^vertx$)/)]
};
module.noParse

As of AlaSQL 0.3.5, you can simply tell Webpack not to parse AlaSQL, which avoids all the dynamic require warnings and avoids using eval/clashing with CSP with script-loader. Read the Webpack docs about noParse

...
//Don't parse alasql
{module:noParse:[/alasql/]}
script-loader

If both of the solutions above fail to meet your requirements, you can load AlaSQL with script-loader.

//Load alasql in the global scope with script-loader
import "script!alasql"

This can cause issues if you have a CSP that doesn't allow eval.

Browserify

Read up on excluding, ignoring, and shimming

Example (using excluding)

var browserify = require("browserify");
var b = browserify("./main.js").bundle();
//Will ignore the modules fs, path, xlsx
["fs","path","xlsx",  ... ].forEach(ignore => { b.ignore(ignore) });

Vue

For some frameworks (lige Vue) alasql cant access XLSX by it self. We recommend handling it by including AlaSQL the following way:

import XLSX from 'xlsx';
alasql.utils.isBrowserify = false;
alasql.utils.global.XLSX = XLSX;

jQuery

Please remember to send the original event, and not the jQuery event, for elements. (Use event.originalEvent instead of myEvent)

JSON-object

You can use JSON objects in your databases (do not forget use == and !== operators for deep comparison of objects):

alasql> SELECT VALUE {a:'1',b:'2'}

{a:1,b:2}

alasql> SELECT VALUE {a:'1',b:'2'} == {a:'1',b:'2'}

true

alasql> SELECT VALUE {a:'1',b:'2'}->b

2

alasql> SELECT VALUE {a:'1',b:(2*2)}->b

4

Try AlaSQL JSON objects in Console [sample](http://alasql.org/console?drop table if exists one;create table one;insert into one values {a:@[1,2,3],c:{e:23}}, {a:@[{b:@[1,2,3]}]};select * from one)

Experimental

Useful stuff, but there might be dragons

Graphs

AlaSQL is a multi-paradigm database with support for graphs that can be searched or manipulated.

// Who loves lovers of Alice?
var res = alasql('SEARCH / ANY(>> >> #Alice) name');
console.log(res) // ['Olga','Helen']

See more in the wiki

localStorage and DOM-storage

You can use browser localStorage and DOM-storage as a data storage. Here is a sample:

alasql('CREATE localStorage DATABASE IF NOT EXISTS Atlas');
alasql('ATTACH localStorage DATABASE Atlas AS MyAtlas');
alasql('CREATE TABLE IF NOT EXISTS MyAtlas.City (city string, population number)');
alasql('SELECT * INTO MyAtlas.City FROM ?',[ [
        {city:'Vienna', population:1731000},
        {city:'Budapest', population:1728000}
] ]);
var res = alasql('SELECT * FROM MyAtlas.City');

Try this sample in jsFiddle. Run this sample two or three times, and AlaSQL store more and more data in localStorage. Here, "Atlas" is the name of localStorage database, where "MyAtlas" is a memory AlaSQL database.

You can use localStorage in two modes: SET AUTOCOMMIT ON to immediate save data to localStorage after each statement or SET AUTOCOMMIT OFF. In this case, you need to use COMMIT statement to save all data from in-memory mirror to localStorage.

Plugins

AlaSQL supports plugins. To install a plugin you need to use the REQUIRE statement. See more in the wiki

Alaserver - simple database server

Yes, you can even use AlaSQL as a very simple server for tests.

To run enter the command:

$ alaserver

then open http://127.0.0.1:1337/?SELECT%20VALUE%20(2*2) in your browser

Warning: Alaserver is not multi-threaded, not concurrent, and not secured.

Tests

Regression tests

AlaSQL currently has over 1200 regression tests, but they only cover Coverage of the codebase.

AlaSQL uses mocha for regression tests. Install mocha and run

$ npm test

or open test/index.html for in-browser tests (Please serve via localhost with, for example, http-server).

Tests with AlaSQL ASSERT from SQL

You can use AlaSQL's ASSERT operator to test the results of previous operation:

CREATE TABLE one (a INT);             ASSERT 1;
INSERT INTO one VALUES (1),(2),(3);   ASSERT 3;
SELECT * FROM one ORDER BY a DESC;    ASSERT [{a:3},{a:2},{a:1}];

SQLLOGICTEST

AlaSQL uses SQLLOGICTEST to test its compatibility with SQL-99. The tests include about 2 million queries and statements.

The testruns can be found in the testlog.

Contributing

See Contributing for details.

Thanks to all the people who already contributed!

License

MIT - see MIT licence information

Main contributors

AlaSQL is an OPEN Open Source Project. This means that:

Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

We appreciate any and all contributions we can get. If you feel like contributing, have a look at CONTRIBUTING.md

Rebuilding the parser

To rebuild the parser, follow these steps:

  • Make changes to alasqlparser.jison
  • npm install -g jison
  • npm run jison
  • npm test to validate the changes made
  • Commit changes to alasqlparser.jison and alasqlparser.js

Credits

Many thanks to:

and other people for useful tools, which make our work much easier.

Related projects that have inspired us

  • AlaX - Export to Excel with colors and formats
  • AlaMDX - JavaScript MDX OLAP library (work in progress)
  • Other similar projects - list of databases on JavaScript

AlaSQL logo © 2014-2024, Andrey Gershun ([email protected]) & Mathias Rangel Wulff ([email protected])

See this article for a bit of information about the motivation and background.

alasql's People

Contributors

agershun avatar akhaneev avatar alsundukov avatar ambujsahu81 avatar bjornblomqvist avatar bjouhier avatar bopjesvla avatar dependabot[bot] avatar forestfang-stripe avatar gangadhargo avatar jimmywarting avatar julias0 avatar kanghj avatar macrat avatar martinstarman avatar mathiasrw avatar n-a-m-e avatar nbdamian avatar nickdeis avatar paulrutter avatar positively4th avatar renovate[bot] avatar renxida avatar seb-ster avatar sljohnsondev avatar sytone avatar vishal6557 avatar webdevme avatar wisehorn avatar workmad3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

alasql's Issues

Format Dates

I'm trying to format dates in the select statement:

CONVERT(VARCHAR(10),GETDATE(),110) returns an error. How should this be handled?

Thanks!

Question: Should Alasql implements fluent / Linq interface and plug-ins for jQuery and d3.js?

First case: There is an opinion, that JS programmers are more familiar with fuent interface (a la Linq), rather than "pure" SQL.

    var res = alasql('SELECT TOP 10 a,b FROM one GROUP BY a,b ORDER BY b');
    // replace with
    var res = alasql.select(['a','b']).top(10).from('one').groupby(['a','b']).orderby('b').exec();

The second case - integration with d3.js, jQuery, and other libraries: Should Alasq SQL support plug-in functions for these popular libraries?

    $("div").alasql("SELECT * FROM ? WHERE textContent LIKE "A%"");
    d3.select('body').selectAll('div')
    .alasql('SELECT * FROM XLSX("mydata.xlsx") WHERE total>10')
    .enter().append('div').text(function(d){return d.name});

Should Alasql extend API with fluent interface functions?

WebSQL shim over IndexedDB

Request: "To have a true WebSQL shim over IndexedDB, i.e. similar to what the Cordova SQLite plugin does, but usable in Firefox/IE over IndexedDB. ... it would be really easy to test, because you could just fire up Chrome and verify that the WebSQL inputs/outputs are the same."

Probably, it can be a separate project (because Alasql and SQLite have different internal structure), but it possible to do with some limitations. I will open a separate project for this.

Support CREATE VIEW statement

_Reasons_
All demo databases (like Chinook) include views. Currently, Alasql can emulate view with this approach:

    var myview = alasql.compile('SELECT * FROM one GROUP BY a');
    var res = alasql('SELECT b FROM ?',[myview()]);

but this is only emulation.

ORDER BY clause on three or more UNIONS

Now parsing algorithm uses ORDER BY function from the second UNION part, but not last one. To fix this issue I need to rewrite some parsing rules. I think the same issue for INTERSECT and other similar operators.

Several typos in README

Hello,

I’ve found the following typos in README.md:

  • it can search JSON objects like JavScript and NoSQL databases
  • Try this sample in jsFiddel
  • Here, "Atals" is the name of localStorage database

Error in Where

alasql> select * from cities where population like "3041579"

TypeError: undefined is not a function

alasql> select * from cities where population like "3041579"

TypeError: undefined is not a function

alasql> select * from cities where population like "%3041579%"

TypeError: undefined is not a function

And simple SQL like SELECT * FROM person WHERE sex='F'
http://jsfiddle.net/38hj2uwy/37/

|# |city |population|
|1 |"Madrid" |3041579|
|2 |"Rome" |2863223|
|3 |"Paris" |2249975|

alasql> select * from cities WHERE population="3041579"

[ ]

alasql>

Alasql vs WebSQL perf test - actually store data?

Hey, really cool library. I tried out the Alasql vs WebSQL perf test, though, and I was kinda surprised to see that it didn't seem to be actually storing anything in LocalStorage/IndexedDB/anywhere.

So apparently it's using the in-memory version of the database? If so, the test is a little misleading, because WebSQL is actually writing stuff to disk (and it's asynchronous), whereas Alasql is just doing everything in memory.

Add CSS properties for HTML() into-function

Based on question from StackOverflow: Set condition for innerHTML of a table

If you use HTML() function it is a good idea to give a functionality to change a color or alignment of result cells. Something like:

    alasql('SELECT TOP 10 City \
                    TH {style:{textAlign:"right",textContent:"Town"}}  \
                    TD {color:(CASE WHEN Population > 1000000 THEN "red" ELSE "green" END)} \
               INTO HTML("#res") FROM ? ORDER BY Population DESC',[cities]);

The information, required for table formatting:

  • Column width
  • Column header
  • TH class
  • TD class

Another option: use Alasql together with Handsontable grid, like:

    alasql('SELECT * INTO HOT("#res") FROM ?',[City]);

Short SELECT syntax

I have an idea to cut traditional SELECT syntax and make two clause not necessary:

    SELECT TOP 10 * FROM ? 
    alasql('TOP 10',[data]);

    SELECT * FROM ? WHERE Population > 100000
    alasql('WHERE Population > 1000000', [data]);

    SELECT * FROM ? GROUP BY Country
    alasql('GROUP BY Country',[cities]);

    SELECT * FROM ? ORDER BY Population
    alasql('ORDER BY Population',[cities]);

    SELECT * FROM XLSX('medals.xlsx')
    alasql('FROM XLSX("medals.xlsx")');

    SELECT City, Population FROM ?
    alasql('SELECT City, Population',[cities]);

It is not a standard SQL, but can make the program code size smaller.

The problem can be with ParamValues:

    SELECT TOP ? * FROM ? WHERE Population > ? 
    alasql('TOP ? WHERE Population > ?',[numTop, data, minPopulation]);

Set right default table aliases for proper column names

In some complex queries it is not easy to define which table name is proper for column

SELECT Format, Importance as Priority, Data.Code, css_class FROM Data JOIN Metadata ON Metadata.code = Data.code WHERE Importance < 3

Error installing latest version with npm

Trying to install 0.0.36 alasql with nom on mac os 10.9 returns the following error:

npm ERR! Error: ENOENT, chmod '/Users/drexjojo/Development/QCLogServer/node_modules/alasql/bin/alacon.js'
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/npm/npm/issues>

npm ERR! System Darwin 13.3.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/drexjojo/Development/QCLogServer
npm ERR! node -v v0.10.35
npm ERR! npm -v 1.4.28
npm ERR! path /Users/drexjojo/Development/QCLogServer/node_modules/alasql/bin/alacon.js
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! not ok code 0

Installing 0.0.35 works without any problems

Check compatibility Alasql with Cordova

Goal: Check, if Alasql can work with Cordova

Questions:
a) If Alasql core functions work?
b) Which persistence databases work with Alasql and Cordova?

Results

a) Alasql works with Cordova (at least in browser and iOS)
b) Alasql can work with localStorage (at least in browser and iOS)

IndexedDB is not working with iOS

The reason: Alasql uses Chrome's specific function indexedDB.webkitGetDatabaseNames().
This function is not critical, and can be modeled with openDatabase().

UPDATE
The same problem with Firefox

Fix USING bug

JOIN USING works wrong in this example

      var data = { COLORS: [[1,"red"],[2,"yellow"],[3,"orange"]],            
       "FRUITS":[[1,"apple"],[2,"banana"],[3,"orange"]]};

     data.NEW_FRUITS = alasql('SELECT MATRIX COLORS.[0], COLORS.[1], \
     FRUITS.[1] AS [2] FROM ? AS COLORS JOIN ? AS FRUITS USING [0]',
     [data.COLORS, data.FRUITS]);

Bu everything works OK with JOIN ON in this example:

   var data = { COLORS: [[1,"red"],[2,"yellow"],[3,"orange"]],            
       "FRUITS":[[1,"apple"],[2,"banana"],[3,"orange"]]};

    data.NEW_FRUITS = alasql('SELECT MATRIX COLORS.[0], COLORS.[1], FRUITS.[1] AS [2] \
    FROM ? AS COLORS JOIN ? AS FRUITS ON COLORS.[0] = FRUITS.[0]',
    [data.COLORS,     data.FRUITS]);

ROLLUP() - error: undefined is not a function

error when running ROLLUP():

var testData = [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Val: 5 },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Val: 20 },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Val: 25 },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Val: 40 }
];

res = alasql('SELECT Phase, Step, SUM(Val) AS Val FROM ? GROUP BY ROLLUP(Phase,Step)', [testData]);

I just want to say that this project is incredible. Thanks so much for sharing it.

AVG() does not work

I far as I know, to calculate AVG I need to keep one or two additional numbers in group:

SUM() and COUNT() - more precise approach, requires two variables

or

AVG() and COUNT() - less precise, but requires only one additional variable

I am not still sure, which approach to use.

SELECT * INTO SQL() do not put NULL values

If there are empty values in source table INTO SQL should insert NULL instead ,,.

    alasql('SELECT * INTO SQL("res.sql",{tableid:"mybase.dbo.ofline"}) \
                      FROM XLSX("hse.xlsx",{headers:true,sheetid:"New (2)"})');

in some lines generates:

    INSERT INTO mybase.dbo.ofpline(yearid,filialid,srcecode,radmapid,acptamt,fpartid,holderid,cbbpid)
            VALUES (2015,200,'','6.2.3.',3800000,25021502,,1);

See ,, at the end of the record.

Add formatting functions

Add functionality to format money, etc. suppose, Alasql can use standard solutions like moment.js or numbers.js or other JavaScript libraries.

alasql & web worker

Very impressive library Andrey.
I tried to use alasql in web workers: I had to remove some references to 'window' with indexedDB and disable FileSaver.js because of 'createElementNS'.
It seems to work quite well but I may have overlooked something.
I have seen WebWorker on your TODO : is it planned to be supported any time soon?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.