drop DBHelper

This commit is contained in:
Sergey Tsalkov
2021-06-27 05:35:28 +00:00
parent a10b76b2de
commit d944430401
3 changed files with 0 additions and 103 deletions

View File

@@ -1072,56 +1072,6 @@ class MeekroDBException extends Exception {
public function getQuery() { return $this->query; }
}
class DBHelper {
/*
verticalSlice
1. For an array of assoc rays, return an array of values for a particular key
2. if $keyfield is given, same as above but use that hash key as the key in new array
*/
public static function verticalSlice($array, $field, $keyfield = null) {
$array = (array) $array;
$R = array();
foreach ($array as $obj) {
if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n");
if ($keyfield) {
if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n");
$R[$obj[$keyfield]] = $obj[$field];
} else {
$R[] = $obj[$field];
}
}
return $R;
}
/*
reIndex
For an array of assoc rays, return a new array of assoc rays using a certain field for keys
*/
public static function reIndex() {
$fields = func_get_args();
$array = array_shift($fields);
$array = (array) $array;
$R = array();
foreach ($array as $obj) {
$target =& $R;
foreach ($fields as $field) {
if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n");
$nextkey = $obj[$field];
$target =& $target[$nextkey];
}
$target = $obj;
}
return $R;
}
}
class MeekroDBEval {
public $text = '';

View File

@@ -1,51 +0,0 @@
<?php
class HelperTest extends SimpleTest {
function test_1_verticalslice() {
$all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
$names = DBHelper::verticalSlice($all, 'username');
$this->assert(count($names) === 5);
$this->assert($names[0] === 'Abe');
$ages = DBHelper::verticalSlice($all, 'age', 'username');
$this->assert(count($ages) === 5);
$this->assert($ages['Abe'] === '700');
}
function test_2_reindex() {
$all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
$names = DBHelper::reIndex($all, 'username');
$this->assert(count($names) === 5);
$this->assert($names['Bart']['username'] === 'Bart');
$this->assert($names['Bart']['age'] === '15');
$names = DBHelper::reIndex($all, 'username', 'age');
$this->assert($names['Bart']['15']['username'] === 'Bart');
$this->assert($names['Bart']['15']['age'] === '15');
}
function test_3_empty() {
$none = DB::query("SELECT * FROM accounts WHERE username=%s", 'doesnotexist');
$this->assert(is_array($none) && count($none) === 0);
$names = DBHelper::verticalSlice($none, 'username', 'age');
$this->assert(is_array($names) && count($names) === 0);
$names_other = DBHelper::reIndex($none, 'username', 'age');
$this->assert(is_array($names_other) && count($names_other) === 0);
}
function test_4_null() {
DB::query("UPDATE accounts SET password = NULL WHERE username=%s", 'Bart');
$all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
$ages = DBHelper::verticalSlice($all, 'age', 'password');
$this->assert(count($ages) === 5);
$this->assert($ages[''] === '15');
$passwords = DBHelper::reIndex($all, 'password');
$this->assert(count($passwords) === 5);
$this->assert($passwords['']['username'] === 'Bart');
$this->assert($passwords['']['password'] === NULL);
}
}
?>

View File

@@ -37,7 +37,6 @@ require_once __DIR__ . '/ObjectTest.php';
require_once __DIR__ . '/WhereClauseTest.php';
require_once __DIR__ . '/HookTest.php';
require_once __DIR__ . '/TransactionTest.php';
require_once __DIR__ . '/HelperTest.php';
$classes_to_test = array(
'BasicTest',
@@ -47,7 +46,6 @@ $classes_to_test = array(
'ObjectTest',
'HookTest',
'TransactionTest',
'HelperTest',
);
$mysql_version = DB::serverVersion();