From 50fd6ed6d23fae9d12aff0ace521e53d82bae8a3 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Wed, 23 Jun 2021 01:24:16 +0000 Subject: [PATCH] actually bring back deprecated methods, keep them for now to maintain backwards compatability --- db.class.php | 39 +++++++++++++++++++++++++++++++++++++++ simpletest/BasicTest.php | 20 +++++++++++++++----- simpletest/ObjectTest.php | 16 +++++++++++++--- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/db.class.php b/db.class.php index c5cd080..0a3b54c 100644 --- a/db.class.php +++ b/db.class.php @@ -861,6 +861,7 @@ class MeekroDB { return $return; } + public function queryFirstRow() { $args = func_get_args(); $result = call_user_func_array(array($this, 'query'), $args); @@ -868,6 +869,7 @@ class MeekroDB { return reset($result); } + public function queryFirstList() { $args = func_get_args(); $result = call_user_func_array(array($this, 'queryAllLists'), $args); @@ -894,8 +896,45 @@ class MeekroDB { $row = call_user_func_array(array($this, 'queryFirstList'), $args); if ($row == null) return null; return $row[0]; + } + + // --- begin deprecated methods (kept for backwards compatability) + public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); } + public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); } + + public function queryOneField() { + $args = func_get_args(); + $column = array_shift($args); + + $row = call_user_func_array(array($this, 'queryOneRow'), $args); + if ($row == null) { + return null; + } else if ($column === null) { + $keys = array_keys($row); + $column = $keys[0]; + } + + return $row[$column]; } + public function queryOneColumn() { + $args = func_get_args(); + $column = array_shift($args); + $results = call_user_func_array(array($this, 'query'), $args); + $ret = array(); + + if (!count($results) || !count($results[0])) return $ret; + if ($column === null) { + $keys = array_keys($results[0]); + $column = $keys[0]; + } + + foreach ($results as $row) { + $ret[] = $row[$column]; + } + + return $ret; + } } diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index 016edd6..7da665d 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -40,8 +40,14 @@ class BasicTest extends SimpleTest { $field = DB::queryFirstField("SELECT * FROM accounts"); $this->assert($field === null); + $field = DB::queryOneField('nothere', "SELECT * FROM accounts"); + $this->assert($field === null); + $column = DB::queryFirstColumn("SELECT * FROM accounts"); $this->assert(is_array($column) && count($column) === 0); + + $column = DB::queryOneColumn('nothere', "SELECT * FROM accounts"); //TODO: is this what we want? + $this->assert(is_array($column) && count($column) === 0); } function test_2_insert_row() { @@ -107,12 +113,16 @@ class BasicTest extends SimpleTest { array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend'); $this->assert($charlie_password === 'goodbye'); + $charlie_password = DB::queryOneField('password', "SELECT * FROM accounts WHERE username IN %ls AND username = %s", + array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend'); + $this->assert($charlie_password === 'goodbye'); + $passwords = DB::queryFirstColumn("SELECT password FROM accounts WHERE username=%s", 'Bart'); $this->assert(count($passwords) === 1); $this->assert($passwords[0] === 'hello'); $username = $password = $age = null; - list($age, $username, $password) = DB::queryFirstList("SELECT age,username,password FROM accounts WHERE username=%s", 'Bart'); + list($age, $username, $password) = DB::queryOneList("SELECT age,username,password FROM accounts WHERE username=%s", 'Bart'); $this->assert($username === 'Bart'); $this->assert($password === 'hello'); $this->assert($age == 15); @@ -162,7 +172,7 @@ class BasicTest extends SimpleTest { 'height' => 111.15 )); - $row = DB::queryFirstRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah'); + $row = DB::queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah'); $this->assert($row['username'] === 'newguy'); $this->assert($row['age'] === '172'); @@ -172,7 +182,7 @@ class BasicTest extends SimpleTest { ), 'username=%s_name', array('name' => 'newguy')); $row = null; - $row = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'newguy'); + $row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy'); $this->assert($true === true); $this->assert($row['password'] === 'blahblahblahblah'); $this->assert($row['favorite_word'] === null); @@ -246,7 +256,7 @@ class BasicTest extends SimpleTest { $this->assert($rows[1]['password'] === 'somethingelse'); $this->assert($rows[1]['username'] === '2ofmany'); - $nullrow = DB::queryFirstRow("SELECT * FROM accounts WHERE username LIKE %ss", '3ofman'); + $nullrow = DB::queryOneRow("SELECT * FROM accounts WHERE username LIKE %ss", '3ofman'); $this->assert($nullrow['password'] === NULL); $this->assert($nullrow['age'] === '15'); } @@ -268,7 +278,7 @@ class BasicTest extends SimpleTest { DB::insert('store data', array( 'picture' => $smile, )); - DB::queryFirstRow("INSERT INTO %b (picture) VALUES (%s)", 'store data', $smile); + DB::queryOneRow("INSERT INTO %b (picture) VALUES (%s)", 'store data', $smile); $getsmile = DB::queryFirstField("SELECT picture FROM %b WHERE id=1", 'store data'); $getsmile2 = DB::queryFirstField("SELECT picture FROM %b WHERE id=2", 'store data'); diff --git a/simpletest/ObjectTest.php b/simpletest/ObjectTest.php index df12c46..6f7b61e 100644 --- a/simpletest/ObjectTest.php +++ b/simpletest/ObjectTest.php @@ -32,8 +32,14 @@ class ObjectTest extends SimpleTest { $field = $this->mdb->queryFirstField("SELECT * FROM accounts"); $this->assert($field === null); + $field = $this->mdb->queryOneField('nothere', "SELECT * FROM accounts"); + $this->assert($field === null); + $column = $this->mdb->queryFirstColumn("SELECT * FROM accounts"); $this->assert(is_array($column) && count($column) === 0); + + $column = $this->mdb->queryOneColumn('nothere', "SELECT * FROM accounts"); //TODO: is this what we want? + $this->assert(is_array($column) && count($column) === 0); } function test_2_insert_row() { @@ -82,12 +88,16 @@ class ObjectTest extends SimpleTest { array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend'); $this->assert($charlie_password === 'goodbye'); + $charlie_password = $this->mdb->queryOneField('password', "SELECT * FROM accounts WHERE username IN %ls AND username = %s", + array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend'); + $this->assert($charlie_password === 'goodbye'); + $passwords = $this->mdb->queryFirstColumn("SELECT password FROM accounts WHERE username=%s", 'Bart'); $this->assert(count($passwords) === 1); $this->assert($passwords[0] === 'hello'); $username = $password = $age = null; - list($age, $username, $password) = $this->mdb->queryFirstList("SELECT age,username,password FROM accounts WHERE username=%s", 'Bart'); + list($age, $username, $password) = $this->mdb->queryOneList("SELECT age,username,password FROM accounts WHERE username=%s", 'Bart'); $this->assert($username === 'Bart'); $this->assert($password === 'hello'); $this->assert($age == 15); @@ -132,7 +142,7 @@ class ObjectTest extends SimpleTest { 'height' => 111.15 )); - $row = $this->mdb->queryFirstRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah'); + $row = $this->mdb->queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah'); $this->assert($row['username'] === 'newguy'); $this->assert($row['age'] === '172'); @@ -142,7 +152,7 @@ class ObjectTest extends SimpleTest { ), 'username=%s', 'newguy'); $row = null; - $row = $this->mdb->queryFirstRow("SELECT * FROM accounts WHERE username=%s", 'newguy'); + $row = $this->mdb->queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy'); $this->assert($row['password'] === 'blahblahblahblah'); $this->assert($row['favorite_word'] === null);