public function startTestSuite(PHPUnit_Framework_TestSuite $suite) { $dbAdapter = \R::getDatabaseAdapter(); if (null == $dbAdapter) { \R::setup(); } }
/** * Test debugging with custom logger. * * @return void */ public function testDebugCustomLogger() { testpack('Test debug mode with custom logger'); $pdoDriver = new RedBean_Driver_PDO(R::getDatabaseAdapter()->getDatabase()->getPDO()); $customLogger = new CustomLogger(); $pdoDriver->setDebugMode(TRUE, $customLogger); $pdoDriver->Execute('SELECT 123'); asrt(count($customLogger->getLogMessage()), 1); $pdoDriver->setDebugMode(TRUE, NULL); asrt($pdoDriver->getLogger() instanceof RedBean_Logger_Default, TRUE); testpack('Test bean->getProperties method'); $bean = R::dispense('bean'); $bean->property = 'hello'; $props = $bean->getProperties(); asrt(isset($props['property']), TRUE); asrt($props['property'], 'hello'); testpack('Test snake_case vs CamelCase with Query Builder'); list($sql, $params) = R::$f->begin()->camelCase()->getQuery(); asrt(trim($sql), 'camel case'); list($sql, $params) = R::$f->begin()->personASTeacher()->getQuery(); asrt(trim($sql), 'person as teacher'); list($sql, $params) = R::$f->begin()->JOIN()->getQuery(); asrt(trim($sql), 'join'); RedBean_SQLHelper::useCamelCase(FALSE); list($sql, $params) = R::$f->begin()->camelCase()->getQuery(); asrt(trim($sql), 'camelCase'); }
public static function setup() { self::init(); self::$dbh = new PDO('mysql:host=' . self::$server . ';dbname=' . self::$databasename . ';', self::$username, self::$password); self::$dbh->query('SET NAMES utf8'); R::setup(self::$dbh); R::debug(true, 1); R::freeze(true); // self::$logsrv = new SystemlogsService(); self::$queryLogger = R::getDatabaseAdapter()->getDatabase()->getLogger(); }
public function __construct() { if (!\R::getDatabaseAdapter()) { $dbConfig = (require_once __DIR__ . '/cnf.php'); static::$_host = $dbConfig['host']; static::$_dbname = $dbConfig['dbname']; static::$_user = $dbConfig['user']; static::$_password = $dbConfig['password']; \R::setup('mysql:host=' . static::$_host . ';dbname=' . static::$_dbname, static::$_user, static::$_password, true); //for both mysql or mariaDB } $this->_redbeans = \R::getToolBox()->getRedBean(); $this->_rbToolbox = \R::getToolBox(); }
function get_uniques_for_type($type) { $list = array(); $writer = R::getWriter(); $adapter = R::getDatabaseAdapter(); global $currentDriver; switch ($currentDriver) { case 'mysql': $table = $writer->esc($type, TRUE); $columns = $adapter->get(' SELECT information_schema.key_column_usage.constraint_name, information_schema.key_column_usage.column_name FROM information_schema.table_constraints INNER JOIN information_schema.key_column_usage ON ( information_schema.table_constraints.constraint_name = information_schema.key_column_usage.constraint_name AND information_schema.table_constraints.constraint_schema = information_schema.key_column_usage.constraint_schema AND information_schema.table_constraints.constraint_catalog = information_schema.key_column_usage.constraint_catalog ) WHERE information_schema.table_constraints.table_schema IN (SELECT DATABASE()) AND information_schema.table_constraints.table_name = ? AND information_schema.table_constraints.constraint_type = \'UNIQUE\' ', array($table)); $uniques = array(); foreach ($columns as $column) { if (!isset($uniques[$column['constraint_name']])) { $uniques[$column['constraint_name']] = array(); } $uniques[$column['constraint_name']][] = $column['column_name']; } $list = $uniques; break; case 'pgsql': $table = $writer->esc($type, TRUE); $columns = $adapter->get(' SELECT information_schema.key_column_usage.constraint_name, information_schema.key_column_usage.column_name FROM information_schema.table_constraints INNER JOIN information_schema.key_column_usage ON ( information_schema.table_constraints.constraint_name = information_schema.key_column_usage.constraint_name AND information_schema.table_constraints.constraint_schema = information_schema.key_column_usage.constraint_schema AND information_schema.table_constraints.constraint_catalog = information_schema.key_column_usage.constraint_catalog ) WHERE information_schema.table_constraints.table_catalog = current_database() AND information_schema.key_column_usage.table_schema = ANY( current_schemas( FALSE ) ) AND information_schema.table_constraints.table_name = ? AND information_schema.table_constraints.constraint_type = \'UNIQUE\' ', array($table)); $uniques = array(); foreach ($columns as $column) { if (!isset($uniques[$column['constraint_name']])) { $uniques[$column['constraint_name']] = array(); } $uniques[$column['constraint_name']][] = $column['column_name']; } $list = $uniques; break; case 'sqlite': $uniques = array(); $table = $writer->esc($type, TRUE); $indexes = $adapter->get("PRAGMA index_list({$table})"); foreach ($indexes as $index) { if ($index['unique'] == 1) { $info = $adapter->get("PRAGMA index_info({$index['name']})"); if (!isset($uniques[$index['name']])) { $uniques[$index['name']] = array(); } foreach ($info as $piece) { $uniques[$index['name']][] = $piece['name']; } } } $list = $uniques; break; case 'CUBRID': try { $sqlCode = $adapter->get("SHOW CREATE TABLE `{$type}`"); } catch (\Exception $e) { $sqlCode = ''; } if (!isset($sqlCode[0])) { return array(); } $matches = array(); preg_match_all('/CONSTRAINT\\s+\\[([\\w_]+)\\]\\s+UNIQUE\\s+KEY\\s+\\(([^\\)]+)\\)/', $sqlCode[0]['CREATE TABLE'], $matches); $list = array(); if (!isset($matches[0])) { return $list; } $max = count($matches[0]); for ($i = 0; $i < $max; $i++) { $columns = explode(',', $matches[2][$i]); foreach ($columns as $key => $column) { $columns[$key] = trim($column, '[] '); } $list[$matches[1][$i]] = $columns; } break; } return $list; }
function save() { $modified = false; foreach ($this->_oldValues as $key => $value) { if ($value !== $this->{$key}) { $modified = true; } } if (!$this->_canSave || !$modified) { return; } $class = get_called_class(); $table = self::$tables[$class]; $primaryKey = self::$keys[$class]; list($saveType, $columns, $params, $id) = $this->getSaveSQL(); if ($saveType == 1) { // insert $values = array(); foreach ($params as $v) { $values[] = '?'; } $sql = "insert into `{$table}` ("; $sql .= implode(',', $columns); $sql .= ") values ("; $sql .= implode(',', $values); $sql .= ")"; R::exec($sql, $params); $this->{$primaryKey} = R::getDatabaseAdapter()->getInsertID(); } elseif ($saveType == 2) { $sql = "update `{$table}` set "; $sql .= implode(', ', $columns); $sql .= " where `{$primaryKey}` = ?"; $params[] = $id; R::exec($sql, $params); # $this->$primaryKey = R::getDatabaseAdapter()->getInsertID(); } }
public static function getLog() { $log = R::getDatabaseAdapter()->getDatabase()->getLogger(); return $log->getLogs(); }
/** * Test reflectional functions of database. * * @return void */ public function testDatabaseProperties() { testpack('Testing Database Properties'); $adapter = R::getDatabaseAdapter(); if (method_exists(R::$adapter->getDatabase(), 'getPDO')) { asrt($adapter->getDatabase()->getPDO() instanceof PDO, TRUE); } asrt(strlen($adapter->getDatabase()->getDatabaseVersion()) > 0, TRUE); asrt(strlen($adapter->getDatabase()->getDatabaseType()) > 0, TRUE); }