-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
115 lines (100 loc) · 2.22 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* @package vdm/data
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git VDM Data Library <https://git.vdm.dev/joomla/vdm-data>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VastDevelopmentMethod\Joomla\Abstraction;
use Joomla\CMS\Factory as JoomlaFactory;
use VastDevelopmentMethod\Joomla\Utilities\Component\Helper;
/**
* Database
*
* @since 3.2.0
*/
abstract class Database
{
/**
* Database object to query local DB
*
* @since 3.2.0
*/
protected $db;
/**
* Core Component Table Name
*
* @var string
* @since 3.2.0
*/
protected string $table;
/**
* Constructor
*
* @throws \Exception
* @since 3.2.0
*/
public function __construct()
{
$this->db = JoomlaFactory::getDbo();
// 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)
{
if ($value === null) // hmm the null does pose an issue (will keep an eye on this)
{
return 'NULL';
}
if (is_numeric($value))
{
if (filter_var($value, FILTER_VALIDATE_INT))
{
return (int) $value;
}
elseif (filter_var($value, FILTER_VALIDATE_FLOAT))
{
return (float) $value;
}
}
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)
{
return $this->db->quote($value->format('Y-m-d H:i:s'));
}
// For other data types, just escape it
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;
}
}