simple.lpr 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. program simple;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. Classes,
  8. zorba,
  9. ctypes;
  10. {$IFDEF WINDOWS}{$R simple.rc}{$ENDIF}
  11. procedure stream_write(stream: XQC_OutputStream; const buf: pchar; length: cuint); cdecl;
  12. var
  13. S: String;
  14. begin
  15. SetLength(S, length);
  16. Move(buf^, S[1], length);
  17. WriteLn(S);
  18. end;
  19. procedure stream_free(stream: XQC_OutputStream); cdecl;
  20. begin
  21. end;
  22. const
  23. streamdef: XQC_OutputStream_s = (
  24. write:@stream_write;
  25. free:@stream_free;
  26. data:nil
  27. );
  28. var
  29. stream: XQC_OutputStream = @streamdef;
  30. (**
  31. * A simple C API example
  32. * Compile a query and print the result. No error checking is done.
  33. *)
  34. function example_1(impl: XQC_Implementation): boolean;
  35. var
  36. lXQuery: XQC_Query;
  37. begin
  38. // compile the query
  39. impl^.prepare(impl, '(1+2, 3, 4)', nil, nil, lXQuery);
  40. // execute it and print the result on standard out
  41. lXQuery^.serialize_stream(lXQuery, nil, stream);
  42. // release the query
  43. lXQuery^.free(lXQuery);
  44. Result := True;
  45. end;
  46. (**
  47. * A simple C API example
  48. * Compile a query, iterate over the item sequence, and print the string value for each item.
  49. * No error checking is done.
  50. *)
  51. function example_2(impl: XQC_Implementation): boolean;
  52. var
  53. lXQuery : XQC_Query;
  54. lItem : XQC_Item;
  55. lResult : XQC_Sequence;
  56. lStringValue : pchar;
  57. begin
  58. impl^.create_item(impl, lItem);
  59. // compile the query and get the result as a sequence
  60. impl^.prepare(impl, 'for $i in 1 to 10 return $i', nil, nil, lXQuery);
  61. lXQuery^.sequence(lXQuery, lResult);
  62. while lResult^.next(lResult, lItem) = XQ_NO_ERROR do
  63. begin
  64. lItem^.string_value(lItem, &lStringValue);
  65. write(lStringValue, ' ');
  66. end;
  67. writeln;
  68. // release all aquired resources
  69. lItem^.free(lItem);
  70. lResult^.free(lResult);
  71. lXQuery^.free(lXQuery);
  72. Result := True;
  73. end;
  74. var
  75. impl: XQC_Implementation;
  76. store: Pointer;
  77. begin
  78. store := create_simple_store();
  79. if zorba_implementation(impl, store) <> XQ_NO_ERROR then
  80. Exit;
  81. writeln('executing PASCAL example 1');
  82. if not example_1(impl) then
  83. Exit;
  84. writeln;
  85. writeln('executing PASCAL example 2');
  86. if not example_2(impl) then
  87. Exit;
  88. writeln;
  89. impl^.free(impl);
  90. shutdown_simple_store(store);
  91. end.