db.php File Reference


Detailed Description

Everything connected with accessing database.

Definition in file db.php.

Go to the source code of this file.

Functions

 build_insert_sql_from_array ($array, $pre= ', $post= ', $quote=FALSE, $value_getter=get_identity_value')
 Creates string which you can pass to INSERT SQL command as VALUES based on an array.
 exists ($sql)
 get_id_value ($obj)
 Helper for build_insert_sql_from_array().
 get_identity_value ($obj)
 Helper for build_insert_sql_from_array().
 insert ($sql)
 For INSERT with autoincremented PK.
 insert_unique ($sql, $errormsg)
 Executes INSERT or UPDATE SQL statement.
 one_line_query ($sql, $errormsg=Error.')
 Performs SQL query that return only one record.
 query ($sql)
 update ($sql)
 For INSERT/UPDATE/DELETE.


Function Documentation

build_insert_sql_from_array array,
pre = ',
post = ',
quote = FALSE,
value_getter = get_identity_value'
 

Creates string which you can pass to INSERT SQL command as VALUES based on an array.

This is useful for adding multiple values in one SQL command. Example:

    $a = array('a', 'b', 'c');
    $s = build_insert_sql_from_array($a, '1', '2', TRUE);
will generate:
    $s == "(1, 'a', 2),(1, 'b', 2),(1, 'c', 2)";
and you can use it like this:
    db_query("INSERT INTO table VALUES $s");
Parameters:
[in] $array Array containing e.g. strings or objects etc.
[in] $pre String prepended for each value.
[in] $post String appended for each value.
[in] $quote Should be the value quoted?
[in] $value_getter Function which for each element in the array returns its value. Default is identity.
Returns:
String usable in SQL command.

Definition at line 257 of file db.php.

00257                                                                                                                             {
00258     if ($pre):
00259       $pre = "$pre,";
00260     endif;
00261     if ($post):
00262       $post = ",$post";
00263     endif;
00264     $sqllines = array();
00265     foreach ($array as $obj):
00266       $value = $value_getter($obj);
00267       if ($quote):
00268         $sqllines[] = "($pre '$value' $post)";
00269       else:
00270         $sqllines[] = "($pre $value $post)";
00271       endif;
00272     endforeach;
00273     return join(',', $sqllines);
00274   }

exists sql  ) 
 

Parameters:
[in] $sql SQL statement in a form of "SELECT COUNT(...) FROM ...".
Returns:
TRUE if statement executed succesfully and it returned value of 1, FALSE otherwise.
Exceptions:
Exception On SQL error.

Definition at line 150 of file db.php.

00150                         {
00151     $db = new Db();
00152     $result = $db->query($sql);
00153 
00154     /*
00155       This clearly shows how is PHP badly designed language.
00156       You cannot simply write
00157       $count = $result->fetch_row()[0];
00158       You even cannot write
00159       $count = ($result->fetch_row())[0];
00160     */
00161     $count = $result->fetch_row();
00162     $count = $count[0];
00163     
00164     $db->close();
00165     if ($count == 1):
00166       return TRUE;
00167     endif;
00168     return FALSE;
00169   }

get_id_value obj  ) 
 

Helper for build_insert_sql_from_array().

Returns:
$obj->get_id().

Definition at line 228 of file db.php.

00228                               {
00229     return $obj->get_id();
00230   }

get_identity_value obj  ) 
 

Helper for build_insert_sql_from_array().

Returns:
$obj

Definition at line 220 of file db.php.

00220                                     {
00221     return $obj;
00222   }

insert sql  ) 
 

For INSERT with autoincremented PK.

Returns:
New record PK value or 0 if it's not INSERT/UPDATE statement or the table does not have autoincremented column.
Exceptions:
Exception On SQL error.

Definition at line 208 of file db.php.

00208                         {
00209     $db = new Db();
00210     $db->query($sql);
00211     $id = $db->insert_id;
00212     $db->close();
00213     return $id;
00214   }

insert_unique sql,
errormsg
 

Executes INSERT or UPDATE SQL statement.

Exceptions:
Exception($errormsg) If the SQL statement violates primary key or unique index uniqueness.
Exception If the SQL statement fails for some other reason.

Definition at line 129 of file db.php.

00129                                           {
00130     $db = new Db();
00131     try {
00132       $db->query($sql);
00133     }
00134     catch (Exception $e) {
00135       if ($db->errno == 1062):
00136         throw new Exception($errormsg);
00137       else:
00138         throw $e;
00139       endif;
00140     }
00141     $db->close();
00142   }

one_line_query sql,
errormsg = Error.'
 

Performs SQL query that return only one record.

Returns:
Fetched object.
Exceptions:
RecordNotFound($errormsg) if the record does not exists.
Exception On SQL error.

Definition at line 182 of file db.php.

00182                                                       {
00183     $db = new Db();
00184     $result = $db->query($sql);
00185     $obj = $result->fetch_object();
00186     if (!$obj):
00187       throw new RecordNotFound($errormsg);
00188     endif;
00189     return $obj;
00190   }

query sql  ) 
 

Returns:
DbQuery object.

Definition at line 117 of file db.php.

Referenced by Db::__construct(), and Db::query().

00117                        {
00118     $q = new DbQuery($sql);
00119     return $q;
00120   }

update sql  ) 
 

For INSERT/UPDATE/DELETE.

Exceptions:
Exception On SQL error.

Definition at line 196 of file db.php.

00196                         {
00197     $db = new Db();
00198     $db->query($sql);
00199     $db->close();
00200   }


Generated on Sat Sep 24 01:26:42 2005 for Easy PHP Framework by  doxygen 1.4.2