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

%include "sqlr$pascal:sqlrdef.pas"

const
   MAX_REC = 100;

type
   pstr = varying[32000] of char;
   portnumber = integer16;
   sqlrcon = integer64;
   sqlrcurs = integer64;
   t1 = record
           f1 : integer;
           f2 : pstr;
        end;

(* error handling functions *)
procedure con_exit(con : sqlrcon);

var
   msg : pstr;

begin
   sqlr$con_errormessage(con, msg.body, msg.length);
   writeln('SQLR connection error: ' + msg);
   halt;
end;

procedure curs_exit(curs : sqlrcurs);

var
   msg : pstr;

begin
   sqlr$cur_errormessage(curs, msg.body, msg.length);
   writeln('SQLR cursor error: ' + msg);
   halt;
end;

procedure other_exit(msg : pstr);

begin
   writeln(msg);
   halt;
end;

(* get f1 given f2 *)
function t1_get_one(con : sqlrcon;
                    f2 : pstr;
                    function ph(num, nam, typ : pstr) : pstr;
                    function bn(num, nam : pstr) : pstr) : integer;

var
   curs : sqlrcurs;
   temp : integer64;
   stat, res, f1 : integer;
   s : pstr;

begin
   (* get cursor *)
   sqlr$cur_alloc(con, curs);
   (* prepare *)
   sqlr$cur_preparequery(curs, 'SELECT f1 FROM t1 WHERE f2 = ' + ph('1', 'f2', 'text'));
   (* bind parameters *)
   sqlr$cur_inputbindstring(curs, bn('1', 'f2'), substr(f2.body, 1, f2.length));
   (* execute *)
   stat := sqlr$cur_executequery(curs, res);
   if (stat mod 2) = 0 then curs_exit(curs);
   (* process result *)
   stat := sqlr$cur_getfieldbyindex(curs, 0, 0, s.body, s.length);
   if (stat mod 2) = 0 then begin
      other_exit('Row not found');
   end;
   sqlr$cur_getfieldasintbyind(curs, 0, 0, temp);
   f1 := temp;
   sqlr$con_endsession(con);
   (* close cursor *)
   sqlr$cur_free(curs);
   t1_get_one := f1;
end;

(* get all records *)
function t1_get_all(con : sqlrcon;
                    var buf : array[$L1..$U1:integer] of t1;
                    bufsiz : integer) : integer;

label
   done;

var
   curs : sqlrcurs;
   stat, count : integer;
   row : unsigned64;
   temp : integer64;
   s : pstr;

begin
   (* get cursor *)
   sqlr$cur_alloc(con, curs);
   (* execute *)
   stat := sqlr$cur_sendquery(curs, 'select f1,f2 from t1');
   if (stat mod 2) = 0 then curs_exit(curs);
   (* process result *)
   sqlr$cur_setresultsetbuffersize(curs, 100);
   row := 0;
   count := 0;
   while true do begin
      if count >= bufsiz then goto done;
      stat := sqlr$cur_getfieldbyindex(curs, row, 0, s.body, s.length);
      if (stat mod 2) = 0 then goto done;
      count := count + 1;
      sqlr$cur_getfieldasintbyind(curs, row, 0, temp);
      buf[count].f1 := temp;
      sqlr$cur_getfieldbyindex(curs, row, 1, buf[count].f2.body, buf[count].f2.length);
      row := row + 1;
   end;
done:
   sqlr$con_endsession(con);
   (* close cursor *)
   sqlr$cur_free(curs);
   t1_get_all := count;
end;

(* add record *)
procedure t1_put(con : sqlrcon;
                 f1 : integer;
                 f2 : pstr; function ph(num, nam, typ : pstr) : pstr;
                 function bn(num, nam : pstr) : pstr);

var
   curs : sqlrcurs;
   stat, res : integer;
   temp : integer64;
   n : unsigned64;

begin
   (* get cursor *)
   sqlr$cur_alloc(con, curs);
   (* prepare *)
   sqlr$cur_preparequery(curs, 'INSERT INTO t1 VALUES(' + ph('1', 'f1', 'int') + ', ' + ph('2', 'f2', 'text') + ')');
   (* bind parameters *)
   temp := f1;
   sqlr$cur_inputbindlong(curs, bn('1', 'f1'), temp);
   sqlr$cur_inputbindstring(curs, bn('2', 'f2'), substr(f2.body, 1, f2.length));
   (* execute *)
   stat := sqlr$cur_executequery(curs, res);
   if (stat mod 2) = 0 then curs_exit(curs);
   sqlr$cur_affectedrows(curs, n);
   if n <> 1 then begin
      other_exit('INSERT did not insert 1 row');
   end;
   sqlr$con_endsession(con);
   (* close cursor *)
   sqlr$cur_free(curs);
end;

(* delete record *)
procedure t1_remove(con : sqlrcon;
                    f1 : integer;
                    function ph(num, nam, typ : pstr) : pstr;
                    function bn(num, nam : pstr) : pstr);

var
   curs : sqlrcurs;
   stat, res : integer;
   temp : integer64;
   n : unsigned64;

begin
   (* get cursor *)
   sqlr$cur_alloc(con, curs);
   (* prepare *)
   sqlr$cur_preparequery(curs, 'DELETE FROM t1 WHERE f1 = ' + ph('1', 'f1', 'int'));
   (* bind parameters *)
   temp := f1;
   sqlr$cur_inputbindlong(curs, bn('1', 'f1'), temp);
   (* execute *)
   stat := sqlr$cur_executequery(curs, res);
   if (stat mod 2) = 0 then curs_exit(curs);
   sqlr$cur_affectedrows(curs, n);
   if n <> 1 then begin
      other_exit('DELETE did not delete 1 row');
   end;
   sqlr$con_endsession(con);
   (* close cursor *)
   sqlr$cur_free(curs);
end;

(* print all records *)
procedure t1_dump(con : sqlrcon);

var
   buf : array [1..MAX_REC] of t1;
   i, n : integer;

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

procedure test(host : packed array[$L1..$U1:integer] of char;
               port : portnumber;
               un : packed array[$L2..$U2:integer] of char;
               pw : packed array[$L3..$U3:integer] of char;
               function ph(num, nam, typ : pstr) : pstr;
               function bn(num, nam : pstr) : pstr);

var
   con : sqlrcon;
   socket : packed array[1..1] of char value '?';
   f1 : integer;

begin
   writeln('Connect host=', host, ' port=', port:1); 
   (* open *)
   sqlr$con_alloc(host, port, socket, un, pw, 0, 1, con);
   (* test t1_get_one *)   
   f1 := t1_get_one(con, 'BB', ph, bn);
   writeln(f1);
   (* test t1_get_all *)   
   t1_dump(con);   
   (* test t1_put *)
   t1_put(con, 999, 'XXX', ph, bn);
   t1_dump(con);
   (* test t1_remove *)
   t1_remove(con, 999, ph, bn);
   t1_dump(con);
   (* close *)
   sqlr$con_free(con);
end;

(* standard (ODBC, DB2, MySQL) *)
function std_ph(num, nam, typ : pstr) : pstr;

begin
   std_ph := '?';
end;

function std_bn(num, nam : pstr) : pstr;

begin
   std_bn := num;
end;

(* PostgreSQL *)
function pgsql_ph(num, nam, typ : pstr) : pstr;

begin
   pgsql_ph := '$' + num + '::' + typ;
end;

function pgsql_bn(num, nam : pstr) : pstr;

begin
   pgsql_bn := num;
end;

(* Oracle *)
function ora_ph(num, nam, typ : pstr) : pstr;

begin
   ora_ph := ':' + nam;
end;

function ora_bn(num, nam : pstr) : pstr;

begin
   ora_bn := nam;
end;

begin
   test('arnepc4', 9001, 'arne', 'topsecret', std_ph, std_bn); (* 9001 = localmysql *)
   test('arnepc4', 9002, 'arne', 'topsecret', pgsql_ph, pgsql_bn); (* 9002 = localpgsql *)
   test('arnepc4', 9003, 'arne', 'topsecret', std_ph, std_bn); (* 9003 = localdb2 *)
   test('arnepc4', 9004, 'arne', 'topsecret', ora_ph, ora_bn); (* 9004 = localora *)
   test('arnepc4', 9005, 'arne', 'topsecret', std_ph, std_bn); (* 9005 = odbcsqlsrv *)
   test('arnepc4', 9006, 'arne', 'topsecret', std_ph, std_bn); (* 9006 = odbcmysql *)
   test('arnepc4', 9007, 'arne', 'topsecret', std_ph, std_bn); (* 9007 = odbcpgsql *)
   test('arnepc4', 9008, 'arne', 'topsecret', std_ph, std_bn); (* 9008 = odbcdb2 *)
   test('arnepc4', 9010, 'arne', 'topsecret', std_ph, std_bn); (* 9010 = vmsmysql *)
   test('arnepc4', 9011, 'arne', 'topsecret', std_ph, std_bn); (* 9011 = vmsodbcrdb *)
   test('arnepc4', 9013, 'arne', 'topsecret', std_ph, std_bn); (* 9013 = vmsodbcmimer *)
end.
