testib60.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {
  2. }
  3. program testib;
  4. {$IFDEF Linux} // not for *BSD
  5. {$LINKLIB dl}
  6. {$ENDIF}
  7. uses Ibase60, strings;
  8. {$h-}
  9. Const
  10. { Change to YOUR database server }
  11. ServerDb : pchar = 'testdb.gdb';
  12. { CHange to YOUR username and password. These may be empty }
  13. username = '';
  14. PWD = '';
  15. { Don't edit after this }
  16. dbinfo : array [1..3] of byte
  17. = (isc_info_page_size,isc_info_num_buffers,isc_info_end);
  18. query : pchar = 'select * from FPDev;';
  19. flag : array[0..2] of shortint = (0,0,0);
  20. Type
  21. TStatusArray = Array[0..19] of ISC_Status;
  22. Var
  23. DB : Tisc_db_handle;
  24. TA : TISC_tr_handle;
  25. statement : TISC_stmt_handle;
  26. DPB : String;
  27. Status : TStatusArray;
  28. sqlda : PXSQLDA;
  29. name,email : String;
  30. i,id : longint;
  31. fs : longint;
  32. Function CheckIBstatus (Const Status : TStatusArray) : Boolean;
  33. begin
  34. CheckIBstatus:=Not ((Status[0]=1) and (status[1]<>0))
  35. end;
  36. Procedure DoError (Const status : TStatusArray);
  37. begin
  38. Writeln ('Failed:');
  39. isc_print_status(@status);
  40. halt(1);
  41. end;
  42. begin
  43. db:=Nil;
  44. dpb:=chr(isc_dpb_version1);
  45. If UserName<>'' then
  46. begin
  47. dpb:=dpb+chr(isc_dpb_user_name)+chr(length(UserName))+username;
  48. If pwd<>'' then
  49. dpb:=dpb+chr(isc_dpb_password)+chr(length(pwd))+pwd;
  50. end;
  51. Write ('Connecting to ',serverdb,': ');
  52. isc_attach_database(@Status[0],strlen(serverdb),serverdb,@db,length(dpb),@dpb[1]);
  53. if Not CheckIBStatus(Status) then
  54. DoError(status)
  55. else
  56. Writeln ('OK.');
  57. Write ('Starting Transaction : ');
  58. If ISC_start_transaction (@status[0],@ta,1,[@db,0,Nil])<>0 then
  59. DoError(Status)
  60. else
  61. Writeln ('OK.');
  62. getmem (sqlda,XSQLDA_Length(3));
  63. sqlda^.sqln:=3;
  64. sqlda^.sqld:=3;
  65. sqlda^.version:=1;
  66. Write('Allocating statement : ');
  67. If isc_dsql_allocate_statement(@status ,@db,@statement)<>0 then
  68. DoError(Status)
  69. else
  70. Writeln ('OK.');
  71. Write ('Preparing statement : ');
  72. if ISC_dsql_prepare(@status,@ta,@statement,0,query,1,sqlda)<>0 then
  73. DoError(Status)
  74. else
  75. Writeln ('OK.');
  76. I:=0;
  77. With sqlda^.sqlvar[i] do
  78. begin
  79. sqldata := @id;
  80. sqltype := sql_long;
  81. sqlind := @flag[0];
  82. end;
  83. inc(i);
  84. With sqlda^.sqlvar[i] do
  85. begin
  86. sqldata := @name[1];
  87. sqltype := sql_text;
  88. sqlind := @flag[1];
  89. end;
  90. inc(i);
  91. With sqlda^.sqlvar[i] do
  92. begin
  93. sqldata := @email[1];
  94. sqltype := sql_text;
  95. sqlind := @flag[2];
  96. end;
  97. Write ('Executing statement : ');
  98. if isc_dsql_execute(@status,@ta,@statement,1,Nil)<>0 then
  99. DoError(Status)
  100. else
  101. Writeln ('OK.');
  102. Writeln ('Fetching rows :');
  103. Repeat
  104. FS:=isc_dsql_fetch(@status,@statement,1,sqlda);
  105. If FS=0 then
  106. begin
  107. I:=255;
  108. While Name[I]=' ' do Dec(i);
  109. setlength(Name,i);
  110. I:=255;
  111. While Email[I]=' ' do Dec(i);
  112. setlength(email,i);
  113. Writeln ('(',ID,',',name,',',email,')');
  114. end;
  115. until FS<>0;
  116. If FS<>100 then
  117. DoError(status)
  118. else
  119. Writeln ('At end.');
  120. Write ('Freeing statement : ');
  121. if isc_dsql_free_statement(@status,@statement,DSQL_Close)<>0 then
  122. DoError(Status)
  123. else
  124. Writeln ('OK.');
  125. Write ('Committing transaction : ');
  126. If ISC_Commit_transaction(@status,@ta)<>0 then
  127. doerror(status)
  128. else
  129. Writeln ('OK.');
  130. Write ('Disconnecting from database: ');
  131. isc_detach_database(@status,@db);
  132. If CheckIBStatus (Status) Then
  133. Writeln ('OK.')
  134. else
  135. doerror(status);
  136. end.