$fields = implode($headers, ','); foreach ($rows as $row) { $results .= "INSERT INTO {$this->name} ({$fields}) VALUES ("; foreach ($row as $column) { $cols[] = "'{$column}'"; } $results .= implode($cols, ","); unset($cols); $results .= ");\n"; } $results .= "</pre>"; break; case 'sqlschema': require_once 'setup.gensql.php'; $results = "<pre>\n"; $results .= genSql($this->name); $results .= "</pre>"; break; case 'alm': header('Content-type: text/plain'); header('Content-Disposition: attachment; filename="' . $filename . '.php";'); $results = "<?php\nrequire('../classes/app.class.php');\n"; $results .= '$data = new ' . $this->name . 'Table();' . "\n"; $results .= '$data->readEnv();' . "\n"; $results .= 'if (isset($data->request[$data->key])) {' . "\n"; $results .= ' $row = $data->readRecord();' . "\n"; $results .= ' $smarty->assign(\'row\',$row);' . "\n"; if (isset($this->child)) { $results .= ' if (isset($data->child)) {' . "\n"; $results .= ' $children = preg_split(\'/,/\',$data->child);' . "\n"; $results .= ' foreach ($children as $child) {' . "\n";
private function _addTableByMeta($tableMeta, $force = false) { $tbl = $tableMeta['name']; if (tableExists($this->dbh, $tbl)) { if (!$force) { # check whether to add missing fields $rs = $this->execSql("SELECT * FROM (SELECT 1 AS id) t0 LEFT JOIN {$tbl} ON 1<>1", true); $row = $rs[0]; $found = false; foreach ($tableMeta["fields"] as $f) { if (preg_match('/\\w+/', $f, $ms)) { $fieldName = $ms[0]; if (!array_key_exists($fieldName, $row)) { $found = true; $this->_addColByMeta($tbl, $f); } } } return $found; } $this->execSql("DROP TABLE {$tbl}"); } logstr("-- {$tbl}: " . join(',', $tableMeta['fields']) . "\n"); $sql = genSql($tableMeta); logstr("{$sql}\n"); $this->dbh->exec($sql); return true; }
exit; } $sqlFilePath = CUR_PATH . '/../Data/' . $tableName . '.sql'; if (!file_exists($sqlFilePath)) { echo "Error: sql file {$sqlFilePath} not exists!\n"; exit; } $sqlContent = file_get_contents($sqlFilePath); $sqlContent = trim($sqlContent); $outputSql = ''; foreach ($tableMap['map'] as $mapItem) { $dbName = isset($mapItem['db']) ? $mapItem['db'] : 'db'; if (!isset($dbsConfig['servers'][$dbName])) { echo "Error: no such db server as db = {$dbName} !\n"; exit; } $outputSql .= "\n/**\n * DB: {$dbsConfig['servers'][$dbName]['host']} {$dbsConfig['servers'][$dbName]['name']}\n */\n"; $charset = isset($dbsConfig['servers'][$dbName]['charset']) ? $dbsConfig['servers'][$dbName]['charset'] : 'utf8'; if (isset($mapItem['start']) && isset($mapItem['end'])) { for ($i = $mapItem['start']; $i <= $mapItem['end']; $i++) { $outputSql .= genSql($tableMap['prefix'] . $tableName . '_' . $i, $tableMap['key'], $sqlContent, $engine, $charset); } } else { $outputSql .= genSql($tableMap['prefix'] . $tableName, $tableMap['key'], $sqlContent, $engine, $charset); } } echo $outputSql; function genSql($tableName, $tableKey, $sqlContent, $engine, $charset) { return sprintf("\nCREATE TABLE `%s` (\n `%s` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n %s\n `ext_data` text COMMENT 'json data here',\n PRIMARY KEY (`%s`)\n ) ENGINE=%s DEFAULT CHARSET=%s;\n \n", $tableName, $tableKey, $sqlContent, $tableKey, $engine, $charset); }