-
Notifications
You must be signed in to change notification settings - Fork 0
/
StashQueries.php
155 lines (127 loc) · 4.43 KB
/
StashQueries.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
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
<?php
/**
* Stash Queries - PHP SQL layer using stash queries (file) design integration
* PHP Version 5.3.6+
* @package Skyfire/DB
* @link https://github.com/SkyfirePHP/DB The Skyfire DB GitHub project
* @author Travis Font (tfont) <[email protected]>
* @license MIT
*/
// not defined from Skyfire or outside framework
if (!defined('PARENT_DIRECTORY'))
{
define('PARENT_DIRECTORY', dirname(getcwd()));
}
// SPL autoloader
spl_autoload_register(function ($classname)
{
// __DIR__ is only in PHP 5.3+
// closures / anonymous functions only in PHP 5.3+
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.$classname.'.php';
if (is_readable($filename))
{
require_once $filename;
}
}, TRUE, TRUE);
final class DB extends DB_Connector
{
public static $exceptionType;
public $dbh;
private function __construct()
{
if ($this->dbh == NULL)
{
try
{
if (self::$persistent === TRUE)
{
$this->dbh = new PDO('mysql:host='.self::$DATABASE_HOST.';dbname='.self::$DATABASE_NAME.';charset='.self::$charset, self::$DATABASE_USER, self::$DATABASE_PASSWORD, array(PDO::ATTR_PERSISTENT => TRUE, PDO::MYSQL_ATTR_FOUND_ROWS => TRUE));
}
else
{
$this->dbh = new PDO('mysql:host='.self::$DATABASE_HOST.';dbname='.self::$DATABASE_NAME.';charset='.self::$charset, self::$DATABASE_USER, self::$DATABASE_PASSWORD, array(PDO::MYSQL_ATTR_FOUND_ROWS => TRUE));
}
if (self::$errmode === TRUE)
{
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
catch (PDOException $exception)
{
echo self::PDOException($exception, self::DISPLAY_TEXT);
return FALSE;
}
}
return $this->dbh;
}
public static function utf8($text)
{
return mb_convert_encoding($text, "HTML-ENTITIES", 'UTF-8');
}
public static function query($statement, array $parameters = NULL)
{
if (self::$db == NULL)
{
self::$db = new self();
}
return new RawQuery($statement, $parameters, self::$db);
}
public static function select($sql_file)
{
if (self::$db == NULL)
{
self::$db = new self();
}
return new ProcessQuery($sql_file, 'select', self::$db);
}
public static function update($sql_file)
{
if (self::$db == NULL)
{
self::$db = new self();
}
return new ProcessQuery($sql_file, 'update', self::$db);
}
public static function insert($sql_file)
{
if (self::$db == NULL)
{
self::$db = new self();
}
return new ProcessQuery($sql_file, 'insert', self::$db);
}
public static function drop($table_name) {}
public static function truncate($table_name) {}
public static function fullempty($database_name)
{
$sql = vsprintf('%s %s %s', array
(
'SET FOREIGN_KEY_CHECKS=0;',
sprintf('SHOW FULL TABLES FROM `%s` WHERE table_type = \'BASE TABLE\';', $database_name),
'SET FOREIGN_KEY_CHECKS=1;'
));
/* also to delete views, functions, and events */
// SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA` = 'database_name' AND `TABLE_TYPE` = 'VIEW';
// SELECT `SPECIFIC_NAME` FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE `ROUTINE_SCHEMA` = 'database_name' AND ROUTINE_TYPE = 'FUNCTION';
// SELECT `EVENT_NAME` FROM `INFORMATION_SCHEMA`.`EVENTS` WHERE `EVENT_SCHEMA` = 'database_name' ORDER BY EVENT_NAME;
$drop = 'SET FOREIGN_KEY_CHECKS=0;';
foreach (self::query($sql)->execute() as $table)
{
$drop .= vsprintf('DROP TABLE `%s`.`%s`;', [$database_name, reset($table)]);
}
$drop .= 'SET FOREIGN_KEY_CHECKS=1;';
self::query($drop)->execute();
}
// this type needs to be a display type (e.g. DISPLAY_TEST)
public static function setExceptionType($type)
{
self::$exceptionType = $type;
}
public static function sanitize($string)
{
$string = stripslashes($string);
$string = strip_tags($string);
$string = htmlentities($string);
return $string;
}
}