#!/usr/bin/php to_php())); fputs($stream, "?>\n"); fclose($stream); } $pdo=&$S['pdo']; $r=$pdo->query('SHOW TABLES'); $tables=$r->fetchAll(PDO::FETCH_COLUMN); $check_existing=prompt_bool('Check existing classes for changes from the database?'); foreach ($tables as $i => $table) { if ($class=sql_row_obj::table_to_class($table)) { unset($tables[$i]); if (!$check_existing) { continue; } if (substr($class, 0, 4) == 'sql_') { $origfile=substr($class, 4); } else { $origfile=$class; } $origfile=SHARED.'/classes/'.$origfile.'.php'; if (is_file($origfile)) { write_table_class_to_file($table, $class, '.temp_class'); $lines=count(explode("\n", rtrim(file_get_contents('.temp_class'))))-2; $diff=`diff "$origfile" .temp_class`; $difftmp=fopen('.temp_diff', 'w'); $diffname='.temp_diff'; $domerge=false; foreach (explode("\n", $diff) as $line) { $line.="\n"; if (strlen($line) && is_numeric(substr($line, 0, 1))) { if (preg_match('/^[0-9]+(?:,[0-9]+)?[a-zA-Z](?:[0-9]+,)?([0-9]+)?\s/', $line, $match)) { $endl=$match[1]; if ($endl > $lines-3) { break; } } } if (strlen(trim($line))) { $domerge=true; } fputs($difftmp, $line); } fclose($difftmp); if ($domerge) { copy($origfile, '.temp_class'); shell_exec('patch .temp_class < .temp_diff'); passthru('colordiff -u "'.$origfile.'" .temp_class'); if (prompt_bool('Keep changes?')) { rename('.temp_class', $origfile); } } unlink('.temp_diff'); if (is_file('.temp_class')) unlink('.temp_class'); // TODO unlink others } } } if ($tables) { echo 'Found table(s) without corresponding class: '.implode(', ', $tables)."\n"; if (!prompt_bool('Generate classes for these tables?')) { unset($tables); } } else { echo "No tables found without corresponding classes.\n"; } if (!$tables) { die("Exiting.\n"); /* echo 'Which tables should we generate classes for? (space-separated) '; $tables=explode(' ', trim(fgets(STDIN))); if ($tables[0] == '') { echo "Exiting.\n"; exit; } */ } echo 'Generating classes for table(s): '.implode(', ', $tables)."\n"; foreach ($tables as $table) { $default='sql_'; if (substr($table, -1) == 's') { $default.=substr($table, 0, strlen($table)-1); } else { $default.=$table; } $class=prompt_string('Class name for table '.$table.':', $default); if (substr($class, 0, 4) == 'sql_') { $default=substr($class, 4); } else { $default=$class; } $default.='.php'; $file=SHARED.'/classes/'.prompt_string('Filename for class '.$class.': '.SHARED.'/classes/', $default); if (is_file($file)) { if (!prompt_bool($file.' exists. Overwrite?', false)) { echo 'Skipping table '.$table."\n"; continue; } } echo 'Writing '.$file.'...'; write_table_class_to_file($table, $class, $file); echo "done\n"; } ?>