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

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.

François - aka fser - Serman

2014/06/24

Definition at line 11 of file db_mysql.php.

Constructor & Destructor Documentation

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

Constructor.

Definition at line 46 of file db_mysql.php.

References $db, $host, and $user.

46  {
47 
48  $dsn = sprintf('mysql:dbname=%s;host=%s', $db, $host);
49 
50  try {
51  $this->pdo_instance = new PDO($dsn, $user, $passwd);
52  } catch (PDOException $e) {
53  echo "Mysql", "PDO instance", $e->getMessage();
54  return FALSE;
55  }
56  }
$user
Definition: bootstrap.php:84
$host
Definition: config.php:86
global $db
Definition: bootstrap.php:22

Member Function Documentation

DB_Sql::affected_rows ( )

Definition at line 243 of file db_mysql.php.

243  {
244  return $this->pdo_query->rowCount();
245  }
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 70 of file db_mysql.php.

References $Database, $err, $Host, $Password, $User, and 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 ( )

Definition at line 275 of file db_mysql.php.

References $Record.

275  {
276  return $this->Record;
277  }
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 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)
Parameters
string$Name
Returns
integer

Definition at line 268 of file db_mysql.php.

Referenced by nextid().

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

Discard the query result.

This function discards the last query result.

Definition at line 102 of file db_mysql.php.

Referenced by next_record().

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

Definition at line 357 of file db_mysql.php.

References haltmsg().

Referenced by connect(), lock(), next_record(), nextid(), query(), and 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)

Definition at line 367 of file db_mysql.php.

Referenced by 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 ( )

Definition at line 106 of file db_mysql.php.

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

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

Definition at line 110 of file db_mysql.php.

References $Error.

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

Definition at line 283 of file db_mysql.php.

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

Definition at line 203 of file db_mysql.php.

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

Referenced by 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 
)

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

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 free(), and halt().

Referenced by nextid(), and 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)

Definition at line 308 of file db_mysql.php.

References $id, $q, f(), halt(), is_connected(), lock(), next_record(), query(), and 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 ( )

Definition at line 256 of file db_mysql.php.

References num_rows().

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

Definition at line 260 of file db_mysql.php.

References num_rows().

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

Definition at line 251 of file db_mysql.php.

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

Definition at line 247 of file db_mysql.php.

Referenced by nf(), and np().

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

Definition at line 279 of file db_mysql.php.

Referenced by table_names().

279  {
280  print $this->Record[$Name];
281  }
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 122 of file db_mysql.php.

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

Referenced by lock(), nextid(), table_names(), and 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)

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

Definition at line 374 of file db_mysql.php.

References next_record(), p(), and 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 ( )

Definition at line 231 of file db_mysql.php.

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

Referenced by 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::$Auto_Free = False
private

Definition at line 20 of file db_mysql.php.

DB_Sql::$Database
private

Definition at line 15 of file db_mysql.php.

Referenced by connect().

DB_Sql::$Debug = False
private

Definition at line 21 of file db_mysql.php.

DB_Sql::$Errno
private

Definition at line 31 of file db_mysql.php.

DB_Sql::$Error
private

Definition at line 32 of file db_mysql.php.

Referenced by last_error().

DB_Sql::$Halt_On_Error = "no"
private

Definition at line 22 of file db_mysql.php.

DB_Sql::$Host
private

Definition at line 14 of file db_mysql.php.

Referenced by connect().

DB_Sql::$num_rows
private

Definition at line 28 of file db_mysql.php.

DB_Sql::$Password
private

Definition at line 17 of file db_mysql.php.

Referenced by connect().

DB_Sql::$pdo_instance = NULL
private

Definition at line 40 of file db_mysql.php.

DB_Sql::$pdo_query = NULL
private

Definition at line 41 of file db_mysql.php.

DB_Sql::$Query_String
private

Definition at line 36 of file db_mysql.php.

Referenced by query().

DB_Sql::$Record = array()

Definition at line 26 of file db_mysql.php.

Referenced by current_record().

DB_Sql::$Row = 0
private

Definition at line 27 of file db_mysql.php.

DB_Sql::$Seq_Table = "db_sequence"
private

Definition at line 23 of file db_mysql.php.

DB_Sql::$User
private

Definition at line 16 of file db_mysql.php.

Referenced by connect().


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