Skip to content

Commit

Permalink
Merge 56e21ac into 3dc80d1
Browse files Browse the repository at this point in the history
  • Loading branch information
ngdaniels committed Sep 6, 2019
2 parents 3dc80d1 + 56e21ac commit 04e6217
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ Yarn:
```javascript
const QueryBuilder = require('fish-query');

const queryClass = new QueryBuilder();
const config = {
datastore: 'postgresql' //Currently support 'postgresql' and 'sqlserver'. Default to 'postgresql' when not specified.
};

const queryClass = new QueryBuilder(config);

let queryString = queryClass
.addSelect('firstName')
Expand Down
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
class QueryBuilder {
constructor() {
constructor(config) {
let { datastore } = config ? config : { datastore: 'postgresql' } ;

this.selectField = [];
this.fromField = [];
this.whereField = [];
this.orderField = [];
this.firstTable = '';
this.limit = null;
this.offset = null;
this.datastore = datastore;
}

addSelect(value) {
Expand Down Expand Up @@ -68,7 +71,7 @@ class QueryBuilder {
}
else if (typeof select === 'object') {
selectString = `${select.tableName}."${select.columnName}"`;
if (select.columnAlias) selectString += ` AS ${select.columnAlias}`;
if (select.columnAlias) selectString += ` AS "${select.columnAlias}"`;
}

if (index === this.selectField.length - 1) {
Expand Down Expand Up @@ -140,8 +143,14 @@ class QueryBuilder {
}

if (this.limit && this.offset) {
queryString += `LIMIT ${this.limit} `;
if (this.offset) queryString += `OFFSET ${this.offset} `;
if (this.datastore === 'postgresql') {
queryString += `LIMIT ${this.limit} `;
queryString += `OFFSET ${this.offset} `;
}
else if (this.datastore === 'sqlserver') {
queryString += `OFFSET ${this.offset} ROWS `;
queryString += `FETCH NEXT ${this.limit} ROWS ONLY `;
}
}

queryString = queryString.trim();
Expand All @@ -154,4 +163,4 @@ class QueryBuilder {
}
}

module.exports = QueryBuilder;
module.exports = QueryBuilder;

0 comments on commit 04e6217

Please sign in to comment.