* Copyright (c) 2007 Platon Group, http://platon.sk/ * Licensed under terms of GNU General Public License. * All rights reserved. * * Changelog: * 2007-07-21 - created (Salt Lake City, UT) * 2007-07-24 - updated (Phoenix, AZ) * */ /* $Platon$ */ if ($_SERVER['argc'] < 3) { echo "Usage: php aim-import.php < -schema | -data > \n"; exit; } $type = ''; if (! strcasecmp($_SERVER['argv'][1], '-schema')) { $type = 'schema'; } elseif (! strcasecmp($_SERVER['argv'][1], '-data')) { $type = 'data'; } else { echo "ERROR: Bad type specified, use -schema or -data option.\n"; exit; } if (($f = @fopen($_SERVER['argv'][2], 'r')) == false) { echo 'ERROR: Unable to open file: ',$_SERVER['argv'][2], "\n"; exit; } $tables = array(); $cur_row = array(); $cur_table = ''; $cur_column = ''; while (($str = fgets($f)) != false) { $str = trim($str); if (preg_match('/^(.*)$|', $str, $matches)) { $cur_column = $matches[1]; } elseif (preg_match('|^(.*)$|', $str, $matches)) { $tables[$cur_table][$cur_column] = preg_replace('|identity|', '', $matches[1]); } elseif (preg_match('|^(.*)$|', $str, $matches)) { $coltype = strtolower(trim($tables[$cur_table][$cur_column])); if ($coltype == 'char' || $coltype == 'varchar') { $tables[$cur_table][$cur_column] .= "($matches[1])"; } } elseif ($type == 'data' && preg_match('|^$|', $str)) { $cur_row = array(); } elseif ($type == 'data' && preg_match('|^$|', $str)) { $keys = ''; $vals = ''; foreach ($cur_row as $key => $val) { $keys .= "`$key`,"; $vals .= "'".addslashes(trim($val))."',"; } printf("INSERT INTO `%s` (%s primary_key_id) VALUES (%s 0);\n", $cur_table, $keys, $vals); } elseif ($type == 'data' && preg_match('|^(.*)$|', $str, $matches)) { $cur_row[$matches[1]] = $matches[2]; } } if ($type == 'schema') { /* * Creating table structure */ foreach (array_keys($tables) as $table) { printf("DROP TABLE IF EXISTS `%s`;\n", $table); printf("CREATE TABLE `%s` (\n", $table); printf("primary_key_id bigint NOT NULL auto_increment,\n"); foreach (array_keys($tables[$table]) as $column) { $type = $tables[$table][$column]; $column = "`$column`"; printf("%20s %20s,\n", $column, $type); $sep == ' ' && $sep = ','; } printf("PRIMARY KEY (primary_key_id));\n"); printf("\n"); } } // if ($type == 'schema') /* Modeline for ViM {{{ * vim: set ts=4: * vim600: fdm=marker fdl=0 fdc=0: * }}} */ ?>