Mercurial > hg > freeDiameter
annotate contrib/app_acct_tools/purge_to_file.php @ 533:4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Tue, 07 Sep 2010 16:57:48 +0900 |
parents | |
children |
rev | line source |
---|---|
533
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
1 <?php |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
2 # This file may be called either from CLI or web PHP installation. |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
3 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
4 # The copyright is the same as the freeDiameter project. Licence is BSD. |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
5 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
6 /*-------------------------------------------------------*/ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
7 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
8 /* -- These parameters must match the app_acct.conf file -- */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
9 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
10 /* The Connection String used to access the database. |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
11 Example: "host=localhost dbname=app_acct user=freediameter password=foo" */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
12 $CONNSTR=""; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
13 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
14 /* The name of the table were the records are saved by app_acct.fdx */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
15 $TABLE="incoming"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
16 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
17 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
18 /* -- These parameters are specific to this script -- */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
19 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
20 /* Name of the dump file to create. It can be relative or absolute. |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
21 The script fails if this file already exists. |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
22 Use for example "data-".date("Ymdhis").".sql" to avoid collisions. */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
23 $DUMPFILE="incoming_data.sql"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
24 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
25 /* Does the script also echoes an HTML TABLE of the data processed? */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
26 $DISPLAY_HTML=FALSE; // it would be better to detect if we are called through Web or CLI... |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
27 /* In addition, issue a full HTML page (including headers) ? */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
28 $HTML_HEADERS=TRUE; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
29 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
30 /*-------------------------------------------------------*/ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
31 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
32 /* Connect to the database */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
33 $dbconn = pg_connect($CONNSTR) |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
34 or die('Could not connect: ' . pg_last_error() . "\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
35 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
36 /* Check if the file exists */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
37 $file = fopen($DUMPFILE, "xb") |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
38 or die("The file '$DUMPFILE' already exists or cannot be created, aborting.\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
39 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
40 /* First, query the table format */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
41 $format_sql = "SELECT ". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
42 " a.attname AS field,". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
43 " t.typname AS type,". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
44 /* " a.attlen AS length,". */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
45 " a.atttypmod AS lengthvar,". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
46 " a.attnotnull AS notnull". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
47 " FROM". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
48 " pg_class c,". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
49 " pg_attribute a,". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
50 " pg_type t". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
51 " WHERE". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
52 " c.relname = '" . $TABLE . "'". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
53 " AND a.attnum > 0". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
54 " AND a.attrelid = c.oid". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
55 " AND a.atttypid = t.oid". |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
56 " ORDER BY a.attnum"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
57 $result = pg_query($dbconn, $format_sql) or die('Query failed: ' . pg_last_error() . "\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
58 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
59 /* app_acct.fdx only uses a few different fields types: */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
60 $conv_types = array( |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
61 "timestamptz" => "timestamp with time zone", /* recorded on */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
62 "bytea" => "bytea", /* octet string */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
63 "int4" => "integer", /* unsigned32, integer32, float32 */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
64 "int8" => "bigint" /* unsigned64, integer64, float64 */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
65 ); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
66 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
67 $fields_types=array(); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
68 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
69 fwrite($file, " -- Data purged on ".date(DATE_RFC822)."\n\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
70 fwrite($file, " -- Format of the table it was extracted from:\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
71 fwrite($file, " -- CREATE TABLE \"".$TABLE."\" (\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
72 $i = 0; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
73 while ($field = pg_fetch_array($result, null, PGSQL_ASSOC)) { |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
74 if ($i++) |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
75 fwrite($file, ",\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
76 fwrite($file, " -- \"".$field["field"]."\" "); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
77 if (array_key_exists($field["type"], $conv_types)) |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
78 $fields_types[$field["field"]] = $conv_types[$field["type"]]; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
79 else |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
80 $fields_types[$field["field"]] = $field["type"]; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
81 fwrite($file, $fields_types[$field["field"]]); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
82 if ($field["lengthvar"] != "-1") |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
83 fwrite($file, "(".$field["lengthvar"].")"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
84 if ($field["notnull"] == "t") |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
85 fwrite($file, " NOT NULL"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
86 } |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
87 fwrite($file, "\n -- );\n\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
88 pg_free_result($result); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
89 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
90 /* Now, the data */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
91 $result = pg_query($dbconn, "SELECT * FROM \"".$TABLE."\"") or die('Query failed: ' . pg_last_error() . "\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
92 if (pg_num_rows($result) > 0) { |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
93 fwrite($file, "INSERT INTO \"".$TABLE."\"\n (\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
94 $i = pg_num_fields($result); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
95 if ($DISPLAY_HTML && $HTML_HEADERS) echo "<HTML>\n<HEAD><TITLE>Purge</TITLE></HEAD>\n<BODY>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
96 if ($DISPLAY_HTML) echo "<TABLE>\n <TR>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
97 for ($j = 0; $j < $i; $j++) { |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
98 fwrite($file, ($j ? ", " : "") . "\"" . pg_escape_string(pg_field_name($result, $j)). "\""); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
99 if ($DISPLAY_HTML) echo " <TD>".htmlentities(pg_field_name($result, $j))."</TD>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
100 } |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
101 fwrite($file, "\n )\n VALUES \n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
102 if ($DISPLAY_HTML) echo " </TR>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
103 $i = 0; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
104 while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) { |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
105 if ($i++) |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
106 fwrite($file, ",\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
107 fwrite($file, " ( "); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
108 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
109 if ($DISPLAY_HTML) echo " <TR>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
110 $j = 0; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
111 $sql = ""; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
112 foreach ($line as $f => $v) { |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
113 if (!is_null($v)) |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
114 switch ($fields_types[$f]) { |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
115 case "bytea": |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
116 $v = "E'".pg_escape_bytea(pg_unescape_bytea($v))."'"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
117 break; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
118 case "timestamp with time zone": |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
119 $v = "E'".pg_escape_string($v)."'"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
120 break; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
121 } |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
122 else |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
123 $v = "NULL"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
124 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
125 if ($DISPLAY_HTML) echo " <TD>".htmlentities(print_r($line[$f], TRUE))."</TD>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
126 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
127 fwrite($file, ($j ? ", " : "") . $v); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
128 $sql .= ($j ? " AND " : "") . "\"".pg_escape_string($f)."\" "; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
129 if (is_null($line[$f])) |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
130 $sql .= " IS NULL"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
131 else |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
132 $sql .= " = " . $v; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
133 $j++; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
134 } |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
135 fwrite($file, ")"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
136 if ($DISPLAY_HTML) echo " </TR>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
137 $res = pg_query( "DELETE FROM \"".$TABLE."\" WHERE ".$sql) or die('DELETE query failed: ' . pg_last_error() . "\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
138 pg_free_result($res); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
139 } |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
140 fwrite($file, "\n;\n"); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
141 if ($DISPLAY_HTML) echo "</TABLE>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
142 if ($DISPLAY_HTML && $HTML_HEADERS) echo "</BODY>\n</HTML>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
143 else echo $i." records have been successfully written to '".$DUMPFILE."' and removed from database.\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
144 } else { |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
145 if ($DISPLAY_HTML) echo "<p><em>No new record in the database</em></p>\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
146 else echo "No new record in the database, the generated file is empty.\n"; |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
147 } |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
148 pg_free_result($result); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
149 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
150 /* Closing connection */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
151 pg_close($dbconn); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
152 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
153 /* Closing the file */ |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
154 fclose($file); |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
155 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
156 |
4cdf146f11d5
Added a set of example PHP scripts to parse the app_acct.fdx data.
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
157 ?> |