-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleApp.dpr
110 lines (95 loc) · 2.72 KB
/
SampleApp.dpr
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
program SampleApp;
// DEBUG only: Step through code to see it in action there is now output to the screen
{$APPTYPE CONSOLE}
{$R *.res}
uses
kdb,
System.DateUtils,
System.SysUtils;
const
_Host = '127.0.0.1';
_Port = 1234;
type
// We are going to add more methods to the basic structure
Tk0_Helper = record helper for Tk0
public
procedure NoError;
procedure Release;
end;
procedure Tk0_Helper.NoError;
begin
if @Self = nil then
raise Excpt.Create('NoError: Self is nil');
if t = ktError then
raise Excpt.CreateFmt('Error %s',[s]);
end;
procedure Tk0_Helper.Release;
begin
r0(@Self);
end;
var
r,v: pK;
Session: Integer;
function k_(const Command: RawByteString): pK; overload; {$IFNDEF DEBUG} inline; {$ENDIF}
begin
Result := k(Session,pAnsiChar(Command),nil);
end;
function k_(const Command: RawByteString; Param1: pK): pK; overload; {$IFNDEF DEBUG} inline; {$ENDIF}
begin
Result := k(Session,pAnsiChar(Command),Param1,nil);
end;
begin
try
Session := khp(_Host,_Port);
if(Session <=0) then
raise Tk0.Excpt.CreateFmt('Connection error port host<%s> port <%d> make sure q.com has been run locally with "q -p 1234" command line',[_Host,_Port]);
// First store 2 + 2 in x
r := k_('x:2+2');
if r^.t <> ktUnaryPrimitive then
raise Tk0.Excpt.CreateFmt('Wrong Type %d',[Ord(r^.t)]);
r^.Release;
// get value of x
r := k_('x');
if r^.t <> ktLong then
raise Tk0.Excpt.CreateFmt('Wrong Type %d',[Ord(r^.t)]);
if r^.i <> 4 then
raise Tk0.Excpt.CreateFmt('Wrong result 2+2 = %d',[r^.i]);
r^.Release;
// k parmaters
// use k types (symbol)
r := k_('.[`x;();:;]',ks('hello'));
if r^.t <> ktSymbol then
raise Tk0.Excpt.CreateFmt('Wrong Type %d',[Ord(r^.t)]);
if r^.s <> 'x' then
raise Tk0.Excpt.CreateFmt('Wrong Result %s',[r^.s]);
r^.Release;
// get x
r := k_('x');
if r^.t <> ktSymbol then
raise Tk0.Excpt.CreateFmt('Wrong Type %d',[Ord(r^.t)]);
if r^.s <> 'hello' then
raise Tk0.Excpt.CreateFmt('Wrong result 2+2 = %d',[r^.i]);
r^.Release;
// Date sample
// Create kDate Type
v := ktj(ktMonth,MonthsBetween(StrToDate('03/10/2017'),tk0._DateBase));
v^.NoError;
// Store Date in x
r := k_('.[`x;();:;]',v);
// v was released by the k function
r^.NoError;
r^.Release;
// Get x value
r := k_('x');
if r^.t <> ktMonth then
raise Tk0.Excpt.CreateFmt('Wrong Type %d',[Ord(r^.t)]);
if r^.i <> 206 then
raise Tk0.Excpt.CreateFmt('Month result Type %d',[Ord(r^.i)]);
r^.Release;
kclose(Session);
{ TODO -oUser -cConsole Main : Insert code here }
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.