Skip to content

Commit

Permalink
Merge pull request #5 from sroehrl/port-support
Browse files Browse the repository at this point in the history
Port support & OOP wrapper
  • Loading branch information
sroehrl authored Sep 26, 2020
2 parents b2859d0 + bacbe86 commit f2ef387
Show file tree
Hide file tree
Showing 9 changed files with 2,083 additions and 98 deletions.
10 changes: 6 additions & 4 deletions Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ private static function handleConditions($conditionArray)
public static function handleResults($qStr)
{
self::init();
if (defined('db_hard_debug')) {
if (self::$_env->get('debug')) {
$exclusions = self::$_ops->getExclusions();
self::$_ops->clearExclusions();
return [
'sql' => $qStr,
'exclusions' => self::$_ops->getExclusions()
'exclusions' => $exclusions
];
}
$result = 0;
Expand Down Expand Up @@ -417,7 +419,7 @@ private static function connect()
try {
self::$_db =
new mysqli(self::$_env->get('host'), self::$_env->get('user'), self::$_env->get('password'),
self::$_env->get('name'));
self::$_env->get('name'), self::$_env->get('port'));
} catch (mysqli_sql_exception $e) {
self::$_ops->formatError(['****'], 'Check defines! Can\'t connect to db');
}
Expand Down Expand Up @@ -626,7 +628,7 @@ private static function deprecationWarning()
*/
public static function debug()
{
define('db_hard_debug', true);
self::$_env->set('debug', true);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions DbEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function __construct()
'db_charset' => defined('db_charset') ? db_charset : 'utf8mb4',
'db_filter_characters' => defined('db_filter_characters') ? db_filter_characters : '/[^a-zA-Z\_\^\.\s\*]/',
'db_casing' => defined('db_casing') ? db_casing : 'snake',
'db_port' => defined('db_port') ? db_port : 3306,
'db_debug' => defined('db_debug') ? db_debug : false
];
return $this;
}
Expand Down
32 changes: 32 additions & 0 deletions DbOOP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php


namespace Neoan3\Apps;


class DbOOP
{
function __construct($environmentVariables = [])
{
if(!empty($environmentVariables)){
$this->setEnvironment($environmentVariables);
}
}

/**
* @param $vars
* @throws DbException
*/
function setEnvironment($vars)
{
Db::setEnvironment($vars);
}
function easy($selectString, $conditions = [], $callFunctions = [])
{
return Db::easy($selectString, $conditions, $callFunctions);
}
function smart($tableOrString, $conditions = null, $callFunctions = null)
{
return Db::ask($tableOrString, $conditions, $callFunctions);
}
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ try {
| db_file_location | folder of SQL-files relative to app_root | 'component' |
| db_filter_characters | filters table-names & array-keys | '/[^a-zA-Z\_\\^\\.\s\*]/' |
| db_casing | *camel* or *snake* for column names | 'snake' |
| db_port | (int) port number | 3306 |
| db_debug | When set to true, SQL is not executed | false |
| db_dev_errors | When set to true, error reporting exposes handled values | false |

Environment variables can either be set as global constants or using Db::setEnvironment()

Expand Down Expand Up @@ -253,6 +256,17 @@ This means that foreign keys must be in the format "master"_id.
Db::easy('user.first_name user.last_name user_email.email user_password.confirm_date');
```

## OOP & Testing
Although you want to write your own wrapper depending on the Interface your framework uses,
this library comes with a simple wrapper out of the box to encourage dependency injection usage.

```PHP
$environment = ['name' => 'my_db'];
$database = new \Neoan3\Apps\DbOOP($environment);
$database->easy('user.*', ['^delete_date']); //executes & returns Db::easy
$database->smart('user', ['name'=>'sam', 'user_type'=>'admin']); //executes & returns Db::ask

```

## Heads up
The general approach of the db-app has been applied for years. While the difference to common wrappers for mysqli of pdo seems rather big,
Expand Down
8 changes: 4 additions & 4 deletions UuidHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ function __construct($ops)
*/
public function newUuid()
{
$q = Db::query('SELECT REPLACE(UUID(),"-","") as id');

while ($row = $q['result']->fetch_assoc()) {
$this->uuid = strtoupper($row['id']);
$q = Db::query('SELECT UPPER(REPLACE(UUID(),"-","")) as id');
while ($row = $q['result']->fetch_object()) {
$this->uuid = $row->id;
}

return $this;
}

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "neoan3-apps/db",
"description": "neoan3 mysqli class",
"version": "0.2.1",
"version": "0.3.0",
"license": "MIT",
"require": {
"php": "^7.0",
"ext-mysqli": "*",
"ext-ctype": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.3"
},
"authors": [
{
"name": "Stefan Roehrl",
Expand Down
Loading

0 comments on commit f2ef387

Please sign in to comment.