* @param $strSortType String containing either asc or desc [default to asc]
* @desc Naturally sorts an array using by the column $strSortBy
*/
+define('EXPORT_MAX_INSERT', 50000);
+
function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
{
// if the parameters are invalid
}
// update per company files
- $cnt = count($db_connections);
+ $cnt = max(1, count($db_connections));
for($i = 0; $i < $cnt; $i++)
{
$newexts = $extensions;
return true;
}
-function db_create_db($connection)
-{
- $db = mysql_connect($connection["host"] ,
- $connection["dbuser"], $connection["dbpassword"]);
- if (!mysql_select_db($connection["dbname"], $db))
- {
- $sql = "CREATE DATABASE " . $connection["dbname"] . "";
- if (!mysql_query($sql))
- return 0;
- mysql_select_db($connection["dbname"], $db);
- }
- return $db;
-}
-
-function db_drop_db($connection)
-{
-
- if ($connection["tbpref"] == "")
- {
- $sql = "DROP DATABASE " . $connection["dbname"] . "";
- return mysql_query($sql);
- }
- else
- {
- $res = db_query("show table status");
- $all_tables = array();
- while($row = db_fetch($res))
- $all_tables[] = $row;
- // get table structures
- foreach ($all_tables as $table)
- {
- if (strpos($table['Name'], $connection["tbpref"]) === 0)
- db_query("DROP TABLE `".$table['Name'] . "`");
- }
- //deleting the tables, how??
- return true;
- }
-}
-
function db_import($filename, $connection, $force=true)
{
- global $db, $go_debug;
+ global $db, $go_debug, $sql_trail;
+
+ $sql_trail = false;
$allowed_commands = array(
"create" => 'table_queries',
+ "delimiter" => 'table_queries',
"alter table" => 'table_queries',
"insert" => 'data_queries',
"update" => 'data_queries',
- "drop table if exists" => 'drop_queries');
+ "drop table if exists" => 'drop_queries',
+ "drop function if exists" => 'drop_queries',
+ "drop trigger if exists" => 'drop_queries',
+ );
$ignored_mysql_errors = array( //errors ignored in normal (non forced) mode
'1022', // duplicate key
'1050', // Table %s already exists
// parse input file
$query_table = '';
+ $delimiter = ';';
foreach($lines as $line_no => $line)
{
$line = trim($line);
{
if (strtolower(substr($line, 0, strlen($cmd))) == $cmd)
{
+ if ($cmd == 'delimiter') {
+ $delimiter = trim(substr($line, 10));
+ continue 2;
+ }
$query_table = $table;
${$query_table}[] = array('', $line_no+1);
break;
if($query_table != '') // inside allowed query
{
$table = $query_table;
- if (substr($line, -1) == ';') // end of query found
+
+ if (substr($line, -strlen($delimiter)) == $delimiter) // end of query found
{
- $line = substr($line, 0, strlen($line) - 1); // strip ';'
+ $line = substr($line, 0, strlen($line) - strlen($delimiter)); // strip delimiter
$query_table = '';
}
${$table}[count(${$table}) - 1][0] .= $line . "\n";
}
db_query("SET foreign_key_checks=1");
+ if ($delimiter != ';') db_query("delimiter ;"); // just for any case
if (count($sql_errors)) {
// display first failure message; the rest are probably derivative
return explode("\n", $file_data);
}
-function db_backup($conn, $ext='no', $comm='', $tbpref = TB_PREF)
+function db_backup($conn, $ext='no', $comm='', $path=null)
{
if ($conn['tbpref'] != "")
$filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
else
$filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
- return db_export($conn, $filename, $ext, $comm, $tbpref);
+ if (!isset($path))
+ $path = BACKUP_PATH;
+
+ return db_export($conn, $path . clean_file_name($filename), $ext, $comm);
}
// generates a dump of $db database
// $drop and $zip tell if to include the drop table statement or dry to pack
-function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, $no_default=false)
+function db_export($conn, $filename, $zip='no', $comment='', $no_default=false)
{
global $app_title, $version, $power_url, $path_to_root;
+
$error = false;
// set max string size before writing to file
$max_size = 1048576 * 2; // 2 MB
$backupfile = $filename . ".zip";
else
$backupfile = $filename;
- $company = get_company_pref('coy_name', $tbpref);
+ $company = $conn['name']; // get_company_pref('coy_name');
//create comment
$out="# MySQL dump of database '".$conn["dbname"]."' on host '".$conn["host"]."'\n";
// if ($auto_incr[$tablename])
// $out.=" AUTO_INCREMENT=".$auto_incr[$tablename];
$out.=" ;";
- $out.="\n\n\n";
+ $out.="\n\n";
// export data
if (!$error)
{
- $out.="### Data of table `".$tablename."` ###\n\n";
+ $out.="### Data of table `".$tablename."` ###\n";
// check if field types are NULL or NOT NULL
$res3 = db_query("SHOW COLUMNS FROM `" . $tablename . "`");
}
$res2 = db_query("SELECT * FROM `" . $tablename . "`");
+ $maxinsert = 0;
+ $insert = '';
for ($j = 0; $j < db_num_rows($res2); $j++)
{
- $out .= "INSERT INTO `" . $tablename . "` VALUES (";
$row2 = db_fetch_row($res2);
- // run through each field
+ $values = '(';
for ($k = 0; $k < $nf = db_num_fields($res2); $k++)
{
- $out .= db_escape($row2[$k], $field_null[$k]);
+ $values .= db_escape($row2[$k], $field_null[$k]);
if ($k < ($nf - 1))
- $out .= ", ";
+ $values .= ', ';
+ }
+ $values .= ')';
+ $len = strlen($values);
+ if ($maxinsert < $len+1)
+ {
+ $maxinsert = EXPORT_MAX_INSERT;
+ if ($insert)
+ {
+ $out .= $insert .';'; // flush insert query
+ $insert = '';
+ }
+ }
+
+ if ($insert == '')
+ {
+ $insert = "\nINSERT INTO `" . $tablename . "` VALUES\n";
+ $maxinsert -= strlen($insert);
+ } else {
+ $insert .= ",\n";
}
- $out .= ");\n";
+
+ $maxinsert -= $len;
+ $insert .= $values;
// if saving is successful, then empty $out, else set error flag
if (strlen($out) > $max_size && $zip != "zip")
$error = true;
}
}
-
+ if ($insert)
+ $out .= $insert. ';';
// an error occurred! Try to delete file and return error status
}
elseif ($error)
{
- @unlink(BACKUP_PATH . $backupfile);
+ @unlink($backupfile);
return false;
}
}
else
{
- @unlink(BACKUP_PATH . $backupfile);
+ @unlink($backupfile);
return false;
}
- // if (mysql_error()) return "DB_ERROR";
- //@mysql_close($con);
-
//if ($zip == "zip")
// $zip = $time;
if (save_to_file($backupfile, $zip, $out))
}
else
{
- @unlink(BACKUP_PATH . $backupfile);
+ @unlink($backupfile);
return false;
}
return $backupfile;
// saves the string in $fileData to the file $backupfile as gz file or not ($zip)
// returns backup file name if name has changed (zip), else TRUE. If saving failed, return value is FALSE
-function save_to_file($backupfile, $zip, $fileData)
+function save_to_file($path, $zip, $fileData)
{
global $path_to_root;
+ $backupfile = basename($path);
+
if ($zip == "gzip")
{
- if ($zp = @gzopen(BACKUP_PATH . $backupfile, "a9"))
+ if ($zp = @gzopen($path, "a9"))
{
@gzwrite($zp, $fileData);
@gzclose($zp);
$c_len = strlen($zdata);
// dos time
- $timearray = getdate($zip);
+ $timearray = getdate();
$dostime = (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
$dtime = dechex($dostime);
// total # of entries "on this disk", total # of entries overall, size of central dir, offset to start of central dir, .zip file comment length
$fileData .= pack('v', 1) . pack('v', 1) . pack('V', strlen($cdrec)) . pack('V', strlen($fr)) . "\x00\x00";
- if ($zp = @fopen(BACKUP_PATH . $backupfile, "a"))
+ if ($zp = @fopen($path, "a"))
{
@fwrite($zp, $fileData);
@fclose($zp);
}
else
{
- if ($zp = @fopen(BACKUP_PATH . $backupfile, "a"))
+ if ($zp = @fopen($path, "a"))
{
@fwrite($zp, $fileData);
@fclose($zp);
@fclose($f);
}
}
+
+//
+// Checks $field existence in $table with given field $properties
+// $table - table name without prefix
+// $field - optional field name
+// $properties - optional properties of field defined by MySQL:
+// 'Type', 'Null', 'Key', 'Default', 'Extra'
+//
+function check_table($pref, $table, $field=null, $properties=null)
+{
+ $tables = @db_query("SHOW TABLES LIKE '".$pref.$table."'");
+ if (!db_num_rows($tables))
+ return 1; // no such table or error
+
+ $fields = @db_query("SHOW COLUMNS FROM ".$pref.$table);
+ if (!isset($field))
+ return 0; // table exists
+
+ while( $row = db_fetch_assoc($fields))
+ {
+ if ($row['Field'] == $field)
+ {
+ if (!isset($properties))
+ return 0;
+ foreach($properties as $property => $value)
+ {
+ if ($row[$property] != $value)
+ return 3; // failed type/length check
+ }
+ return 0; // property check ok.
+ }
+ }
+ return 2; // field not found
+}
+
?>
\ No newline at end of file