function genSQL($object) { global $admin_dsn; $o = $object . "Table"; $data = new $o(); list($dbtype, $tmp) = preg_split('/:\\/\\//', $admin_dsn); $sql = "CREATE TABLE {$data->name} (\n"; $i = 0; if ($data->definition) { foreach ($data->definition as $column) { unset($size); if (isset($type) && $type == 'external') { next($data->definition); } if ($i) { $sql .= " ,\n"; } $sql .= genColumnSQL($column, $dbtype); ++$i; } } $sql .= ', PRIMARY KEY ( ' . join(',', $data->keys) . ' ) '; $sql .= "\n);\n\n"; return $sql; }
foreach ($campos as $campo) { $sqlcmd = "SELECT {$campo} FROM {$data->name}"; @$data->execSql($sqlcmd); if (PEAR::isError($data->data)) { $tables_output .= " <small>Error en campo {$campo} <!--" . $data->data->getMessage() . "--></small><br/>"; $size = $data->dd[$campo]['size'] > 0 ? '(' . $data->dd[$campo]['size'] . ')' : ''; if (!isset($data->key)) { $data->key = false; } if (!isset($global_dd[$campo])) { $global_dd[$campo] = null; } if (!isset($global_dd[$campo]['name'])) { $global_dd[$campo]['name'] = null; } $sql_fix .= "ALTER TABLE {$data->name} ADD COLUMN " . genColumnSQL($data->dd[$campo], $dbtype, $global_dd[$campo]['name'] === $data->key) . ";\n"; } } } } else { $tables_output .= "{$green}<br/>"; } } break; case 'exec': if (isset($_REQUEST['sqlcmd'])) { $sqlcmd = almdata::escape($_REQUEST['sqlcmd']); } else { $sqlcmd = ''; } if (!isset($_REQUEST['fix'])) {
$this->execSql($sql_fix); echo "AUTO SQL {$sql_fix}<br/>\n"; } else { $campos = preg_split('/,/', $this->table_fields); foreach ($campos as $campo) { #$existe = $this->catalogColumnExists($campo['name']); $sqlcmd = "SELECT {$campo} FROM {$this->name} LIMIT 1"; @$this->execSql($sqlcmd); if (almdata::isError($this->data)) { list($tmp, $campo) = preg_split('/\\./', $campo); $size = isset($this->dd[$campo]['size']) && $this->dd[$campo]['size'] > 0 ? '(' . $this->dd[$campo]['size'] . ')' : ''; if (!isset($this->key)) { $this->key = false; } if (!isset($global_dd[$campo])) { $global_dd[$campo] = null; } if (!isset($global_dd[$campo]['name'])) { $global_dd[$campo]['name'] = null; } $sql_fix = "ALTER TABLE {$this->name} ADD COLUMN " . genColumnSQL($this->dd[$campo], $dbtype, $global_dd[$campo]['name'] === $this->key) . "; "; $this->execSql($sql_fix); echo "AUTO SQL {$sql_fix}<br/>\n"; } } } } #if (!empty($sql_fix)) { #$this->execSql($sql_fix); #echo "AUTO SQL en $this->name : $sql_fix<br/>\n"; #}