Skip to content

Update everything #45

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

Merged
merged 101 commits into from
Jun 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
71d17fb
Set to /usr/bin if not previously set. Allows to run 'make' instead …
lovasoa Apr 23, 2014
46cff2d
Correct printf format
lovasoa Apr 23, 2014
6c237cf
clean. Use clang
lovasoa Apr 23, 2014
78d60a7
Remove warnings
lovasoa Apr 23, 2014
e2b768b
Correct dependencies
lovasoa Apr 23, 2014
347786e
Better GUI
lovasoa Apr 24, 2014
fb064ee
Reflect code changes
lovasoa Apr 24, 2014
2c951d9
change title
lovasoa Apr 24, 2014
f651f75
Add informations about whats new in the fork
lovasoa Apr 24, 2014
67a14ec
Add ability to save and load a database.
lovasoa Apr 27, 2014
a8bdf14
extend list of new features
lovasoa Apr 27, 2014
5ee0316
Update Makefile according to @kripken advices.
lovasoa Apr 29, 2014
f5a11ac
Whoops! I forgot to remove my nasty hack to the Makefile.
lovasoa Apr 29, 2014
6d6e4bd
Separate javascript code from HTML
lovasoa Apr 29, 2014
2a65161
Add an outlining_limit again. Otherwise, it would take several second…
lovasoa Apr 29, 2014
a7cc65a
More simple function to generate HTML tables.
lovasoa Apr 29, 2014
6949388
Add an outlining performance benchmark
lovasoa May 3, 2014
1d514c7
Update the benchmarks. Choose the default OUTLINING_LIMIT according t…
lovasoa May 3, 2014
8d5f15a
Improve compilation time by disabling inlining instead of enabling ou…
lovasoa May 5, 2014
dc13d20
Object-oriented API, with support for prepared statements
lovasoa May 7, 2014
787cc78
The API, written in coffeescript
lovasoa May 7, 2014
26b7580
Documentation
lovasoa May 7, 2014
958673e
corrections
lovasoa May 7, 2014
bfddd8f
Bug correction
lovasoa May 7, 2014
88d61c2
Bug correction, getErrors -> handleErrors
lovasoa May 7, 2014
f91ca55
Final bug corrections. Now the new API works.
lovasoa May 7, 2014
4a249da
Ad supprort for parameter binding to prepered statements.
lovasoa May 7, 2014
58fafff
Add support for freeing and resetting prepared statements.
lovasoa May 7, 2014
00c288e
clarify the README
lovasoa May 7, 2014
02d0e73
Add export to Uint8array
lovasoa May 11, 2014
e38ecaf
Do not pollute the global namespace
lovasoa May 11, 2014
4c9d107
Include the compiled js api
lovasoa May 11, 2014
763d162
Merge pull request #9 from lovasoa/api
lovasoa May 11, 2014
15070f5
Bug corrections
lovasoa May 13, 2014
4e5ad04
Bug correction
lovasoa May 13, 2014
c9e7a53
Use the new api in the gui
lovasoa May 13, 2014
2217694
Compile api with all optimizations.
lovasoa May 14, 2014
0aa2f98
Merge pull request #10 from lovasoa/api
lovasoa May 14, 2014
3341c5c
Update API
lovasoa May 23, 2014
6c5f42a
Add proper test files and remove old useless files
lovasoa May 23, 2014
e089e6e
Beautify the makefile
lovasoa May 23, 2014
a8bcdaf
Delete old files. Now the new api is exported in the file 'sql.js'.
lovasoa May 23, 2014
ed1b798
commit shell files (they avoid leaking variable to the global namespace)
lovasoa May 23, 2014
bafb517
Remove old coffee file
lovasoa May 23, 2014
d12bdf0
Update README with examples
lovasoa May 23, 2014
2a00f18
Update README with blob example
lovasoa May 23, 2014
0811189
Add link to docs
lovasoa May 23, 2014
8d6ef29
Add link to the GUI
lovasoa May 23, 2014
89c71ad
Remove GUI from master.
lovasoa May 23, 2014
78912eb
Add separate test for the Database class
lovasoa May 23, 2014
7f0054c
Rename test_api to test_statemnt, and remove general database testing…
lovasoa May 23, 2014
ca4ad52
Add Statement.getColumnNames and Statement.getAsObject
lovasoa May 23, 2014
1da8cfd
Present stmt.getAsObject()
lovasoa May 24, 2014
6621399
set the pointer to a statement to NULL when it's free() method is cal…
lovasoa May 24, 2014
d26d241
Reflect the recent new features in the API to the README
lovasoa May 24, 2014
937e927
Free all related statements before closing a database.
lovasoa May 24, 2014
f4f61e8
Add a test to test error throwing
lovasoa May 24, 2014
85127d6
Add a package.jon
lovasoa May 24, 2014
ca481a8
test UTF8 text (with 粵語😄)
lovasoa May 24, 2014
8bafedb
Free memory used by bound parameters manually.
lovasoa May 24, 2014
e6a6ac8
Split API code and data
lovasoa May 24, 2014
9bb8d0e
Improve build infrastructure
lovasoa May 24, 2014
36c3a83
Update sql.js to match previous changes in api.coffee
lovasoa May 24, 2014
d3c1069
Improve test infrastructure
lovasoa May 24, 2014
7b2ec0d
Add travis configuration file
lovasoa May 24, 2014
ada2f38
Only test against node O.10
lovasoa May 24, 2014
7915245
Update package.json
lovasoa May 24, 2014
1baae62
Add the test database file
lovasoa May 24, 2014
3d1ba01
Add travis build status
lovasoa May 24, 2014
087b059
Publish on npm
lovasoa May 24, 2014
64b703c
export compiled functions in a separate coffee file
lovasoa May 24, 2014
b049682
API CHANGE. Update db.exec, and add db.run
lovasoa May 24, 2014
977f5d4
Update readme to add travis build status
lovasoa May 24, 2014
54810c3
add the compiled js file
lovasoa May 24, 2014
ef8241c
Add support for web workers
lovasoa May 25, 2014
e234863
Add test for the worker API
lovasoa May 25, 2014
e06b972
Update examples
lovasoa May 25, 2014
612c346
Improve tests coverage
lovasoa May 25, 2014
9ff31e8
Add db.each
lovasoa May 25, 2014
07042f1
Remove useless console.log
lovasoa May 25, 2014
f5a20f2
worker: Don't throw an error when id is missing
lovasoa May 25, 2014
0190b08
Update the compiled worker file
lovasoa May 25, 2014
5857536
Add an example of how to write a database to the disk in node
lovasoa May 26, 2014
fc3f619
Add download links
lovasoa May 26, 2014
2f20c02
Update HTML example
lovasoa May 26, 2014
060ebf3
Update node example
lovasoa May 26, 2014
bca3458
Increment version
lovasoa May 26, 2014
3ff4f49
Add @hankinsoft to the authors
lovasoa Jun 3, 2014
5bb3690
Call postMessage without second argument if using transferable elemen…
lovasoa Jun 3, 2014
aa448b3
Fix a bug in internet explorer.
lovasoa Jun 3, 2014
3738933
bump version
lovasoa Jun 3, 2014
5275839
Add debug file, as suggested in #15
lovasoa Jun 3, 2014
de92cea
Improve error handling.
lovasoa Jun 8, 2014
a909703
Update version number
lovasoa Jun 10, 2014
44e183c
Update compiled files
lovasoa Jun 10, 2014
f460869
Correct headings level
lovasoa Jun 10, 2014
7a08327
Add link & reformat downloads
lovasoa Jun 10, 2014
6bf6684
Use lowercase keywords
lovasoa Jun 10, 2014
1d1292d
Update version
lovasoa Jun 10, 2014
999fd41
Update sqlite to 3.8.5
lovasoa Jun 11, 2014
7356d05
Optimize memory management.
lovasoa Jun 11, 2014
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
4 changes: 4 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Ophir LOJKINE <pere.jobs@gmail.com> (https://github.com/lovasoa)
@kripken
@hankinsoft

48 changes: 37 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
# Need $(EMSCRIPTEN), for example run with emmake make

EMCC=$(EMSCRIPTEN)/emcc -s RESERVED_FUNCTION_POINTERS=2 -O2 --closure 1 -s ASM_JS=0
# -s INLINING_LIMIT=0
CFLAGS=-DSQLITE_DISABLE_LFS -DLONGDOUBLE_TYPE=double -DSQLITE_INT64_TYPE="long long int" -DSQLITE_THREADSAFE=0
EMSCRIPTEN?=/usr/bin

all: js/sql.js test/benchmark.js test/benchmark
EMCC=$(EMSCRIPTEN)/emcc

js/sql.js: c/sqlite3.c
$(EMCC) $(CFLAGS) c/sqlite3.c c/main.c --pre-js js/pre.js --post-js js/post.js -o js/sql.js -s EXPORTED_FUNCTIONS="['_sqlite3_open', '_sqlite3_close', '_sqlite3_exec']"
CFLAGS=-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DLONGDOUBLE_TYPE=double -DSQLITE_INT64_TYPE="long long int" -DSQLITE_THREADSAFE=0

test/benchmark.js: c/sqlite3.c c/benchmark.c
$(EMCC) $(CFLAGS) c/sqlite3.c c/benchmark.c -o test/benchmark.js
all: js/sql.js

test/benchmark: c/benchmark.c
gcc $(CFLAGS) -pthread -O2 c/sqlite3.c c/benchmark.c -o test/benchmark -ldl
debug: EMFLAGS= -O1 -g -s INLINING_LIMIT=10
debug: js/sql-debug.js

optimized: EMFLAGS= --closure 1 -O3 -s INLINING_LIMIT=50
optimized: js/sql-optimized.js

js/sql.js: optimized
cp js/sql-optimized.js js/sql.js

js/sql%.js: js/shell-pre.js js/sql%-raw.js js/shell-post.js
cat $^ > $@

js/sql%-raw.js: c/sqlite3.bc js/api.js exported_functions
$(EMCC) $(EMFLAGS) -s EXPORTED_FUNCTIONS=@exported_functions c/sqlite3.bc --post-js js/api.js -o $@

js/api.js: coffee/api.coffee coffee/exports.coffee coffee/api-data.coffee
coffee --bare --compile --join $@ --compile $^

# Web worker API
worker: js/worker.sql.js
js/worker.js: coffee/worker.coffee
coffee --bare --compile --join $@ --compile $^

js/worker.sql.js: js/sql.js js/worker.js
cat $^ > $@

c/sqlite3.bc: c/sqlite3.c
# Generate llvm bitcode
$(EMCC) $(CFLAGS) c/sqlite3.c -o c/sqlite3.bc

module.tar.gz: test package.json AUTHORS README.md js/sql.js
tar --create --gzip $^ > $@

clean:
rm js/sql.js test/benchmark.js test/benchmark
rm -rf js/sql*.js js/api.js js/sql*-raw.js c/sqlite3.bc

201 changes: 169 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# sql.js
# SQLite compiled to javascript
[![Build Status](https://travis-ci.org/lovasoa/sql.js.svg?branch=master)](http://travis-ci.org/lovasoa/sql.js)

This is my fork of sql.js, by kripken. Try it online here: http://lovasoa.github.io/sql.js/GUI/

sql.js is a port of SQLite to JavaScript, by compiling the SQLite C code with Emscripten.
no C bindings or node-gyp compilation here.
Expand All @@ -7,40 +10,174 @@ SQLite is public domain, sql.js is MIT licensed.

## Usage

```coffeescript
Sql = require 'node-sqlite-purejs'
Sql.open 'db/development.sqlite', {}, (err, db) ->
throw err if err
db.exec '''
/* Demo DB */
CREATE TABLE employees( id integer, name text,
designation text, manager integer,
hired_on date, salary integer,
commission float, dept integer);

INSERT INTO employees VALUES (1,'JOHNSON','ADMIN',6,'12-17-1990',18000,NULL,4);
INSERT INTO employees VALUES (2,'HARDING','MANAGER',9,'02-02-1998',52000,300,3);
INSERT INTO employees VALUES (3,'TAFT','SALES I',2,'01-02-1996',25000,500,3);
INSERT INTO employees VALUES (4,'HOOVER','SALES I',2,'04-02-1990',27000,NULL,3);
INSERT INTO employees VALUES (5,'LINCOLN','TECH',6,'06-23-1994',22500,1400,4);
INSERT INTO employees VALUES (6,'GARFIELD','MANAGER',9,'05-01-1993',54000,NULL,4);
INSERT INTO employees VALUES (7,'POLK','TECH',6,'09-22-1997',25000,NULL,4);
INSERT INTO employees VALUES (8,'GRANT','ENGINEER',10,'03-30-1997',32000,NULL,2);
INSERT INTO employees VALUES (9,'JACKSON','CEO',NULL,'01-01-1990',75000,NULL,4);
INSERT INTO employees VALUES (10,'FILLMORE','MANAGER',9,'08-09-1994',56000,NULL,2);
INSERT INTO employees VALUES (11,'ADAMS','ENGINEER',10,'03-15-1996',34000,NULL,2);
INSERT INTO employees VALUES (12,'WASHINGTON','ADMIN',6,'04-16-1998',18000,NULL,4);
INSERT INTO employees VALUES (13,'MONROE','ENGINEER',10,'12-03-2000',30000,NULL,2);
INSERT INTO employees VALUES (14,'ROOSEVELT','CPA',9,'10-12-1995',35000,NULL,1);
'''

db.exec "SELECT * FROM employees WHERE designation = 'CEO';", (err, results) ->
assert.deepEqual [{"id":"9","name":"JACKSON","designation":"CEO","manager":"(null)","hired_on":"01-01-1990","salary":"75000","commission":"(null)","dept":"4"}], results
```javascript
var sql = require('./js/sql-api.js');
// or sql = window.SQL if you are in a browser

// Create a database
var db = new sql.Database();
// NOTE: You can also use new sql.Database(data) where
// data is an Uint8Array representing an SQLite database file

// Execute some sql
sqlstr = "CREATE TABLE hello (a int, b char);";
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
db.run(sqlstr); // Run the query without returning anything

var res = db.exec("SELECT * FROM hello");
/*
[
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
]
*/

// Prepare an sql statement
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");

// Bind values to the parameters and fetch the results of the query
var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
console.log(result); // Will print {a:1, b:'world'}

// Bind other values
stmt.bind([0, 'hello']);
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']

// free the memory used by the statement
stmt.free();
// You can not use your statement anymore once it has been freed.
// But not freeing your statements causes memory leaks. You don't want that.

// Export the database to an Uint8Array containing the SQLite database file
var binaryArray = db.export();
```

## Demo
There is an online demo available here : http://lovasoa.github.io/sql.js/GUI

## Exemples
The test files provide up to date example of the use of the api.
### Inside the browser
#### Example **HTML** file:
```html
<script src='js/sql.js'></script>
<script>
//Create the database
var db = new SQL.Database();
// Run a query without reading the results
db.run("CREATE TABLE test (col1, col2);");
// Insert two rows: (1,111) and (2,222)
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);

// Prepare a statement
var stmt = db.prepare("SELECT * FROM test WHERE a BETWEEN $start AND $end");
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}

// Bind new values
stmt.bind({$start:1, $end:2});
while(stmt.step()) { //
var row = stmt.getAsObject();
// [...] do something with the row of result
}
</script>
```

#### Creating a database from a file choosen by the user
`SQL.Database` constructor takes an array of integer representing a database file as an optional parameter.
The following code uses an HTML input as the source for loading a database:
```javascript
dbFileElm.onchange = function() {
var f = dbFileElm.files[0];
var r = new FileReader();
r.onload = function() {
var Uints = new Uint8Array(r.result);
db = new SQL.Database(Uints);
}
r.readAsArrayBuffer(f);
}
```
See : http://lovasoa.github.io/sql.js/GUI/gui.js

### Use from node.js

`sql.js` is [hosted on npm](https://www.npmjs.org/package/sql.js). To install it, you can simply run `npm install sql.js`.
Alternatively, you can simply download the file `sql.js`, from the download link below.

#### read a database from the disk:
```javascript
var fs = require('fs');
var SQL = require('sql.js');
var filebuffer = fs.readFileSync('test.sqlite');

// Load the db
var db = new SQL.Database(filebuffer);
```

#### write a database to the disk
You need to convert the result of `db.export` to a buffer
```javascript
var fs = require("fs");
// [...] (create the database)
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync("filename.sqlite", buffer);
```

See : https://github.com/lovasoa/sql.js/blob/master/test/test_node_file.js

### Use as web worker
If you don't want to run CPU-intensive SQL queries in your main application thread,
you can use the *more limited* WebWorker API.

You will need to download `worker.sql.js`

Example:
```html
<script>
var worker = new Worker("js/worker.sql.js"); // You can find worker.sql.js in this repo
worker.onmessage = function() {
console.log("Database opened");
worker.onmessage = function(event){
console.log(event.data); // The result of the query
};
worker.postMessage({
id: 2,
action: 'exec',
sql: 'SELECT * FROM test'
});
};

worker.onerror = function(e) {console.log("Worker error: ", e)};
worker.postMessage({
id:1,
action:'open',
buffer:buf, /*Optional. An ArrayBuffer representing an SQLite Database file*/
});
</script>
```

See : https://github.com/lovasoa/sql.js/blob/master/test/test_worker.js

## Documentation
The API is fully documented here : http://lovasoa.github.io/sql.js/documentation/

## Downloads
- You can download `sql.js` here : http://lovasoa.github.io/sql.js/js/sql.js
- And the Web Worker version: http://lovasoa.github.io/sql.js/js/worker.sql.js

see [test/test.coffee](https://github.com/mikesmullin/node-sqlite-purejs/blob/stable/test/test.coffee) for more examples.
## Differences from the original sql.js
* Support for BLOBs
* Support for prepared statements
* Cleaner API
* More recent version of SQLite (3.8.4)
* Compiled to asm.js (should be faster, at least on firefox)
* Changed API. Results now have the form <code>[{'columns':[], values:[]}]</code>
* Improved GUI of the demo. It now has :
* syntax highlighting
* nice HTML tables to display results
* ability to load and save sqlite database files

## Related

* [In-Browser/Client-Side Demo](http://kripken.github.io/sql.js/test/demo.html)
* [In-Browser/Client-Side Demo](http://lovasoa.github.io/sql.js/GUI/)

87 changes: 0 additions & 87 deletions c/benchmark.c

This file was deleted.

3 changes: 0 additions & 3 deletions c/main.c

This file was deleted.

Loading