SourceForge.net LogoPHPGallue
[ class tree: libbase ] [ index: libbase ] [ all elements ]

Class: GALDatabase

Source Location: /libbase/db.inc.php

Class Overview


This class is a wrapper around a mysql connection ressource.


Author(s):

Implements interfaces:

Variables

Methods



Class Details

[line 32]
This class is a wrapper around a mysql connection ressource.

It provides wrapper for the most important mysql functions and additionally has basic support for transactions.




Tags:

todo:  Translate and internationalize error messages.


[ Top ]


Class Variables

$database =

[line 49]



Tags:

access:  protected

Type:   string


[ Top ]

$handle =

[line 54]

The database connection.



Tags:

access:  protected

Type:   resource


[ Top ]

$hostname =

[line 37]



Tags:

access:  protected

Type:   string


[ Top ]

$passwd =

[line 45]



Tags:

access:  protected

Type:   string


[ Top ]

$persistent =  false

[line 58]



Tags:

access:  protected

Type:   bool


[ Top ]

$resultFactory =

[line 83]



Tags:

access:  protected

Type:   GALResultFactory;


[ Top ]

$rollingBack =  false

[line 75]

Indicates if a nested transaction is rolled back.

If currently a nested transaction is rolled back, this database connection object doesn't accept calls of query() or rawQuery() until $transactionNestLevel has reached 0 by subsequently calling rollback().




Tags:

access:  protected

Type:   bool


[ Top ]

$tableFactory =

[line 87]



Tags:

access:  protected

Type:   GALTableFactory;


[ Top ]

$toDelete = array()

[line 79]



Tags:

access:  protected

Type:   array


[ Top ]

$transactionNestLevel =  0

[line 66]

Indicates the current depth of a nested transaction.

MySQL doesn't support nested transactions, that means transactions with nested queries of "START TRANSACTION". This class allows subsequent calls of startTransaction().




Tags:

access:  protected

Type:   int


[ Top ]

$username =

[line 41]



Tags:

access:  protected

Type:   string


[ Top ]



Class Methods


constructor __construct [line 98]

GALDatabase __construct( string $host, string $user, string $pw, string $db)

Creates a GALDatabase object.

The constructor doesn't connect to the database, this must be done with connect() or pconnect().




Parameters:

string   $host   The hostname or ip address of the database server.
string   $user   The username used to log in at the database server.
string   $pw   The password used to log in at the database server.
string   $db   The database that will be used.

[ Top ]

destructor __destruct [line 117]

void __destruct( )

Destroy a GALDatabase object.

This method is automatically called when a GALDatabase object is deleted, e.g. with unset() or the scope of a local variable is left. Usually there's no need for programmers to call it by hand. To disconnect a database connection, use close().




[ Top ]

method affected_rows [line 184]

int affected_rows( )

Returns the number of rows that were affected by the last query.

This method is a wrapper for mysql_affected_rows().




Tags:

return:  The number of rows that were affected by the last query.


[ Top ]

method buildResult [line 539]

void buildResult( resource $result)

Implements the GALResultFactory interface.




Implementation of:
GALResultFactory::buildResult()

Parameters:

resource   $result  

[ Top ]

method buildTable [line 562]

void buildTable( GALDatabase $db, string $tableName, resource $descriptorHandle)

Implements the GALTableFactory interface.




Implementation of:
GALTableFactory::buildTable()

Parameters:

GALDatabase   $db  
string   $tableName  
resource   $descriptorHandle  

[ Top ]

method cleanName [line 320]

string cleanName( string $string)

Remove characters from the given string that might mangle a SQL query.

Used to escape table and field names in SQL queries if they aren't given as string constants, but e.g. read from a configuration file or passed as function argument. This method triggers an E_USER_NOTICE if it finds a character that is going to be removed.




Tags:

return:  The string without unwanted characters.


Parameters:

string   $string   The string to clean.

[ Top ]

method close [line 195]

void close( )

Closes the connection.

If this object is connected, a currently running transaction is committed and the connection is closed with mysql_close(). If this object isn't connected, nothing happens.




[ Top ]

method commit [line 472]

void commit( )

Commits the current transaction.

$transactionNestLevel gets decreased by one. If $transactionNestLevel reaches 0, "COMMIT" is submitted.




[ Top ]

method connect [line 218]

void connect( )

Connects to the database server.

This method uses the information given in the constructor to connect to a database server. If connnecting fails, a GAL_EDatabase is thrown.




[ Top ]

method connected [line 157]

bool connected( )

Checks if GALDatabase::handle is set.



[ Top ]

method deleteDelayed [line 459]

void deleteDelayed( string $file)

Deletes a file only after the transaction succeeds.

This method was introduced, because sometimes a file is associated with a database entity, and when deleting the database entity, the file has to be deleted, too. If you want to delete the entity within a transaction, one has to make sure that the whole operation, including the deletion of the file, can be rolled back. The solution implemented here is to delete the file after committing the transaction. This can result in orphan files (when deleting them fails), but in many cases this is better than a missing file. If called while the database is inside a transaction, the file or directory is stored in toDelete and deleted after the transaction is committed. If deleting fails, only warning messages are generated, but the transaction succeeds. If the database isn't inside a transaction, the file is deleted immediately.




Parameters:

string   $file   The file or directory to delete.

[ Top ]

method errno [line 261]

int errno( )

Wrapper for mysql_errno().



Tags:

return:  The error number associated with this database connection.


[ Top ]

method error [line 269]

string error( )

Wrapper for mysql_error().



Tags:

return:  The error message associated with this database connection.


[ Top ]

method escape [line 301]

string escape( string $string)

Use mysql_real_escape_string() to escape 'evil' characters.

This method calls mysql_real_escape_string() with the given string and the current database connection. It fails and throws a GAL_EDatabase if this database object isn't connected.




Tags:

return:  The escaped string.


Parameters:

string   $string   The string to convert.

[ Top ]

method executeDeletion [line 491]

void executeDeletion( )

Execute the delayed deletion of files.

This method loops throuhg all files stored in toDelete and tries to delete them. On failure it triggers an error message. Finally toDelete is made empty.




Tags:

access:  protected


[ Top ]

method getResultFactory [line 553]

GALResultFactory getResultFactory( )

Returns the factory to construct results.



[ Top ]

method getTableFactory [line 576]

GALTableFactory getTableFactory( )

Returns the factory to construct tables.



[ Top ]

method last_insert_id [line 277]

int last_insert_id( )

Wrapper for mysql_insert_id().



Tags:

return:  The last field created in an INSERT operation using the AUTO_INCREMENT feature of MySQL.


[ Top ]

method list_fields [line 286]

resource list_fields( string $tableName)

Wrapper for mysql_list_fields().



Parameters:

string   $tableName   The fields of this table are listed.

[ Top ]

method pconnect [line 239]

void pconnect( )

Opens a persistent connection to the database server.

This method is similar to connect(), but instead of mysql_connect() it uses mysql_pconnect().




[ Top ]

method query [line 363]

GALResult query( string $querystring)

Queries the database.

Submits the given query to the database and returns the result as GALResult instead of a MySQL resource.




Tags:

throws:  GAL_EDatabase On failure.


Parameters:

string   $querystring  

[ Top ]

method rawQuery [line 341]

resource rawQuery( string $querystring)

Wrapper for mysql_query().

This method calls mysql_query() with the given querystring on this connection.




Tags:

return:  A MySQL query result resource.
throws:  GAL_EDatabase On failure.


Parameters:

string   $querystring  

[ Top ]

method reconnect [line 167]

void reconnect( )

Closes the current connection and open a new one.

This method forces committing a currently running transaction, closes the current connection and reopens it.




[ Top ]

method rollback [line 514]

void rollback( $e)

Roll back the current transaction.

This method immediately submits "ROLLBACK" to the database, sets GALDatabase::rollingBack to TRUE and decreases $transactionNestLevel by one. When rolling back, this connection only accepts rollback() until $transactionNestLevel has reached 0.




Parameters:

Exception   $e   An exception that is thrown at the end of this method.

[ Top ]

method setResultFactory [line 546]

void setResultFactory( GALResultFactory $factory)

Sets the factory to construct results.



Parameters:

GALResultFactory   $factory  

[ Top ]

method setTableFactory [line 569]

void setTableFactory( GALTableFactory $factory)

Sets the factory to construct tables.



Parameters:

GALTableFactory   $factory  

[ Top ]

method startTransaction [line 435]

void startTransaction( )

Starts a SQL transaction.

This method submits "START TRANSACTION" to the database. As a useful extension to the SQL standard it supports nested transactions, i.e. transactions within transactions. This requires that for every startTransaction() commit() or rollback() is called. Internally, only the first call submits "START TRANSACTION", the subsequent calls increase $transactionNestLevel. When calling commit(), $transactionNestLevel is decreased. The transaction is committed to the database as soon as $transactionNestLevel has reached 0 without errors. If a transaction fails, a "ROLLBACK" is submitted immediately, but it is required that rollback() is called until $transactionNestLevel has reached 0. In the mean time this connection won't accept normal queries, commit() or startTransaction(). An easy way to achieve this is to use exception handling. Note that rollback() automatically re-throws the exception given as argument!

  1.  function aa(GALDatabase $db{
  2.      try {
  3.          $db->startTransaction();
  4.          $db->rawQuery("INSERT INTO `foo` (`bar`, `blubb`) VALUES ('42', 'bla')");
  5.          $db->rawQuery("DELETE FROM `foo` WHERE `bar`<'42'");
  6.          if (!everythingOK()) // whatever additional error handling you want
  7.              throw new Exception('Something failed.');
  8.          }
  9.          $db->commit();
  10.      }
  11.      catch (Exception $e{
  12.          echo $e;
  13.          $db->rollback($e);
  14.      }
  15.  }
  16.  function bb(GALDatabase $db{
  17.      try {
  18.          $db->startTransaction();
  19.          $db->rawQuery("REPLACE INTO `example` (`ID`, `value`) VALUES ('13', 'something')");
  20.          aa($db);
  21.          if (somethingFailed()) {
  22.              throw new Exception('Ooops!');
  23.          }
  24.          $db->commit();
  25.      }
  26.      catch (Exception $e{
  27.          echo $e;
  28.          $db->rollback($e);
  29.      }
  30.  }




[ Top ]

method table [line 376]

GALTable table( mixed $tableName)

Constructs a GALTable object that references the table with the given name in this database.



Tags:

throws:  GAL_Edatabase


[ Top ]


Documentation generated on Wed, 16 Aug 2006 15:35:36 +0200 by phpDocumentor 1.3.0RC6