ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/examples/consolemode/project1.lpr
Revision: 45
Committed: Tue Dec 6 10:33:46 2016 UTC (7 years, 3 months ago) by tony
File size: 4101 byte(s)
Log Message:
Committing updates for Release R2-0-0

File Contents

# Content
1 program project1;
2
3 {$mode objfpc}{$H+}
4
5 { CONSOLE MODE EXAMPLE
6
7 This is a simple use of IBX to access the employee database in console mode.
8 The program opens the database, runs a query and writes the result to stdout.
9
10 Key points to note:
11
12 1. In console mode, you have to create the Database and Transaction objects
13 explicitly and link them to each other.
14
15 2. The Database properties have to be set explicitly. This includes username
16 and password. In the example, these are set from literals. You could update
17 this to (e.g.) parse the command line arguments from "ParamStr".
18 However, this is left as an exercise for the implementor.
19
20 3. It's a good idea to have the application own the IBDatabase. This ensures that
21 exceptions are routed through your application object's exception handler.
22 }
23
24 uses
25 {$IFDEF UNIX}{$IFDEF UseCThreads}
26 cthreads,
27 {$ENDIF}{$ENDIF}
28 Classes, SysUtils, CustApp, IBDatabase, IBQuery
29 { you can add units after this };
30
31 const
32 sqlExample =
33 'with recursive Depts As ( '+
34 'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+
35 'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+
36 'From DEPARTMENT Where HEAD_DEPT is NULL '+
37 'UNION ALL '+
38 'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+
39 'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+
40 'From DEPARTMENT D '+
41 'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+
42 ')'+
43
44 'Select A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+
45 'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+
46 'From EMPLOYEE A '+
47 'JOIN Depts D On D.DEPT_NO = A.DEPT_NO';
48
49 type
50
51 { TMyApplication }
52
53 TMyApplication = class(TCustomApplication)
54 private
55 FIBDatabase: TIBDatabase;
56 FIBTransaction: TIBTransaction;
57 procedure DoQuery;
58 protected
59 procedure DoRun; override;
60 public
61 constructor Create(TheOwner: TComponent); override;
62 destructor Destroy; override;
63 procedure WriteHelp; virtual;
64 end;
65
66 { TMyApplication }
67
68 procedure TMyApplication.DoQuery;
69 var i, rowno: integer;
70 begin
71 with TIBQuery.Create(self) do
72 try
73 AllowAutoActivateTransaction := true;
74 Database := FIBDatabase;
75 SQL.Text := sqlExample;
76 Active := true;
77 rowno := 1;
78 while not EOF do
79 begin
80 writeln('Record No. ',rowno);
81 Inc(rowno);
82 writeln;
83 for i := 0 to FieldCount - 1 do
84 begin
85 writeln(Fields[i].FieldName + ': ',Fields[i].AsString);
86 end;
87 writeln;
88 next;
89 end;
90 finally
91 Free;
92 end;
93 end;
94
95 procedure TMyApplication.DoRun;
96 var
97 ErrorMsg: String;
98 begin
99 // quick check parameters
100 ErrorMsg:=CheckOptions('h','help');
101 if ErrorMsg<>'' then begin
102 ShowException(Exception.Create(ErrorMsg));
103 Terminate;
104 Exit;
105 end;
106
107 // parse parameters
108 if HasOption('h','help') then begin
109 WriteHelp;
110 Terminate;
111 Exit;
112 end;
113
114 { In console Mode the application should own the database
115 - ensures centralised exception handling }
116 FIBDatabase := TIBDatabase.Create(self);
117 FIBTransaction := TIBTransaction.Create(self);
118 FIBDatabase.DatabaseName := 'employee';
119 FIBDatabase.Params.Add('user_name=SYSDBA'); {You may have to modify this!}
120 FIBDatabase.Params.Add('password=masterkey'); {You may have to modify this!}
121 FIBDatabase.Params.Add('lc_ctype=UTF8');
122 FIBTransaction.DefaultDatabase := FIBDatabase;
123 DoQuery;
124
125 // stop program loop
126 Terminate;
127 end;
128
129 constructor TMyApplication.Create(TheOwner: TComponent);
130 begin
131 inherited Create(TheOwner);
132 StopOnException:=True;
133 end;
134
135 destructor TMyApplication.Destroy;
136 begin
137 inherited Destroy;
138 end;
139
140 procedure TMyApplication.WriteHelp;
141 begin
142 { add your help code here }
143 writeln('Usage: ',ExeName,' -h');
144 end;
145
146 var
147 Application: TMyApplication;
148 begin
149 Application:=TMyApplication.Create(nil);
150 Application.Title:='IBX In Console Mode';
151 Application.Run;
152 {$IFDEF WINDOWS}
153 Readln; {Gives a chance to see the program output}
154 {$ENDIF}
155 Application.Free;
156 end.
157