/** * 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.. } }
/** * 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; } } }
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); }
public function createIndexIfNotExist($table, $indexName, $indexColumns, $drop=true) { $indexName = $this->adapter->escape($indexName); $sql = "select t.relname as table_name, i.relname as index_name, a.attname as column_name from pg_class t, pg_class i, pg_index ix, pg_attribute a where t.oid = ix.indrelid and i.oid = ix.indexrelid and a.attrelid = t.oid and a.attnum = ANY(ix.indkey) and t.relkind = 'r' and t.relname = ? order by t.relname, i.relname;"; $indexes = $this->adapter->get($sql,array($table)); print_r($indexes); foreach($indexes as $index) { if ($index["index_name"]===$indexName) { if (!$drop) return false; $sql = "DROP INDEX $indexName "; $this->adapter->exec($sql); break; } } foreach($indexColumns as $key=>$indexColumn) { $indexColumns[$key] = $this->safeColumn($indexColumn); } $columnStr = implode(",", $indexColumns); $indexName = $this->safeTable($indexName); $sql = "CREATE INDEX $indexName ON ".$this->safeTable($table)." ($columnStr) "; $this->adapter->exec($sql); return true; }
/** * 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)); }