$friendly — ";
# execute the query
$result = @DB::execute($query);
// begin if: error executing query
if ( $result === FALSE )
{
echo ' FAILED ';
$err = DB::getError();
echo 'Error: ', $err[2], '';
$upgrade_failures++;
}
// else: query was successful
else
{
echo ' SUCCESS! ';
} // end if
echo '', "\n";
return $result;
}
/**
* Tries to update database version, gives a message when failed
*
* @param $version
* Schema version the database has been upgraded to
*/
function update_version($version)
{
global $upgrade_failures;
$message = 'Updating DatabaseVersion in config table to ' . $version;
// begin if: no upgrade failures; update the database version in the config table
if ( $upgrade_failures == 0 )
{
$query = 'UPDATE %s SET value = "%s" WHERE name = "DatabaseVersion";';
$query = sprintf($query, sql_table('config'), $version);
upgrade_query($message, $query);
}
// else: display 'not executed' message
else
{
echo '
', $message, ' — NOT EXECUTED Errors occurred during upgrade process.
';
} // end if
}
/**
*
*
* @param string $table table to check (without prefix)
* @param array $columns array of column names included
* @return int
*/
function upgrade_checkIfIndexExists($table, $columns)
{
// get info for indices from database
$indices = array();
$query = 'SHOW INDEX FROM ' . sql_table($table);
$result = @DB::getResult($query);
// begin loop: each result object
foreach ( $result as $row )
{
// begin if: key has not been added to the indeces array yet
if ( !isset($indices[$row['Key_name']]) )
{
$indices[$row['Key_name']] = array();
} // end if
array_push($indices[$row['Key_name']], $row['Column_name']);
}
// compare each index with parameter
foreach ( $indices as $key_name => $index_columns )
{
$diff = array_diff($index_columns, $columns);
if ( count($diff) == 0 )
{
return 1;
} // end if
} // end loop
return 0;
}
/**
* Checks to see if a given table exists
*
* @param string $table name of table to check existence of
* @return bool TRUE if table exists, FALSE otherwise.
*/
function upgrade_checkIfTableExists($table)
{
$query = 'SHOW TABLES LIKE ' . sql_table($table);
$result = DB::getResult($query);
// begin if: query executed successfully and one row was returned
if ( ($result !== FALSE) && ($result->rowCount() == 1) )
{
return TRUE;
}
// else: query error or no results returned
else
{
return FALSE;
} // end if
}
/**
* Checks to see if a given configuration value exists
*
* @param string $value config value to check for existance of (paramater must be MySQL escaped already)
* @return bool TRUE if configuration value exists, FALSE otherwise.
*/
function upgrade_checkIfCVExists($value)
{
$query = 'SELECT name FROM ' . sql_table('config') . ' WHERE name = "' . $value . '"';
$result = DB::getResult($query);
// begin if: query executed successfully and one row was returned
if ( ($result !== FALSE) && ($result->rowCount() == 1) )
{
return TRUE;
}
// else: query error or no results returned
else
{
return FALSE;
} // end if
}
/**
* Checks to see if a given column exists
*
* @param string $table name of table to check for column in
* @param string $column name of column to check for existance of
* @return bool TRUE if column exists, FALSE otherwise.
*/
function upgrade_checkIfColumnExists($table, $column)
{
$query = 'DESC ' . sql_table($table) . ' ' . $column;
$result = DB::getResult($query);
// begin if: query executed successfully and one row was returned
if ( ($result !== FALSE) && ($result->rowCount() == 1) )
{
return TRUE;
}
// else: query error or no results returned
else
{
return FALSE;
} // end if
}