PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/backup/lib/MySQLDump.class.php

http://openbiz-cubi.googlecode.com/
PHP | 103 lines | 90 code | 13 blank | 0 comment | 18 complexity | 562b4131b51cfba06c57cbc2aadb4c33 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. class MySQLDump {
  3. var $tables = array();
  4. var $connected = false;
  5. var $output;
  6. var $droptableifexists = false;
  7. var $mysql_error;
  8. function connect($host,$user,$pass,$db,$charset=null) {
  9. $return = true;
  10. $conn = @mysql_connect($host,$user,$pass);
  11. if (!$conn) { $this->mysql_error = mysql_error(); $return = false; }
  12. $seldb = @mysql_select_db($db);
  13. if (!$seldb) { $this->mysql_error = mysql_error(); $return = false; }
  14. if($charset) {
  15. @mysql_query("SET NAMES '$charset';");
  16. $this->mysql_error = mysql_error();
  17. if($this->mysql_error){
  18. $return = false;
  19. }
  20. }
  21. $this->connected = $return;
  22. return $return;
  23. }
  24. function list_tables() {
  25. $return = true;
  26. if (!$this->connected) { $return = false; }
  27. $this->tables = array();
  28. $sql = mysql_query("SHOW FULL TABLES");
  29. while ($row = mysql_fetch_array($sql)) {
  30. $this->tables[$row[0]] = $row[1];
  31. }
  32. return $return;
  33. }
  34. function dump() {
  35. $this->output="";
  36. $this->list_tables() or trigger_error($this->mysql_error, E_USER_ERROR);
  37. foreach($this->tables as $table=>$type){
  38. switch($type){
  39. case "VIEW":
  40. $this->dump_view($table, false);
  41. break;
  42. case "BASE TABLE":
  43. default:
  44. $this->dump_table($table, false);
  45. break;
  46. }
  47. }
  48. return true;
  49. }
  50. function list_values($tablename) {
  51. $sql = mysql_query("SELECT * FROM `$tablename`");
  52. $this->output .= "\n\n-- Dumping data for table: `$tablename`\n\n";
  53. while ($row = mysql_fetch_array($sql)) {
  54. $broj_polja = count($row) / 2;
  55. $this->output .= "INSERT INTO `$tablename` VALUES(";
  56. $buffer = '';
  57. for ($i=0;$i < $broj_polja;$i++) {
  58. $vrednost = trim($row[$i]);
  59. $vrednost = str_replace("\n",'',$vrednost);
  60. if (!is_integer($vrednost)) { $vrednost = "'".addslashes($vrednost)."'"; }
  61. $buffer .= $vrednost.', ';
  62. }
  63. $buffer = substr($buffer,0,count($buffer)-3);
  64. $this->output .= $buffer . ");\n";
  65. }
  66. }
  67. function dump_table($tablename, $single = true) {
  68. if ($single == true) $this->output = "";
  69. $this->get_table_structure($tablename);
  70. $this->list_values($tablename);
  71. }
  72. function dump_view($tablename, $single = true) {
  73. if ($single == true) $this->output = "";
  74. $this->output .= "\n\n-- Dumping structure for view: `$tablename`\n\n";
  75. if ($this->droptableifexists) {
  76. $this->output .= "DROP VIEW IF EXISTS `$tablename`;\n";
  77. }
  78. $sql = mysql_query("SHOW CREATE VIEW `$tablename`");
  79. $this->output .= mysql_result($sql, 0, 'Create View').';'; //" PRIMARY KEY (`$primary`)\n);\n";
  80. }
  81. function get_table_structure($tablename) {
  82. $this->output .= "\n\n-- Dumping structure for table: `$tablename`\n\n";
  83. if ($this->droptableifexists) {
  84. $this->output .= "DROP TABLE IF EXISTS `$tablename`;\n";
  85. }
  86. $sql = mysql_query("SHOW CREATE TABLE `$tablename`");
  87. $this->output .= mysql_result($sql, 0, 'Create Table').';'; //" PRIMARY KEY (`$primary`)\n);\n";
  88. }
  89. }
  90. ?>