DB.php
1.95 KB
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
<?php
/**
* @package Regular Labs Library
* @version 18.2.10140
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Library;
defined('_JEXEC') or die;
use JFactory;
/**
* Class DB
* @package RegularLabs\Library
*/
class DB
{
/**
* Check if a table exists in the database
*
* @param string $table
*
* @return bool
*/
public static function tableExists($table)
{
$db = JFactory::getDbo();
if (strpos($table, '#__') === 0)
{
$table = $db->getPrefix() . substr($table, 3);
}
if (strpos($table, $db->getPrefix()) !== 0)
{
$table = $db->getPrefix() . $table;
}
$query = 'SHOW TABLES LIKE ' . $db->quote($table);
$db->setQuery($query);
$result = $db->loadResult();
return ! empty($result);
}
/**
* Create an IN statement
* Reverts to a simple equals statement if array just has 1 value
*
* @param string|array $value
* @param bool $include
*
* @return string
*/
public static function in($value, $include = true)
{
$db = JFactory::getDbo();
$value = $db->quote($value);
if (empty($value) && ! is_array($value))
{
return ' = 0';
}
$operator = $include ? ' = ' : ' != ';
if ( ! is_array($value))
{
return $operator . $value;
}
if (count($value) == 1)
{
return $operator . reset($value);
}
$operator = $include ? ' IN ' : ' NOT IN ';
$values = empty($value) ? "''" : implode(',', $value);
return $operator . '(' . $values . ')';
}
/**
* Create an LIKE statement
*
* @param string $value
* @param bool $include
*
* @return string
*/
public static function like($value, $include = true)
{
$db = JFactory::getDbo();
$operator = $include ? ' LIKE ' : ' NOT LIKE ';
return $operator . $db->quote($value);
}
}