ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/examples/consolemode/project1.lpr
Revision: 143
Committed: Fri Feb 23 12:11:21 2018 UTC (6 years, 1 month ago) by tony
File size: 4961 byte(s)
Log Message:
Fixes Merged

File Contents

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