/**
  * 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..
     }
 }
Esempio n. 2
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);
             //Select a random column for optimization.
             $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]);
             //Is the type too wide?
             if ($type < $typeInField) {
                 try {
                     @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " drop __test");
                 } catch (Exception $e) {
                 }
                 //Try to re-fit the entire column; by testing it.
                 $type = $this->writer->typeno_sqltype[$type];
                 //Add a test column.
                 @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " add __test " . $type);
                 //Copy the values and see if there are differences.
                 @$this->adapter->exec("update " . $this->writer->noKW($table) . " set __test=" . $this->writer->noKW($column) . "");
                 $rows = $this->adapter->get("select " . $this->writer->noKW($column) . " as a, __test as b from " . $this->writer->noKW($table));
                 $diff = 0;
                 foreach ($rows as $row) {
                     $diff += $row["a"] != $row["b"];
                 }
                 if (!$diff) {
                     //No differences; shrink column.
                     @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " change " . $this->writer->noKW($column) . " " . $this->writer->noKW($column) . " " . $type);
                 }
                 //Throw away test column; we don't need it anymore!
                 @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " drop __test");
             } else {
                 $this->MySQLSpecificColumns($table, $column, $fields[$column], $value);
             }
         }
     } catch (RedBean_Exception_SQL $e) {
         //optimizer might make mistakes, don't care.
         //echo $e->getMessage()."<br>";
     }
 }
Esempio n. 3
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) {
                 try {
                     @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " drop __test");
                 } catch (Exception $e) {
                 }
                 $type = $this->writer->typeno_sqltype[$type];
                 @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " add __test " . $type);
                 @$this->adapter->exec("update " . $this->writer->noKW($table) . " set __test=" . $this->writer->noKW($column) . "");
                 $rows = $this->adapter->get("select " . $this->writer->noKW($column) . " as a, __test as b from " . $this->writer->noKW($table));
                 $diff = 0;
                 foreach ($rows as $row) {
                     $diff += $row["a"] != $row["b"];
                 }
                 if (!$diff) {
                     @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " change " . $this->writer->noKW($column) . " " . $this->writer->noKW($column) . " " . $type);
                 }
                 @$this->adapter->exec("alter table " . $this->writer->noKW($table) . " drop __test");
             } else {
                 $this->MySQLSpecificColumns($table, $column, $fields[$column], $value);
             }
         }
     } catch (RedBean_Exception_SQL $e) {
     }
 }