Guest User

Untitled

a guest
Sep 26th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. showMessage( "MySql2Sqlite v1.0" );
  4. showMessage( "by Daniele Ugoletti" );
  5. showMessage( "-------------------" );
  6.  
  7. // check the arguments
  8. if ( $argc < 5)
  9. {
  10. showMessageAndDie( "Usage: ".$argv[ 0 ]." <mysql_host> <mysql_username> <mysql_password> <dbName> [<outSqliteFile] [useUTF8 true|false]" );
  11. }
  12. if ( !class_exists( "PDO" ) )
  13. {
  14. showMessageAndDie( "ERROR: PDO not supported" );
  15. }
  16.  
  17. $hostname = $argv[ 1 ];
  18. $username = $argv[ 2 ];
  19. $password = $argv[ 3 ];
  20. $database = $argv[ 4 ];
  21. $outDb = $argc >= 6 ? $argv[ 5 ] : $argv[ 4 ].".db";
  22. $useUTF8 = $argc >= 7 ? $argv[ 6 ] == "true" : false;
  23.  
  24. // only for OS X users that use MAMP, check if there is a mysql socket to use
  25. $socket = file_exists( "/Applications/MAMP/tmp/mysql/mysql.sock" ) ? "/Applications/MAMP/tmp/mysql/mysql.sock" : ini_get( "mysqli.default_socket" );
  26.  
  27. // open mySql connection
  28. try
  29. {
  30. $options = $useUTF8 ? array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ) : array();
  31. $mysqli = new PDO( "mysql:host=".$hostname.";dbname=".$database.";unix_socket=".$socket, $username, $password, $options );
  32. }
  33. catch( PDOException $e )
  34. {
  35. showMessageAndDie( $e->getMessage() );
  36. }
  37.  
  38. // open Sqlite connection
  39. try
  40. {
  41. @unlink( $outDb );
  42. $sqlite = new PDO( "sqlite:".$outDb );
  43. }
  44. catch( PDOException $e )
  45. {
  46. showMessageAndDie( $e->getMessage() );
  47. }
  48.  
  49. showMessage( "Start converting mysql:".$database." to sqlite:".$outDb );
  50. foreach ( $mysqli->query( "SHOW TABLES;" ) as $row )
  51. {
  52. $tableName = $row[ 0 ];
  53. showMessage( "Convert table: ".$tableName );
  54. converTable( $mysqli, $sqlite, $tableName );
  55. }
  56. showMessage( "Done!" );
  57.  
  58.  
  59. function converTable( $mysqli, $sqlite, $tableName )
  60. {
  61. $createFields = array();
  62. $pkFields = array();
  63. $indexFields = array();
  64. $tableFields = array();
  65.  
  66. foreach ( $mysqli->query( "SHOW COLUMNS FROM ".$tableName ) as $row )
  67. {
  68. $tableFields[] = $row[ "Field" ];
  69. $fieldType = "TEXT";
  70. if ( stripos( $row[ "Type" ], "int(" ) !== false )
  71. {
  72. $fieldType = "INTEGER";
  73. }
  74. elseif ( stripos( $row[ "Type" ], "datetime") !== false )
  75. {
  76. $fieldType = "DATETIME";
  77. }
  78. elseif ( stripos( $row[ "Type" ], "date" ) !== false )
  79. {
  80. $fieldType = "DATE";
  81. }
  82.  
  83. if ( $row[ "Key" ] == "PRI" )
  84. {
  85. //$fieldType = "INTEGER";
  86. $pkFields[] = $row[ "Field" ];
  87. }
  88. else if ( $row[ "Key" ] == "MUL" )
  89. {
  90. $indexFields[] = "CREATE INDEX ".$row[ "Field" ]."_index ON ".$tableName."(".$row[ "Field" ].")";
  91. }
  92. $createFields[] = $row[ "Field" ]." ".$fieldType;
  93. }
  94.  
  95. if ( count( $pkFields ) )
  96. {
  97. array_push( $createFields, "PRIMARY KEY (".implode( ",", $pkFields ).")" );
  98. }
  99.  
  100. // create the table
  101. $sqlite->exec( "CREATE TABLE ".$tableName." (".implode(",", $createFields).")" );
  102.  
  103. // insert statement
  104. $insertSqlPart = str_repeat( "?,", count( $tableFields ) );
  105. $insertSqlPart = substr( $insertSqlPart, 0, -1 );
  106. $insertSql = "INSERT INTO ".$tableName."(".implode(",", $tableFields).") VALUES ( ".$insertSqlPart." ) ";
  107. $sth = $sqlite->prepare( $insertSql );
  108.  
  109. // get the number of records in the table
  110. $sthCount = $mysqli->query( "SELECT count(*) FROM ".$tableName );
  111. $row = $sthCount->fetch();
  112. $numRows = $row[ 0 ];
  113. $sthCount->closeCursor();
  114.  
  115. // read and convert all records
  116. $pageLength = 100000;
  117. $currentPage = 0;
  118. $i = 0;
  119. while ( true )
  120. {
  121. $sqlite->beginTransaction();
  122. foreach ( $mysqli->query( "SELECT * FROM ".$tableName." LIMIT ".$currentPage.",".$pageLength ) as $row )
  123. {
  124. $params = array();
  125. foreach( $tableFields as $v )
  126. {
  127. $params[] = $row[ $v ];
  128. }
  129.  
  130. $r = $sth->execute( $params );
  131. if ( !$r )
  132. {
  133. // error
  134. showMessageAndDie( print_r( $sqlite->errorInfo(), true) );
  135. }
  136.  
  137. $i++;
  138. }
  139. $sqlite->commit();
  140.  
  141. if ( $i < $numRows )
  142. {
  143. echo ".";
  144. $currentPage += $pageLength;
  145. }
  146. else
  147. {
  148. break;
  149. }
  150. }
  151.  
  152. showMessage( " imported: ".$i." rows" );
  153.  
  154. // create index
  155. if ( count( $indexFields ) )
  156. {
  157. showMessage( " create index: ".implode( ";", $indexFields ) );
  158. $sqlite->exec( implode( ";", $indexFields ) );
  159. }
  160. }
  161.  
  162. function showMessage( $message )
  163. {
  164. echo $message."\n";
  165. }
  166.  
  167. function showMessageAndDie( $message )
  168. {
  169. die( $message."\n\n" );
  170. }
  171. ?>
Add Comment
Please, Sign In to add comment