Alternc  latest
Alternc logiel libre pour l'hébergement
DB_users Class Reference

MySQL user database management for AlternC. More...

+ Inheritance diagram for DB_users:

Public Member Functions

 __construct ()
 Creator. 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...
 
 fetchAll ()
 
 fetch ($mode=PDO::FETCH_ASSOC)
 
 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

 $Host
 
 $HumanHostname
 
 $User
 
 $Password
 
 $Client
 
 $Record = array()
 

Private Attributes

 $Database
 
 $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 user database management for AlternC.

This class manage user's databases in MySQL, and user's MySQL accounts.

Definition at line 27 of file m_mysql.php.

Constructor & Destructor Documentation

◆ __construct()

DB_users::__construct ( )

Creator.

Definition at line 34 of file m_mysql.php.

34  {
35  // Sometimes we need to create this object with empty parameters, but by default we fill them with those of the current user's DB
36  global $cuid, $db, $msg;
37 
38  $db->query("select db_servers.* from db_servers, membres where membres.uid= ? and membres.db_server_id=db_servers.id;", array($cuid));
39  if (!$db->next_record()) {
40  $msg->raise("ERROR", 'db_user', _("There are no databases in db_servers for this user. Please contact your administrator."));
41  die();
42  }
43 
44  // Create the object
45  $this->HumanHostname = $db->f('name');
46  $this->Host = $db->f('host');
47  $this->User = $db->f('login');
48  $this->Password = $db->f('password');
49  $this->Client = $db->f('client');
50  $this->Database = "mysql";
51 
52  parent::__construct("mysql", $db->f('host'), $db->f('login'), $db->f('password') );
53 
54  }
global $db
Definition: bootstrap.php:26
$msg
Definition: bootstrap.php:75
$cuid
Definition: bootstrap.php:43

References $cuid, $db, and $msg.

Member Function Documentation

◆ affected_rows()

DB_Sql::affected_rows ( )
inherited

evaluate the result (size, width)

Definition at line 313 of file db_mysql.php.

313  {
314  if (!$this->pdo_query) return 0;
315  return $this->pdo_query->rowCount();
316  }

◆ connect()

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

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

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

◆ current_record()

DB_Sql::current_record ( )
inherited

Definition at line 351 of file db_mysql.php.

351  {
352  return $this->Record;
353  }

References DB_Sql\$Record.

◆ exec()

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

381  {
382  return $this->pdo_instance->exec($query);
383  }
$query
Definition: 3.0.0~3.php:37

References $query.

◆ f()

DB_Sql::f (   $Name)
inherited
Parameters
string$Name
Returns
integer

Definition at line 343 of file db_mysql.php.

343  {
344  if (isset($this->Record[$Name]))
345  return $this->Record[$Name];
346  else
347  return false;
348  }

Referenced by DB_Sql\nextid().

◆ fetch()

DB_Sql::fetch (   $mode = PDO::FETCH_ASSOC)
inherited

Definition at line 246 of file db_mysql.php.

246  {
247  if (!$this->pdo_query) {
248  $this->halt("next_record called with no query pending.");
249  return FALSE;
250  }
251 
252  $data = $this->pdo_query->fetch($mode);
253  $this->Errno = $this->pdo_query->errorCode();
254  $this->Error = $this->pdo_query->errorInfo();
255 
256  if ($data == FALSE) {
257  if ($this->Auto_Free)
258  $this->free();
259  return FALSE;
260  }
261 
262  return $data;
263  }
$mode
Definition: adm_tldedit.php:40
free()
Discard the query result.
Definition: db_mysql.php:120

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

◆ fetchAll()

DB_Sql::fetchAll ( )
inherited

Definition at line 226 of file db_mysql.php.

226  {
227  if (!$this->pdo_query) {
228  $this->halt("next_record called with no query pending.");
229  return FALSE;
230  }
231 
232  $data = $this->pdo_query->fetchAll(PDO::FETCH_BOTH);
233  $this->Errno = $this->pdo_query->errorCode();
234  $this->Error = $this->pdo_query->errorInfo();
235 
236  if ($data == FALSE) {
237  if ($this->Auto_Free)
238  $this->free();
239  return FALSE;
240  }
241 
242  return $data;
243  }

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

◆ free()

DB_Sql::free ( )
inherited

Discard the query result.

This function discards the last query result.

Definition at line 120 of file db_mysql.php.

120  {
121  $this->pdo_query->closeCursor();
122  }

Referenced by DB_Sql\fetch(), DB_Sql\fetchAll(), and DB_Sql\next_record().

◆ halt()

DB_Sql::halt (   $msg)
inherited

private: error handling

Definition at line 443 of file db_mysql.php.

443  {
444  if ($this->Halt_On_Error == "no")
445  return;
446 
447  $this->haltmsg($msg);
448 
449  if ($this->Halt_On_Error != "report")
450  die("Session halted.");
451  }
haltmsg($msg)
private: error handling
Definition: db_mysql.php:457

References $msg, and DB_Sql\haltmsg().

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

◆ haltmsg()

DB_Sql::haltmsg (   $msg)
inherited

private: error handling

Definition at line 457 of file db_mysql.php.

457  {
458  printf("</td></tr></table><b>Database error:</b> %s<br />\n", $msg);
459  printf("<b>MySQL Error</b>: %s (%s)<br />\n",
460  $this->Errno,
461  implode("\n", $this->Error));
462  }

References $msg.

Referenced by DB_Sql\halt().

◆ is_connected()

DB_Sql::is_connected ( )
inherited

Definition at line 125 of file db_mysql.php.

125  {
126  return $this->pdo_instance != FALSE;
127  }

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

◆ last_error()

DB_Sql::last_error ( )
inherited

Definition at line 130 of file db_mysql.php.

130  {
131  return $this->Error;
132  }

References DB_Sql\$Error.

◆ lastid()

DB_Sql::lastid ( )
inherited

Definition at line 361 of file db_mysql.php.

361  {
362  return $this->pdo_instance->lastInsertId();
363  }

◆ lock()

DB_Sql::lock (   $table,
  $mode = "write" 
)
inherited

table locking

Definition at line 268 of file db_mysql.php.

268  {
269  if (!$this->is_connected())
270  return FALSE;
271 
272  $query="lock tables ";
273  if (is_array($table)) {
274  while (list($key,$value)=each($table)) {
275  if ($key=="read" && $key!=0) {
276  $query.="$value read, ";
277  } else {
278  $query.="$value $mode, ";
279  }
280  }
281  $query=substr($query,0,-2);
282  } else {
283  $query.="$table $mode";
284  }
285 
286 
287  if (!$this->query($query)) {
288  $this->halt("lock($table, $mode) failed.");
289  return FALSE;
290  }
291 
292  return TRUE;
293 
294  }
query($Query_String, $arguments=false)
Perform a query.
Definition: db_mysql.php:144
is_connected()
Definition: db_mysql.php:125
$value
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14

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

Referenced by DB_Sql\nextid().

◆ metadata()

DB_Sql::metadata (   $table = '',
  $full = false 
)
inherited

DEPRECATED return table metadata.

Definition at line 434 of file db_mysql.php.

434  {
435  global $msg;
436  $msg->raise("ERROR", 'Mysql', 'function is no longer implemented (metadata())');
437  return FALSE;
438  }

References $msg.

◆ next_record()

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

205  {
206  if (!$this->pdo_query) {
207  $this->halt("next_record called with no query pending.");
208  return FALSE;
209  }
210 
211  $this->Record = $this->pdo_query->fetch(PDO::FETCH_BOTH);
212  $this->Row++;
213  $this->Errno = $this->pdo_query->errorCode();
214  $this->Error = $this->pdo_query->errorInfo();
215 
216  if ($this->Record == FALSE) {
217  if ($this->Auto_Free)
218  $this->free();
219  return FALSE;
220  }
221 
222  return TRUE;
223  }

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

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

◆ nextid()

DB_Sql::nextid (   $seq_name)
inherited

get next sequence numbers

Definition at line 389 of file db_mysql.php.

389  {
390  if (!$this->is_connected())
391  return FALSE;
392 
393  if ($this->lock($this->Seq_Table)) {
394  /* get sequence number (locked) and increment */
395  $q = sprintf("select nextid from %s where seq_name = '%s'",
396  $this->Seq_Table,
397  $seq_name);
398  $this->query($q);
399  $this->next_record();
400 
401  $id = $this->f('nextid');
402 
403  /* No current value, make one */
404  if (!$id) {
405  $currentid = 0;
406  $q = sprintf("insert into %s values('%s', %s)",
407  $this->Seq_Table,
408  $seq_name,
409  $currentid);
410  $this->query($q);
411  } else {
412  $currentid = $id;
413  }
414 
415  $nextid = $currentid + 1;
416  $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
417  $this->Seq_Table,
418  $nextid,
419  $seq_name);
420  $this->query($q);
421  $this->unlock();
422  } else {
423  $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
424  return FALSE;
425  }
426 
427  return $nextid;
428  }
f($Name)
Definition: db_mysql.php:343
lock($table, $mode="write")
table locking
Definition: db_mysql.php:268
next_record()
walk result set
Definition: db_mysql.php:205
unlock()
table unlocking
Definition: db_mysql.php:299
$q
Definition: menu_aws.php:32
if(!isset($is_include)) if(! $key &&! $crt) $id

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().

◆ nf()

DB_Sql::nf ( )
inherited

shorthand notation

Definition at line 330 of file db_mysql.php.

330  {
331  return $this->num_rows();
332  }
num_rows()
Definition: db_mysql.php:317

References DB_Sql\num_rows().

◆ np()

DB_Sql::np ( )
inherited

Definition at line 334 of file db_mysql.php.

334  {
335  print $this->num_rows();
336  }
print
Definition: mail_add.php:92

References DB_Sql\num_rows(), and print.

◆ num_fields()

DB_Sql::num_fields ( )
inherited

Definition at line 322 of file db_mysql.php.

322  {
323  if (!$this->pdo_query) return 0;
324  return $this->pdo_query->columnCount();
325  }

◆ num_rows()

DB_Sql::num_rows ( )
inherited

Definition at line 317 of file db_mysql.php.

317  {
318  if (!$this->pdo_query) return 0;
319  return $this->pdo_query->rowCount();
320  }

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

◆ p()

DB_Sql::p (   $Name)
inherited

Definition at line 356 of file db_mysql.php.

356  {
357  print $this->Record[$Name];
358  }

References print.

Referenced by DB_Sql\table_names().

◆ query()

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

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

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().

◆ quote()

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

371  {
372  return $this->pdo_instance->quote($string);
373  }
$string
Definition: logs_tail.php:38

References $string.

◆ table_names()

DB_Sql::table_names ( )
inherited

Definition at line 465 of file db_mysql.php.

465  {
466  $this->query("SHOW TABLES");
467  $return = array();
468  while ($this->next_record())
469  $return[] = array('table_name' => $this->p(0), 'tablespace_name' => $this->Database, 'database' => $this->Database);
470 
471  return $return;
472  }
p($Name)
Definition: db_mysql.php:356

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

◆ unlock()

DB_Sql::unlock ( )
inherited

table unlocking

Definition at line 299 of file db_mysql.php.

299  {
300  if (!$this->is_connected())
301  return FALSE;
302 
303  if (!$this->query('unlock tables')) {
304  $this->halt("unlock() failed.");
305  return FALSE;
306  }
307  }

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

Referenced by DB_Sql\nextid().

Member Data Documentation

◆ $Auto_Free

DB_Sql::$Auto_Free = False
privateinherited

Definition at line 36 of file db_mysql.php.

◆ $Client

DB_users::$Client

Definition at line 29 of file m_mysql.php.

◆ $Database

DB_Sql::$Database
privateinherited

Definition at line 31 of file db_mysql.php.

Referenced by DB_Sql\connect().

◆ $Debug

DB_Sql::$Debug = False
privateinherited

Definition at line 37 of file db_mysql.php.

◆ $Errno

DB_Sql::$Errno
privateinherited

Definition at line 47 of file db_mysql.php.

◆ $Error

DB_Sql::$Error
privateinherited

Definition at line 48 of file db_mysql.php.

Referenced by DB_Sql\last_error().

◆ $Halt_On_Error

DB_Sql::$Halt_On_Error = "no"
privateinherited

Definition at line 38 of file db_mysql.php.

◆ $Host

DB_users::$Host

Definition at line 29 of file m_mysql.php.

◆ $HumanHostname

DB_users::$HumanHostname

Definition at line 29 of file m_mysql.php.

◆ $num_rows

DB_Sql::$num_rows
privateinherited

Definition at line 44 of file db_mysql.php.

◆ $Password

DB_users::$Password

Definition at line 29 of file m_mysql.php.

◆ $pdo_instance

DB_Sql::$pdo_instance = NULL
privateinherited

Definition at line 54 of file db_mysql.php.

◆ $pdo_query

DB_Sql::$pdo_query = NULL
privateinherited

Definition at line 55 of file db_mysql.php.

◆ $Query_String

DB_Sql::$Query_String
privateinherited

Definition at line 51 of file db_mysql.php.

Referenced by DB_Sql\query().

◆ $Record

DB_Sql::$Record = array()
inherited

Definition at line 42 of file db_mysql.php.

Referenced by DB_Sql\current_record().

◆ $Row

DB_Sql::$Row = 0
privateinherited

Definition at line 43 of file db_mysql.php.

◆ $Seq_Table

DB_Sql::$Seq_Table = "db_sequence"
privateinherited

Definition at line 39 of file db_mysql.php.

◆ $User

DB_users::$User

Definition at line 29 of file m_mysql.php.


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