/** * Constructor * * @access protected * @param array full liveuser configuration array * @return void * @see LiveUser::factory() */ function LiveUser_Admin_Perm_Container_MDB_Simple(&$connectOptions) { if (is_array($connectOptions)) { $function = 'connect'; if (isset($connectOptions['function'])) { $function = $connectOptions['function']; unset($connectOptions['function']); } foreach ($connectOptions as $key => $value) { if (isset($this->{$key})) { $this->{$key} = $value; } } if (isset($connectOptions['connection']) && MDB::isConnection($connectOptions['connection'])) { $this->dbc =& $connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['optimize'] = 'portability'; if ($function == 'singleton') { $this->dbc =& MDB::singleton($connectOptions['dsn'], $options); } else { $this->dbc =& MDB::connect($connectOptions['dsn'], $options); } if (!MDB::isError($this->dbc)) { $this->init_ok = true; } } } }
function testSetDbInstanceDefault() { $db =& MDB::connect(DB_DSN, $GLOBALS['DB_OPTIONS']); $qt =& new MDB_QueryTool(); $qt->setDbInstance($db); $dbActual =& $qt->getDbInstance(); $this->assertEqual($db->fetchmode, $dbActual->fetchmode); }
/** * Check if the two instances are the same by comparing * the fetchMode, since this is the easiest to compare if * two objects are the same in PHP4. * We can do that since the querytool sets the fetch mode to * MDB_FETCHMODE_ASSOC. * Not very nice but it works. * */ function test_default() { $db =& MDB::connect(DB_DSN); $qt =& new MDB_QueryTool(); $qt->setDbInstance($db); $dbActual =& $qt->getDbInstance(); $this->assertEquals($db->fetchmode, $dbActual->fetchmode); }
function MetabaseSetupDatabaseObject($arguments, &$db) { _convertArguments($arguments, $dsninfo, $options); $db =& MDB::connect($dsninfo, $options); if (MDB::isError($db) || !is_object($db)) { return $db->getMessage(); } return ''; }
/** * Connects to the db * * @return object DB The database object * @access private */ function &_db_Connect($dsn) { $this->_debugMessage('_db_Connect($dsn)'); if (is_object($this->db)) { return $this->db; } $db =& MDB::connect($dsn); $this->_testFatalAbort($db, __FILE__, __LINE__); return $db; }
function setUp() { $this->dsn = $GLOBALS['dsn']; $this->options = $GLOBALS['options']; $this->database = $GLOBALS['database']; $this->db =& MDB::connect($this->dsn, $this->options); if (MDB::isError($this->db)) { $this->assertTrue(false, 'Could not connect to database in setUp'); exit; } $this->db->setDatabase($this->database); $this->fields = array('user_name', 'user_password', 'subscribed', 'user_id', 'quota', 'weight', 'access_date', 'access_time', 'approved'); $this->types = array('text', 'text', 'boolean', 'integer', 'decimal', 'float', 'date', 'time', 'timestamp'); $this->clearTables(); }
/** * Connect to database by using the given DSN string * * @access private * @param mixed DSN string | array | mdb object * @return mixed Object on error, otherwise bool */ function _connect($dsn) { if (is_string($dsn) || is_array($dsn)) { $this->db =& MDB::connect($dsn, $this->options['db_options']); } elseif (is_a($dsn, 'mdb_common')) { $this->db = $dsn; } elseif (is_object($dsn) && MDB::isError($dsn)) { return PEAR::raiseError($dsn->getMessage(), $dsn->code); } else { return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__, 41, PEAR_ERROR_RETURN, null, null); } if (MDB::isError($this->db) || PEAR::isError($this->db)) { return PEAR::raiseError($this->db->getMessage(), $this->db->code); } return true; }
/** * Connect to database by using the given DSN string * * @param mixed &$db DSN string | array | mdb object * * @return boolean|PEAR_Error on error * @access private */ function _connect(&$db) { if (is_object($db) && is_a($db, 'MDB_Common')) { $this->db =& $db; } elseif (is_string($db) || is_array($db)) { include_once 'MDB.php'; $this->db =& MDB::connect($db); } elseif (is_object($db) && MDB::isError($db)) { return PEAR::raiseError($db->getMessage(), $db->code); } else { return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__, TRANSLATION2_ERROR_CANNOT_CONNECT, PEAR_ERROR_RETURN); } if (PEAR::isError($this->db)) { return $this->db; } return true; }
/** * Init MDB container * * @access public * @return void * @throws HTTP_FloodControl_Exception if it is impossible to establish database connection. */ public function set() { $dsn = $this->_options['dsn']; if (is_string($dsn) || is_array($dsn)) { $this->_db = MDB::connect($dsn); } else { if (is_object($dsn) && is_a($dsn, 'mdb_common')) { $this->_db = $dsn; } else { throw new HTTP_FloodControl_Exception('Incorrect DSN.'); } } if (PEAR::isError($this->_db)) { throw new HTTP_FloodControl_Exception($this->_db->getMessage(), $this->_db->getCode()); } }
/** * Connect to database by using the given DSN string * * @param string $dsn DSN string * * @access private * @return mixed Object on error, otherwise bool */ function _connect($dsn) { if (is_string($dsn) || is_array($dsn)) { $this->db = MDB::connect($dsn); } else { if (is_object($dsn) && is_a($dsn, 'mdb_common')) { $this->db = $dsn; } else { if (is_object($dsn) && MDB::isError($dsn)) { return new MDB_Error($dsn->code, PEAR_ERROR_DIE); } else { return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__, 41, PEAR_ERROR_RETURN, null, null); } } } if (MDB::isError($this->db)) { return new MDB_Error($this->db->code, PEAR_ERROR_DIE); } return true; }
/** * Class constructor. * * @see LiveUser::factory() * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_Container_MDB(&$connectOptions) { $this->LiveUser_Auth_Common($connectOptions); if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (isset($connectOptions['connection']) && MDB::isConnection($connectOptions['connection'])) { $this->dbc =& $connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $function = null; if (isset($connectOptions['function'])) { $function = $connectOptions['function']; } $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['optimize'] = 'portability'; if ($function == 'singleton') { $this->dbc =& MDB::singleton($connectOptions['dsn'], $options); } else { $this->dbc =& MDB::connect($connectOptions['dsn'], $options); } if (!MDB::isError($this->dbc)) { $this->init_ok = true; } } } }
} echo $db_type . '<br>'; // Data Source Name: This is the universal connection string $dsn['username'] = $user; $dsn['password'] = $pass; $dsn['hostspec'] = $host; $dsn['phptype'] = $db_type; // MDB::connect will return a Pear DB object on success // or a Pear MDB error object on error // You can also set to TRUE the second param // if you want a persistent connection: // $db = MDB::connect($dsn, TRUE); // you can alternatively build a dsn here //$dsn = "$db_type://$user:$pass@$host/$db_name"; Var_Dump::display($dsn); $db =& MDB::connect($dsn); // With MDB::isError you can differentiate between an error or // a valid connection. if (MDB::isError($db)) { die(__LINE__ . $db->getMessage()); } $manager =& new MDB_Manager(); $input_file = 'metapear_test_db.schema'; // you can either pass a dsn string, a dsn array or an exisiting db connection $manager->connect($db); // lets create the database using 'metapear_test_db.schema' // if you have allready run this script you should have 'metapear_test_db.schema.before' // in that case MDB will just compare the two schemas and make any necessary modifications to the existing DB echo Var_Dump::display($manager->updateDatabase($input_file, $input_file . '.before')) . '<br>'; echo 'updating database from xml schema file<br>'; echo 'switching to database: ' . $db_name . '<br>';
/** * Load the storage container * * @param mixed &$conf Name of array containing the configuration. * @param string $containerName name of the container that should be used * @return boolean true on success or false on failure * * @access public */ function init(&$conf, $containerName) { parent::init($conf, $containerName); if (is_array($conf['storage'])) { if (isset($conf['storage']['connection']) && MDB::isConnection($conf['storage']['connection'])) { $this->dbc =& $conf['storage']['connection']; } elseif (isset($conf['storage']['dsn'])) { $this->dsn = $conf['storage']['dsn']; $function = null; if (isset($conf['storage']['function'])) { $function = $conf['storage']['function']; } $options = null; if (isset($conf['storage']['options'])) { $options = $conf['storage']['options']; } $options['optimize'] = 'portability'; if ($function == 'singleton') { $this->dbc =& MDB::singleton($conf['storage']['dsn'], $options); } else { $this->dbc =& MDB::connect($conf['storage']['dsn'], $options); } if (PEAR::isError($this->dbc)) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => 'could not connect: ' . $this->dbc->getMessage())); return false; } } } return true; }
/** * use this method if you want to connect manually * @param mixed $dsn DSN string, DSN array or MDB object * @param array $options */ function connect($dsn, $options = array()) { if (is_object($dsn)) { $res = $this->db =& $dsn; } else { $res = $this->db =& MDB::connect($dsn, $options); } if (MDB::isError($res)) { // FIXXME what shall we do here? $this->_errorLog($res->getUserInfo()); } else { $this->db->setFetchMode(MDB_FETCHMODE_ASSOC); } }
<?php require_once 'MDB.php'; require_once 'LiveUser.php'; // Plase configure the following file according to your environment $db_user = '******'; $db_pass = '******'; $db_host = 'localhost'; $db_name = 'pear_test'; $dsn = "mysql://{$db_user}:{$db_pass}@{$db_host}/{$db_name}"; $db = MDB::connect($dsn, array('sequence_col_name' => 'id')); if (MDB::isError($db)) { echo $db->getMessage() . ' ' . $db->getUserInfo(); } $db->setFetchMode(MDB_FETCHMODE_ASSOC); $conf = array('autoInit' => true, 'session' => array('name' => 'PHPSESSION', 'varname' => 'ludata'), 'login' => array('method' => 'post', 'username' => 'handle', 'password' => 'passwd', 'force' => false, 'function' => '', 'remember' => 'rememberMe'), 'logout' => array('trigger' => 'logout', 'redirect' => 'home.php', 'destroy' => true, 'method' => 'get', 'function' => ''), 'authContainers' => array(array('type' => 'MDB', 'name' => 'MDB_Local', 'loginTimeout' => 0, 'expireTime' => 3600, 'idleTime' => 1800, 'dsn' => $dsn, 'allowDuplicateHandles' => 0, 'authTable' => 'liveuser_users', 'authTableCols' => array('user_id' => array('name' => 'auth_user_id', 'type' => 'text'), 'handle' => array('name' => 'handle', 'type' => 'text'), 'passwd' => array('name' => 'passwd', 'type' => 'text'), 'lastlogin' => array('name' => 'lastlogin', 'type' => 'timestamp'), 'is_active' => array('name' => 'is_active', 'type' => 'boolean'), 'owner_user_id' => array('name' => 'owner_user_id', 'type' => 'integer'), 'owner_group_id' => array('name' => 'owner_group_id', 'type' => 'integer')))), 'permContainer' => array('dsn' => $dsn, 'type' => 'MDB_Medium', 'prefix' => 'liveuser_')); function logOut() { } function logIn() { } PEAR::setErrorHandling(PEAR_ERROR_RETURN); $usr = LiveUser::singleton($conf); $usr->setLoginFunction('logIn'); $usr->setLogOutFunction('logOut'); $e = $usr->init(); if (PEAR::isError($e)) { //var_dump($usr); die($e->getMessage() . ' ' . $e->getUserinfo()); }
/** * Set the database connection with the given parameters. * * @param string $dsn * @param string $options * @access public */ function RDF_Store_MDB($dsn, $options = null) { require_once 'MDB.php'; // create a new connection object $this->dbConn =& MDB::connect($dsn, $options); }
/** * Connect to database by using the given DSN string * * @access private * @param mixed DSN string | array | mdb object * @return mixed Object on error, otherwise bool */ function _connect($dsn) { $this->log('Auth_Container_MDB::_connect() called.', AUTH_LOG_DEBUG); if (is_string($dsn) || is_array($dsn)) { $this->db =& MDB::connect($dsn, $this->options['db_options']); } elseif (is_subclass_of($dsn, 'mdb_common')) { $this->db = $dsn; } elseif (is_object($dsn) && MDB::isError($dsn)) { return PEAR::raiseError($dsn->getMessage(), $dsn->code); } else { return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__, 41, PEAR_ERROR_RETURN, null, null); } if (MDB::isError($this->db) || PEAR::isError($this->db)) { return PEAR::raiseError($this->db->getMessage(), $this->db->code); } if ($this->options['auto_quote']) { if (strpos('.', $this->options['table']) === false) { $this->options['final_table'] = $this->db->quoteIdentifier($this->options['table']); } else { $t = explode('.', $this->options['table']); for ($i = 0, $count = count($t); $i < $count; $i++) { $t[$i] = $this->db->quoteIdentifier($t[$i]); } $this->options['final_table'] = implode('.', $t); } $this->options['final_usernamecol'] = $this->db->quoteIdentifier($this->options['usernamecol']); $this->options['final_passwordcol'] = $this->db->quoteIdentifier($this->options['passwordcol']); } else { $this->options['final_table'] = $this->options['table']; $this->options['final_usernamecol'] = $this->options['usernamecol']; $this->options['final_passwordcol'] = $this->options['passwordcol']; } return true; }
function testConnect() { $db =& MDB::connect($this->dsn, $this->options); if (MDB::isError($db)) { $this->assertTrue(false, 'Connect failed bailing out - ' . $db->getMessage() . ' - ' . $db->getUserInfo()); } if (MDB::isError($this->db)) { exit; } }
$db_host = The hostname of the database server $db_port = Port used by the database (if not to default) $db_login = The username to use when connecting to the database $db_password = The database account password $db_database = The database name. $db_protocol = How to connect to the database (tcp, unix socket) Including this file connects you to the database, or exits on error. Establish a database connection. On connection error, the message will be output without a proper HTML header. There is no way I can see around this; if track_errors isn't on there seems to be no way to supress the automatic error message output and still be able to access the error text. */ require_once "MDB.php"; $mdb =& MDB::connect(array("phptype" => $dbsys, "username" => $db_login, "password" => $db_password, "hostspec" => $db_host, "protocol" => $db_protocol, "port" => $db_port, "database" => $db_database), array('persistent' => !$db_nopersist, 'optimize' => 'portability', 'HOME' => $oci8_home)); if (MDB::isError($mdb)) { if ($debug_flag) { echo "Error: " . $mdb->getMessage() . "<BR>"; die("Error: " . $mdb->getUserInfo() . "<BR>"); } else { die("<BR><p><BR>" . $vocab['failed_connect_db'] . "<BR>"); } } // Release a mutual-exclusion lock on the named table. See sql_mutex_lock. // All locks are released by closing the transaction. // In most DBMS, a locked table remains locked until you either commit your // transaction or roll it back, either entirely or to a savepoint before you // locked the table; there is no other way. function sql_mutex_unlock($name) {
/** * Create a new MDB connection object and connect to the specified * database * * @param mixed $dbinfo 'data source name', see the MDB::parseDSN * method for a description of the dsn format. * Can also be specified as an array of the * format returned by MDB::parseDSN. * Finally you can also pass an existing db * object to be used. * @param mixed $options An associative array of option names and * their values. * @return mixed MDB_OK on success, or a MDB error object * @access public * @see MDB::parseDSN */ function &connect(&$dbinfo, $options = FALSE) { if (is_object($this->database) && !MDB::isError($this->database)) { $this->disconnect(); } if (is_object($dbinfo)) { $this->database =& $dbinfo; } else { $this->database =& MDB::connect($dbinfo, $options); if (MDB::isError($this->database)) { return $this->database; } } if (is_array($options)) { $this->options = array_merge($options, $this->options); } return MDB_OK; }
/** * Returns a MDB connection with the requested DSN. * A newnew MDB connection object is only created if no object with the * reuested DSN exists yet. * * IMPORTANT: In order for MDB to work properly it is necessary that * you make sure that you work with a reference of the original * object instead of a copy (this is a PHP4 quirk). * * For example: * $mdb =& MDB::sngleton($dsn); * ^^ * And not: * $mdb = MDB::singleton($dsn); * ^^ * * @param mixed $dsn 'data source name', see the MDB::parseDSN * method for a description of the dsn format. * Can also be specified as an array of the * format returned by MDB::parseDSN. * @param mixed $options An associative array of option names and * their values. * @return mixed a newly created MDB connection object, or a MDB * error object on error * @access public * @see MDB::parseDSN */ function &singleton($dsn = NULL, $options = FALSE) { if ($dsn) { $dsninfo = MDB::parseDSN($dsn); $dsninfo_default = array('phptype' => NULL, 'username' => NULL, 'password' => NULL, 'hostspec' => NULL, 'database' => NULL); $dsninfo = array_merge($dsninfo_default, $dsninfo); $keys = array_keys($GLOBALS['_MDB_databases']); for ($i = 0, $j = count($keys); $i < $j; ++$i) { $tmp_dsn = $GLOBALS['_MDB_databases'][$keys[$i]]->getDSN('array'); if ($dsninfo['phptype'] == $tmp_dsn['phptype'] && $dsninfo['username'] == $tmp_dsn['username'] && $dsninfo['password'] == $tmp_dsn['password'] && $dsninfo['hostspec'] == $tmp_dsn['hostspec'] && $dsninfo['database'] == $tmp_dsn['database']) { MDB::setOptions($GLOBALS['_MDB_databases'][$keys[$i]], $options); return $GLOBALS['_MDB_databases'][$keys[$i]]; } } } else { if (is_array($GLOBALS['_MDB_databases']) && reset($GLOBALS['_MDB_databases'])) { $db =& $GLOBALS['_MDB_databases'][key($GLOBALS['_MDB_databases'])]; return $db; } } $db =& MDB::connect($dsn, $options); return $db; }
/** * Initialize the storage container * * @param array Array with the storage configuration * @return bool true on success, false on failure. * * @access public */ function init($storageConf) { parent::init($storageConf); if (!MDB::isConnection($this->dbc) && !is_null($this->dsn)) { $this->options['optimize'] = 'portability'; if ($this->function == 'singleton') { $dbc =& MDB::singleton($this->dsn, $this->options); } else { $dbc =& MDB::connect($this->dsn, $this->options); } if (PEAR::isError($dbc)) { $this->stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => 'could not connect: ' . $dbc->getMessage(), 'debug' => $dbc->getUserInfo())); return false; } $this->dbc =& $dbc; } if (!MDB::isConnection($this->dbc)) { $this->stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => 'storage layer configuration missing')); return false; } return true; }
function &connect($dsn, $options = FALSE) { if (!is_array($options) && $options) { $options['persistent'] = TRUE; } $db =& MDB::connect($dsn, $options); if (PEAR::isError($db)) { return $db; } $obj =& new MDB_PEAR_PROXY($db); return $obj; }
/** * use this method if you want to connect manually * * @param mixed $dsn DSN string, DSN array or MDB object * @param array $options db options * @param integer $MDBversion MDB version (1=MDB, 2=MDB2) * * @return void */ function connect($dsn, $options = array(), $MDBversion = 1) { if (is_object($dsn)) { $res =& $dsn; } elseif ($MDBversion == 2) { include_once 'MDB2.php'; if (!isset($options['idxname_format'])) { $options['idxname_format'] = '%s'; } $res =& MDB2::connect($dsn, $options); } else { include_once 'MDB.php'; $res =& MDB::connect($dsn, $options); } if (PEAR::isError($res)) { // FIXXME what shall we do here? $this->_errorLog($res->getUserInfo()); return; } $this->setDbInstance($res); }
/** * * * * @param array &$storageConf Array with the storage configuration * @return boolean true on success, false on failure. * * @access public */ function init($storageConf) { if (!parent::init($storageConf)) { return false; } if (array_key_exists('connection', $storageConf) && MDB::isConnection($storageConf['connection'])) { $this->dbc =& $storageConf['connection']; } elseif (array_key_exists('dsn', $storageConf)) { $this->dsn = $storageConf['dsn']; $function = null; if (array_key_exists('function', $storageConf)) { $function = $storageConf['function']; } $options = null; if (array_key_exists('options', $storageConf)) { $options = $storageConf['options']; } $options['optimize'] = 'portability'; if ($function == 'singleton') { $this->dbc =& MDB::singleton($storageConf['dsn'], $options); } else { $this->dbc =& MDB::connect($storageConf['dsn'], $options); } if (PEAR::isError($this->dbc)) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => 'could not connect: ' . $this->dbc->getMessage())); return false; } } return true; }