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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
<?php
/**
* SQL Helper.
*
* @package Silla.IO
* @subpackage Core\Helpers;
* @author Plamen Nikolov <plamen@athlonsofia.com>
* @copyright Copyright (c) 2015, Silla.io
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3.0 (GPLv3)
*/
namespace Core\Helpers;
use Core;
/**
* SQL Class Helper definition.
*/
class SQL
{
/**
* Custom query wrapper.
*
* @param string $table Table name.
* @param mixed $attributes Column names to query.
* @param string $where Condition.
* @param string $order Order clause.
* @param integer $offset Result set offset.
* @param integer $limit Result set limit.
*
* @access public
* @static
*
* @return string
*/
public static function customQuery(
$table,
$attributes = '*',
$where = '1',
$order = null,
$offset = null,
$limit = null
) {
if (is_array($attributes)) {
$attributes = implode(',', $attributes);
}
$where = $where ? $where : 1;
$sql = "SELECT {$attributes} FROM {$table} WHERE {$where}";
$sql .= $order ? ' ORDER BY ' . $order : '';
$sql .= $limit ? ' LIMIT ' . (int)$limit : '';
$sql .= $offset ? ' OFFSET ' . (int)$offset : '';
return $sql;
}
/**
* Builds a UNION SQL query with shared ORDER BY and LIMIT.
*
* @param array $queries Array of queries to union.
* @param string $order Order clouse.
* @param integer $offset Offset count.
* @param integer $limit Limit count.
*
* @access public
* @static
*
* @return string
*/
public static function union(array $queries, $order = null, $offset = null, $limit = null)
{
$sql = '(' . implode(') UNION (', $queries) . ')';
$sql .= $order ? ' ORDER BY ' . $order : '';
$sql .= $limit ? ' LIMIT ' . (int)$limit : '';
$sql .= $offset ? ' OFFSET ' . (int)$offset : '';
return $sql;
}
/**
* Builds a WHERE part of the query suitable for filters.
*
* @param array $attributes Filtering attributes.
* @param boolean $use_prepared_statements Whether to use prepared statements or not.
*
* @access public
* @static
*
* @return string|array
*/
public static function filter(array $attributes, $use_prepared_statements = true)
{
$sql = array();
$attributes = array_filter($attributes, array(__CLASS__, 'removeFilterAttributes'));
if ($use_prepared_statements) {
foreach ($attributes as $key => $value) {
$sql[] = "{$key} = ?";
}
return array(implode(' AND ', $sql), array_values($attributes));
} else {
foreach ($attributes as $key => $value) {
$sql[] = "{$key} = " . DB()->escapeString($value);
}
return implode(' AND ', $sql);
}
}
/**
* Builds a WHERE part of the query suitable for filters with defined data types of the attribute.
*
* @param array $attributes Filtering attributes.
*
* @access public
* @static
*
* @return string
*/
public static function filterByType(array $attributes)
{
$sql = array();
foreach ($attributes as $key => $attribute) {
if ($attribute['value']) {
if (is_array($attribute['value'])) {
if (isset($attribute['value']['start'], $attribute['value']['end'])
&& !empty($attribute['value']['start'])
&& !empty($attribute['value']['end'])
) {
if ('datetime' === $attribute['type']) {
$sql[] = "({$key} BETWEEN "
. Core\DB()->escapeString($attribute['value']['start'] . ' 00:00:00')
. " AND "
. Core\DB()->escapeString($attribute['value']['end'] . ' 23:59:59')
. ')';
} else {
$sql[] = "({$key} BETWEEN "
. Core\DB()->escapeString($attribute['value']['start'])
. " AND "
. Core\DB()->escapeString($attribute['value']['end'])
. ')';
}
}
} else {
if ($attribute['type'] === 'string') {
$value_to_match = trim(Core\DB()->escapeString($attribute['value']), "'");
$sql[] = "({$key} LIKE \"%{$value_to_match}%\")";
} else {
$sql[] = "({$key} = " . Core\DB()->escapeString($attribute['value']) . ')';
}
}
}
}
return implode(' AND ', $sql);
}
/**
* Builds a set - offset part of a query.
*
* @param integer $offset Offset count.
* @param integer $limit Limit count.
*
* @access public
* @static
*
* @return string
*/
public static function setOffsetLimit($offset, $limit)
{
$offset = intval($offset);
$limit = intval($limit);
return "{$offset}, {$limit}";
}
/**
* Removes filtered attributes.
*
* @param string $attr Attribute value.
*
* @access private
* @static
*
* @return boolean
*/
private static function removeFilterAttributes($attr)
{
return $attr != '';
}
}