You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.6 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. #!@PERL@
  2. # Copyright (C) 2000-2002 MySQL AB
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # Convert given tables in a database to MYISAM
  17. use DBI;
  18. use Getopt::Long;
  19. $opt_help=$opt_version=$opt_verbose=$opt_force=0;
  20. $opt_user=$opt_database=$opt_password=undef;
  21. $opt_host="localhost";
  22. $opt_socket="";
  23. $opt_type="MYISAM";
  24. $opt_port=0;
  25. $exit_status=0;
  26. GetOptions("force","help","host=s","password=s","user=s","type=s","verbose","version","socket=s", "port=i") ||
  27. usage(0);
  28. usage($opt_version) if ($#ARGV < 0 || $opt_help || $opt_version);
  29. $opt_database=shift(@ARGV);
  30. if (uc($opt_type) eq "HEAP")
  31. {
  32. print "Converting to type HEAP would delete your tables; aborting\n";
  33. exit(1);
  34. }
  35. $connect_opt="";
  36. if ($opt_port)
  37. {
  38. $connect_opt.= ";port=$opt_port";
  39. }
  40. if (length($opt_socket))
  41. {
  42. $connect_opt.=";mysql_socket=$opt_socket";
  43. }
  44. $dbh = DBI->connect("DBI:mysql:$opt_database:${opt_host}$connect_opt",
  45. $opt_user,
  46. $opt_password,
  47. { PrintError => 0})
  48. || die "Can't connect to database $opt_database: $DBI::errstr\n";
  49. if ($#ARGV < 0)
  50. {
  51. # Fetch all table names from the database
  52. my ($sth,$row);
  53. $sth=$dbh->prepare("show tables");
  54. $sth->execute || die "Can't get tables from $opt_database; $DBI::errstr\n";
  55. while (($row = $sth->fetchrow_arrayref))
  56. {
  57. push(@ARGV,$row->[0]);
  58. }
  59. $sth->finish;
  60. }
  61. print "Converting tables:\n" if ($opt_verbose);
  62. foreach $table (@ARGV)
  63. {
  64. my ($sth,$row);
  65. # Check if table is already converted
  66. $sth=$dbh->prepare("show table status like '$table'");
  67. if ($sth->execute && ($row = $sth->fetchrow_arrayref))
  68. {
  69. if (uc($row->[1]) eq uc($opt_type))
  70. {
  71. print "$table is already of type $opt_type; Ignored\n";
  72. next;
  73. }
  74. }
  75. print "converting $table\n" if ($opt_verbose);
  76. if (!$dbh->do("ALTER TABLE $table type=$opt_type"))
  77. {
  78. print STDERR "Can't convert $table: Error $DBI::errstr\n";
  79. exit(1) if (!$opt_force);
  80. $exit_status=1;
  81. }
  82. }
  83. $dbh->disconnect;
  84. exit($exit_status);
  85. sub usage
  86. {
  87. my($version)=shift;
  88. print "$0 version 1.1\n";
  89. exit(0) if ($version);
  90. print <<EOF;
  91. Conversion of a MySQL tables to other table types.
  92. Usage: $0 database [tables]
  93. If no tables has been specifed, all tables in the database will be converted.
  94. The following options are available:
  95. --force
  96. Continue even if there is some error.
  97. --help or --Information
  98. Shows this help
  99. --host='host name' (Default $opt_host)
  100. Host name where the database server is located.
  101. --password='password'
  102. Password for the current user.
  103. --port=port
  104. TCP/IP port to connect to if host is not "localhost".
  105. --socket='/path/to/socket'
  106. Socket to connect with.
  107. --type='table-type'
  108. Converts tables to the given table type (Default: $opt_type)
  109. MySQL 3.23 supports at least the BDB, ISAM and MYISAM types.
  110. --user='user_name'
  111. User name to log into the SQL server.
  112. --verbose
  113. This is a test specific option that is only used when debugging a test.
  114. Print more information about what is going on.
  115. --version
  116. Shows the version of this program.
  117. EOF
  118. exit(1);
  119. }