-
Notifications
You must be signed in to change notification settings - Fork 10
/
janSQLcli.lpr
100 lines (86 loc) · 2.56 KB
/
janSQLcli.lpr
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
program janSQLcli;
{ /* **************************************************************************
# janSQLcli is a console app to access .txt files data using janSQL library.
janSQL library is required to compile this program.
************************************************************************** */ }
{$MODE OBJFPC}{$H+}{$J-}
uses
SysUtils, janSQL;
var
sampleDB: TjanSQL;
quit: boolean = false;
sqlResult: integer = 0;
sqlCommand: string = '';
procedure printResult(resultIndex: integer);
var
s: string;
i,j: integer;
row,col: integer;
l: array of integer;
begin
col := sampleDB.RecordSets[resultIndex].FieldCount;
row := sampleDB.RecordSets[resultIndex].RecordCount;
SetLength(l, col);
// calculate column width
for i := 0 to col-1 do
begin
// get the longest text
l[i] := 0;
for j := 0 to row-1 do
begin
s := sampleDB.RecordSets[resultIndex].Records[j].Fields[i].Value;
if Length(s) > l[i] then l[i] := Length(s);
if Length(s) > 99 then l[i] := 100;
end;
// print column title
s := sampleDB.RecordSets[resultIndex].FieldNames[i];
if Length(s) > l[i] then l[i] := Length(s);
if Length(s) < l[i] then s := Format('%0:-'+IntToStr(l[i])+'s', [s]);
if i < col-1 then write(s,' | ') else writeln(s);
end;
// print cell value
for i := 0 to row-1 do
for j := 0 to col-1 do
begin
s := sampleDB.RecordSets[resultIndex].Records[i].Fields[j].Value;
if Length(s) > 99 then s := Copy(s, 1, 99) + '...'; // cut long text
if Length(s) < l[j] then s := Format('%0:-'+IntToStr(l[j])+'s', [s]);
if j < col-1 then write(s,' | ') else writeln(s);
end;
// print summary
if row > 0 then
writeln('Found: ',row,' record(s) with ',col,' field(s).')
else
writeln('No records found.');
end;
begin
sampleDB := TjanSQL.Create;
repeat
// input query
write('> ');
readln(sqlCommand);
quit := (LowerCase(Trim(sqlCommand)) = 'quit') or
(LowerCase(Trim(sqlCommand)) = 'quit;');
if quit then break;
// execute query
sqlResult := sampleDB.SQLDirect(sqlCommand);
// check query result
if sqlResult <> 0 then
begin
if sqlResult > 0 then
begin
writeln('SQL result:');
printResult(sqlResult);
// release recordset
if not sampleDB.RecordSets[sqlResult].Intermediate then
sampleDB.ReleaseRecordset(sqlResult);
end
else
writeln('SQL executed.');
end
else
writeln('ERROR: ',sampleDB.Error);
until quit;
writeln('OK');
sampleDB.Free;
end.