next up previous contents
Next: Addresses - SQL Up: Addresses Previous: Addresses

Addresses - EmbeddedSQL

 


MODULE Addresses;

IMPORT ESQL := EmbeddedSQL, Files, Out, In;

TYPE
  Address = RECORD
    firstName, lastName, street: ARRAY 32 OF CHAR;
    town: ARRAY 20 OF CHAR;
    zipCode: INTEGER
  END ;

VAR
  conn: ESQL.Connection;
  connStr: ARRAY 256 OF CHAR;
  stat: ESQL.Statement;

PROCEDURE Open*;
BEGIN
  conn := ESQL.OpenUI(connStr, connStr)
END Open;

PROCEDURE Execute*;
  VAR sqlString: ARRAY 256 OF CHAR;
BEGIN
  In.Open; In.String(sqlString);
  IF In.Done THEN 
    stat := conn.Prepare(sqlString);
    stat.Execute()
  END
END Execute;

PROCEDURE ListAll*;
  VAR adr: Address;
BEGIN
  stat := conn.Prepare("SELECT * FROM Addresses INTO :adr");
  stat.Execute();
  WHILE stat.Fetch() DO
    Out.String(adr.firstName); Out.Char(" "); Out.String(adr.lastName);
    Out.String(", ");
    IF ~stat.IsNull("adr.street") THEN Out.String(adr.street) END ;
    Out.String(", ");
    Out.Int(adr.zipCode, 0); Out.Char(" "); Out.String(adr.town); Out.Ln
  END
END ListAll;

PROCEDURE ListByName*; (** ^ | "lastName" *)
  VAR adr: Address; name: ARRAY 32 OF CHAR;
BEGIN
  In.Open; In.String(name);
  IF In.Done THEN
    stat := conn.Prepare("SELECT * FROM Addresses WHERE lastName = :name INTO :adr");
    stat.Execute();
    WHILE stat.Fetch() DO
      Out.String(adr.firstName); Out.Char(" "); Out.String(adr.lastName);
      Out.String(", ");
      IF ~stat.IsNull("adr.street") THEN Out.String(adr.street) END ;
      Out.String(", ");
      Out.Int(adr.zipCode, 0); Out.Char(" "); Out.String(adr.town); Out.Ln
    END
  END
END ListByName;

PROCEDURE Insert*; (** ^ | "firstName" "lastName" "street" "town" zipCode *)
  VAR adr: Address;
BEGIN
  In.Open;
  stat := conn.Prepare("INSERT INTO Addresses values (:adr)");
  REPEAT
    In.String(adr.firstName); In.String(adr.lastName); In.String(adr.street);
    In.String(adr.town); In.Int(adr.zipCode);
    IF adr.street = "NULL" THEN stat.SetNull("adr.street") END ;
    IF In.Done THEN stat.Execute() END
  UNTIL ~In.Done
END Insert;

BEGIN
  connStr := ""
END Addresses.



Christoph Steindl
Thu Jul 24 14:37:19 MET DST 1997