summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/oracle/examples/oraclew.pp
blob: 2fcfd78a69b6e262b235079bc531bff763e5ebff (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
{
    Copyright (c) 1999-2000 by Pavel Stingl <stingp1.eti@mail.cez.cz>


    OCI workaround

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit oraclew;

interface

{$H+}
{$mode objfpc}

uses OCI, oratypes,Classes, SysUtils;

{ all pos parameters are indexed from 1..x! }

    procedure OraInit;
    procedure OraFin;
    procedure OraLogin(name, pass, server: string);
    procedure OraLogout;
    procedure OraSQLExec(sql: string);
    function OraGetFieldAsString(pos : integer) : string;
    function OraGetFieldAsInteger(pos : integer) : longint;
    function OraNext: boolean;
    function OraGetFieldCount: integer;
    function OraGetFieldName(pos : integer) : string;
    function OraGetFieldType(pos : integer) : longint;
    function IsFieldDate(Pos : integer): boolean;
    procedure OraError(errcode: integer; err: POCIError; msg : string);

const
    cDescribeBuf = 1024;
    cPCharBufLen = 4097;
    cPrefetchCnt = 100;

type

  PDescribeRec = ^TDescribeRec;
  TDescribeRec = record
    dbsize      : sb4;
    dbtype      : sb2;
    buf         : array [0..cDescribeBuf] of char;
    buflen      : sb4;
    dsize       : sb4;
    precision   : sb2;
    scale       : sb2;
    nullok      : sb2;

    // Define part
      valbuf    : array [0..cDescribeBuf] of char;
      flt_buf   : double;
      int_buf   : cardinal;
      int64_buf : int64;
      indp      : sb2;
      col_retlen: ub2;
      col_retcode: ub2;
  end;

var
    Env : POCIEnv;
    Err : POCIError;
    Svc : POCISvcCtx;
    Stmt: POCIStmt;
    FieldList : TList;

    ecode : integer;

implementation

  function DecodeDataType(dtype : longint): string;
  begin
    case dtype of
        SQLT_CHR : DecodeDataType := '(ORANET TYPE) character string';
        SQLT_NUM : DecodeDataType := '(ORANET TYPE) oracle numeric';
        SQLT_INT : DecodeDataType := '(ORANET TYPE) integer';
        SQLT_FLT : DecodeDataType := '(ORANET TYPE) Floating point number';
        SQLT_STR : DecodeDataType := 'zero terminated string';
        SQLT_VNU : DecodeDataType := 'NUM with preceding length byte';
        SQLT_PDN : DecodeDataType := '(ORANET TYPE) Packed Decimal Numeric';
        SQLT_LNG : DecodeDataType := 'long';
        SQLT_VCS : DecodeDataType := 'Variable character string';
        SQLT_NON : DecodeDataType := 'Null/empty PCC Descriptor entry';
        SQLT_RID : DecodeDataType := 'rowid';
        SQLT_DAT : DecodeDataType := 'date in oracle format';
        SQLT_VBI : DecodeDataType := 'binary in VCS format';
        SQLT_BIN : DecodeDataType := 'binary data(DTYBIN)';
        SQLT_LBI : DecodeDataType := 'long binary';
        SQLT_UIN : DecodeDataType := 'unsigned integer';
        SQLT_SLS : DecodeDataType := 'Display sign leading separate';
        SQLT_LVC : DecodeDataType := 'Longer longs (char)';
        SQLT_LVB : DecodeDataType := 'Longer long binary';
        SQLT_AFC : DecodeDataType := 'Ansi fixed char';
        SQLT_AVC : DecodeDataType := 'Ansi Var char';
        SQLT_CUR : DecodeDataType := 'cursor  type';
        SQLT_RDD : DecodeDataType := 'rowid descriptor';
        SQLT_LAB : DecodeDataType := 'label type';
        SQLT_OSL : DecodeDataType := 'oslabel type';
        SQLT_NTY : DecodeDataType := 'named object type';
        SQLT_REF : DecodeDataType := 'ref type';
        SQLT_CLOB : DecodeDataType := 'character lob';
        SQLT_BLOB : DecodeDataType := 'binary lob';
        SQLT_BFILEE : DecodeDataType := 'binary file lob';
        SQLT_CFILEE : DecodeDataType := 'character file lob';
        SQLT_RSET : DecodeDataType := 'result set type';
        SQLT_NCO : DecodeDataType := 'named collection type (varray or nested table)';
        SQLT_VST : DecodeDataType := 'OCIString type';
        SQLT_ODT : DecodeDataType := 'OCIDate type';
    else DecodeDataType := 'Unknown';
    end;
  end;

  procedure FieldListClear;
  var
        x: longint;
        PDesc: PDescribeRec;
  begin
        if FieldList.Count = 0 then Exit;
        for x := 0 to FieldList.Count - 1 do
        begin
            PDesc := FieldList[x];
            Dispose(PDesc);
        end;
        FieldList.Clear;
  end;

       procedure Describe;
       var
        fldc    : longint;
        paramd  : POCIParam;
        colname : PChar;
        colsize : ub4;
        Rec     : PDescribeRec;
       begin
        fldc := 1;

        FieldListClear;
        ecode := OCIParamGet(Stmt, OCI_HTYPE_STMT, Err, paramd, fldc);
        if ecode <> OCI_SUCCESS then
            ORAError(ecode, Err, 'OCIParamGetError');
        while ecode = OCI_SUCCESS do
        begin
            New(Rec);
            FillChar(Rec^.buf, sizeof(Rec^.buf), #0);
            ecode := OCIAttrGet(paramd, OCI_DTYPE_PARAM, @Rec^.dbtype, nil,
                OCI_ATTR_DATA_TYPE, Err);
            if ecode <> 0 then
            begin
                ORAError(ecode, Err, 'Retrieving DTYPE_PARAM:');
            end;
            colsize := 0;
            colname := nil;
            ecode := OCIAttrGet(paramd, OCI_DTYPE_PARAM, @colname, @colsize,
                OCI_ATTR_NAME, Err);
            if ecode <> 0 then
            begin
                ORAError(ecode, Err, 'Retrieving DTYPE_PARAM:');
            end;
            Move(Colname^,Rec^.buf, colsize);
            Rec^.buflen := colsize;
//          WriteLn('Column: ',Rec^.buf:15,'    DataType: ',DecodeDataType(Rec^.dbtype));
            inc(fldc);

            FieldList.Add(Rec);
            ecode := OCIParamGet(Stmt, OCI_HTYPE_STMT, Err, paramd, fldc);
        end;
    end;

    procedure Define;
    var
        x : longint;
        def: POCIDefine;
        PDesc : PDescribeRec;
        defptr: pointer;
        deflen: sword;
        deftyp: sword;
    begin
        def := nil;
        for x := 0 to FieldList.Count - 1 do
        begin
            PDesc := FieldList[x];
            case PDesc^.dbtype of
                SQLT_NUM: begin
                    if PDesc^.scale <> 0 then
                    begin
                        defptr := @PDesc^.flt_buf;
                        deflen := SizeOf(PDesc^.flt_buf);
                        deftyp := SQLT_FLT;
                        PDesc^.dbtype := SQLT_FLT;
                    end
                    else begin
              if PDesc^.dbsize > 4 then
              begin
                // WriteLn('BIG FAT WARNING!!!! dbsize int > 4 (',PDesc^.dbsize,')');
                defptr := @PDesc^.int64_buf;
                deflen := SizeOf(PDesc^.int64_buf);
                deftyp := SQLT_INT;
                PDesc^.dbtype := SQLT_INT;
              end
              else begin
                defptr := @PDesc^.int_buf;
                            deflen := SizeOf(PDesc^.int_buf);
                            deftyp := SQLT_INT;
                            PDesc^.dbtype := SQLT_INT;
              end;
                    end;
                end;
                else begin
                    defptr := @PDesc^.valbuf;
                    deflen := cDescribeBuf;
                    deftyp := PDesc^.dbtype;
                end;
            end;
            ecode := OCIDefineByPos(Stmt, def, Err, x + 1, defptr,
                deflen, deftyp, @PDesc^.indp, @PDesc^.col_retlen,
                @PDesc^.col_retcode, OCI_DEFAULT);
            if ecode <> 0 then
            begin
                OraError(ecode, Err, 'OCIDefineByPos: ');
            end;
        end;
    end;

    procedure OraError( errcode : integer; err: POCIError; msg : string );
    var
        buff : array [0..1024] of char;

    begin
        if err <> nil then
        begin
            case errcode of
                OCI_INVALID_HANDLE: Msg := Msg + ' OCI_INVALID_HANDLE';
            end;
            OCIErrorGet( err, 1, nil, errcode, @buff[0], 1024, OCI_HTYPE_ERROR);
            writeln(stderr, msg, ' ', buff);
        end
        else begin
            WriteLn(stderr, msg);
            Halt(1);
        end;
    end;

    procedure OraInit;
    begin
        ecode := OCIInitialize({OCI_DEFAULT or }OCI_OBJECT, nil, nil, nil, nil);
        if ecode <> 0 then OraError( ecode, nil, 'Error initializing OCI');
        ecode := OCIEnvInit(Env, OCI_DEFAULT, 0, nil);
        if ecode <> 0 then OraError( ecode, nil, 'Error initializing OCI environment');
        ecode := OCIHandleAlloc(Env, Err, OCI_HTYPE_ERROR, 0, nil);
        if ecode <> 0 then OraError( ecode, nil, 'Error allocating error handle');
        ecode := OCIHandleAlloc(Env, Stmt, OCI_HTYPE_STMT, 0, nil);
        if ecode <> 0 then OraError( ecode, nil, 'Error allocating statement handle');
    end;

    procedure OraLogin(name, pass, server: string);
    begin
        ecode := OCILogon(Env, Err, Svc, @name[1], Length(name),
            @pass[1], Length(pass), @server[1], Length(server));
        if ecode <> 0 then OraError(ecode, Err, '');
    end;

    procedure OraLogout;
    begin
        ecode := OCILogoff(Svc, Err);
        if ecode <> 0 then
            OraError(ecode, Err, 'OCILogoff: ');
    end;

    procedure OraFin;
    begin
        OCIHandleFree(Stmt, OCI_HTYPE_STMT);
        OCIHandleFree(Err, OCI_HTYPE_ERROR);
    end;

    procedure OraSQLExec(sql: string);
    var
        dtype: longint;
    begin
//    writeLn(Length(sql));
        ecode := OCIStmtPrepare(Stmt, Err, @sql[1], Length(sql),
            OCI_NTV_SYNTAX, OCI_DEFAULT);
        if ecode <> 0 then
        begin
            OraError(ecode, Err, 'OCIStmtPrepare:');
            Exit;
        end;

    dtype := cPrefetchCnt;
    ecode := OCIAttrSet(Stmt, OCI_HTYPE_STMT, @dtype,
      SizeOf(dtype), OCI_ATTR_PREFETCH_ROWS, Err);
        if ecode <> 0 then
        begin
            OraError(ecode, Err, 'ociattrset:');
            Exit;
        end;

        dtype := 0;
        ecode := OCIAttrGet(Stmt, OCI_HTYPE_STMT, @dtype, nil,
            OCI_ATTR_STMT_TYPE, Err);
        if ecode <> 0 then
        begin
            OraError(ecode, Err, 'ociattrget:');
            Exit;
        end;

        ecode := 0;
        if dtype = OCI_STMT_SELECT then
            ecode := OCIStmtExecute(Svc, Stmt, Err, 0, 0, nil, nil, OCI_DEFAULT)
        else ecode := OCIStmtExecute(Svc, Stmt, Err, 1, 0, nil, nil, OCI_DEFAULT);
        if ecode <> 0 then
        begin
            OraError(ecode, Err, 'OCIStmtExecute:');
            Exit;
        end;

        if dtype = OCI_STMT_SELECT then
        begin
            Describe;
            Define;
        end;
    end;

    function OraGetFieldCount : integer;
    begin
        OraGetFieldCount := FieldList.Count;
    end;

    function IsFieldDate(Pos : integer): boolean;
    var
      Desc : TDescribeRec;
    begin
      Result := False;
      if (Pos > FieldList.Count) or (Pos < 1) then
        Exit;
      Desc := TDescribeRec(FieldList[Pos-1]^);
      Result := (Desc.dbtype = SQLT_DAT);
    end;

    function OraGetFieldAsString(pos : integer) : string;
    var
        Desc : TDescribeRec;
    Date : array [0..6] of byte;
    begin
        if (Pos > FieldList.Count) or (Pos < 1) then
            Exit;
        Desc := TDescribeRec(FieldList[pos-1]^);
    if Desc.indp < 0 then
    begin
      OraGetFieldAsString := 'null';
      Exit;
    end;
        if Desc.dbtype = SQLT_STR then
        begin
            Desc.valbuf[Desc.col_retlen] := #0;
            OraGetFieldAsString := strpas(Desc.valbuf);
        end
        else if Desc.dbtype = SQLT_CHR then
        begin
            Desc.valbuf[Desc.col_retlen] := #0;
            OraGetFieldAsString := strpas(Desc.valbuf);
        end
        else if Desc.dbtype = SQLT_INT then
    begin
            OraGetFieldAsString := IntToStr(Desc.int_buf);
    end
        else if Desc.dbtype = SQLT_FLT then
            OraGetFieldAsString := FloatToStr(Desc.flt_buf)
        else if Desc.dbtype = SQLT_DAT then
    begin
        Move(Desc.valbuf,Date,SizeOf(Date));
            OraGetFieldAsString :=
          Format('%0.2d.%0.2d.%0.4d %0.2d:%0.2d:%0.2d',
          [Date[3],Date[2],(((Date[0]-100)*100)+(Date[1] - 100)),
           Date[4]-1,
           Date[5]-1,
           Date[6]-1]);
    end
        else if Desc.dbtype = SQLT_AFC then
        begin
            Desc.valbuf[Desc.col_retlen] := #0;
            OraGetFieldAsString := strpas(Desc.valbuf);
        end
        else OraGetFieldAsString := 'dbtype not implemented ' + IntToStr(Desc.dbtype);
    end;

    function OraGetFieldAsInteger(pos : integer) : longint;
    begin
        OraGetFieldAsInteger := 0;
    end;

    function OraNext: boolean;
    begin
        ecode := OCIStmtFetch(Stmt, Err, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
        if ecode = 0 then
            OraNext := true
        else if ecode = OCI_SUCCESS_WITH_INFO then
            OraNext := false
        else if ecode = OCI_NO_DATA then
            OraNext := false
        else begin
            OraNext := false;
            OraError(ecode, err, 'OCIStmtFetch:');
        end;
    end;

    function OraGetFieldType(pos : integer) : longint;
    begin
      if (Pos > FieldList.Count) or (pos < 1) then
        Exit;
      OraGetFieldType := TDescribeRec(FieldList[pos-1]^).dbtype;
    end;

    function OraGetFieldName(pos : integer) : string;
    begin
        if (Pos > FieldList.Count) or (Pos < 1) then
            Exit;
        OraGetFieldName := strpas(TDescribeRec(FieldList[pos-1]^).buf);
    end;

initialization

    FieldList := TList.Create;

finalization

    FieldListClear;
    FieldList.Free;

end.