-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_util.php
213 lines (188 loc) · 4.53 KB
/
db_util.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
<?php
# Gestion de la conexion
require_once( 'config.php' );
# Utilidades para construir las queries
require_once( 'sql_util.php' );
try
{
$con = new PDO( 'mysql:host=' . PATH . ';dbname=' . DATABASE, USER, PASSWORD,
array( PDO::ATTR_PERSISTENT => true) );
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch ( PDOException $ex )
{
print "Error!: " . $ex->getMessage();
die(); # Falló la aplicacion
}
# És inútil, ya veremos
function doQuery( $sentence )
{
global $con;
error_log('Trying query: ' . $sentence );
try
{
$result_set = $con->query( $sentence );
return $result_set->fetchAll( PDO::FETCH_ASSOC );
}
catch ( PDOException $ex )
{
error_log( $ex->getMessage() );
return array();
}
}
function doQueryOne( $sentence ) {
$res = doQuery( $sentence );
if ( count( $res ) != 0 )
{
return $res[0];
}
return null;
}
function doQueryTo( $table, $where, $data )
{
global $con;
$query = getSelect( $table, $where );
error_log( 'Custom where query: ' . $query );
$pss = $con->prepare( $query );
foreach ( $where as $cond )
{
error_log('Traying match ' . $cond . ' has value ' . $data[ $cond ] );
$pss->bindParam( ':' . $cond, $data[ $cond ] );
}
$pss->execute();
$rs = $pss->fetchAll( PDO::FETCH_ASSOC );
if ( is_array( $rs ) )
{
return $res = array('resultado' => true, 'items' => $rs);
}
else // Si es de cualquier otro tipo, significa que es erroneo
{
return $res = array('resultado' => false, 'error' => 'No se pudo completar la consulta');
}
return $res;
}
function doQueryById( $table, $data )
{
global $con;
try
{
$pss = $con->prepare( getSelectByIds( $table ) );
$bindings = getFNames( $table['fields'], $table['form'] );
foreach ( $table['ids'] as $fn )
{
$pss->bindParam( ':' . $bindings[$fn], $data[$bindings[$fn]] );
}
$pss->execute();
$item = $pss->fetch( PDO::FETCH_ASSOC );
if ( is_array( $item ) )
{
$res = array('resultado' => true, 'item' => $item);
}
else
{
$res = array('resultado' => false, 'error' => 'Objeto no encontrado');
}
}
catch ( PDOException $ex )
{
$res = array('resultado' => false,
'error' => 'Error de base de datos: ' . $ex->getMessage());
}
//error_log( 'Resultado: '. $res );
return $res;
}
function doInsert( $table, $data )
{
global $con;
# Obtener los nombres de los comodines
$names = array();
foreach ( $table['fields'] as $f )
{
if ( array_key_exists( $f, $table['form'] ) )
{
$names[] = $table['form'][$f];
}
else
{
$names[] = $f;
}
}
# Enlazar los comodines con su valor real
$data['id'] = uniqid( 'usr' );
$qi = getInsert( $table );
# error_log( 'Query for insert' . $qi );
$psi = $con->prepare( getInsert( $table ) );
foreach ( $names as $ph )
{
$psi->bindParam( ':' . $ph, $data[$ph] );
}
# Ejecutar la query
try
{
$ok = $psi->execute(); # Query ejecutada
$ok = $ok and ( $psi->rowCount() != 0 ); # Si hubo filas afectadas
$res = array( 'resultado' => $ok );
}
catch ( PDOException $ex )
{
$res = array( 'resultado' => false, 'error' => $ex->getMessage() );
}
return $res;
}
function doUpdate( $table, $data )
{
global $con;
# Obtener los nombres de los campos
$names = getFNames( $table['fields'], $table['form'] );
$uq = getUpdate( $table ); # Update query
error_log( 'Update: ' . $uq );
# Preparar y vincular la query
$psu = $con->prepare( $uq );
foreach ( $names as $fn )
{
$psu->bindParam( ':' . $fn, $data[$fn] );
}
# Ejecutar la query
try
{
$ok = $psu->execute();
$ok = $ok and ( $psu->rowCount() != 0 ); # Si hubo filas afectadas
$res = array('resultado' => $ok );
}
catch ( PDOException $ex )
{
$res = array( 'resultado' => false, 'error' => $ex->getMessage() );
}
return $res;
}
function doDelete( $table, $data )
{
global $con; # La conexion a bd
$name = getFNames( $table['fields'], $table['form'] );
$dq = getDelete( $table );
error_log('Query delete: ' . $dq);
$psd = $con->prepare( $dq );
# Encontrar las claves del arreglo para enlazarlos del $data a la query
foreach ( $table['ids'] as $pos )
{
$field = $table['fields'][$pos];
$form = array_key_exists( $field, $table['form'] ) ? $table['form'][$field] : $field;
$psd->bindParam( ':' . $field, $data[$form] );
}
try
{
$ok = $psd->execute();
$ok = $ok and ( $psd->rowCount() != 0 );
$res = array('resultado' => $ok);
}
catch ( PDOException $ex )
{
$res = array( 'resultado' => false, 'error' => $ex->getMessage() );
}
return $res;
}
function is_valid_string( $var )
{
return strlen( $var ) != 0;
}
?>