Enhance IDb.php, pdo.php, and mysql.db.php with deleteRowById method for improved database handling.
This commit is contained in:
10
core/a/mysql.db.php
Normal file → Executable file
10
core/a/mysql.db.php
Normal file → Executable file
@@ -169,5 +169,13 @@ abstract class Db implements IDb
|
|||||||
return Pdo::fetchRowInArrayByWhere(self::$table['table'], $field['field'], $field['value']);
|
return Pdo::fetchRowInArrayByWhere(self::$table['table'], $field['field'], $field['value']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @desc Deletes a row from the database table by id.
|
||||||
|
* @param int $id The id of the row to delete.
|
||||||
|
* @return bool Returns true if the deletion is successful, otherwise false.
|
||||||
|
*/
|
||||||
|
public function deleteRowById(int $id = 0): bool
|
||||||
|
{
|
||||||
|
Pdo::deleteRowById( self::getTable()['table'], $id );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ namespace Nibiru;
|
|||||||
* @category - [PLEASE SPECIFIY]
|
* @category - [PLEASE SPECIFIY]
|
||||||
* @license - BSD License
|
* @license - BSD License
|
||||||
*/
|
*/
|
||||||
final class pdo extends Mysql implements IPdo
|
final class Pdo extends Mysql implements IPdo
|
||||||
{
|
{
|
||||||
private static $section = false;
|
private static $section = false;
|
||||||
|
|
||||||
@@ -56,10 +56,9 @@ final class pdo extends Mysql implements IPdo
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $string
|
* @param string $string
|
||||||
*
|
* @return mixed
|
||||||
* @return array|bool
|
|
||||||
*/
|
*/
|
||||||
public static function query( $string = self::PLACE_NO_QUERY ): array|bool
|
public static function query( $string = self::PLACE_NO_QUERY ): mixed
|
||||||
{
|
{
|
||||||
|
|
||||||
if(!strstr($string, IOdbc::PLACE_SQL_UPDATE))
|
if(!strstr($string, IOdbc::PLACE_SQL_UPDATE))
|
||||||
@@ -413,6 +412,63 @@ final class pdo extends Mysql implements IPdo
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @desc Deletes a row from the specified table by its ID.
|
||||||
|
* @param string $tablename The name of the table from which to delete the row. If empty, uses the default table.
|
||||||
|
* @param int $id The ID of the row to delete.
|
||||||
|
* @return bool Returns true if the deletion was successful, false otherwise.
|
||||||
|
*/
|
||||||
|
public static function deleteRowById(string $tablename = '', int $id = 0): bool
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// Validate that id is a valid number
|
||||||
|
if (!is_numeric($id) || $id <= 0)
|
||||||
|
{
|
||||||
|
throw new \InvalidArgumentException("FATAL ERROR in main CORE deleteRowById: Invalid ID value. Must be a positive number.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate table name
|
||||||
|
$validTables = self::loadTableNames();
|
||||||
|
if (!in_array($tablename, $validTables, true))
|
||||||
|
{
|
||||||
|
throw new \InvalidArgumentException("FATAL ERROR in main CORE deleteRowById: Invalid table name: {$tablename}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get PDO instance
|
||||||
|
$pdo = parent::getInstance(self::getSettingsSection())->getConn();
|
||||||
|
|
||||||
|
// Fetch the primary key field name
|
||||||
|
$queryPrimaryKey = "SELECT COLUMN_NAME FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_NAME = :tableName
|
||||||
|
AND COLUMN_KEY = 'PRI'
|
||||||
|
LIMIT 1";
|
||||||
|
$stmtPrimaryKey = $pdo->prepare($queryPrimaryKey);
|
||||||
|
$stmtPrimaryKey->bindValue(':tableName', $tablename);
|
||||||
|
$stmtPrimaryKey->execute();
|
||||||
|
$primaryKeyResult = $stmtPrimaryKey->fetch(\PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
if (!$primaryKeyResult) {
|
||||||
|
throw new \RuntimeException('FATAL ERROR in main CORE deleteRowById: No primary key found for table ' . $tablename);
|
||||||
|
}
|
||||||
|
|
||||||
|
$primaryKeyField = $primaryKeyResult['COLUMN_NAME'];
|
||||||
|
|
||||||
|
// Prepare and execute DELETE statement
|
||||||
|
$query = "DELETE FROM " . $tablename . " WHERE " . $primaryKeyField . " = :id";
|
||||||
|
$stmt = $pdo->prepare($query);
|
||||||
|
$stmt->bindValue(':id', $id, \PDO::PARAM_INT);
|
||||||
|
|
||||||
|
return $stmt->execute();
|
||||||
|
} catch (\PDOException $e) {
|
||||||
|
error_log($e->getMessage());
|
||||||
|
return false;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
error_log($e->getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @desc will insert the array with fieldnames into the database, if the last parameter is set it should be a string containing the
|
* @desc will insert the array with fieldnames into the database, if the last parameter is set it should be a string containing the
|
||||||
* fieldname that should be encrypted
|
* fieldname that should be encrypted
|
||||||
|
|||||||
@@ -100,4 +100,11 @@ interface IDb
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function loadPasswordByUsername( $user_name = false );
|
public function loadPasswordByUsername( $user_name = false );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @desc Deletes a row from the database by its ID.
|
||||||
|
* @param int $id The ID of the row to be deleted. Defaults to 0.
|
||||||
|
* @return bool Returns true if the deletion was successful, false otherwise.
|
||||||
|
*/
|
||||||
|
public function deleteRowById( int $id = 0 ): bool;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user