function editColumn(knjdb_column $col, $newdata) { $table = $col->getTable(); $sql = "ALTER TABLE " . $this->driver->sep_table . $table->get("name") . $this->driver->sep_table; if ($col->get("name") != $newdata["name"]) { $sql .= " CHANGE " . $this->driver->sep_col . $col->get("name") . $this->driver->sep_col . " " . $this->getColumnSQL($newdata, array("skip_primary" => true)); } else { $sql .= " MODIFY " . $this->getColumnSQL($newdata, array("skip_primary" => true)); } $this->knjdb->query($sql); if ($col->get("name") != $newdata["name"]) { unset($col->getTable()->columns[$col->get("name")]); $col->getTable()->columns[$newdata["name"]] = $col; } }
/** NOTE: This is actually just a pure copy from the SQLite3-driver. */ function editColumn(knjdb_column $col, $newdata) { $table = $col->getTable(); $table_name = $table->get("name"); $tempname = $table->get("name") . "_temp"; $indexes = $this->knjdb->indexes()->getIndexes($table); $table->rename($tempname); $newcolumns = array(); foreach ($table->getColumns() as $column) { if ($column->get("name") == $col->get("name")) { $newcolumns[] = $newdata; } else { $newcolumns[] = $column->data; } } if (count($newcolumns) <= 0) { throw new Exception("wtf"); } //Makinig SQL for creating the new table with updated columns and executes it. $this->knjdb->tables()->createTable($table_name, $newcolumns); $table_new = $this->knjdb->getTable($table_name); //Making SQL for inserting into it from the temp-table. $sql_insert = "INSERT INTO '" . $table_name . "' ("; $sql_select = "SELECT * "; $count = 0; $first = true; foreach ($table->getColumns() as $column) { if ($count > 0) { $sql_insert .= ", "; } $sql_insert .= $newcolumns[$count]["name"]; $count++; } $sql_select .= " FROM " . $tempname; $sql_insert .= ") " . $sql_select; $this->knjdb->query($sql_insert); $table->drop(); //drop old table which has been renamed. //Creating indexes again from the array, that we saved at the beginning. In short terms this will //rename the columns which have indexes to the new names, so that they wont be removed. $newindexes = array(); if ($indexes) { foreach ($indexes as $index_key => $index) { foreach ($index->getColumns() as $column_key => $column) { if ($column->get("name") == $col->get("name")) { $newindexes[$index_key][] = $table_new->getColumn($newdata["name"]); } else { $newindexes[$index_key][] = $table_new->getColumn($column->get("name")); } } } } foreach ($newindexes as $key => $cols) { $table_new->addIndex($cols); } $table->data = $table_new->data; //if not it will bug up, if some other code has cached this object. }