0) { $val = pg_fetch_array($result, $row); // read from encoded data while($data = each($val)) { $val[$data["key"]] = decode_text($data["value"]); } return $val; } else return 0; } function number_of_rows($result) { return pg_numrows($result); } function number_of_columns($result) { return pg_numfields($result); } function select_all_from($table, $condition, $sort, $order) { // returns query string if (($order != "DESC") and ($order != "desc") and ($order != "")) $order = "ASC"; $query = "SELECT * FROM \"$table\""; if ($condition != "") $query .= " WHERE ($condition)"; if ($sort != "") $query .= " ORDER BY \"$sort\" $order"; $query .= ';'; return $query; } function get_whole_table($db, $link, $table) { return execute_query($db, $link, select_all_from($table, "", "", "")); } function get_any_from($db, $link, $table, $condition) { // this assumes that at least one entry will be returned $query = select_all_from($table, $condition, "", ""); $result = execute_query($db, $link, $query); return fetch_result_array($result, 0); } function select_max_from($table, $field) { $query = "SELECT MAX(\"$field\") FROM \"$table\";"; return $query; } function get_max_from($db, $link, $table, $field) { $query = select_max_from($table, $field); $val = fetch_result_array(execute_query($db, $link, $query), 0); return $val["max"]; } function field_name($result, $fieldnr) { return pg_fieldname($result, $fieldnr); } function drop_table($table) { return "DROP TABLE '$table';"; } function create_table($table, $types) { // "types" is supposed to be an associative array $query = "CREATE TABLE \"$table\""; $first_field = true; while($field = each($types)) { if($first_field) { $first_field = false; $query .= ' ('; } else { $query .= ', '; } $query .= "\"" . $field["key"] . "\"" . ' ' . $field["value"]; } $query .= ');'; return $query; } function insert_into($table, $assign) { // "assign" is supposed to be an associative array $fields = ""; $values = ""; $first_field = true; while($field = each($assign)) { if($first_field) $first_field = false; else { $fields .= ', '; $values .= ', '; } $this_value = encode_text($field["value"]); $fields .= "\"" . $field["key"] . "\""; $values .= "'$this_value'"; } $query .= "INSERT INTO \"$table\" ($fields) VALUES ($values);"; return $query; } function insert_next($db, $link, $table, $index_name, $assign) { $nextid = get_max_from($db, $link, $table, $index_name) + 1; $assign[$index_name] = $nextid; return insert_into($table, $assign); } function update_set($table, $assign, $condition) { $query = "UPDATE \"$table\" SET"; $first_field = true; while($field = each($assign)) { if($first_field) $first_field = false; else { $query .= ', '; } $this_field = $field["key"]; $this_value = encode_text($field["value"]); $query .= "\"$this_field\" = '$this_value'"; } if ($condition != "") $query .= " WHERE ($condition)"; $query .= ';'; return $query; } function field_to_field_copy($db, $link, $table, $source, $target, $condition) { $query = "UPDATE \"$table\" SET \"$target\" = \"$source\" WHERE ($condition);"; return execute_query($db, $link, $query); } function delete_from($table, $condition) { $query = "DELETE FROM \"$table\""; if ($condition != "") $query .= " WHERE ($condition)"; $query .= ";"; return $query; } function condition_of_identity($field, $value) { return "\"$field\" = '" . encode_text($value) . "'"; } function condition_of_exact_identity($field, $value) { return "\"$field\" = '" . encode_text($value) . "'"; } function condition_of_less($field, $value) { return "\"$field\" < '" . encode_text($value) . "'"; } function condition_of_more($field, $value) { return "\"$field\" > '" . encode_text($value) . "'"; } function condition_of_truth($field) { global $true_value; return condition_of_identity($field, $true_value); } function conjunction($F, $G) { return "($F) AND ($G)"; } function disjunction($F, $G) { return "($F) OR ($G)"; } function negation($F) { return "NOT ($F)"; } Function ShowResults($result) { if ($result) { $rows = pg_NumRows($result); $cols = pg_NumFields($result); echo("\n"); /* Create the headers */ echo("\n"); for($i = 0; $i < $cols; $i++) { printf("\n", pg_FieldName($result, $i)); } echo(""); for($j = 0; $j < $rows; $j++) { echo("\n"); for($i = 0; $i < $cols; $i++) { printf("\n", pg_result($result, $j, $i)); } echo(""); } echo("
%s
%s
"); } else { echo(pg_errormessage); } } function ShowTableRows($selectedtable) { $rownames = pg_query($conn, "SELECT column_name FROM information_schema.columns WHERE table_name = $selectedtable;"); } ?>