Skip to content

Commit

Permalink
Implement parse sql operation test
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Dec 29, 2024
1 parent b7305f8 commit 9183f16
Show file tree
Hide file tree
Showing 5 changed files with 466 additions and 137 deletions.
5 changes: 3 additions & 2 deletions src/Library/Database/Operation/MangodbOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public function __construct(string|array $operations = ["*"]){
* Exemple : `*value`
* Description : Performs a pattern match (like SQL's LIKE)
*
* @param string|array $input
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseLike(string|array $input, array $operation):mixed {
public function parseLike(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseLike($input, $operation);
Expand Down
145 changes: 121 additions & 24 deletions src/Library/Database/Operation/SqlOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
*/
class SqlOperation extends Operation {

/** Private parameters
******************************************************
*/

/** @var string $entity Table to add as prefix */
private string $entity = "";

/**
* Constructor
*
Expand All @@ -39,10 +46,12 @@ class SqlOperation extends Operation {
* @param string|array $Operation Exemple ["=", "[]"] or ["contains", "between"] or "@>" or "contains" or "*" (for all operations)
* @return self
*/
public function __construct(string|array $operations = ["*"]){
public function __construct(string|array $operations = "@all", string $entity = ""){

# Parent
parent::__construct($operations);
parent::__construct($operations, [
"prefix" => $entity ? trim($entity)."." : $entity
]);

}

Expand All @@ -58,15 +67,22 @@ public function __construct(string|array $operations = ["*"]){
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseEqual(string|array $input, array $operation):mixed {
public function parseEqual(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseEqual($input, $operation);

# Get value
$value = $parentResult["value"][1] ?? "";

# Process options
$this->_processOptions($value, $options);

# Push input in operations
$result = '= "'.$parentResult["value"].'"';
$result = "= `$value`";

# Return input
return $result;
Expand All @@ -81,15 +97,22 @@ public function parseEqual(string|array $input, array $operation):mixed {
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseNotEqual(string|array $input, array $operation):mixed {
public function parseNotEqual(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseNotEqual($input, $operation);

# Get value
$value = $parentResult["value"][1] ?? "";

# Process options
$this->_processOptions($value, $options);

# Push input in operations
$result = '<> "'.$parentResult["value"].'"';
$result = "<> `$value`";

# Return input
return $result;
Expand All @@ -104,24 +127,32 @@ public function parseNotEqual(string|array $input, array $operation):mixed {
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseLessThanOrEqual(string|array $input, array $operation):mixed {
public function parseLessThanOrEqual(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseLessThanOrEqual($input, $operation);

# Get value
$value = $parentResult["value"][1] ?? "";

# Process options
$this->_processOptions($value, $options);

# Check is numeric
if(is_numeric($parentResult["value"]))
if(is_numeric($value))

# Push input in operations
$result = '<= '.floatval($parentResult["value"]);
$result = '<= '.floatval($value);

# Error
else

# Error
throw new CrazyException(
"\"".$parentResult["value"]."\" value cannot be less than or equal...",
"\"".$parentResult["value"][1]."\" value cannot be less than or equal...",
500,
[
"custom_code" => "sqloperator-001"
Expand All @@ -141,18 +172,25 @@ public function parseLessThanOrEqual(string|array $input, array $operation):mixe
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseGreaterThanOrEqual(string|array $input, array $operation):mixed {
public function parseGreaterThanOrEqual(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseGreaterThanOrEqual($input, $operation);

# Get value
$value = $parentResult["value"][1] ?? "";

# Process options
$this->_processOptions($value, $options);

# Check is numeric
if(is_numeric($parentResult["value"]))
if(is_numeric($value))

# Push input in operations
$result = '>= '.floatval($parentResult["value"]);
$result = '>= '.floatval($value);

else

Expand All @@ -178,18 +216,25 @@ public function parseGreaterThanOrEqual(string|array $input, array $operation):m
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseSmaller(string|array $input, array $operation):mixed {
public function parseSmaller(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseSmaller($input, $operation);

# Get value
$value = $parentResult["value"][1] ?? "";

# Process options
$this->_processOptions($value, $options);

# Check is numeric
if(is_numeric($parentResult["value"]))
if(is_numeric($value))

# Push input in operations
$result = '< '.floatval($parentResult["value"]);
$result = '< '.floatval($value);

else

Expand All @@ -215,18 +260,25 @@ public function parseSmaller(string|array $input, array $operation):mixed {
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseGreater(string|array $input, array $operation):mixed {
public function parseGreater(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseGreater($input, $operation);

# Get value
$value = $parentResult["value"][1] ?? "";

# Process options
$this->_processOptions($value, $options);

# Check is numeric
if(is_numeric($parentResult["value"]))
if(is_numeric($value))

# Push input in operations
$result = '> '.floatval($parentResult["value"]);
$result = '> '.floatval($value);

else

Expand All @@ -252,15 +304,31 @@ public function parseGreater(string|array $input, array $operation):mixed {
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseNotBetween(string|array $input, array $operation):mixed {
public function parseNotBetween(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseNotBetween($input, $operation);

# Get value A
$valueA = $parentResult["value"][2];

# Get value B
$valueA = $parentResult["value"][3];

# Process options on value A
$this->_processOptions($valueA, $options);

# Process options on value B
$this->_processOptions($valueB, $options);

# Push input in operations
$result = 'NOT BETWEEN '.$parentResult[0].' AND '.$parentResult[1];
$result = is_numeric($valueA) && is_numeric($valueB)
? "NOT BETWEEN ".floatval($valueA)." AND ".floatval($valueB)
: "NOT BETWEEN `$valueA` AND `$valueB`"
;

# Return input
return $result;
Expand All @@ -275,9 +343,10 @@ public function parseNotBetween(string|array $input, array $operation):mixed {
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseBetween(string|array $input, array $operation):mixed {
public function parseBetween(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseBetween($input, $operation);
Expand All @@ -297,9 +366,10 @@ public function parseBetween(string|array $input, array $operation):mixed {
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseDefault(string|array $input):mixed {
public function parseDefault(string|array $input, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseDefault($input);
Expand All @@ -320,9 +390,10 @@ public function parseDefault(string|array $input):mixed {
*
* @param string|array $input
* @param array $operation
* @param array $options
* @return mixed
*/
public function parseLike(string|array $input, array $operation):mixed {
public function parseLike(string|array $input, array $operation, array $options = []):mixed {

# Set result of parent
$parentResult = parent::parseLike($input, $operation);
Expand Down Expand Up @@ -352,4 +423,30 @@ public function parseLike(string|array $input, array $operation):mixed {

}

/** Private parameters
******************************************************
*/

/**
* Process Options
*
* @param array &$result
* @param array $options
*/
private function _processOptions(&$result, $options):void {

# Check prefix
if($options["prefix"] ?? false)

# Set prefix in result
$result = $options["prefix"].$result;

# Check suffix
if($options["suffix"] ?? false)

# Set prefix in result
$result = $result.$options["suffix"];

}

}
Loading

0 comments on commit 9183f16

Please sign in to comment.