Re: db inside a class
| From: | Ian P. Christian | Date: | Sat, 29 Mar 2003 20:20:30 +0000 |
| Subject: | Re: db inside a class | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-4513@lists.php.net to get a copy of this message | ||
Quoting Mika Tuupola <tuupola@appelsiini.net>:
> On Fri, 28 Mar 2003, Patrik Berggren wrote:
>
> > I'm trying to use the pear db insade a class of my own, I know that
> > this have been up on the list before(did a search) but I could't get
> > the help need from the privous aswers so here I go:
A snippet from some work I'm doing under the Horde framework atm. Hope this
helps?
Ian.
class Sas_domain_Driver_sql extends Sas_domain_Driver {
/** Hash containing connection parameters. */
var $_params = array();
/** Handle for the current database connection.
@var object DB $db */
var $_db;
/** Boolean indicating whether or not we're connected to the SQL server. */
var $_connected = false;
/**
* Attempts to open a persistent connection to the SQL server.
*
* @return boolean True on success; exits (Horde::fatal()) on error.
*/
function _connect()
{
if (!$this->_connected) {
if (!is_array($this->_params)) {
Horde::fatal(new PEAR_Error(_("No configuration information
specified for SQL Tasks.")), __FILE__, __LINE__);
}
if (!isset($this->_params['phptype'])) {
Horde::fatal(new PEAR_Error(_("Required 'phptype' not
specified in tasks configuration.")), __FILE__, __LINE__);
}
if (!isset($this->_params['hostspec'])) {
Horde::fatal(new PEAR_Error(_("Required 'hostspec' not
specified in tasks configuration.")), __FILE__, __LINE__);
}
if (!isset($this->_params['username'])) {
Horde::fatal(new PEAR_Error(_("Required 'username' not
specified in tasks configuration.")), __FILE__, __LINE__);
}
if (!isset($this->_params['password'])) {
Horde::fatal(new PEAR_Error(_("Required 'password' not
specified in tasks configuration.")), __FILE__, __LINE__);
}
if (!isset($this->_params['database'])) {
Horde::fatal(new PEAR_Error(_("Required 'database' not
specified in tasks configuration.")), __FILE__, __LINE__);
}
if (!isset($this->_params['domain_table'])) {
Horde::fatal(new PEAR_Error(_("Required 'domain_table' not
specified in tasks configuration.")), __FILE__, __LINE__);
}
if (!isset($this->_params['dns_table'])) {
Horde::fatal(new PEAR_Error(_("Required 'dns_table' not
specified in tasks configuration.")), __FILE__, __LINE__);
}
/* Connect to the SQL server using the supplied parameters. */
include_once 'DB.php';
$this->_db = &DB::connect($this->_params, true);
if (DB::isError($this->_db)) {
Horde::fatal($this->_db, __FILE__, __LINE__);
}
/* Enable the "portability" option. */
$this->_db->setOption('optimize', 'portability');
$this->_connected = true;
}
return true;
}
/**
* Disconnect from the SQL server and clean up the connection.
*
* @return boolean true on success, false on failure.
*/
function _disconnect()
{
if ($this->_connected) {
$this->_connected = false;
return $this->_db->disconnect();
}
return true;
}
/**
* Retrieves the user's tasks from the database.
*
* @return mixed True on success, PEAR_Error on failure.
*/
function retrieve()
{
/* Make sure we have a valid database connection. */
$this->_connect();
// stuff
return true;
}
/**
* Stores the user's tasks to SQL server.
*
* @return mixed True on success, PEAR_Error on failure.
*/
function store()
{
/* Make sure we have a valid database connection. */
$this->_connect();
// more stuff
return true;
}
}
?>