From bdc8790368e2f8b247c8492507d4083ddfbd61c1 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 00:39:54 -0500 Subject: + Added generic database interface to use throughout the app since I'm planning on supporting multiple database engines + Defined interface for Mysql DBMS for Scrott --- app/class/mysql.class.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 app/class/mysql.class.php (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php new file mode 100644 index 0000000..b08257f --- /dev/null +++ b/app/class/mysql.class.php @@ -0,0 +1,63 @@ +db = new mysqli($host, $username, $password, $dbName); + + if ($this->db->connect_error) + throw new Exception("Can not connect to Mysql database. Please check your Scrott configuration."); + } + + /* + * Destructor + */ + function __destruct() + { + $this->close(); + } + + /* + * Close connection to DB + */ + function close() + { + $this->db->close(); + } + + /* + * Make a query of the database. Return data as an array of arrays + */ + function query($query) + { + $arr = array(); + $res = $this->db->query($query); + + if ($res === true || $res === false) + return $arr; + + foreach ($res as $r) + $arr[] = $r->fetch_assoc(); + + return $arr; + } + + /* + * Escape a string for use in a query + */ + function esc($string) + { + return $this->db->real_escape_string($string); + } +} + +?> -- cgit v1.2.3 From 0f9b65d812b601c5e047838b07b96098cbe8ad35 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 13:21:49 -0500 Subject: * Bug fix in Mysql support class -- misuse of Mysql result object and its member function fetch_assoc --- app/class/mysql.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php index b08257f..317468c 100644 --- a/app/class/mysql.class.php +++ b/app/class/mysql.class.php @@ -45,8 +45,8 @@ class Mysql implements Database if ($res === true || $res === false) return $arr; - foreach ($res as $r) - $arr[] = $r->fetch_assoc(); + while ($r = $res->fetch_assoc()) + $arr[] = $r; return $arr; } -- cgit v1.2.3 From ec7186ed4e1c2a41ff9052cdd1624b8cabbb047c Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 26 May 2016 23:46:22 -0400 Subject: Add copyright notice to Scrott class files --- app/class/mysql.class.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php index 317468c..f8f456a 100644 --- a/app/class/mysql.class.php +++ b/app/class/mysql.class.php @@ -1,5 +1,19 @@ Date: Sat, 22 Oct 2016 00:29:30 -0400 Subject: Deprecate application code Setup to perform an iteration of development focused on a simpler implementation and eliminating redundancy in design. --- app/class/mysql.class.php | 77 ----------------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 app/class/mysql.class.php (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php deleted file mode 100644 index f8f456a..0000000 --- a/app/class/mysql.class.php +++ /dev/null @@ -1,77 +0,0 @@ -db = new mysqli($host, $username, $password, $dbName); - - if ($this->db->connect_error) - throw new Exception("Can not connect to Mysql database. Please check your Scrott configuration."); - } - - /* - * Destructor - */ - function __destruct() - { - $this->close(); - } - - /* - * Close connection to DB - */ - function close() - { - $this->db->close(); - } - - /* - * Make a query of the database. Return data as an array of arrays - */ - function query($query) - { - $arr = array(); - $res = $this->db->query($query); - - if ($res === true || $res === false) - return $arr; - - while ($r = $res->fetch_assoc()) - $arr[] = $r; - - return $arr; - } - - /* - * Escape a string for use in a query - */ - function esc($string) - { - return $this->db->real_escape_string($string); - } -} - -?> -- cgit v1.2.3 From b6f82bb6552517d8bc442a2087c6c37a33bd18bd Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 23 Oct 2016 19:01:52 -0400 Subject: Add mysql class --- app/class/mysql.class.php | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 app/class/mysql.class.php (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php new file mode 100644 index 0000000..90a4016 --- /dev/null +++ b/app/class/mysql.class.php @@ -0,0 +1,74 @@ +db = new mysqli($host, $username, $password, $dbName); + + if ($this->db->connect_error) + throw new Exception("Can not connect to MySQL database. Please check your configuration."); + } + + /* + * Destructor + */ + public function __destruct() + { + $this->close(); + } + + /* + * Close connection to DB + */ + public function close() + { + $this->db->close(); + } + + /* + * Make a query of the database. Return data as an array of arrays. + */ + public function query(string $query) : array + { + $arr = array(); + $res = $this->db->query($query); + + if ($res === true || $res === false) + return $arr; + + while (($arr[] = $res->fetch_assoc())); + return $arr; + } + + /* + * Escape a string for use in a query + */ + public function esc(string $str) : string + { + return $this->db->real_escape_string($str); + } +} + +?> -- cgit v1.2.3 From dae3964e7682dcd0d64075dfc28a23c12ef6c52e Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 14 Jan 2017 02:26:28 -0500 Subject: Reset working directory for clean Scrott implementation --- app/class/mysql.class.php | 74 ----------------------------------------------- 1 file changed, 74 deletions(-) delete mode 100644 app/class/mysql.class.php (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php deleted file mode 100644 index 90a4016..0000000 --- a/app/class/mysql.class.php +++ /dev/null @@ -1,74 +0,0 @@ -db = new mysqli($host, $username, $password, $dbName); - - if ($this->db->connect_error) - throw new Exception("Can not connect to MySQL database. Please check your configuration."); - } - - /* - * Destructor - */ - public function __destruct() - { - $this->close(); - } - - /* - * Close connection to DB - */ - public function close() - { - $this->db->close(); - } - - /* - * Make a query of the database. Return data as an array of arrays. - */ - public function query(string $query) : array - { - $arr = array(); - $res = $this->db->query($query); - - if ($res === true || $res === false) - return $arr; - - while (($arr[] = $res->fetch_assoc())); - return $arr; - } - - /* - * Escape a string for use in a query - */ - public function esc(string $str) : string - { - return $this->db->real_escape_string($str); - } -} - -?> -- cgit v1.2.3 From 92491a36a3f5f02d11b2aa9bf8f3c1418af6e558 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 15 Jan 2017 20:29:14 -0500 Subject: Add mysql class --- app/class/mysql.class.php | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 app/class/mysql.class.php (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php new file mode 100644 index 0000000..fae09bb --- /dev/null +++ b/app/class/mysql.class.php @@ -0,0 +1,68 @@ +db = new mysqli($host, $uname, $passwd, $dbname); + + if ($this->db->connect_error) + throw new Exception("Can not connect to MySQL database. Please check your configuration."); + } + + /* + * Close connection to DB + */ + public function close() : void + { + $this->db->close(); + } + + /* + * Make a query of the database. Return data as an array of arrays. + */ + public function query(string $query) : array + { + $arr = array(); + $res = $this->db->query($query); + + if ($res === true || $res === false) + return $arr; + + while (($arr[] = $res->fetch_assoc())); + return $arr; + } + + /* + * Escape a string for use in a query + */ + public function esc(string $str) : string + { + return $this->db->real_escape_string($str); + } +} + +?> -- cgit v1.2.3 From 27cfca44b3c43c8bbd3ea5ac602e1f34753e1e90 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 5 Feb 2017 02:33:55 -0500 Subject: Fix bug in mysql query function Need to remove the NULL return value from fetch_assoc() to fix usages of count($res) for determining the number of results from a MySQL query. This NULL return value is returned from mysqli's fetch_assoc() function to signal no more result rows. --- app/class/mysql.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php index fae09bb..db0eb7d 100644 --- a/app/class/mysql.class.php +++ b/app/class/mysql.class.php @@ -53,7 +53,7 @@ class mysql extends database return $arr; while (($arr[] = $res->fetch_assoc())); - return $arr; + return array_filter($arr, function ($val) { return !is_null($val); }); } /* -- cgit v1.2.3 From 56a6dda13bb85b25f590fc8a64535bb53c3c2fd2 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Mon, 6 Feb 2017 00:47:55 -0500 Subject: Update database API The abstract functions of database have been made protected and their names prefixed with '_'. The database class has been given new static functions query() and esc(), which call the _query() and _esc() function from the database instance object. This change was made to address the use of db routines from static contexes. Calls like `database::get()->query()` which mix static and instance function access operators, can now be `database::query()`, and all singleton is abstracted away; the instance's destructor continues to close the db connection. --- app/class/mysql.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/class/mysql.class.php') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php index db0eb7d..57a9819 100644 --- a/app/class/mysql.class.php +++ b/app/class/mysql.class.php @@ -36,7 +36,7 @@ class mysql extends database /* * Close connection to DB */ - public function close() : void + protected function _close() : void { $this->db->close(); } @@ -44,7 +44,7 @@ class mysql extends database /* * Make a query of the database. Return data as an array of arrays. */ - public function query(string $query) : array + protected function _query(string $query) : array { $arr = array(); $res = $this->db->query($query); @@ -59,7 +59,7 @@ class mysql extends database /* * Escape a string for use in a query */ - public function esc(string $str) : string + protected function _esc(string $str) : string { return $this->db->real_escape_string($str); } -- cgit v1.2.3