2022-11-27 07:31:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.Component.Builder
|
|
|
|
*
|
2022-12-04 09:23:43 +00:00
|
|
|
* @created 4th September, 2022
|
2022-11-27 07:31:56 +00:00
|
|
|
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
|
|
|
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
|
|
|
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2023-10-18 07:26:30 +00:00
|
|
|
namespace VDM\Joomla\Abstraction;
|
2022-11-27 07:31:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
use Joomla\CMS\Factory as JoomlaFactory;
|
|
|
|
use VDM\Joomla\Utilities\Component\Helper;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
abstract class Database
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Database object to query local DB
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2024-03-02 20:10:30 +00:00
|
|
|
protected $db;
|
2022-11-27 07:31:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Core Component Table Name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected string $table;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2024-03-02 20:10:30 +00:00
|
|
|
public function __construct()
|
2022-11-27 07:31:56 +00:00
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$this->db = JoomlaFactory::getDbo();
|
2022-11-27 07:31:56 +00:00
|
|
|
|
|
|
|
// set the component table
|
|
|
|
$this->table = '#__' . Helper::getCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a value based on data type
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to set
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @since 3.2.0
|
|
|
|
**/
|
|
|
|
protected function quote($value)
|
|
|
|
{
|
2023-05-02 00:10:55 +00:00
|
|
|
if ($value === null) // hmm the null does pose an issue (will keep an eye on this)
|
|
|
|
{
|
|
|
|
return 'NULL';
|
|
|
|
}
|
|
|
|
|
2022-11-27 07:31:56 +00:00
|
|
|
if (is_numeric($value))
|
|
|
|
{
|
|
|
|
if (filter_var($value, FILTER_VALIDATE_INT))
|
|
|
|
{
|
|
|
|
return (int) $value;
|
|
|
|
}
|
|
|
|
elseif (filter_var($value, FILTER_VALIDATE_FLOAT))
|
|
|
|
{
|
|
|
|
return (float) $value;
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 00:10:55 +00:00
|
|
|
elseif (is_bool($value)) // not sure if this will work well (but its correct)
|
|
|
|
{
|
|
|
|
return $value ? 'TRUE' : 'FALSE';
|
|
|
|
}
|
|
|
|
|
|
|
|
// For date and datetime values
|
|
|
|
if ($value instanceof \DateTime)
|
2022-11-27 07:31:56 +00:00
|
|
|
{
|
2023-05-02 00:10:55 +00:00
|
|
|
return $this->db->quote($value->format('Y-m-d H:i:s'));
|
2022-11-27 07:31:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 00:10:55 +00:00
|
|
|
// For other data types, just escape it
|
2022-11-27 07:31:56 +00:00
|
|
|
return $this->db->quote($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a table name, adding the
|
|
|
|
* core component as needed
|
|
|
|
*
|
|
|
|
* @param string $table The table string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @since 3.2.0
|
|
|
|
**/
|
|
|
|
protected function getTable(string $table): string
|
|
|
|
{
|
|
|
|
if (strpos($table, '#__') === false)
|
|
|
|
{
|
|
|
|
return $this->table . '_' . $table;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $table;
|
2023-10-18 07:26:30 +00:00
|
|
|
}
|
2022-11-27 07:31:56 +00:00
|
|
|
}
|
|
|
|
|