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.

373 lines
9.6 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. #!/usr/bin/perl
  2. # Copyright (c) 2000, 2003, 2006 MySQL AB, 2009 Sun Microsystems, Inc.
  3. # Use is subject to license terms.
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; version 2
  8. # of the License.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the Free
  17. # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. # MA 02110-1301, USA
  19. #
  20. # start initialition
  21. #
  22. $VER = "1.0";
  23. use Getopt::Long;
  24. use Cwd;
  25. use DBI;
  26. $max_row_length=500000; # Don't create bigger SQL rows that this
  27. $opt_lock=1; # lock tables
  28. $pwd = cwd(); $pwd = "." if ($pwd eq '');
  29. require "$pwd/server-cfg" || die "Can't read Configuration file: $!\n";
  30. $|=1;
  31. $opt_from_server= $opt_to_server= "mysql";
  32. $opt_from_host= $opt_to_host= "localhost";
  33. $opt_from_db= $opt_to_db= "test";
  34. $opt_from_user=$opt_from_password=$opt_to_user=$opt_to_password="";
  35. $opt_help=$opt_verbose=$opt_debug=0;
  36. GetOptions("from-server=s","to-server=s","from-host=s","to-host=s","from-db=s",
  37. "to-db=s", "help", "verbose","debug") || usage();
  38. usage() if ($opt_help ||
  39. ($opt_from_server eq $opt_to_server &&
  40. $opt_from_db eq $opt_to_db &&
  41. $opt_from_host eq $opt_to_host));
  42. ####
  43. #### Usage
  44. ####
  45. sub usage
  46. {
  47. print <<EOF;
  48. $0 version $VER by Monty
  49. Copies tables between two database servers. If the destination table doesn\'t
  50. exist it\'s autoamticly created. If the destination table exists, it
  51. should be compatible with the source table.
  52. Because DBI doesn\'t provide full information about the columns in a table,
  53. some columns may not have optimal types in a create tables. Any created
  54. tables will also not have any keys!
  55. Usage: $0 [options] tables...
  56. Options:
  57. --help Show this help and exit
  58. --from-server Source server (Default: $opt_from_server)
  59. --from-host Source hostname (Default: $opt_from_host)
  60. --from-db Source database name (Default: $opt_from_db)
  61. --from-user Source user (Default: $opt_from_password)
  62. --from-password Source password (Default: $opt_from_password)
  63. --to-server Destination server (Default: $opt_to_server)
  64. --to-host Destination hostname (Default: $opt_to_host)
  65. --to-db Destination database name (Default: $opt_to_db)
  66. --to-user Destination user (Default: $opt_to_user)
  67. --to-password Destination password (Default: $opt_to_password)
  68. --verbose Be more verbose
  69. If you the server names ends with _ODBC, then this program will connect
  70. through ODBC instead of using a native driver.
  71. EOF
  72. exit(0);
  73. }
  74. ####
  75. #### Connect
  76. ####
  77. $from_server=get_server($opt_from_server,$opt_from_host,$opt_from_db);
  78. $to_server=get_server($opt_to_server,$opt_to_host,$opt_to_db);
  79. $opt_user=$opt_from_user; $opt_password=$opt_from_password;
  80. print "- connecting to SQL servers\n" if ($opt_verbose);
  81. $from_dbh=$from_server->connect() || die "Can't connect to source server $opt_from_server on host $opt_from_host using db $opt_from_db";
  82. $opt_user=$opt_to_user; $opt_password=$opt_to_password;
  83. $to_dbh=$to_server->connect() || die "Can't connect to source server $opt_to_server on host $opt_to_host using db $opt_to_db";
  84. ####
  85. #### Copy data
  86. ####
  87. foreach $table (@ARGV)
  88. {
  89. print "- querying $table\n" if ($opt_verbose);
  90. $sth=$from_dbh->prepare("select * from $table") || die "Can't prepare query to get $table; $DBI::errstr";
  91. $sth->execute || die "Can't execute query to get data from $table; $DBI::errstr";
  92. if (!table_exists($to_server,$to_dbh,$table))
  93. {
  94. print "- creating $table\n" if ($opt_verbose);
  95. $table_def=get_table_definition($from_server,$from_dbh,$sth);
  96. do_many($to_dbh,$to_server->create($table,$table_def,[]));
  97. }
  98. if ($opt_lock && $to_server->{'lock_tables'})
  99. {
  100. print "- locking $table\n" if ($opt_verbose);
  101. $to_dbh->do("lock tables $table WRITE");
  102. }
  103. $columns=$sth->{NUM_OF_FIELDS};
  104. $columns_to_quote=get_columns_to_quote($sth);
  105. $insert_multi_value=$sth->{'insert_multi_value'};
  106. $query="insert into $table values"; $result="";
  107. print "- copying $table\n" if ($opt_verbose);
  108. while (($row = $sth->fetchrow_arrayref))
  109. {
  110. $tmp="(";
  111. for ($i=0 ; $i < $columns ; $i++)
  112. {
  113. if ($columns_to_quote->[$i])
  114. {
  115. $tmp.= $to_dbh->quote($row->[$i]) . ",";
  116. }
  117. else
  118. {
  119. $tmp.= $row->[$i] . ",";
  120. }
  121. }
  122. substr($tmp,-1)=")"; # Remove last ','
  123. if ($insert_multi_value)
  124. {
  125. $to_dbh->do($query . $tmp) || die "Can't insert row: $DBI::errstr";
  126. }
  127. elsif (length($result)+length($tmp) >= $max_row_length && $result)
  128. {
  129. $to_dbh->do($query . $result) || die "Can't insert row: $DBI::errstr";
  130. $result="";
  131. }
  132. elsif (length($result))
  133. {
  134. $result.= ",$tmp";
  135. }
  136. else
  137. {
  138. $result=$tmp;
  139. }
  140. }
  141. if (length($result))
  142. {
  143. $to_dbh->do($query . $result) || die "Can't insert row: $DBI::errstr";
  144. }
  145. if ($opt_lock && $to_server->{'lock_tables'})
  146. {
  147. $to_dbh->do("unlock tables");
  148. }
  149. }
  150. sub get_table_definition
  151. {
  152. my ($server,$dbh,$sth)=@_;
  153. my ($i,$names,$types,$scale,$precision,$nullable,@res);
  154. $names=$sth->{NAME};
  155. $types=$sth->{TYPE};
  156. $nullable=$sth->{NULLABLE};
  157. if (0)
  158. {
  159. # The following doesn't yet work
  160. $scale=$sth->{SCALE};
  161. $precision=$sth->{PRECISION};
  162. }
  163. else
  164. {
  165. my (@tmp);
  166. @tmp= (undef()) x $sth->{NUM_OF_FIELDS};
  167. $precision= $scale= \@tmp;
  168. }
  169. for ($i = 0; $i < $sth->{NUM_OF_FIELDS} ; $i++)
  170. {
  171. push(@res,$names->[$i] . " " .
  172. odbc_to_sql($server,$types->[$i],$precision->[$i],$scale->[$i]) .
  173. ($nullable->[$i] ? "" : " NOT NULL"));
  174. }
  175. return \@res;
  176. }
  177. sub odbc_to_sql
  178. {
  179. my ($server,$type,$precision,$scale)=@_;
  180. if ($type == DBI::SQL_CHAR())
  181. {
  182. return defined($precision) ? "char($precision)" : "varchar(255)";
  183. }
  184. if ($type == DBI::SQL_NUMERIC())
  185. {
  186. $precision=15 if (!defined($precision));
  187. $scale=6 if (!defined($scale));
  188. return "numeric($precision,$scale)";
  189. }
  190. if ($type == DBI::SQL_DECIMAL())
  191. {
  192. $precision=15 if (!defined($precision));
  193. $scale=6 if (!defined($scale));
  194. return "decimal($precision,$scale)";
  195. }
  196. if ($type == DBI::SQL_INTEGER())
  197. {
  198. return "integer" if (!defined($precision));
  199. return "integer($precision)";
  200. }
  201. if ($type == DBI::SQL_SMALLINT())
  202. {
  203. return "smallint" if (!defined($precision));
  204. return "smallint($precision)";
  205. }
  206. if ($type == DBI::SQL_FLOAT())
  207. {
  208. $precision=12 if (!defined($precision));
  209. $scale=2 if (!defined($scale));
  210. return "float($precision,$scale)";
  211. }
  212. if ($type == DBI::SQL_REAL())
  213. {
  214. $precision=12 if (!defined($precision));
  215. $scale=2 if (!defined($scale));
  216. return "float($precision,$scale)";
  217. }
  218. if ($type == DBI::SQL_DOUBLE())
  219. {
  220. $precision=22 if (!defined($precision));
  221. $scale=2 if (!defined($scale));
  222. return "double($precision,$scale)";
  223. }
  224. if ($type == DBI::SQL_VARCHAR())
  225. {
  226. $precision=255 if (!defined($precision));
  227. return "varchar($precision)";
  228. }
  229. return "date" if ($type == DBI::SQL_DATE());
  230. return "time" if ($type == DBI::SQL_TIME());
  231. return "timestamp" if ($type == DBI::SQL_TIMESTAMP());
  232. return $server->{'text'} if ($type == DBI::SQL_LONGVARCHAR());
  233. return $server->{'blob'} if ($type == DBI::SQL_LONGVARBINARY());
  234. if ($type == DBI::SQL_BIGINT())
  235. {
  236. return "bigint" if (!defined($precision));
  237. return "bigint($precision)";
  238. }
  239. if ($type == DBI::SQL_TINYINT())
  240. {
  241. return "tinyint" if (!defined($precision));
  242. return "tinyint($precision)";
  243. }
  244. die "Can't covert type '$type' to a ODBC type\n";
  245. }
  246. #
  247. # return an array with 1 for all coumns that we have to quote
  248. #
  249. sub get_columns_to_quote($sth)
  250. {
  251. my ($sth)=@_;
  252. my ($i,@res,$type,$tmp);
  253. @res=();
  254. for ($i = 0; $i < $sth->{NUM_OF_FIELDS} ; $i++)
  255. {
  256. $type=$sth->{TYPE}->[$i];
  257. $tmp=1; # String by default
  258. if ($type == DBI::SQL_NUMERIC() || $type == DBI::SQL_DECIMAL() ||
  259. $type == DBI::SQL_INTEGER() || $type == DBI::SQL_SMALLINT() ||
  260. $type == DBI::SQL_SMALLINT() || $type == DBI::SQL_FLOAT() ||
  261. $type == DBI::SQL_REAL() || $type == DBI::SQL_DOUBLE() ||
  262. $type == DBI::SQL_BIGINT() || $type == DBI::SQL_TINYINT())
  263. {
  264. $tmp=0;
  265. }
  266. push (@res,$tmp);
  267. }
  268. return \@res;
  269. }
  270. #
  271. # Check if table exists; Return 1 if table exists
  272. #
  273. sub table_exists
  274. {
  275. my ($server,$dbh,$table)=@_;
  276. if ($server->{'limits'}->{'group_functions'})
  277. {
  278. return !safe_query($dbh,"select count(*) from $table");
  279. }
  280. if ($server->{'limits'}->{'limit'})
  281. {
  282. return !safe_query($dbh,"select * from $table limit 1");
  283. }
  284. die "Don't know how to check if table '$table' exists in destination server\n";
  285. }
  286. #
  287. # execute query; return 0 if query is ok
  288. #
  289. sub safe_query
  290. {
  291. my ($dbh,$query)=@_;
  292. my ($sth);
  293. print "query: $query\n" if ($opt_debug);
  294. if (!($sth= $dbh->prepare($query)))
  295. {
  296. print "error: $DBI::errstr\n" if ($opt_debug);
  297. return 1;
  298. }
  299. if (!$sth->execute)
  300. {
  301. print "error: $DBI::errstr\n" if ($opt_debug);
  302. return 1
  303. }
  304. while ($sth->fetchrow_arrayref)
  305. {
  306. }
  307. $sth->finish;
  308. undef($sth);
  309. return 0;
  310. }
  311. #
  312. # execute an array of queries
  313. #
  314. sub do_many
  315. {
  316. my ($dbh,@statements)=@_;
  317. my ($statement,$sth);
  318. foreach $statement (@statements)
  319. {
  320. print "query: $statement\n" if ($opt_debug);
  321. if (!($sth=$dbh->do($statement)))
  322. {
  323. die "Can't execute command '$statement'\nError: $DBI::errstr\n";
  324. }
  325. }
  326. }