-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
describe('Unit tests for DELETE statement', () => { | ||
test('Delete all from the an index', () => { | ||
expect(true).toBeTruthy(); | ||
import QueryBuilder from '../../src/QueryBuilder'; | ||
import SphinxClient from '../../src/SphinxClient'; | ||
require('iconv-lite').encodingExists('foo'); // fix bug with mysql2 and Jest | ||
|
||
describe('Tests for DELETE statement', () => { | ||
const params = { | ||
host: '127.0.0.1', | ||
port: 9307, | ||
}; | ||
|
||
test('deletes with a where condition', () => { | ||
const conn = new SphinxClient(params); | ||
const compiledQuery = new QueryBuilder(conn) | ||
.delete('rt') | ||
.where('id', '=', 2) | ||
.generate(); | ||
const expectedQuery = `DELETE FROM rt WHERE id = 2`; | ||
|
||
expect(compiledQuery).toBe(expectedQuery); | ||
}); | ||
}); | ||
|
||
test('deletes with a match condition', () => { | ||
const conn = new SphinxClient(params); | ||
const compiledQuery = new QueryBuilder(conn) | ||
.delete('rt') | ||
.match(['title', 'content'], 'dinosaur') | ||
.generate(); | ||
const expectedQuery = `DELETE FROM rt WHERE MATCH('(@(title,content) dinosaur)')`; | ||
|
||
expect(compiledQuery).toBe(expectedQuery); | ||
}); | ||
|
||
test('deletes with multiple conditions', () => { | ||
const conn = new SphinxClient(params); | ||
const compiledQuery = new QueryBuilder(conn) | ||
.delete('rt') | ||
.match(['title', 'content'], 'dinosaur') | ||
.where('published_at', '<', 2030) | ||
.generate(); | ||
const expectedQuery = `DELETE FROM rt WHERE MATCH('(@(title,content) dinosaur)') AND published_at < 2030`; | ||
|
||
expect(compiledQuery).toBe(expectedQuery); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import MatchExprStatement from './statement_expressions/MatchStatement'; | ||
import ClientInterface from '../ClientInterface'; | ||
import WhereStatement from './statement_expressions/WhereStatement'; | ||
import StatementBuilderBase from './StatementBuilderBase'; | ||
|
||
/** | ||
* DELETE FROM index WHERE where_condition | ||
*/ | ||
export default class DeleteStatement { | ||
protected connection: ClientInterface; | ||
protected index: string; | ||
protected matchStatement: MatchExprStatement = new MatchExprStatement(); | ||
protected whereConditions: WhereStatement[] = []; | ||
|
||
public constructor(connection: ClientInterface, index: string) { | ||
this.connection = connection; | ||
this.index = index; | ||
} | ||
|
||
/** | ||
* | ||
* @param columnExpr | ||
* @param operator | ||
* @param value | ||
*/ | ||
public where(columnExpr: string, operator: string, value?: any): DeleteStatement { | ||
if (value === undefined) { | ||
value = operator; | ||
operator = '='; | ||
} | ||
|
||
const condition = new WhereStatement(columnExpr, operator, value); | ||
this.whereConditions = [...this.whereConditions, condition]; | ||
|
||
return this; | ||
} | ||
|
||
public whereIn(column: string, values: any[]) { | ||
const condition = new WhereStatement(column, 'IN', values); | ||
this.whereConditions = [...this.whereConditions, condition]; | ||
|
||
return this; | ||
} | ||
|
||
public whereNotIn(column: string, values: any[]) { | ||
const condition = new WhereStatement(column, 'NOT IN', values); | ||
this.whereConditions = [...this.whereConditions, condition]; | ||
|
||
return this; | ||
} | ||
|
||
public between(column: string, value1: any, value2: any) { | ||
const condtion = new WhereStatement(column, 'BETWEEN', [value1, value2]); | ||
this.whereConditions = [...this.whereConditions, condtion]; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Adds an AND logical operator (by default) to the new full text condition. | ||
* It recieves a field or an array of string fields to match against. | ||
* "value" parameter is used for the text to match. | ||
* If escapeValue is true (default) it will escape full text operators | ||
* to prevent security issues, else the value will contain syntax FT operators | ||
* to make possible use proximity, negation, exact phrase, and so forth. | ||
*/ | ||
public match(fields: string[] | string, value: string, escapeValue: boolean = true) { | ||
this.matchStatement.match(fields.length ? fields : undefined, value, escapeValue); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Adds an OR "|" logical operator to the new full text condition. | ||
* It MUST be used after call "match" method because "orMatch" preppends | ||
* the OR operator. | ||
*/ | ||
public orMatch(fields: string[] | string, value: string, escapeValue: boolean = true) { | ||
this.matchStatement.orMatch(fields.length ? fields : undefined, value, escapeValue); | ||
|
||
return this; | ||
} | ||
|
||
public generate() : string { | ||
let statement = 'DELETE FROM '; | ||
statement += this.index; | ||
|
||
const hasMatchStatement: boolean = this.matchStatement.getParts().length > 0; | ||
const hasWhereStatements: boolean = this.whereConditions.length > 0; | ||
|
||
if (hasWhereStatements || hasMatchStatement) { | ||
statement += ' WHERE '; | ||
|
||
if (hasMatchStatement) { | ||
statement += `MATCH(${this.matchStatement.build()})`; | ||
if (hasWhereStatements) { | ||
statement += ' AND '; | ||
} | ||
} | ||
|
||
let stringStatements: string[]; | ||
stringStatements = this.whereConditions.map((condition: StatementBuilderBase) => condition.build()); | ||
statement += stringStatements.join(' AND '); | ||
} | ||
|
||
return statement; | ||
} | ||
|
||
public execute() { | ||
const query = this.generate(); | ||
// make query | ||
|
||
return query; | ||
} | ||
} |