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

Class for MySQL management in the bureau. More...

+ Inheritance diagram for DB_system:

Public Member Functions

 __construct ()
 
 __construct ()
 
 __construct ($database, $user, $password)
 
 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")
 
 unlock ()
 
 affected_rows ()
 
 num_rows ()
 
 num_fields ()
 
 nf ()
 
 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)
 
 metadata ($table='', $full=false)
 
 halt ($msg)
 
 haltmsg ($msg)
 
 table_names ()
 

Public Attributes

 $Record = array()
 

Detailed Description

Class for MySQL management in the bureau.

This class heriting from the db class of the phplib manages the connection to the MySQL database.

Definition at line 28 of file bootstrap.php.

Constructor & Destructor Documentation

DB_system::__construct ( )

Definition at line 29 of file bootstrap.php.

References $L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, and $L_MYSQL_PWD.

29  {
31  parent::__construct($L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, $L_MYSQL_PWD);
32  }
global $L_MYSQL_DATABASE
Definition: bootstrap.php:22
global $L_MYSQL_PWD
Definition: bootstrap.php:22
global $L_MYSQL_LOGIN
Definition: bootstrap.php:22
global $L_MYSQL_HOST
Definition: bootstrap.php:22
DB_system::__construct ( )

Definition at line 127 of file config.php.

References $L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, and $L_MYSQL_PWD.

127  {
129  parent::__construct($L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, $L_MYSQL_PWD);
130  }
global $L_MYSQL_DATABASE
Definition: bootstrap.php:22
global $L_MYSQL_PWD
Definition: bootstrap.php:22
global $L_MYSQL_LOGIN
Definition: bootstrap.php:22
global $L_MYSQL_HOST
Definition: bootstrap.php:22
DB_system::__construct (   $database,
  $user,
  $password 
)

Definition at line 122 of file bootstrap.php.

References $database, $password, and $user.

122  {
123  parent::__construct($database, '127.0.0.1', $user, $password);
124  }
$user
Definition: bootstrap.php:84
foreach(array(ALTERNC_MAIL, ALTERNC_HTML, ALTERNC_LOGS) as $crdir) $database
Definition: bootstrap.php:83
$password
Definition: bootstrap.php:85

Member Function Documentation

DB_Sql::affected_rows ( )
inherited

Definition at line 243 of file db_mysql.php.

243  {
244  return $this->pdo_query->rowCount();
245  }
DB_Sql::connect (   $Database = "",
  $Host = "",
  $User = "",
  $Password = "" 
)
inherited

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 70 of file db_mysql.php.

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

70  {
71  global $err;
72  $this->halt('Mysql::connect() : This function should no longer be used');
73  /* Handle defaults */
74  if ("" == $Database)
76  if ("" == $Host)
78  if ("" == $User)
80  if ("" == $Password)
82 
83  if (!$this->pdo_instance) {
84  $dsn = sprintf('mysql:dbname=%s;host=%s', $Database, $Host);
85 
86  try {
87  $this->pdo_instance = new PDO($dsn, $User, $Password);
88  } catch (PDOException $e) {
89  $this->halt("Mysql::PDO_instance" . $e->getMessage());
90  return FALSE;
91  }
92  }
93 
94  return True;
95  }
$Database
Definition: db_mysql.php:15
$err
Definition: bootstrap.php:72
halt($msg)
Definition: db_mysql.php:357
$Password
Definition: db_mysql.php:17
DB_Sql::current_record ( )
inherited

Definition at line 275 of file db_mysql.php.

References DB_Sql\$Record.

275  {
276  return $this->Record;
277  }
DB_Sql::exec (   $query)
inherited

Execute a direct query, not getting any result back.

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

Definition at line 302 of file db_mysql.php.

References $query.

302  {
303  return $this->pdo_instance->exec($query);
304  }
$query
Definition: 3.0.0~3.php:43
DB_Sql::f (   $Name)
inherited
Parameters
string$Name
Returns
integer

Definition at line 268 of file db_mysql.php.

Referenced by DB_Sql\nextid().

268  {
269  if (isset($this->Record[$Name]))
270  return $this->Record[$Name];
271  else
272  return false;
273  }
DB_Sql::free ( )
inherited

Discard the query result.

This function discards the last query result.

Definition at line 102 of file db_mysql.php.

Referenced by DB_Sql\next_record().

102  {
103  $this->pdo_query->closeCursor();
104  }
DB_Sql::halt (   $msg)
inherited

Definition at line 357 of file db_mysql.php.

References DB_Sql\haltmsg().

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

357  {
358  if ($this->Halt_On_Error == "no")
359  return;
360 
361  $this->haltmsg($msg);
362 
363  if ($this->Halt_On_Error != "report")
364  die("Session halted.");
365  }
haltmsg($msg)
Definition: db_mysql.php:367
DB_Sql::haltmsg (   $msg)
inherited

Definition at line 367 of file db_mysql.php.

Referenced by DB_Sql\halt().

367  {
368  printf("</td></tr></table><b>Database error:</b> %s<br />\n", $msg);
369  printf("<b>MySQL Error</b>: %s (%s)<br />\n",
370  $this->Errno,
371  implode("\n", $this->Error));
372  }
DB_Sql::is_connected ( )
inherited

Definition at line 106 of file db_mysql.php.

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

106  {
107  return $this->pdo_instance != FALSE;
108  }
DB_Sql::last_error ( )
inherited

Definition at line 110 of file db_mysql.php.

References DB_Sql\$Error.

110  {
111  return $this->Error;
112  }
DB_Sql::lastid ( )
inherited

Definition at line 283 of file db_mysql.php.

283  {
284  return $this->pdo_instance->lastInsertId();
285  }
DB_Sql::lock (   $table,
  $mode = "write" 
)
inherited

Definition at line 203 of file db_mysql.php.

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

Referenced by DB_Sql\nextid().

203  {
204  if (!$this->is_connected())
205  return FALSE;
206 
207  $query="lock tables ";
208  if (is_array($table)) {
209  while (list($key,$value)=each($table)) {
210  if ($key=="read" && $key!=0) {
211  $query.="$value read, ";
212  } else {
213  $query.="$value $mode, ";
214  }
215  }
216  $query=substr($query,0,-2);
217  } else {
218  $query.="$table $mode";
219  }
220 
221 
222  if (!$this->query($query)) {
223  $this->halt("lock($table, $mode) failed.");
224  return FALSE;
225  }
226 
227  return TRUE;
228 
229  }
halt($msg)
Definition: db_mysql.php:357
$query
Definition: 3.0.0~3.php:43
is_connected()
Definition: db_mysql.php:106
$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:122
DB_Sql::metadata (   $table = '',
  $full = false 
)
inherited

Definition at line 350 of file db_mysql.php.

References $err.

350  {
351  global $err;
352  $err->raise('Mysql', 'function is no longer implemented (metadata())');
353  return FALSE;
354  }
$err
Definition: bootstrap.php:72
DB_Sql::next_record ( )
inherited

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 182 of file db_mysql.php.

References DB_Sql\free(), and DB_Sql\halt().

Referenced by DB_Sql\nextid(), and DB_Sql\table_names().

182  {
183  if (!$this->pdo_query) {
184  $this->halt("next_record called with no query pending.");
185  return FALSE;
186  }
187 
188  $this->Record = $this->pdo_query->fetch(PDO::FETCH_BOTH);
189  $this->Row++;
190  $this->Errno = $this->pdo_query->errorCode();
191  $this->Error = $this->pdo_query->errorInfo();
192 
193  if ($this->Record == FALSE) {
194  if ($this->Auto_Free)
195  $this->free();
196  return FALSE;
197  }
198 
199  return TRUE;
200  }
halt($msg)
Definition: db_mysql.php:357
free()
Discard the query result.
Definition: db_mysql.php:102
DB_Sql::nextid (   $seq_name)
inherited

Definition at line 308 of file db_mysql.php.

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

308  {
309  if (!$this->is_connected())
310  return FALSE;
311 
312  if ($this->lock($this->Seq_Table)) {
313  /* get sequence number (locked) and increment */
314  $q = sprintf("select nextid from %s where seq_name = '%s'",
315  $this->Seq_Table,
316  $seq_name);
317  $this->query($q);
318  $this->next_record();
319 
320  $id = $this->f('nextid');
321 
322  /* No current value, make one */
323  if (!$id) {
324  $currentid = 0;
325  $q = sprintf("insert into %s values('%s', %s)",
326  $this->Seq_Table,
327  $seq_name,
328  $currentid);
329  $this->query($q);
330  } else {
331  $currentid = $id;
332  }
333 
334  $nextid = $currentid + 1;
335  $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
336  $this->Seq_Table,
337  $nextid,
338  $seq_name);
339  $this->query($q);
340  $this->unlock();
341  } else {
342  $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
343  return FALSE;
344  }
345 
346  return $nextid;
347  }
f($Name)
Definition: db_mysql.php:268
halt($msg)
Definition: db_mysql.php:357
is_connected()
Definition: db_mysql.php:106
lock($table, $mode="write")
Definition: db_mysql.php:203
next_record()
walk result set
Definition: db_mysql.php:182
unlock()
Definition: db_mysql.php:231
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:122
if(!isset($is_include)) if(!$key &&!$crt) $id
DB_Sql::nf ( )
inherited

Definition at line 256 of file db_mysql.php.

References DB_Sql\num_rows().

256  {
257  return $this->num_rows();
258  }
num_rows()
Definition: db_mysql.php:247
DB_Sql::np ( )
inherited

Definition at line 260 of file db_mysql.php.

References DB_Sql\num_rows().

260  {
261  print $this->num_rows();
262  }
num_rows()
Definition: db_mysql.php:247
DB_Sql::num_fields ( )
inherited

Definition at line 251 of file db_mysql.php.

251  {
252  return $this->pdo_query->columnCount();
253  }
DB_Sql::num_rows ( )
inherited

Definition at line 247 of file db_mysql.php.

Referenced by DB_Sql\nf(), and DB_Sql\np().

247  {
248  return $this->pdo_query->rowCount();
249  }
DB_Sql::p (   $Name)
inherited

Definition at line 279 of file db_mysql.php.

Referenced by DB_Sql\table_names().

279  {
280  print $this->Record[$Name];
281  }
DB_Sql::query (   $Query_String,
  $arguments = false 
)
inherited

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 122 of file db_mysql.php.

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

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

122  {
123  global $debug_alternc;
124 
125  if (empty($Query_String) || !$this->is_connected())
126  return FALSE;
127 
128  $this->Query_String = $Query_String;
129  if ($this->Debug)
130  printf("Debug: query = %s<br />\n", $Query_String);
131 
132  $debug_chrono_start = microtime(true);
133 
134  if ($arguments===false) {
135  $this->pdo_query = $this->pdo_instance->query($Query_String);
136  $exec_state = is_object($this->pdo_query);
137 
138  } else {
139 
140  $this->pdo_query = $this->pdo_instance->prepare($this->Query_String);
141  $exec_state = ($arguments) ? $this->pdo_query->execute($arguments)
142  : $this->pdo_query->execute();
143  // WARNING: this ternary is when we pass array() as $arguments
144  }
145 
146  $debug_chrono_start = (microtime(true) - $debug_chrono_start)*1000;
147  $this->Row = 0;
148 
149  if ($exec_state == FALSE) {
150  if (is_object($this->pdo_query)) {
151  $this->Errno = $this->pdo_query->errorCode();
152  $this->Error = $this->pdo_query->errorInfo();
153  } else {
154  $this->Errno = $this->pdo_instance->errorCode();
155  $this->Error = $this->pdo_instance->errorInfo();
156  }
157 
158  if( defined("THROW_EXCEPTIONS") && THROW_EXCEPTIONS ){
159  throw new \Exception("Mysql query failed : $this->Error");
160  }
161  $this->halt("SQL Error: ".$Query_String);
162  return FALSE;
163  }
164 
165  if (isset($debug_alternc)) {
166  $debug_alternc->add("SQL Query : (".substr($debug_chrono_start,0,5)." ms)\t $Query_String");
167  $debug_alternc->nb_sql_query++;
168  $debug_alternc->tps_sql_query += $debug_chrono_start;
169  }
170 
171  return TRUE;
172  }
$Query_String
Definition: db_mysql.php:36
halt($msg)
Definition: db_mysql.php:357
is_connected()
Definition: db_mysql.php:106
const THROW_EXCEPTIONS
Definition: bootstrap.php:69
DB_Sql::quote (   $string)
inherited

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

Parameters
stringstring to escape
Returns
string escaped string

Definition at line 292 of file db_mysql.php.

References $string.

292  {
293  return $this->pdo_instance->quote($string);
294  }
$string
Definition: logs_tail.php:45
DB_Sql::table_names ( )
inherited

Definition at line 374 of file db_mysql.php.

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

374  {
375  $this->query("SHOW TABLES");
376  $return = array();
377  while ($this->next_record())
378  $return[] = array('table_name' => $this->p(0), 'tablespace_name' => $this->Database, 'database' => $this->Database);
379 
380  return $return;
381  }
p($Name)
Definition: db_mysql.php:279
next_record()
walk result set
Definition: db_mysql.php:182
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:122
DB_Sql::unlock ( )
inherited

Definition at line 231 of file db_mysql.php.

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

Referenced by DB_Sql\nextid().

231  {
232  if (!$this->is_connected())
233  return FALSE;
234 
235  if (!$this->query('unlock tables')) {
236  $this->halt("unlock() failed.");
237  return FALSE;
238  }
239  }
halt($msg)
Definition: db_mysql.php:357
is_connected()
Definition: db_mysql.php:106
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:122

Member Data Documentation

DB_Sql::$Record = array()
inherited

Definition at line 26 of file db_mysql.php.

Referenced by DB_Sql\current_record().


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