Skip to content

Commit 4d9d1bb

Browse files
committed
Support query instead of table
1 parent cb7e72c commit 4d9d1bb

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ In order to configure the source, set the following variables:
5252
| MYSQL_PASSWORD | MySQL password. |
5353
| MYSQL_DB_NAME | MySQL database name. |
5454
| MYSQL_TABLE | MySQL table name. |
55+
| MYSQL_QUERY | MySQL query to use instead of the table. Example: `SELECT * FROM iris WHERE species = 'setosa'` |
5556
| MYSQL_MAX_CONNECTIONS | Max connections in the MySQL connection pool. |
5657
| SOURCE_FIELDS | List of fields, split by commas. Example: `sepallength,sepalwidth,petallength,petalwidth,species` |
57-
| SOURCE_FIELDS_TYPES | For each field, its type. Types are: `nominal`, `numeric`, `logic`, `date` and `text`. |
58+
| SOURCE_FIELDS_TYPES | For each field, its type. Types are: `nominal`, `numeric`, `logic`, `date` and `text`. Example: `numeric,numeric,numeric,numeric,nominal` |

src/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Config {
4141
public mysqlUser: string;
4242
public mysqlPassword: string;
4343
public mysqlDatabase: string;
44-
public mysqlTable: string;
44+
public mysqlTableLike: string;
4545
public mysqlConnections: number;
4646

4747
public deepintURL: string;
@@ -73,7 +73,11 @@ export class Config {
7373
this.mysqlUser = process.env.MYSQL_USER || "root";
7474
this.mysqlPassword = process.env.MYSQL_PASSWORD || "";
7575
this.mysqlDatabase = process.env.MYSQL_DB_NAME || "test";
76-
this.mysqlTable = process.env.MYSQL_TABLE || "iris";
76+
const mysqlTable = process.env.MYSQL_TABLE || "iris";
77+
const mysqlQuery = process.env.MYSQL_QUERY || "";
78+
79+
this.mysqlTableLike = mysqlQuery ? ("( " + mysqlQuery + " ) as query_tmp_table") : ("`" + mysqlTable + "`");
80+
7781
this.mysqlConnections = parseInt(process.env.MYSQL_MAX_CONNECTIONS, 10) || 4;
7882

7983
this.deepintURL = process.env.DEEPINT_API_URL || "https://app.deepint.net/api/v1/";

src/source.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class DataSource {
171171
* @param instances Instances
172172
*/
173173
public async pushInstances(instances: InstanceType[][]): Promise<void> {
174-
let sentence = "INSERT INTO `" + Config.getInstance().mysqlTable + "`(";
174+
let sentence = "INSERT INTO " + Config.getInstance().mysqlTableLike + "(";
175175

176176
sentence += this.fields.map(f => "`" + f.name + "`").join(",");
177177

@@ -206,7 +206,7 @@ export class DataSource {
206206
* @returns Instances count
207207
*/
208208
public async countInstances(filter: QueryTree): Promise<number> {
209-
let sentence = "SELECT COUNT(*) AS `count` FROM `" + Config.getInstance().mysqlTable + "`";
209+
let sentence = "SELECT COUNT(*) AS `count` FROM " + Config.getInstance().mysqlTableLike + "";
210210
const values = [];
211211

212212

@@ -266,7 +266,7 @@ export class DataSource {
266266
sentence += "*";
267267
}
268268

269-
sentence += " FROM `" + Config.getInstance().mysqlTable + "`";
269+
sentence += " FROM " + Config.getInstance().mysqlTableLike + "";
270270

271271
const cond1 = toSQLCondition(this.fields, filter);
272272

@@ -336,7 +336,7 @@ export class DataSource {
336336
const fieldName = this.fields[feature].name;
337337
query = (query || "").toLowerCase();
338338

339-
sentence += '`' + fieldName + '` FROM `' + Config.getInstance().mysqlTable + '`';
339+
sentence += '`' + fieldName + '` FROM ' + Config.getInstance().mysqlTableLike + '';
340340

341341
if (cond1.sql) {
342342
if (query) {

0 commit comments

Comments
 (0)