34 lines
988 B
PHP
34 lines
988 B
PHP
<?php
|
|
|
|
namespace Empik\Marketplace\Utils;
|
|
|
|
class DbUtils
|
|
{
|
|
public static function safeAddColumn($table, $column, $def)
|
|
{
|
|
if (!self::columnExists($table, $column))
|
|
return \Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` ADD `' . $column . '` ' . $def);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function columnExists($table, $column)
|
|
{
|
|
$query = 'SELECT COUNT(*)
|
|
FROM information_schema.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND COLUMN_NAME = \'' . pSQL($column) . '\'
|
|
AND TABLE_NAME = \'' . _DB_PREFIX_ . pSQL($table) . '\'';
|
|
|
|
return (bool)\Db::getInstance()->getValue($query);
|
|
}
|
|
|
|
public static function safeDropColumn($table, $column)
|
|
{
|
|
if (self::columnExists($table, $column)) {
|
|
\Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` DROP `' . $column . '` ');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |