public function getDbService() { static $v; if (!empty($v)) { return $v; } $config = $this->getParameter('db'); try { $v = Am_Db::connect($config['mysql']); } catch (Am_Exception_Db $e) { if (APPLICATION_ENV != 'debug') { amDie("Error establishing a database connection. Please contact site webmaster if this error does not disappear long time"); } else { throw $e; } } return $v; }
function getDb() { if (!$this->_db) { $dsn = array(); $dsn['scheme'] = 'mysql'; $dsn['path'] = $this->getConfig('db'); if ($this->getConfig('other_db') == "1") { $dsn = array_merge($dsn, array('host' => $this->getConfig('host'), 'user' => $this->getConfig('user'), 'pass' => $this->getConfig('pass'))); } else { $appOptions = $this->getDi()->getParameters(); $dbConfig = $appOptions['db']['mysql']; $dsn = array_merge($dsn, array('host' => $dbConfig['host'], 'user' => $dbConfig['user'], 'pass' => $dbConfig['pass'])); if (isset($dbConfig['port']) && $dbConfig['port']) { $dsn['port'] = $dbConfig['port']; } } if ($dsn['host'] && strpos($dsn['host'], ':') !== false && preg_match('/\\:(\\d+)$/', $dsn['host'])) { list($dsn['host'], $dsn['port']) = explode(':', $dsn['host']); } $this->_db = Am_Db::connect($dsn, true); $this->_db->setErrorHandler(array($this, 'dbErrorHandler')); $this->_db->setIdentPrefix($this->getConfig('prefix')); $this->_db->query("USE ?#", $dsn['path']); $this->_db->query("SET NAMES utf8"); $this->_db->query("SET SESSION sql_mode=''"); } if ($this->sqlDebug) { if (!empty($this->getDi()->db->_logger)) { $this->_db->setLogger($this->getDi()->db->_logger); } } return $this->_db; }
function validateDbConnect() { $config = $this->form->getValue(); try { $db = Am_Db::connect($config); if (!$db) { return "Check database settings - could not connect to database"; } $db->query("SELECT * FROM ?_ym_transaction LIMIT 1"); } catch (Exception $e) { return "Check database settings - " . $e->getMessage(); } }
protected function populateForm() { $info = $this->getController()->getValue(); $db = Am_Db::connect($info); $tables = array(); foreach ($info['table'] as $k => $v) { if (!empty($v)) { $tables[$k] = new Am_Table($db, $v); } } /// user table $userOptions = array('expr' => 'PHP Expression', 'string' => 'PHP String'); $class = new ReflectionClass('Am_Protect_Table'); foreach ($class->getConstants() as $k => $v) { if (strpos($k, 'FIELD_') !== 0) { continue; } $userOptions[$class->getName() . '::' . $k] = $class->getName() . ':' . $k; } $this->addTable('user', $tables['user'], 'User Table', $userOptions); /// group table if (!empty($tables['group'])) { $groupOptions = array('id' => 'Group ID', 'title' => 'Group Title'); $this->addFieldSelect('group', $tables['group'], 'Group Table', $groupOptions); } /// user <-> group table if (!empty($tables['usergroup'])) { $usergroupOptions = array('Am_Protect_Table::GROUP_UID' => 'User ID', 'Am_Protect_Table::GROUP_GID' => 'Group ID'); $this->addFieldSelect('usergroup', $tables['usergroup'], 'User<->Group Table', $usergroupOptions); } /// session table if (!empty($tables['session'])) { $sessionOptions = array('Am_Protect_SessionTable::FIELD_SID' => 'Session ID', 'Am_Protect_SessionTable::FIELD_UID' => 'User ID', 'Am_Protect_SessionTable::FIELD_IP' => 'IP Address', 'Am_Protect_SessionTable::FIELD_UA' => 'User Agent', 'Am_Protect_SessionTable::FIELD_CREATED' => 'Created', 'Am_Protect_SessionTable::FIELD_CHANGED' => 'Changed'); $this->addTable('session', $tables['session'], 'Session Table', $sessionOptions); } $this->form->addScript()->setScript(<<<CUT \$(function(){ \$("select.field").change(function(){ var row = \$(this).closest(".row"); var val = \$(this).val(); row.find("input[name\$='[text]']").toggle(val=='expr' || val=='string'); }).change(); }); CUT ); $gr = $this->form->addGroup(null, array('class' => 'no-label', 'style' => 'text-align:center')); $gr->addSubmit($this->getButtonName('back'), array('value' => 'Back')); $gr->addSubmit($this->getButtonName('next'), array('value' => 'Next')); }
static function defaultDatabaseErrorHandler($message, $info) { if (!error_reporting()) { return; } if (self::$inSqlError) { return; } self::$inSqlError = true; if (class_exists('ErrorLog', true)) { Am_Di::getInstance()->errorLogTable->log("MYSQL ERROR" . "<br />\n" . nl2br(print_r($info, true))); } self::$inSqlError = false; if (!class_exists('Am_Exception_Db')) { require_once dirname(__FILE__) . '/Exception.php'; } if ($info['code'] == 1062) { $class = 'Am_Exception_Db_NotUnique'; } else { $class = 'Am_Exception_Db'; } $e = new $class("{$message}({$info['code']}) in query: {$info['query']}", @$info['code']); $e->setDbMessage(preg_replace('/ at.+$/', '', $message)); $e->setLogError(false); // already logged // try to parse table name if ($e instanceof Am_Exception_Db_NotUnique && preg_match('/insert into (\\w+)/i', $info['query'], $regs)) { $prefix = Am_Di::getInstance()->db->getPrefix(); $table = preg_replace('/^' . preg_quote($prefix) . '/', '?_', $regs[1]); $e->setTable($table); } throw $e; }
function getDb() { if (!$this->_db) { $dsn = array(); $dsn['scheme'] = 'mysql'; $dsn['path'] = $this->getConfig('db'); if ($this->getConfig('other_db') == "1") { $dsn = array_merge($dsn, array('host' => $this->getConfig('host'), 'user' => $this->getConfig('user'), 'pass' => $this->getConfig('pass'))); } else { $appOptions = $this->getDi()->getParameters(); $dbConfig = $appOptions['db']['mysql']; $dsn = array_merge($dsn, array('host' => $dbConfig['host'], 'user' => $dbConfig['user'], 'pass' => $dbConfig['pass'])); } $this->_db = Am_Db::connect($dsn, true); $this->_db->setErrorHandler(array($this, 'dbErrorHandler')); $this->_db->setIdentPrefix($this->getConfig('prefix')); $this->_db->query("USE ?#", $dsn['path']); } if ($this->sqlDebug) { if (!empty($this->getDi()->db->_logger)) { $this->_db->setLogger($this->getDi()->db->_logger); } } return $this->_db; }