From: ETd <et...@us...> - 2004-10-08 20:15:08
|
Update of /cvsroot/openbash-org/openbash-org/modules In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30064 Added Files: etdDBContainer.php Log Message: etdDBContainer es una clase que pretende servir de super clase para todos aquellos modulos que tengan que ver con los datos almacenados en una tabla: noticias, quotes,... usa ADOdb como capa de abstraccion --- NEW FILE: etdDBContainer.php --- <?php /** * ETD-Soft '04 * 02 / JUN / 04 * * v1.1: add new function getFields() that returns an array with the name of * the different fields in $this->dbTable * date: 10/aug/04 * author: etd <et...@no...> */ include('lib/adodb/adodb.inc.php'); require_once( 'modules/Error.php' ); define( 'ETD_DB_SELECT_LIMIT', 40); //the limit of SELECT statements class etdDBContainer { var $db; //ADODB database abstraction var $cols; //an array of the different rows var $primary_col_index; //$cols[$primary_col_index] is the Field with Primary Index defined var $primary_field; //the name of the field that is the primary index of the table /* * etdDBContainer class constructor * @param array dbOptions = array( * 'user' => The DB user, * 'pass' => The DB password, * 'host' => The hostname of the DB server, * 'name' => The name of the DB to use * ------------ OPTIONAL ---------------- * 'table_conf' => If the settings are stored on a DB, the table, * 'key' => The column name of the diferent key, * 'value' => The column name of the values of those keys * ------------ /OPTIONAL ---------------- * ); * @returns false if something goes wrong */ function etdDBContainer($dbOptions) { if ( !isset($dbOptions) ) { $this = new Error( 'Las opciones de la DB no se han inicializado correctamente.' ); return false; } $dsn = sprintf ('mysql://%s:%s@%s/%s',$dbOptions['user'], $dbOptions['pass'], $dbOptions['host'], $dbOptions['name'] ); $this->db = NewADOConnection($dsn); if (!$this->db) { $this = new Error( 'DB initialization failed!' ); return false; } if (get_class($this) == 'etddbcontainer' ) { $this = new Error('etdDBContainer is an abstracted class!'); return false; } //check if the passed DB table is a valid one if ( !in_array( $dbOptions['table'], $this->db->metaTables() ) ) { $this = new Error( sprintf ( '`%s` no es una tabla de la base de datos `%s`.', $dbOptions['table'], $dbOptions['name']) ); return false; } $this->dbTable = $dbOptions['table']; //find the primary key for de table //$this->a_debug( $this->db->tableInfo($this->dbTable) ); $this->cols = $this->db->metaColumns($this->dbTable); foreach ( $this->cols as $field ) { if ($field->primary_key == 1) { $this->primary_col_index = strtoupper($field->name); $this->primary_field = $field->name; break; } } //echo sprintf('Se ha detectado el indice primario en `%s`. %s<br />', $this->primary_field, $this->primary_col_index); } /* * addRowToTable.- add a new Row to $dbTable * @param string dbTable the table to ad row into * @param array aValues the values to be inserted into the table * @returns PEAR_Error in case of error, 0 otherwise */ function addRowToTable( $dbTable, $aValues ) { $tmpTable = $this->dbTable; $tmpCols = $this->cols; $this->dbTable = $this->backupTable; $this->cols = $this->db->metaColumns($this->dbTable); $result = $this->addRow( $aValues ); $this->dbTable = $tmpTable; $this->cols = $tmpCols; return $result; } /* * addRow.- add a new Row to $this->dbTable * @param array aValues the values to be inserted into the table * @returns PEAR_Error in case of error, 0 otherwise */ function addRow( $aValues ) { $n = sizeof($this->cols); if ( $n != sizeof($aValues) ) { return Error( sprintf('El numero de valores pasados (%s) no coincide con el numero de campos de la tabla(%s). ', sizeof($aValues), $n) ); } echo '<div style="color: red;">no implementado!addRow</div>'; echo '<div style="border: thin solid gray; background-color: silver; "><pre>'.print_r($this->cols).'</pre></div>'; return; for ( $i = 0; $i<$n; $i++) { $sFields .= sprintf(" `%s`,", $this->cols[$i]['name']); $sValues .= sprintf(" '%s',", $aValues[$i]); } //cut the trailing comma $sFields = substr( $sFields, 0, -1); $sValues = substr( $sValues, 0, -1); $sql = sprintf('INSERT INTO `%s` ( %s ) VALUES ( %s );', $this->dbTable, $sFields, $sValues); $result = $this->db->query( $sql ); if (get_class($result) == 'error' ) { return Error( 'Se ha producido un error al actualizar la base de datos. '.$result->getMessage() ); } return 0; } /* * delRow.- with this function we can delete a row given a * valid $this->cols[$this->primary_col_index] value * @param int iValue the identifier of the row to be deleted * @returns 0 on success, PEAR_Error otherwise */ function delRow( $iValue ) { echo '<div style="color: red;">no implementado!delRow</div>'; return; if ( !$this->isValid($iValue) ) { return Error( sprintf('No se encuentra en la tabla [%s] una fila cuyo campo `%s` sea: %s', $this->dbTable, $this->primary_field, $iValue) ); } $sql = sprintf( "DELETE FROM `%s` WHERE `%s` = '%s' LIMIT 1", $this->dbTable, $this->primary_field, $iValue ); $result = $this->db->Execute( $sql ); if ( get_class($result) == 'error' ) { return Error( 'Error al eliminar la fila solicitada. '.$result->getMessage() ); } return 0; //everything is OK } /* * isValid.- with this function we can determine whether the passed * value is valid or not * @param int iValue the identifier of the row * @returns boolean true if a valid key was passed, false, otherwise */ function isValid( $iValue ) { echo '<div style="color: red;">no implementado!isValid</div>'; return; if ( is_numeric($iValue) ) { $sql = sprintf('SELECT `%s` FROM `%s` WHERE 1 LIMIT 0, %s', $this->primary_field, $this->dbTable, ETD_DB_SELECT_LIMIT); $tmp_ids = $this->db->getCol( $sql ); if ( in_array( $iValue, $tmp_ids ) ) { return true; } } return false; } /* * getRows.- this function returns an associative array * that contains all the data from $this->dbTable * $return array a nested array */ function getRows() { $sql = sprintf('SELECT * FROM `%s`', $this->dbTable); $this->db->SetFetchMode(ADODB_FETCH_ASSOC); $result = $this->db->Execute($sql); if (!$result) { return new Error( 'No se han podido seleccionar todos los elementos de la tabla.'.$this->db->ErrorMsg() ); } while (!$result->EOF) { $tmp[] = $result->fields; $result->MoveNext(); } return $tmp; } /* * getFields.- this function returns an associative array * that contains all the fields from $this->dbTable * $return array a nested array */ function getFields() { $tmp = $this->db->MetaColumns($this->dbTable); foreach( $tmp as $field ) { $result[] = $field->name; } return $result; } } ?> |