ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/examples/consolemode/project1.lpr
Revision: 33
Committed: Sat Jul 18 12:30:52 2015 UTC (9 years, 4 months ago) by tony
File size: 4081 byte(s)
Log Message:
Committing updates for Release R1-3-1

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 ibexpressconsolemode, 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 DEPT_NO, DEPARTMENT, HEAD_DEPT, Depts.DEPT_PATH || '' / '' || DEPARTMENT as DEPT_PATH,'+
39 'Depts.DEPT_KEY_PATH || '';'' || DEPT_NO as DEPT_KEY_PATH '+
40 'From DEPARTMENT '+
41 'JOIN Depts On 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 Database := FIBDatabase;
74 SQL.Text := sqlExample;
75 Active := true;
76 rowno := 1;
77 while not EOF do
78 begin
79 writeln('Record No. ',rowno);
80 Inc(rowno);
81 writeln;
82 for i := 0 to FieldCount - 1 do
83 begin
84 writeln(Fields[i].FieldName + ': ',Fields[i].AsString);
85 end;
86 writeln;
87 next;
88 end;
89 finally
90 Free;
91 end;
92 end;
93
94 procedure TMyApplication.DoRun;
95 var
96 ErrorMsg: String;
97 begin
98 // quick check parameters
99 ErrorMsg:=CheckOptions('h','help');
100 if ErrorMsg<>'' then begin
101 ShowException(Exception.Create(ErrorMsg));
102 Terminate;
103 Exit;
104 end;
105
106 // parse parameters
107 if HasOption('h','help') then begin
108 WriteHelp;
109 Terminate;
110 Exit;
111 end;
112
113 { In console Mode the application should own the database
114 - ensures centralised exception handling }
115 FIBDatabase := TIBDatabase.Create(self);
116 FIBTransaction := TIBTransaction.Create(self);
117 FIBDatabase.DatabaseName := 'localhost:employee';
118 FIBDatabase.Params.Add('user_name=SYSDBA'); {You may have to modify this!}
119 FIBDatabase.Params.Add('password=masterkey'); {You may have to modify this!}
120 FIBDatabase.Params.Add('lc_ctype=UTF8');
121 FIBTransaction.DefaultDatabase := FIBDatabase;
122 DoQuery;
123
124 // stop program loop
125 Terminate;
126 end;
127
128 constructor TMyApplication.Create(TheOwner: TComponent);
129 begin
130 inherited Create(TheOwner);
131 StopOnException:=True;
132 end;
133
134 destructor TMyApplication.Destroy;
135 begin
136 inherited Destroy;
137 end;
138
139 procedure TMyApplication.WriteHelp;
140 begin
141 { add your help code here }
142 writeln('Usage: ',ExeName,' -h');
143 end;
144
145 var
146 Application: TMyApplication;
147 begin
148 Application:=TMyApplication.Create(nil);
149 Application.Title:='IBX In Console Mode';
150 Application.Run;
151 {$IFDEF WINDOWS}
152 Sleep(1000); {Gives a chance to see the program output}
153 {$ENDIF}
154 Application.Free;
155 end.
156