Skip to content

Commit

Permalink
Merge pull request #11 from sroehrl/release/0.3.6
Browse files Browse the repository at this point in the history
better null/integer handling
  • Loading branch information
sroehrl authored Oct 26, 2022
2 parents a4e4764 + 07b13e8 commit 3eecddd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
Empty file added .gitignore
Empty file.
2 changes: 1 addition & 1 deletion Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ private static function smartInsert($table, $fields)
* @return string
* @throws DbException
*/
private static function setFields($fields)
private static function setFields($fields): string
{
$fieldsString = '';
$i = 0;
Expand Down
50 changes: 30 additions & 20 deletions DbOps.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,40 @@ private function prepareBinding($value, $type): array
/**
* EASY markup interpretation to influence conditions-behavior
*
* @param $string
* @param $input
* @param bool $set
* @param bool $prepared
*
* @return array|bool|string
* @return mixed
* @throws DbException
*/
protected function operandi($string, bool $set, bool $prepared)
protected function operandi($input, bool $set, bool $prepared)
{
if (empty($string) && $string !== "0") {
return ($set ? ' = NULL' : ' IS NULL');
switch (gettype($input)){
case 'integer':
return $input;
case 'NULL':
return $this->setNull($input, $set);
}

$firstLetter = strtolower(substr($string, 0, 1));
if (empty($input)) {
return $this->setNull($input, $set);
}
$firstLetter = strtolower(substr($input, 0, 1));
switch ($firstLetter) {
case '=':
// important! this is the first rule and needs to stay as such!
$return = ' = ? ';
$this->addExclusion(substr($string, 1));
$this->addExclusion(substr($input, 1));
break;
case '>':
case '<':
$return = ' ' . $firstLetter . ' "' . intval(substr($string, 1)) . '"';
$return = ' ' . $firstLetter . ' "' . intval(substr($input, 1)) . '"';
break;
case '.':
$return = ' = NOW()';
break;
case '$':
$rest = substr($string, 1);
$rest = substr($input, 1);
if ($prepared) {
$return = ' = UNHEX(?)';
$this->addExclusion($rest, 's');
Expand All @@ -113,39 +118,44 @@ protected function operandi($string, bool $set, bool $prepared)
}
break;
case '!':
if (strtolower($string) == '!null' || strlen($string) == 1) {
if (strtolower($input) == '!null' || strlen($input) == 1) {
if ($set) {
$this->formatError([$string],
'Cannot set "NOT NULL" as value for "' . substr($string, 1) . '"');
$this->formatError([$input],
'Cannot set "NOT NULL" as value for "' . substr($input, 1) . '"');
}
$return = ' IS NOT NULL ';
} else {
if ($set) {
$this->formatError([$string], 'Cannot use "!= ' . substr($string, 1) . '" to set a value');
$this->formatError([$input], 'Cannot use "!= ' . substr($input, 1) . '" to set a value');
}
$return = ' != "' . substr($string, 1) . '"';
$return = ' != "' . substr($input, 1) . '"';
}
break;
case '{':
$return = ' ' . substr($string, 1, -1);
$return = ' ' . substr($input, 1, -1);
break;
case '^':
$return = ($set ? ' = NULL' : ' IS NULL');
break;
default:
if (strtolower($string) == 'null') {
if (strtolower($input) == 'null') {
$return = ($set ? ' = NULL' : ' IS NULL');
} elseif ($prepared) {
$return = preg_match('/%/', $string) ? ' LIKE ? ' : ' = ? ';
$this->addExclusion($string);
$return = preg_match('/%/', $input) ? ' LIKE ? ' : ' = ? ';
$this->addExclusion($input);
} else {
$return = ' = "' . $string . '"';
$return = ' = "' . $input . '"';
}
break;
}
return $return;
}

protected function setNull($value, bool $set): string
{
return ($set ? ' = NULL' : ' IS NULL');
}

/**
* EASY markup interpretation to influence select-behavior
*
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "neoan3-apps/db",
"description": "neoan3 mysqli class",
"version": "0.3.6",
"license": "MIT",
"require": {
"php": "^7.4||^8",
"ext-mysqli": "*",
"ext-ctype": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.5"
},
"authors": [
{
Expand Down
56 changes: 29 additions & 27 deletions test/test.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
<?php
namespace Test;
define('path',dirname(dirname(__FILE__)));
define('db_host','localhost');
define('db_name', 'test');
define('db_user','root');
define('db_password','');

//optional
define('db_assumes_uuid',true);
define('db_dev_errors', true);

require_once path . '/DbOps.php';
require_once path . '/Db.php';
require_once path . '/UuidHandler.php';
require_once path . '/Deprecated.php';
require_once path . '/DbException.php';
require_once path . '/DbEnvironment.php';
require_once path . '/Db.php';

use Neoan3\Apps\Db;
use Neoan3\Apps\DbException;
use Neoan3\Apps\DbOps;
use Neoan3\Apps\DbOOP;

try {
$id = Db::uuid();
} catch (DbException $e) {
var_dump($e->getMessage());
die();
}
require_once '../vendor/autoload.php';

const path = __DIR__;

/*Db::setEnvironment([
'host' => 'localhost',
'assumes_uuid' => true,
'name' => 'test',
'app_root' => __DIR__
]);*/

$db = new DbOOP([
'host' => 'localhost',
'assumes_uuid' => true,
'name' => 'test',
'app_root' => __DIR__
]);
// insert
Db::debug();
$test = $db->smart('user',[
'email'=>'adam@mail.com',
'some' => 0,
'last' => null,
'empty' => ''
]);
//$test = $db->easy('user.*',['^password']);


var_dump($test);

var_dump($id->uuid);
die();

0 comments on commit 3eecddd

Please sign in to comment.