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
7.01k stars 653 forks source link

CREATE VIEW error with localStorage engine #604

Open a-gorbunov opened 8 years ago

a-gorbunov commented 8 years ago

Input:

alasql("CREATE LOCALSTORAGE DATABASE Loc");
alasql("ATTACH LOCALSTORAGE DATABASE Loc");
alasql("CREATE DATABASE Ram");
alasql("CREATE TABLE Loc.A (id)");
alasql("INSERT INTO Loc.A VALUES ?, ?", [{id: 1}, {id: 2}]);
alasql("CREATE TABLE Loc.B (id, val)");
alasql("INSERT INTO Loc.B VALUES ?, ?", [{id: 1, val: 'a'}, {id: 1, val: 'b'}]);

Bug:

alasql("CREATE VIEW Loc.V as SELECT * FROM Loc.A JOIN Loc.B USING [id]");
alasql("SELECT * FROM Loc.V");
// output => []

But if view is created not in localStorage database - everything works right:

alasql("CREATE VIEW Ram.V as SELECT * FROM Loc.A JOIN Loc.B USING [id]");
alasql("SELECT * FROM Ram.V");
// output => [Object, Object]

Sorry, have no time to create JS file with that code. :)

positively4th commented 8 years ago

@mathiasrw @a-gorbunov

I gave this a go and solved it partially by:

  1. in src/60createtable.js yy.CreateTable.prototype.execute: Enusre that the create table has { view: true, select: this.select.compile(this.table.databaseid||databaseid) }

2.in src/420from.js compileFrom: Fiddle with the source.datafn logic if ( tq instanceof yy.Table ).

This solves the issue as reported.

However, neither selecting from the view nor the source table works after detaching and reattaching to the db. This is true even if i stash my changes....

I'm happy to supply a pull request with a test case and my changes. However I am unsure of how to do it. (I cloned the repo so I have made my changes to the develop branch....). Should I create a new branch and the push it to origin and then create the pull request?

mathiasrw commented 8 years ago

@positively4th You are awesome. Thank you!

I will find out the best way to proceed and get back to you as soon as possible.

mathiasrw commented 8 years ago

@positively4th

The best way is to:

Thank you!

I have update https://github.com/agershun/alasql/blob/develop/.github/CONTRIBUTING.md as it was missing information about the forking part

positively4th commented 8 years ago

@mathiasrw

Created a PR with new test case and my trial and error fixes. Hope it helps.

mathiasrw commented 8 years ago

Thank you so much.