Skip to content

Support GROUP BY #7

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -236,7 +236,11 @@ var data = {
query: 'JOIN table_b on table_a.join_id = table_b.id',
},
selects: 'table_b.*',
orderBy: ['join_id'],
groupBy: 'join_id',
orderBy: [
'join_id',
'another_column',
],
};

// Or find and return all data from `table_a`
33 changes: 25 additions & 8 deletions lib/websqlClass.js
Original file line number Diff line number Diff line change
@@ -202,7 +202,7 @@
}
this.queryType = 'drop';
this.queryCallback = this.callback;
this.querySuccessMsg = 'Table' + (data.data.length > 0 ? 's' : '') + ' dropped successfully';
this.querySuccessMsg = 'Table' + (data.data.length === 1 ? '' : 's') + ' dropped successfully';
this.queryFailMsg = 'Error during DROP transaction';
break;
case 'insert':
@@ -286,9 +286,12 @@
break;
case 'find':
this.conditions = '';
this.groupBy = '';
this.orderBy = '';

if (Object.keys(data.data).length > 0) {
if (typeof data.data === 'string') {
this.conditions = data.data;
} else if (Object.keys(data.data).length > 0) {
for (key in data.data) {
var val = data.data[key];

@@ -338,30 +341,44 @@

if (data.selects) {
data.selects = Array.isArray(data.selects) ? data.selects.join(', ') : data.selects;
this.query = 'SELECT ' + data.selects + ', ' + this.table + '.* ';
} else {
this.query = 'SELECT ' + data.selects;

if (data.join) {
this.query = 'SELECT *, ' + this.table + '.* ';
} else {
this.query = 'SELECT * ';
this.query += ', ' + this.table + '.*';
}
} else if (data.join) {
this.query = 'SELECT *, ' + this.table + '.*';
} else {
this.query = 'SELECT *';
}

this.query += ' ';

if (data.join) {
data.join.query = Array.isArray(data.join.query) ? data.join.query.join(' ') : data.join.query;
this.conditions = data.join.query + ' ' + this.conditions;
}

this.query += 'FROM ' + this.table + ' ' + this.conditions;

if (data.groupBy) {
if (this.groupBy.length === 0) {
this.query += ' GROUP BY ';
} else {
this.query += ' ';
}

this.query += Array.isArray(data.groupBy) ? data.orderBy.join(', ') : data.groupBy;
}

if (data.orderBy) {
if (this.orderBy.length === 0) {
this.query += ' ORDER BY ';
} else {
this.query += ' ';
}

this.query += data.orderBy.join(', ');
this.query += Array.isArray(data.orderBy) ? data.orderBy.join(', ') : data.orderBy;
}

this.queryValues = [];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websql-json",
"version": "0.1.5",
"version": "0.1.7",
"description": "Web SQLite access library. Communicate easily and cleanly using JSON objects.",
"main": "./lib/websqlInterface.js",
"scripts": {