(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
program ZeosPP;

uses
  ZConnection, ZDataSet;

const
  MAXREC = 100;

type
  t1 = record
         f1 : integer;
         f2 : string;
  end;
  array_of_t1 = array[1..MAXREC] of t1;

function t1_get_one(con : TZConnection; f2 : string) : integer;

var
   q : TZQuery;
   res : integer;

begin
  q := TZQuery.Create(nil);
  q.Connection := con;
  q.SQL.Text := 'SELECT f1 FROM t1 WHERE f2 = :f2';
  q.Params.ParamByName('f2').AsString := f2;
  q.Open;
  if not q.EOF then begin
    res := q.FieldByName('f1').AsInteger;
  end else begin
    writeln(f2, ' not found');
    halt;
  end;
  q.Close;
  q.Free;
  t1_get_one := res;
end;


function t1_get_all(con : TZConnection; var buf : array_of_t1) : integer;

var
   q : TZQuery;
   count : integer;

begin
  count := 0;
  q := TZQuery.Create(nil);
  q.Connection := con;
  q.SQL.Text := 'SELECT f1,f2 FROM t1';
  q.Open;
  while (not q.EOF) and (count < MAXREC) do begin
    count := count + 1;
    buf[count].f1 := q.FieldByName('f1').AsInteger;
    buf[count].f2 := q.FieldByName('f2').AsString;
    q.Next;
  end;
  q.Close;
  q.Free;
  t1_get_all := count;
end;

procedure t1_put(con : TZConnection; f1 : integer; f2 : string);

var
   q : TZQuery;

begin
  q := TZQuery.Create(nil);
  q.Connection := con;
  q.SQL.Text := 'INSERT INTO t1 VALUES(:f1,:f2)';
  q.Params.ParamByName('f1').AsInteger := f1;
  q.Params.ParamByName('f2').AsString := f2;
  q.ExecSQL;
  if q.RowsAffected <> 1 then begin
    writeln('INSERT did not insert 1 row');
    halt;
  end;
  q.Close;
  q.Free;
end;

procedure t1_remove(con : TZConnection; f1 : integer);

var
   q : TZQuery;

begin
  q := TZQuery.Create(nil);
  q.Connection := con;
  q.SQL.Text := 'DELETE FROM t1 WHERE f1 = :f1';
  q.Params.ParamByName('f1').AsInteger := f1;
  q.ExecSQL;
  if q.RowsAffected <> 1 then begin
    writeln('DELETE did not delete 1 row');
    halt;
  end;
  q.Close;
  q.Free;
end;

procedure t1_dump(con : TZConnection);

var
   buf : array_of_t1;
   count, i : integer;

begin
  count := t1_get_all(con, buf);
  for i := 1 to count do begin
      writeln(buf[i].f1,' ', buf[i].f2);
  end;
end;

procedure test(lbl : string; con : TZConnection);

var
   f1 : integer;

begin
  writeln(lbl, ':');
  con.Connect;
  f1 := t1_get_one(con, 'BB');
  writeln(f1);
  t1_dump(con);
  t1_put(con, 999, 'XXX');
  t1_dump(con);
  t1_remove(con, 999);
  t1_dump(con);
  con.Disconnect;
  con.Free;
end;

procedure test_ado(lbl, constr : string);

var
  con : TZConnection;

begin
  con := TZConnection.Create(nil);
  con.Protocol := 'ado';
  con.Database := constr;
  test(lbl, con);
end;

var
  con : TZConnection;

begin
  (* MySQL *)
  con := TZConnection.Create(nil);
  con.Protocol := 'mysql';
  con.HostName := 'localhost';
  con.User := 'root';
  con.Password := '';
  con.Database := 'Test';
  test('MySQL', con);
  (* PostgreSQL *)
  con := TZConnection.Create(nil);
  con.Protocol := 'postgresql';
  con.HostName := 'localhost';
  con.User := 'postgres';
  con.Password := 'xxxxxx';
  con.Database := 'Test';
  test('PostgreSQL', con);
  (* ADO *)
  test_ado('ADO - MS SQLServer', 'Provider=SQLNCLI11;Server=ARNEPC4;Database=Test;Trusted_Connection=yes;');
  test_ado('ADO - DB2', 'Provider=IBMDADB2;Protocol=TCPIP;Hostname=localhost;Database=Test;Port=50000;Uid=arne;Pwd=xxxxxx;');
end.

