-
Notifications
You must be signed in to change notification settings - Fork 988
/
database_helper.php
227 lines (205 loc) · 5.92 KB
/
database_helper.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
if ( !defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
*/
/**
*
* @param type $sql
* @param type $inputarr
* @param type $silent
* @return CDbDataReader
*/
function dbExecuteAssoc($sql,$inputarr=false,$silent=true)
{
$error = '';
try {
if($inputarr)
{
$dataset=Yii::app()->db->createCommand($sql)->bindValues($inputarr)->query(); //Checked
}
else
{
$dataset=Yii::app()->db->createCommand($sql)->query();
}
} catch(CDbException $e) {
$error = $e->getMessage();
$dataset=false;
}
if (!$dataset && (Yii::app()->getConfig('debug') >0 || !$silent))
{
safeDie('Error executing query in dbExecuteAssoc:'.$error);
}
return $dataset;
}
function dbQueryOrFalse($sql)
{
try {
$dataset=Yii::app()->db->createCommand($sql)->query();
} catch(CDbException $e) {
$dataset=false;
}
return $dataset;
}
function dbSelectLimitAssoc($sql,$numrows=0,$offset=0,$inputarr=false,$dieonerror=true)
{
$query = Yii::app()->db->createCommand($sql.= " ");
if ($numrows)
{
if ($offset)
{
$query->limit($numrows, $offset);
}
else
{
$query->limit($numrows, 0);
}
}
if($inputarr)
{
$query->bindValues($inputarr); //Checked
}
try
{
$dataset=$query->query();
}
catch (CDbException $e)
{
$dataset=false;
}
if (!$dataset && $dieonerror) {safeDie('Error executing query in dbSelectLimitAssoc:'.$query->text);}
return $dataset;
}
/**
* This functions quotes fieldnames accordingly
*
* @param mixed $id Fieldname to be quoted
*/
function dbQuoteID($id)
{
switch (Yii::app()->db->getDriverName())
{
case "mysqli" :
case "mysql" :
return "`".$id."`";
break;
case "dblib":
case "mssql" :
case "sqlsrv" :
return "[".$id."]";
break;
case "pgsql":
return "\"".$id."\"";
break;
default:
return $id;
}
}
/**
* Return the random function to use in ORDER BY sql statements
*
* @return string
*/
function dbRandom()
{
$driver = Yii::app()->db->getDriverName();
// Looked up supported db-types in InstallerConfigForm.php
// Use below statement to find them
//$configForm = new InstallerConfigForm();
//$dbTypes = $configForm->db_names; //Supported types are in this array
switch ($driver)
{
case 'dblib':
case 'mssql':
case 'sqlsrv':
$srandom='NEWID()';
break;
case 'pgsql':
$srandom='RANDOM()';
break;
case 'mysql':
case 'mysqli':
$srandom='RAND()';
break;
default:
//Some db type that is not mentioned above, could fail and if so should get an entry above.
$srandom= 0 + lcg_value()*(abs(1));
break;
}
return $srandom;
}
/**
* Return a sql statement for finding LIKE named tables
* Be aware that you have to escape underscor chars by using a backslash
* otherwise you might get table names returned you don't want
*
* @param mixed $table
*/
function dbSelectTablesLike($table)
{
switch (Yii::app()->db->getDriverName()) {
case 'mysqli':
case 'mysql' :
return "SHOW TABLES LIKE '$table'";
case 'dblib' :
case 'mssql' :
case 'sqlsrv' :
return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE' and TABLE_NAME LIKE '$table' ESCAPE '\'";
case 'pgsql' :
return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' and table_name like '$table'";
default: safeDie ("Couldn't create 'select tables like' query for connection type '".Yii::app()->db->getDriverName()."'");
}
}
/**
* Gets the table names. Do not prefix.
* @param string $table String to match
* @uses dbSelectTablesLike() To get the tables like sql query
* @return array Array of matched table names
*/
function dbGetTablesLike($table)
{
return (array) Yii::app()->db->createCommand(dbSelectTablesLike("{{{$table}}}"))->queryColumn();
}
/**
* Creates a table using the YII DB Schema function but properly handles custom field types for the various DB types
*
* @param mixed $sTableName
* @param mixed $aColumns
* @param mixed $sOptions
*/
function createTable($sTableName, $aColumns, $sOptions=null)
{
$sDBDriverName=Yii::app()->db->getDriverName();
if ($sDBDriverName=='sqlsrv' || $sDBDriverName=='mssql' || $sDBDriverName=='dblib')
{
foreach ($aColumns as $sName=>&$sType)
{
$sType=str_replace('text','varchar(max)',$sType);
$sType=str_replace('binary','text',$sType);
if ($sType=='pk') $sType.=' NOT NULL';
if (stripos($sType,'null')===false && stripos($sType,'PRIMARY KEY')===false) $sType.=' NULL';
}
}
if ($sDBDriverName=='pgsql')
{
foreach ($aColumns as $sName=>&$sType)
{
$sType=str_replace('varchar','character varying',$sType);
}
}
if (Yii::app()->db->driverName == 'mysql' || Yii::app()->db->driverName == 'mysqli')
{
if (is_null($sOptions))
$sOptions='ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
}
Yii::app()->db->createCommand()->createTable($sTableName,$aColumns,$sOptions);
}