summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/zorba/examples/simple/simple.lpr
blob: e9ab34e57657332f91b2ab1218a0e235b89f31bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
program simple;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,
  zorba,
  ctypes;

{$IFDEF WINDOWS}{$R simple.rc}{$ENDIF}

procedure stream_write(stream: XQC_OutputStream; const buf: pchar; length: cuint); cdecl;
var
  S: String;
begin
  SetLength(S, length);
  Move(buf^, S[1], length);
  WriteLn(S);
end;

procedure stream_free(stream: XQC_OutputStream); cdecl;
begin
end;

const
  streamdef: XQC_OutputStream_s = (
    write:@stream_write;
    free:@stream_free;
    data:nil
  );

var
  stream: XQC_OutputStream = @streamdef;




(**
 * A simple C API example
 * Compile a query and print the result.  No error checking is done.
 *)
function example_1(impl: XQC_Implementation): boolean;
var
  lXQuery: XQC_Query;
begin
  // compile the query
  impl^.prepare(impl, '(1+2, 3, 4)', nil, nil, lXQuery);

  // execute it and print the result on standard out
  lXQuery^.serialize_stream(lXQuery, nil, stream);

  // release the query
  lXQuery^.free(lXQuery);

  Result := True;
end;



(**
 * A simple C API example
 * Compile a query, iterate over the item sequence, and print the string value for each item.
 * No error checking is done.
 *)
function example_2(impl: XQC_Implementation): boolean;
var
  lXQuery      : XQC_Query;
  lItem        : XQC_Item;
  lResult      : XQC_Sequence;
  lStringValue : pchar;
begin
  impl^.create_item(impl, lItem);

  // compile the query and get the result as a sequence
  impl^.prepare(impl, 'for $i in 1 to 10 return $i', nil, nil, lXQuery);

  lXQuery^.sequence(lXQuery, lResult);

  while lResult^.next(lResult, lItem) = XQ_NO_ERROR do
  begin
    lItem^.string_value(lItem, &lStringValue);
    write(lStringValue, ' ');
  end;
  writeln;

  // release all aquired resources
  lItem^.free(lItem);
  lResult^.free(lResult);
  lXQuery^.free(lXQuery);

  Result := True;
end;



var
  impl: XQC_Implementation;
  store: Pointer;
begin
  store := create_simple_store();
  if zorba_implementation(impl, store) <> XQ_NO_ERROR then
    Exit;

  writeln('executing PASCAL example 1');
  if not example_1(impl) then
    Exit;
  writeln;

  writeln('executing PASCAL example 2');
  if not example_2(impl) then
    Exit;
  writeln;

  impl^.free(impl);
  shutdown_simple_store(store);
end.