/** * Checks if a table or column exists * * @param $table_name string * @param $column_name string * @return boolean true if the object exists in current database */ public function exists($table_name, $column_name = null) { if (isset($column_name)) { $table = Table_Builder_Mysqli::build($this, $table_name); return $table->hasColumn($column_name); } else { $res = $this->query('SHOW TABLES'); /** @var $table Table */ while ($table = $res->fetch_row()) { if ($table[0] == $table_name) { $res->free(); return true; } } $res->free(); return false; } }
/** * Update table structure corresponding to a data class * * @param $mysqli mysqli * @param $class_name string * @return boolean true if an update query has been generated and executed */ public static function updateTable(mysqli $mysqli, $class_name) { $result = false; foreach ((new Table_Builder_Class())->build($class_name) as $class_table) { $mysql_table = Table_Builder_Mysqli::build($mysqli, $class_table->getName()); if (!$mysql_table) { $mysqli->query((new Create_Table($class_table))->build()); $result = true; } else { $mysql_columns = $mysql_table->getColumns(); $builder = new Alter_Table($mysql_table); foreach ($class_table->getColumns() as $column) { if (!isset($mysql_columns[$column->getName()])) { $builder->addColumn($column); } elseif (!$column->equiv($mysql_columns[$column->getName()])) { $builder->alterColumn($column->getName(), $column); } } if ($builder->isReady()) { $mysqli->query($builder->build()); $result = true; } } } return $result; }