예제 #1
0
 /**
  * Implementation of the onEvent() method for Observer interface.
  * If a query gets executed this method gets invoked because the
  * adapter will send a signal to the attached logger.
  *
  * @param  string $eventName          ID of the event (name)
  * @param  RedBean_DBAdapter $adapter adapter that sends the signal
  *
  * @return void
  */
 public function onEvent($eventName, $adapter)
 {
     if ($eventName == "sql_exec") {
         $sql = $adapter->getSQL();
         $this->logs[] = $sql;
     }
 }
예제 #2
0
 /**
  *
  * @param RedBean_OODBBean $parent
  * @return array $childObjects
  */
 public function children(RedBean_OODBBean $parent)
 {
     try {
         $ids = $this->adapter->getCol("SELECT id FROM\n\t\t\t`" . $parent->getMeta("type") . "`\n\t\t\tWHERE `" . $this->property . "` = " . intval($parent->id) . "\n\t\t");
     } catch (RedBean_Exception_SQL $e) {
         return array();
     }
     return $this->oodb->batch($parent->getMeta("type"), $ids);
 }
 public function preLoad($type, $ids)
 {
     $insertid = $this->writer->insertRecord("__log", array("action", "tbl", "itemid"), array(array(1, '__no_type__', 0)));
     $values = array();
     foreach ($ids as $id) {
         $this->stash[$id] = $insertid;
         $values[] = array(1, $type, $id);
     }
     $this->writer->insertRecord("__log", array("action", "tbl", "itemid"), $values);
 }
예제 #4
0
 /**
  * Does an optimization cycle for each UPDATE event
  * @param string $event
  * @param RedBean_OODBBean $bean
  */
 public function onEvent($event, $bean)
 {
     try {
         if ($event == "update") {
             $arr = $bean->export();
             unset($arr["id"]);
             if (count($arr) == 0) {
                 return;
             }
             $table = $this->adapter->escape($bean->getMeta("type"));
             $columns = array_keys($arr);
             $column = $this->adapter->escape($columns[array_rand($columns)]);
             $value = $arr[$column];
             $type = $this->writer->scanType($value);
             $fields = $this->writer->getColumns($table);
             if (!in_array($column, array_keys($fields))) {
                 return;
             }
             $typeInField = $this->writer->code($fields[$column]);
             if ($type < $typeInField) {
                 $type = $this->writer->typeno_sqltype[$type];
                 $this->adapter->exec("alter table `{$table}` add __test " . $type);
                 $this->adapter->exec("update `{$table}` set __test=`{$column}`");
                 $diff = $this->adapter->getCell("select\n\t\t\t\t\t\t\tcount(*) as df from `{$table}` where\n\t\t\t\t\t\t\tstrcmp(`{$column}`,__test) != 0");
                 if (!$diff) {
                     $this->adapter->exec("alter table `{$table}` change `{$column}` `{$column}` " . $type);
                 }
                 $this->adapter->exec("alter table `{$table}` drop __test");
             }
         }
     } catch (RedBean_Exception_SQL $e) {
         //optimizer might make mistakes, dont care..
     }
 }
예제 #5
0
 /**
  * Loads a Batch of Beans at once
  * @param string $type
  * @param array $ids
  * @return array $beans
  */
 public function batch($type, $ids)
 {
     if (!$ids) {
         return array();
     }
     $collection = array();
     try {
         $rows = $this->writer->selectRecord($type, $ids);
     } catch (RedBean_Exception_SQL $e) {
         if ($e->getSQLState() != "42S02" && $e->getSQLState() != "42S22") {
             throw $e;
         }
         $rows = false;
     }
     $this->stash = array();
     if (!$rows) {
         return array();
     }
     foreach ($rows as $row) {
         $this->stash[$row["id"]] = $row;
     }
     foreach ($ids as $id) {
         $collection[$id] = $this->load($type, $id);
     }
     $this->stash = NULL;
     return $collection;
 }
예제 #6
0
 /**
  * Implementation of the onEvent() method for Observer interface.
  * If a query gets executed this method gets invoked because the
  * adapter will send a signal to the attached logger.
  *
  * @param  string $eventName          ID of the event (name)
  * @param  RedBean_DBAdapter $adapter adapter that sends the signal
  *
  * @return void
  */
 public function onEvent($eventName, $adapter)
 {
     if ($eventName == 'sql_exec') {
         $sql = $adapter->getSQL();
         $this->logs[] = $sql;
         if (strpos($sql, 'ALTER') === 0) {
             $write = "-- " . date('Y-m-d H:i') . " | Altering table. \n";
             $write .= $sql;
             $write .= "\n\n";
         }
         if (strpos($sql, 'CREATE') === 0) {
             $write = "-- " . date('Y-m-d H:i') . " | Creating new table. \n";
             $write .= $sql;
             $write .= "\n\n";
         }
         if (isset($write)) {
             file_put_contents($this->file, $write, FILE_APPEND);
         }
     }
 }
예제 #7
0
 /**
  * Removes all tables and views from the database.
  */
 public function wipeAll()
 {
     $this->adapter->exec('SET CONSTRAINTS ALL DEFERRED');
     //$this->adapter->startTransaction();
     foreach ($this->getTables() as $t) {
         $t = $this->noKW($t);
         try {
             $this->adapter->exec("drop table if exists {$t} CASCADE ");
         } catch (Exception $e) {
         }
         try {
             $this->adapter->exec("drop view if exists {$t} CASCADE ");
         } catch (Exception $e) {
             throw $e;
         }
     }
     //$this->adapter->commit();
     $this->adapter->exec('SET CONSTRAINTS ALL IMMEDIATE');
 }
예제 #8
0
 /**
  * Adds a Unique index constrain to the table.
  * @param string $table
  * @param string $col1
  * @param string $col2
  * @return void
  */
 public function addUniqueIndex($table, $columns)
 {
     sort($columns);
     //else we get multiple indexes due to order-effects
     foreach ($columns as $k => $v) {
         $columns[$k] = "`" . $this->adapter->escape($v) . "`";
     }
     $table = $this->check($table);
     $r = $this->adapter->get("SHOW INDEX FROM {$table}");
     $name = "UQ_" . sha1(implode(',', $columns));
     if ($r) {
         foreach ($r as $i) {
             if ($i["Key_name"] == $name) {
                 return;
             }
         }
     }
     $sql = "ALTER IGNORE TABLE `{$table}`\n                ADD UNIQUE INDEX `{$name}` (" . implode(",", $columns) . ")";
     $this->adapter->exec($sql);
 }
예제 #9
0
 /**
  * Removes all relations for a bean
  * @param RedBean_OODBBean $bean
  * @param <type> $type
  */
 public function clearRelations(RedBean_OODBBean $bean, $type)
 {
     $this->oodb->store($bean);
     $table = $this->getTable(array($bean->getMeta("type"), $type));
     if ($type == $bean->getMeta("type")) {
         //echo "<b>CROSS</b>";
         $property2 = $type . "2_id";
         $cross = 1;
     } else {
         $cross = 0;
     }
     $property = $bean->getMeta("type") . "_id";
     $sql = "DELETE FROM `{$table}`\n\t\tWHERE " . $this->adapter->escape($property) . " = " . $this->adapter->escape($bean->id);
     if ($cross) {
         $sql .= " OR  " . $this->adapter->escape($property2) . " = " . $this->adapter->escape($bean->id);
     }
     try {
         $this->adapter->exec($sql);
     } catch (RedBean_Exception_SQL $e) {
         if ($e->getSQLState() != "42S02" && $e->getSQLState() != "42S22") {
             throw $e;
         }
     }
 }
예제 #10
0
 /**
  * Implementation of the onEvent() method for Observer interface.
  * If a query gets executed this method gets invoked because the
  * adapter will send a signal to the attached logger.
  *
  * @param  string $eventName          ID of the event (name)
  * @param  RedBean_DBAdapter $adapter adapter that sends the signal
  *
  * @return void
  */
 public function onEvent($eventName, $adapter)
 {
     if ($eventName == 'sql_exec') {
         $this->logs[] = $adapter->getSQL();
     }
 }
예제 #11
0
 /**
  * Toggles DEBUG mode.
  * In Debug mode all SQL that happens under the hood will
  * be printed to the screen.
  *
  * @param boolean $tf
  */
 public static function debug($tf = true)
 {
     self::$adapter->getDatabase()->setDebugMode($tf);
 }
예제 #12
0
	/**
	 * Convenience function to execute Queries directly.
	 * Executes SQL.
	 *
	 * @param string $sql	 sql
	 * @param array  $values values
	 *
	 * @return array $results
	 */
	public static function getCol( $sql, $values=array() ) {
		return self::secureExec(function($sql, $values) {
			return R::$adapter->getCol( $sql, $values );
		}, array(),$sql, $values);
	}
예제 #13
0
파일: oodb.php 프로젝트: Jamongkad/Sunfish
 /**
  * Removes a bean from the database and breaks associations if required
  * @param $bean
  * @return unknown_type
  */
 public static function trash(OODBBean $bean)
 {
     self::checkBean($bean);
     if (intval($bean->id) === 0) {
         return;
     }
     self::deleteAllAssoc($bean);
     self::openBean($bean);
     self::$db->exec("DELETE FROM " . self::$db->escape($bean->type) . " WHERE id = " . intval($bean->id));
 }
예제 #14
0
파일: rb.php 프로젝트: u007/FlexiPHP
 public function deleteByCrit($table, $crits)
 {
     $table = $this->noKW($this->adapter->escape($table));
     $values = array();
     foreach ($crits as $key => $val) {
         $key = $this->noKW($this->adapter->escape($key));
         $values[] = $val;
         $conditions[] = $key . "= ? ";
     }
     $sql = "DELETE FROM {$table} WHERE " . implode(" AND ", $conditions);
     return $this->adapter->exec($sql, $values);
 }
예제 #15
0
파일: test.php 프로젝트: Jamongkad/Sunfish
 if (!RedBean_OODB::optimizeIndexes() && count(RedBean_DBAdapter::getLogs()) < 1) {
     die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
 } else {
     SmartTest::instance()->progress();
 }
 if (!RedBean_OODB::clean() && count(RedBean_DBAdapter::getLogs()) < 1) {
     die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
 } else {
     SmartTest::instance()->progress();
 }
 if (!RedBean_OODB::registerUpdate("cheese") && count(RedBean_DBAdapter::getLogs()) < 1) {
     die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
 } else {
     SmartTest::instance()->progress();
 }
 if (!RedBean_OODB::registerSearch("cheese") && count(RedBean_DBAdapter::getLogs()) < 1) {
     die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
 } else {
     SmartTest::instance()->progress();
 }
 //test convenient tree functions
 SmartTest::instance()->canwe = "convient tree functions";
 if (!class_exists("Person")) {
     RedBean_OODB::gen("person");
 }
 $donald = new Person();
 $donald->setName("Donald");
 $donald->save();
 $kwik = new Person();
 $kwik->setName("Kwik");
 $kwik->save();
예제 #16
0
파일: Facade.php 프로젝트: ryjkov/redbean
 /**
  * Facade Convience method for adapter transaction system.
  * Rolls back a transaction.
  *
  * @static
  * @return void
  */
 public static function rollback()
 {
     self::$adapter->rollback();
 }