<HTML><HEAD><TITLE>MSP example: database query</TITLE></HEAD>
<BODY BGCOLOR="#fbf2e7">

<H2>MSP example: database queries</H2>

This page was generated at <?MSP= Date.toString
(Date.fromTimeLocal(Time.now())) ?> to illustrate some database
facilities with ML Server Pages.  The script behind the page was last
updated 2000-01-06.

<P>The time to generate, compile, and link the script that generated
this page is approximately 0.2 sec (user+sys time as well as real
time).  Most of this time is spent compiling and linking the 84 lines
of SML code generated from the script (which is 80 lines).  The size
of the compiled and linked script is 12 KB.  The script needs to be
regenerated, recompiled, and relinked only after a change to its
source.

<P>The time to execute the compiled script (for generating the 19 KB
of HTML code making up this page) is approximately 0.1 sec user+sys
time in the script and another 0.25 sec in the database server, for a
total real time delay of 0.35 sec, including time for communication
between script and database server.

<P>All times are for a Dell Latitude PII 266 MHz notebook with 128 MB
RAM running SuSE Linux 6.1, Postgres 6.3, and Moscow ML 1.44.

<?MSP (* Open database connection *)

val db = Postgres.openbase
	 { dbhost    = NONE,		(* database server host *)
	   dbname    = SOME "messages",	(* database name        *)
	   dboptions = NONE,		(* options              *)
	   dbport    = NONE,		(* database server port *)
	   dbpwd     = NONE,		(* user passwd          *)
	   dbtty     = NONE,		(* tty for error log    *)
	   dbuser    = SOME "nobody"	(* database user        *)
	 };
?>

<H2>Example database query</H2>

We open a database connection db and execute the following SML code

<PRE>
    Postgres.showquery db 
    "SELECT * FROM message WHERE name = 'Peter Sestoft' ORDER BY day" 
</PRE>

<P>The result is this table, automatically generated by
Postgres.showquery:

<P>
<?MSP$ Postgres.showquery db 
    "SELECT * FROM message WHERE name = 'Peter Sestoft' ORDER BY day" 
?>

<H2>Another query</H2>

We execute the following SML code

<PRE>
    Postgres.showquery db "SELECT name, COUNT(msg) FROM message GROUP BY name" 
</PRE>

<P>The result is:

<P>
<?MSP$ Postgres.showquery db "SELECT name, COUNT(msg) FROM message GROUP BY name" 
?>

<?MSP (* Should always close the database after use: *)
    (* But the combination of SuSE 6.1 and PostgreSQL 6.3 causes a 
       segmentation fault, so for now ... *)
    (* val _ = Postgres.closebase db *)
?>

</BODY>
</HTML>
