Pattern Condition Generate
Pattern Conditions
Overview
The patternConditions
module provides functions for processing and generating SQL-like pattern conditions. These conditions allow for pattern matching within SQL queries, including support for logical operators like $or
and $and
.
Interface
patternType
Defines the structure of pattern conditions, including both positive ($pattern
) and negative ($not_pattern
) patterns.
$pattern
: Specifies the positive pattern conditions.$not_pattern
: Specifies the negative pattern conditions.
Functions
processPatternConditions
Processes pattern conditions recursively and generates a string representation.
- Parameters:
conditions
: The pattern condition object.operatorKeyword
: The keyword to be used before the condition (e.g.,NOT
).subOperator
: The sub-operator to be used between multiple conditions (e.g.,AND
,OR
).
- Returns: The generated pattern conditions as a string.
patternConditions
Generates SQL-like pattern conditions based on the provided input.
- Parameters:
condition
: ThepatternType
object containing conditions.subOperator
(optional): The sub-operator to be used between multiple conditions.
- Returns: The generated pattern conditions as a string.
Usage
Example usage of the patternConditions
module:
import { patternConditions } from "mysql-query-geniex";;
const condition = {
"$pattern": {
field1: 'value1%',
"$or": {
field2: '%value2',
field3: '%value3%'
}
},
"$not_pattern": {
field4: '%value4',
"$and": {
field5: '_value%',
field6: 'val%ue'
}
}
};
const sqlPatternConditions = patternConditions(condition);
console.log(sqlPatternConditions);
This will output the SQL-like pattern conditions string based on the provided conditions.
The wildcard characters and provide more examples:
A wildcard character is used to substitute one or more characters in a string.
Wildcard Character | Description | Example | Matches |
---|---|---|---|
% | Matches zero or more characters, including none. | '%apple%' | 'apple' , 'pineapple' , 'caramel-apple' , etc. |
_ | Matches exactly one character. | '_at' | 'cat' , 'bat' , 'hat' , but not 'at' |
[charlist] | Matches any single character within the set. | '[bc]at' | 'bat' , 'cat' |
[^ charlist] | Matches any single character not within the set. | '[^abc]at' | 'mat' , 'hat' , 'sat' , but not 'cat' |
\ | Escapes special characters to be treated as literals. | '10%' | '10%' in the data |
? | Matches any single character (Equivalent to _ ). | 'c?t' | 'cat' , 'cot' , but not 'cart' |
* | Matches zero or more occurrences of any character. | 'c*t' | 'cat' , 'ct' , 'cart' , 'cot' , 'ceiling' |
+ | Matches one or more occurrences of any character. | 'c+t' | 'cat' , 'ct' , but not 'cot' , 'ceiling' |
a | Matches any alphabetic character. | '%acat' | 'acat' , 'bcat' , 'ccat' , but not '9cat' |
d | Matches any digit. | '%d%' | '1' , '2' , '3' , etc. |
s | Matches any whitespace character. | '%s%' | ' ' , ' ' , etc. |
w | Matches any word character (alphanumeric and underscore). | 'w+' | 'word' , '123' , 'word123' , but not '!' |
Z | Matches the end of the string. | 'wordZ' | 'word' at the end of the string |
{ n } | Matches exactly 'n' occurrences of the previous character. | 'ba{2}' | 'baa' , but not 'ba' |
{ n, m } | Matches at least 'n' and at most 'm' occurrences. | 'ba{1,3}' | 'ba' , 'baa' , 'baaa' , but not 'baaaa' |
^ | Matches the start of the string. | '^apple' | 'apple' at the beginning of the string |
$ | Matches the end of the string. | 'apple$' | 'apple' at the end of the string |
| Matches a word boundary (position between a word character and a non-word character). | 'word' | 'word' as a separate word, not within another word |
B | Matches a non-word boundary (opposite of ). | 'BwordB' | 'word' within another word, not as a separate word |
d{ 3 } | Matches exactly three digits. | 'd{3}' | '123' , '456' , etc. |
w{ 4 } | Matches exactly four word characters. | 'w{4}' | 'word' , '1234' , but not '!@#%' |
[a - z] | Matches any lowercase alphabetic character. | '[a-z]%' | Words starting with a lowercase letter |
[A - Z] | Matches any uppercase alphabetic character. | '[A-Z]%' | Words starting with an uppercase letter |
MORE:
Here are some more wildcard characters and their descriptions:
Wildcard Character | Description | Example | Matches |
---|---|---|---|
[0 - 9] | Matches any digit from 0 to 9. | '[0-9]%' | Words starting with a digit |
[a - zA - Z] | Matches any alphabetic character (lowercase or uppercase). | '[a-zA-Z]%' | Words starting with any letter |
[^ 0 - 9] | Matches any character except digits. | '^[^0-9]' | Words that don't start with a digit |
. | Matches any single character except newline. | 'a.' | 'ab' , 'ac' , but not 'a' or 'abc' |
* | Matches zero or more occurrences of the previous character. | 'ab*' | 'a' , 'ab' , 'abb' , 'abbb' , etc. |
+ | Matches one or more occurrences of the previous character. | 'ab+' | 'ab' , 'abb' , 'abbb' , but not 'a' |
? | Matches zero or one occurrence of the previous character. | 'ab?' | 'a' , 'ab' , but not 'abb' or 'abc' |
(... ) | Groups multiple tokens together. | '(abc)+' | 'abc' , 'abcabc' , but not 'ab' or 'abcd' |
These wildcard characters, when used in combination with regular expressions or pattern matching functions in SQL queries, provide powerful tools for searching and filtering data in databases.