/** * 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.. } }
/** * Convenience function to execute Queries directly. * Executes SQL. * * @param string $sql sql * @param array $values values * * @return array $results */ public static function exec($sql, $values = array()) { if (!self::$redbean->isFrozen()) { try { $rs = RedBean_Facade::$adapter->exec($sql, $values); } catch (RedBean_Exception_SQL $e) { if (self::$writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { return NULL; } else { throw $e; } } return $rs; } else { return RedBean_Facade::$adapter->exec($sql, $values); } }
/** * 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'); }
/** * 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); }
/** * 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; } } }
/** * @see RedBean_QueryWriter::wipe */ public function wipe($type) { $table = $type; $table = $this->esc($table); $sql = "TRUNCATE {$table} CASCADE"; $this->adapter->exec($sql); }
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); }
/** * Convenience function to execute Queries directly. * Executes SQL. * * @param string $sql sql * @param array $values values * * @return array $results */ public static function exec( $sql, $values=array() ) { return self::secureExec(function($sql, $values) { return R::$adapter->exec( $sql, $values ); }, NULL,$sql, $values ); }
/** * 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)); }