* *************************************************************************** * The contents of this file are subject to the Mozilla Public License * * Version 1.0 (the "License"); you may not use this file except in * * compliance with the License. You may obtain a copy of the License at * * http://www.mozilla.org/MPL/ * * * * Software distributed under the License is distributed on an "AS IS" * * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * * License for the specific language governing rights and limitations * * under the License. * * * * The Original Code is SQL Admin. * * The Initial Developer of the Original Code is * * Alessandro Vernet . * * Contributor(s): Julien Merçay . * ***************************************************************************/ /* TODO: * - Add sort order. * - Add simple view. * - Add some documentation. */ /* LIMITATIONS: * - Works only with mSQL. * - Is written for PHP2 (will not work with PHP3). */ /* HISTORY: * - 97-11-05 (avernet) Corrected a bug with quote. * - 98-01-01 (avernet) Added a sortColumn parameter to * administrationTable function. * - 98-03-14 (avernet) Added function addTable to enable users to * add (but not modify) en entry to the database. * - 98-05-19 (avernet) Submitted to PX. */ function escapeforhtml $string ( $result = $string; $result = ereg_replace ("\"", """, $result); $result = ereg_replace ("<", "<", $result); $result = ereg_replace (">", ">", $result); return $result; ); function displayTuple $fieldsNumber $fieldNames $fieldLengths $values $mode ( $result = ""; $result += "
" + "
"; $result += ""; $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $result += ""; $fieldIndex++; } $result += "
" + $fieldNames [$fieldIndex] + ""; if ($fieldLengths [$fieldIndex] <= 128) { $result += ""; } else { $result += ""; } $result += "" + "
"; if ($mode == "modify") { $result += ""; $result += ""; } else { $result += ""; } $result += "
"; return $result; ); function fieldFromType $text $type ( if ($type == "int" || $type == "uint" || $type =="real") { $result = $text; } else { $result = "'" + $text + "'"; } return $result; ); function executeMsql $database $command ( /*echo "" + $command + "
";*/ msql ($database, $command); ); function handleRemove $database $table $fieldsNumber $fieldNames $fieldLengths $fieldTypes ( global $remove; if ($remove != "") { $command = "DELETE FROM " + $table + " WHERE "; $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $fieldName = "old-" + $fieldNames [$fieldIndex]; global $$fieldName; $command += $fieldNames [$fieldIndex] + "=" + fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); if ($fieldIndex != $fieldsNumber - 1) { $command += " AND "; } $fieldIndex++; } executeMsql ($database, $command); } ); function handleUpdate $database $table $fieldsNumber $fieldNames $fieldLengths $fieldTypes ( global $update; if ($update != "") { $command = "UPDATE " + $table + " SET "; $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $fieldName = $fieldNames [$fieldIndex]; global $$fieldName; $command += $fieldName + "=" + fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); if ($fieldIndex != $fieldsNumber - 1) { $command += ", "; } $fieldIndex++; } $command += " WHERE "; $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $fieldName = "old-" + $fieldNames [$fieldIndex]; global $$fieldName; $command += $fieldNames [$fieldIndex] + "=" + fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); if ($fieldIndex != $fieldsNumber - 1) { $command += " AND "; } $fieldIndex++; } executeMsql ($database, $command); } ); function handleAdd $database $table $fieldsNumber $fieldNames $fieldLengths $fieldTypes ( global $add; if ($add != "") { $command = "INSERT INTO " + $table + " ("; $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $command += $fieldNames [$fieldIndex]; if ($fieldIndex != $fieldsNumber - 1) { $command += ", "; } $fieldIndex++; } $command += ") VALUES ("; $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $fieldName = $fieldNames [$fieldIndex]; global $$fieldName; $command += fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); if ($fieldIndex != $fieldsNumber - 1) { $command += ", "; } $fieldIndex++; } $command += ")"; executeMsql ($database, $command); } ); function displayRemoveUpdate $database $table $sortColumn $fieldsNumber $fieldNames $fieldLengths ( $result = ""; if ($sortColumn != "") { $sortColumn = " ORDER BY " + $sortColumn; } $msqlresult = msql ($database, "SELECT * FROM " + $table + $sortColumn); $tuplesNumber = msql_numrows ($msqlresult); $tupleIndex = 0; while ($tupleIndex < $tuplesNumber) { $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $values [$fieldIndex] = msql_result ($msqlresult, $tupleIndex, $fieldNames [$fieldIndex]); $fieldIndex++; } $result += displayTuple ($fieldsNumber, $fieldNames, $fieldLengths, $values, "modify"); $tupleIndex++; } return $result; ); function displayAdd $fieldsNumber $fieldNames $fieldLengths ( $result = ""; $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $values [$fieldIndex] = ""; $fieldIndex++; } $result += displayTuple ($fieldsNumber, $fieldNames, $fieldLengths, $values, "add"); msql_close (); return $result; ); function administrationTable $database $table $sortColumn ( $result = ""; msql_connect ("localhost"); $msqlresult = msql ($database, "SELECT * FROM " + $table); $fieldsNumber = msql_numfields ($msqlresult); $msqlresult = msql_listfields ($database, $table); $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $fieldNames [$fieldIndex] = msql_fieldname ($msqlresult, $fieldIndex); $fieldLengths [$fieldIndex] = msql_fieldlen ($msqlresult, $fieldIndex); $fieldTypes [$fieldIndex] = msql_fieldtype ($msqlresult, $fieldIndex); $fieldIndex++; } handleRemove ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); handleUpdate ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); handleAdd ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); $result += displayRemoveUpdate ($database, $table, $sortColumn, $fieldsNumber, $fieldNames, $fieldLengths); $result += displayAdd ($fieldsNumber, $fieldNames, $fieldLengths); return $result; ); function addTable $database $table ( $result = ""; msql_connect ("localhost"); $msqlresult = msql ($database, "SELECT * FROM " + $table); $fieldsNumber = msql_numfields ($msqlresult); $msqlresult = msql_listfields ($database, $table); $fieldIndex = 0; while ($fieldIndex < $fieldsNumber) { $fieldNames [$fieldIndex] = msql_fieldname ($msqlresult, $fieldIndex); $fieldLengths [$fieldIndex] = msql_fieldlen ($msqlresult, $fieldIndex); $fieldTypes [$fieldIndex] = msql_fieldtype ($msqlresult, $fieldIndex); $fieldIndex++; } handleAdd ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); $result += displayAdd ($fieldsNumber, $fieldNames, $fieldLengths); return $result; ); >