Alternc  latest
Alternc logiel libre pour l'h├ębergement
 All Classes Namespaces Files Functions Variables Pages
DB_Sql Class Reference

Mysql Database class. More...

+ Inheritance diagram for DB_Sql:

Public Member Functions

 __construct ($db, $host, $user, $passwd)
 Constructor: Connect to the database server. More...
 
 connect ($Database="", $Host="", $User="", $Password="")
 function for MySQL database connection management More...
 
 free ()
 Discard the query result. More...
 
 is_connected ()
 
 last_error ()
 
 query ($Query_String, $arguments=false)
 Perform a query. More...
 
 next_record ()
 walk result set More...
 
 lock ($table, $mode="write")
 table locking More...
 
 unlock ()
 table unlocking More...
 
 affected_rows ()
 evaluate the result (size, width) More...
 
 num_rows ()
 
 num_fields ()
 
 nf ()
 shorthand notation More...
 
 np ()
 
 f ($Name)
 
 current_record ()
 
 p ($Name)
 
 lastid ()
 
 quote ($string)
 Escape a string to use it into a SQL PDO query. More...
 
 exec ($query)
 Execute a direct query, not getting any result back. More...
 
 nextid ($seq_name)
 get next sequence numbers More...
 
 metadata ($table='', $full=false)
 DEPRECATED return table metadata. More...
 
 halt ($msg)
 private: error handling More...
 
 haltmsg ($msg)
 private: error handling More...
 
 table_names ()
 

Public Attributes

 $Record = array()
 

Private Attributes

 $Host
 
 $Database
 
 $User
 
 $Password
 
 $Auto_Free = False
 
 $Debug = False
 
 $Halt_On_Error = "no"
 
 $Seq_Table = "db_sequence"
 
 $Row = 0
 
 $num_rows
 
 $Errno
 
 $Error
 
 $Query_String
 
 $pdo_instance = NULL
 
 $pdo_query = NULL
 

Detailed Description

Mysql Database class.

Definition at line 27 of file db_mysql.php.

Constructor & Destructor Documentation

DB_Sql::__construct (   $db,
  $host,
  $user,
  $passwd 
)

Constructor: Connect to the database server.

Definition at line 61 of file db_mysql.php.

References $db, $host, and $user.

61  {
62 
63  $dsn = sprintf('mysql:dbname=%s;host=%s', $db, $host);
64 
65  try {
66  $this->pdo_instance = new PDO($dsn, $user, $passwd);
67  } catch (PDOException $e) {
68  echo "Mysql", "PDO instance", $e->getMessage();
69  return FALSE;
70  }
71  }
$user
Definition: bootstrap.php:84
$host
Definition: config.php:71
global $db
Definition: bootstrap.php:22

Member Function Documentation

DB_Sql::affected_rows ( )

evaluate the result (size, width)

Definition at line 271 of file db_mysql.php.

271  {
272  return $this->pdo_query->rowCount();
273  }
DB_Sql::connect (   $Database = "",
  $Host = "",
  $User = "",
  $Password = "" 
)

function for MySQL database connection management

This function manages the connection to the MySQL database.

Parameters
$Databasename of the database
$HostDNS of the MySQL hosting server
$Userthe user's name
$Passwordthe user's password
Returns
the class variable $Link_ID

Definition at line 86 of file db_mysql.php.

References $Database, $Host, $Password, $User, and halt().

86  {
87  $this->halt('Mysql::connect() : This function should no longer be used');
88  /* Handle defaults */
89  if ("" == $Database)
91  if ("" == $Host)
93  if ("" == $User)
95  if ("" == $Password)
97 
98  if (!$this->pdo_instance) {
99  $dsn = sprintf('mysql:dbname=%s;host=%s', $Database, $Host);
100 
101  try {
102  $this->pdo_instance = new PDO($dsn, $User, $Password);
103  } catch (PDOException $e) {
104  $this->halt("Mysql::PDO_instance" . $e->getMessage());
105  return FALSE;
106  }
107  }
108 
109  return True;
110  }
$Database
Definition: db_mysql.php:31
halt($msg)
private: error handling
Definition: db_mysql.php:398
$Password
Definition: db_mysql.php:33
DB_Sql::current_record ( )

Definition at line 306 of file db_mysql.php.

References $Record.

306  {
307  return $this->Record;
308  }
DB_Sql::exec (   $query)

Execute a direct query, not getting any result back.

Parameters
querystring query to execute
Returns
integer the number of affected rows

Definition at line 336 of file db_mysql.php.

References $query.

336  {
337  return $this->pdo_instance->exec($query);
338  }
$query
Definition: 3.0.0~3.php:37
DB_Sql::f (   $Name)
Parameters
string$Name
Returns
integer

Definition at line 298 of file db_mysql.php.

Referenced by nextid().

298  {
299  if (isset($this->Record[$Name]))
300  return $this->Record[$Name];
301  else
302  return false;
303  }
DB_Sql::free ( )

Discard the query result.

This function discards the last query result.

Definition at line 117 of file db_mysql.php.

Referenced by next_record().

117  {
118  $this->pdo_query->closeCursor();
119  }
DB_Sql::halt (   $msg)

private: error handling

Definition at line 398 of file db_mysql.php.

References $msg, and haltmsg().

Referenced by connect(), lock(), next_record(), nextid(), query(), and unlock().

398  {
399  if ($this->Halt_On_Error == "no")
400  return;
401 
402  $this->haltmsg($msg);
403 
404  if ($this->Halt_On_Error != "report")
405  die("Session halted.");
406  }
$msg
Definition: config.php:155
haltmsg($msg)
private: error handling
Definition: db_mysql.php:412
DB_Sql::haltmsg (   $msg)

private: error handling

Definition at line 412 of file db_mysql.php.

References $msg.

Referenced by halt().

412  {
413  printf("</td></tr></table><b>Database error:</b> %s<br />\n", $msg);
414  printf("<b>MySQL Error</b>: %s (%s)<br />\n",
415  $this->Errno,
416  implode("\n", $this->Error));
417  }
$msg
Definition: config.php:155
DB_Sql::is_connected ( )

Definition at line 122 of file db_mysql.php.

Referenced by lock(), nextid(), query(), and unlock().

122  {
123  return $this->pdo_instance != FALSE;
124  }
DB_Sql::last_error ( )

Definition at line 127 of file db_mysql.php.

References $Error.

127  {
128  return $this->Error;
129  }
DB_Sql::lastid ( )

Definition at line 316 of file db_mysql.php.

316  {
317  return $this->pdo_instance->lastInsertId();
318  }
DB_Sql::lock (   $table,
  $mode = "write" 
)

table locking

Definition at line 226 of file db_mysql.php.

References $key, $query, $value, each(), halt(), is_connected(), and query().

Referenced by nextid().

226  {
227  if (!$this->is_connected())
228  return FALSE;
229 
230  $query="lock tables ";
231  if (is_array($table)) {
232  while (list($key,$value)=each($table)) {
233  if ($key=="read" && $key!=0) {
234  $query.="$value read, ";
235  } else {
236  $query.="$value $mode, ";
237  }
238  }
239  $query=substr($query,0,-2);
240  } else {
241  $query.="$table $mode";
242  }
243 
244 
245  if (!$this->query($query)) {
246  $this->halt("lock($table, $mode) failed.");
247  return FALSE;
248  }
249 
250  return TRUE;
251 
252  }
halt($msg)
private: error handling
Definition: db_mysql.php:398
$query
Definition: 3.0.0~3.php:37
is_connected()
Definition: db_mysql.php:122
$value
x each(["height","width"], function(e, n){x.cssHooks[n]={get:function(e, r, i){return r?0===e.offsetWidth &&Xt.test(x.css(e,"display"))?x.swap(e, Qt, function(){return sn(e, n, i)}):sn(e, n, i):t}, set:function(e, t, r){var i=r &&Rt(e);return on(e, t, r?an(e, n, r, x.support.boxSizing &&"border-box"===x.css(e,"boxSizing",!1, i), i):0)}}})
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:141
DB_Sql::metadata (   $table = '',
  $full = false 
)

DEPRECATED return table metadata.

Definition at line 389 of file db_mysql.php.

References $msg.

389  {
390  global $msg;
391  $msg->raise("ERROR", 'Mysql', 'function is no longer implemented (metadata())');
392  return FALSE;
393  }
$msg
Definition: config.php:155
DB_Sql::next_record ( )

walk result set

This function tests if a new record is available in the current query result.

Returns
TRUE if a new record is available

Definition at line 202 of file db_mysql.php.

References free(), and halt().

Referenced by nextid(), and table_names().

202  {
203  if (!$this->pdo_query) {
204  $this->halt("next_record called with no query pending.");
205  return FALSE;
206  }
207 
208  $this->Record = $this->pdo_query->fetch(PDO::FETCH_BOTH);
209  $this->Row++;
210  $this->Errno = $this->pdo_query->errorCode();
211  $this->Error = $this->pdo_query->errorInfo();
212 
213  if ($this->Record == FALSE) {
214  if ($this->Auto_Free)
215  $this->free();
216  return FALSE;
217  }
218 
219  return TRUE;
220  }
halt($msg)
private: error handling
Definition: db_mysql.php:398
free()
Discard the query result.
Definition: db_mysql.php:117
DB_Sql::nextid (   $seq_name)

get next sequence numbers

Definition at line 344 of file db_mysql.php.

References $id, $q, f(), halt(), is_connected(), lock(), next_record(), query(), and unlock().

344  {
345  if (!$this->is_connected())
346  return FALSE;
347 
348  if ($this->lock($this->Seq_Table)) {
349  /* get sequence number (locked) and increment */
350  $q = sprintf("select nextid from %s where seq_name = '%s'",
351  $this->Seq_Table,
352  $seq_name);
353  $this->query($q);
354  $this->next_record();
355 
356  $id = $this->f('nextid');
357 
358  /* No current value, make one */
359  if (!$id) {
360  $currentid = 0;
361  $q = sprintf("insert into %s values('%s', %s)",
362  $this->Seq_Table,
363  $seq_name,
364  $currentid);
365  $this->query($q);
366  } else {
367  $currentid = $id;
368  }
369 
370  $nextid = $currentid + 1;
371  $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
372  $this->Seq_Table,
373  $nextid,
374  $seq_name);
375  $this->query($q);
376  $this->unlock();
377  } else {
378  $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
379  return FALSE;
380  }
381 
382  return $nextid;
383  }
f($Name)
Definition: db_mysql.php:298
halt($msg)
private: error handling
Definition: db_mysql.php:398
is_connected()
Definition: db_mysql.php:122
lock($table, $mode="write")
table locking
Definition: db_mysql.php:226
next_record()
walk result set
Definition: db_mysql.php:202
unlock()
table unlocking
Definition: db_mysql.php:257
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:141
if(!isset($is_include)) if(!$key &&!$crt) $id
DB_Sql::nf ( )

shorthand notation

Definition at line 285 of file db_mysql.php.

References num_rows().

285  {
286  return $this->num_rows();
287  }
num_rows()
Definition: db_mysql.php:274
DB_Sql::np ( )

Definition at line 289 of file db_mysql.php.

References num_rows(), and print.

289  {
290  print $this->num_rows();
291  }
print
Definition: mail_add.php:92
num_rows()
Definition: db_mysql.php:274
DB_Sql::num_fields ( )

Definition at line 278 of file db_mysql.php.

278  {
279  return $this->pdo_query->columnCount();
280  }
DB_Sql::num_rows ( )

Definition at line 274 of file db_mysql.php.

Referenced by nf(), and np().

274  {
275  return $this->pdo_query->rowCount();
276  }
DB_Sql::p (   $Name)

Definition at line 311 of file db_mysql.php.

References print.

Referenced by table_names().

311  {
312  print $this->Record[$Name];
313  }
print
Definition: mail_add.php:92
DB_Sql::query (   $Query_String,
  $arguments = false 
)

Perform a query.

This function performs the MySQL query described in the string parameter

Parameters
astring describing the MySQL query
argumentsis an optionnal array for future use with PDO parametrized requests
Returns
the $Query_ID class variable (null if fails)

Definition at line 141 of file db_mysql.php.

References $Query_String, halt(), is_connected(), and THROW_EXCEPTIONS.

Referenced by lock(), nextid(), table_names(), and unlock().

141  {
142  global $debug_alternc;
143 
144  if (empty($Query_String) || !$this->is_connected())
145  return FALSE;
146 
147  $this->Query_String = $Query_String;
148  if ($this->Debug)
149  printf("Debug: query = %s<br />\n", $Query_String);
150 
151  $debug_chrono_start = microtime(true);
152 
153  if ($arguments===false) {
154  $this->pdo_query = $this->pdo_instance->query($Query_String);
155  $exec_state = is_object($this->pdo_query);
156 
157  } else {
158 
159  $this->pdo_query = $this->pdo_instance->prepare($this->Query_String);
160  $exec_state = ($arguments) ? $this->pdo_query->execute($arguments)
161  : $this->pdo_query->execute();
162  // WARNING: this ternary is when we pass array() as $arguments
163  }
164 
165  $debug_chrono_start = (microtime(true) - $debug_chrono_start)*1000;
166  $this->Row = 0;
167 
168  if ($exec_state == FALSE) {
169  if (is_object($this->pdo_query)) {
170  $this->Errno = $this->pdo_query->errorCode();
171  $this->Error = $this->pdo_query->errorInfo();
172  } else {
173  $this->Errno = $this->pdo_instance->errorCode();
174  $this->Error = $this->pdo_instance->errorInfo();
175  }
176 
177  if( defined("THROW_EXCEPTIONS") && THROW_EXCEPTIONS ){
178  throw new \Exception("Mysql query failed : $this->Error");
179  }
180  $this->halt("SQL Error: ".$Query_String);
181  return FALSE;
182  }
183 
184  if (isset($debug_alternc)) {
185  $debug_alternc->add("SQL Query : (".substr($debug_chrono_start,0,5)." ms)\t $Query_String");
186  $debug_alternc->nb_sql_query++;
187  $debug_alternc->tps_sql_query += $debug_chrono_start;
188  }
189 
190  return TRUE;
191  }
$Query_String
Definition: db_mysql.php:51
halt($msg)
private: error handling
Definition: db_mysql.php:398
is_connected()
Definition: db_mysql.php:122
const THROW_EXCEPTIONS
Definition: bootstrap.php:69
DB_Sql::quote (   $string)

Escape a string to use it into a SQL PDO query.

Parameters
stringstring to escape
Returns
string escaped string

Definition at line 326 of file db_mysql.php.

References $string.

326  {
327  return $this->pdo_instance->quote($string);
328  }
$string
Definition: logs_tail.php:38
DB_Sql::table_names ( )

Definition at line 420 of file db_mysql.php.

References next_record(), p(), and query().

420  {
421  $this->query("SHOW TABLES");
422  $return = array();
423  while ($this->next_record())
424  $return[] = array('table_name' => $this->p(0), 'tablespace_name' => $this->Database, 'database' => $this->Database);
425 
426  return $return;
427  }
p($Name)
Definition: db_mysql.php:311
next_record()
walk result set
Definition: db_mysql.php:202
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:141
DB_Sql::unlock ( )

table unlocking

Definition at line 257 of file db_mysql.php.

References halt(), is_connected(), and query().

Referenced by nextid().

257  {
258  if (!$this->is_connected())
259  return FALSE;
260 
261  if (!$this->query('unlock tables')) {
262  $this->halt("unlock() failed.");
263  return FALSE;
264  }
265  }
halt($msg)
private: error handling
Definition: db_mysql.php:398
is_connected()
Definition: db_mysql.php:122
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:141

Member Data Documentation

DB_Sql::$Auto_Free = False
private

Definition at line 36 of file db_mysql.php.

DB_Sql::$Database
private

Definition at line 31 of file db_mysql.php.

Referenced by connect().

DB_Sql::$Debug = False
private

Definition at line 37 of file db_mysql.php.

DB_Sql::$Errno
private

Definition at line 47 of file db_mysql.php.

DB_Sql::$Error
private

Definition at line 48 of file db_mysql.php.

Referenced by last_error().

DB_Sql::$Halt_On_Error = "no"
private

Definition at line 38 of file db_mysql.php.

DB_Sql::$Host
private

Definition at line 30 of file db_mysql.php.

Referenced by connect().

DB_Sql::$num_rows
private

Definition at line 44 of file db_mysql.php.

DB_Sql::$Password
private

Definition at line 33 of file db_mysql.php.

Referenced by connect().

DB_Sql::$pdo_instance = NULL
private

Definition at line 54 of file db_mysql.php.

DB_Sql::$pdo_query = NULL
private

Definition at line 55 of file db_mysql.php.

DB_Sql::$Query_String
private

Definition at line 51 of file db_mysql.php.

Referenced by query().

DB_Sql::$Record = array()

Definition at line 42 of file db_mysql.php.

Referenced by current_record().

DB_Sql::$Row = 0
private

Definition at line 43 of file db_mysql.php.

DB_Sql::$Seq_Table = "db_sequence"
private

Definition at line 39 of file db_mysql.php.

DB_Sql::$User
private

Definition at line 32 of file db_mysql.php.

Referenced by connect().


The documentation for this class was generated from the following file: