File: AuxFuncsGrt.pas

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (374 lines) | stat: -rw-r--r-- 9,640 bytes parent folder | download
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
unit AuxFuncsGrt;

interface

uses
  gnugettext, SysUtils, Classes,
  myx_public_interface, myx_grt_public_interface,
  TntClasses, TntSysUtils;

type
  EGrtError = class(Exception)
  private
    FErrorNumber: Integer;
    FDescription: WideString;
  public
    constructor Create(ErrorNumber: Integer; Description: WideString);

    property ErrorNumber: Integer read FErrorNumber;
    property Description: WideString read FDescription;
  end;

  GrtMethodExecutionThread = class(TThread)
  private
    FExecutionFinished: Boolean;
    MultiReadSync: TMultiReadExclusiveWriteSynchronizer;

    FStartTime,
      FEndTime: TDateTime;
    FGrt: Pointer;
    FModulName,
      FFunctionName: WideString;
    FSearchParent: Boolean;
    FFunctionArgument: Pointer;
    FError: PMYX_GRT_ERROR;
    FResult: Pointer;

    function GetExecutionFinished: Boolean;
    function GetExecutionTime: TDateTime;
    function GetResult: Pointer;
  public
    constructor Create(Grt: Pointer; ModulName: WideString;
      FunctionName: WideString; SearchParent: Boolean;
      FunctionArgument: Pointer; Error: PMYX_GRT_ERROR);
    destructor Destroy; override;

    procedure Execute; override;
    property ExecutionFinished: Boolean read GetExecutionFinished;
    property ExecutionTime: TDateTime read GetExecutionTime;
    property Result: Pointer read GetResult;
  end;

function CallGrtModuleMethod(Grt: Pointer;
  ModulName: WideString; FunctionName: WideString;
  FunctionArgument: Pointer; MessageOutput: TTntStrings = nil;
  SearchParent: Boolean = True; AllowNullAsResult: Boolean = False): Pointer;

function BuildGrtParamList(Params: array of const): Pointer;

function GetListMemberCount(Grt: Pointer; StructName: WideString;
  OnlyCheck: Boolean): Integer;
function GetListMember(Grt: Pointer; StructName: WideString;
  Index: Integer): Pointer;

implementation

// -----------------------------------------------------------------------------

constructor EGrtError.Create(ErrorNumber: Integer; Description: WideString);

begin
  FErrorNumber := ErrorNumber;
  FDescription := Description;
end;

// -----------------------------------------------------------------------------

constructor GrtMethodExecutionThread.Create(
  Grt: Pointer; ModulName: WideString;
  FunctionName: WideString; SearchParent: Boolean;
  FunctionArgument: Pointer; Error: PMYX_GRT_ERROR);

begin
  inherited Create(True);

  FExecutionFinished := False;
  FGrt := Grt;
  FModulName := ModulName;
  FFunctionName := FunctionName;
  FSearchParent := SearchParent;
  FFunctionArgument := FunctionArgument;
  FError := Error;

  MultiReadSync := TMultiReadExclusiveWriteSynchronizer.Create;
end;

// -----------------------------------------------------------------------------

destructor GrtMethodExecutionThread.Destroy;

begin
  MultiReadSync.Free;

  inherited Destroy;
end;

// -----------------------------------------------------------------------------

function GrtMethodExecutionThread.GetExecutionTime: TDateTime;

begin
  Result := FEndTime - FStartTime;
end;

// -----------------------------------------------------------------------------

function GrtMethodExecutionThread.GetResult: Pointer;

begin
  Result := FResult;
end;

// -----------------------------------------------------------------------------

function GrtMethodExecutionThread.GetExecutionFinished: Boolean;

begin
  MultiReadSync.BeginRead;
  try
    Result := FExecutionFinished;
  finally
    MultiReadSync.EndRead;
  end;
end;

// -----------------------------------------------------------------------------

procedure GrtMethodExecutionThread.Execute;

begin
  FStartTime := Now;

  //Search for the function and call it with a list of params
  FResult := myx_grt_function_get_and_call(FGrt,
    FModulName, FFunctionName, Ord(FSearchParent),
    FFunctionArgument, @FError);

  FEndTime := Now;

  MultiReadSync.BeginWrite;
  try
    FExecutionFinished := True;
  finally
    MultiReadSync.EndWrite;
  end;
end;

// -----------------------------------------------------------------------------

function CallGrtModuleMethod(Grt: Pointer;
  ModulName: WideString; FunctionName: WideString;
  FunctionArgument: Pointer; MessageOutput: TTntStrings = nil;
  SearchParent: Boolean = True; AllowNullAsResult: Boolean = False): Pointer;

var
  Error: MYX_GRT_ERROR;
  FunctionErrorString: WideString;
  PMsgs: PMYX_GRT_MSGS;
  Msgs: TMYX_GRT_MSGS;
  i, j: Integer;
  s: WideString;
  ExecTime: TDateTime;
  StartTime: TDateTime;
  //ExecThread: GrtMethodExecutionThread;

begin
  {ExecThread := GrtMethodExecutionThread.Create(Grt,
    ModulName, FunctionName, SearchParent,
    FunctionArgument, @error);
  try
    //Start thread execution
    ExecThread.Resume;

    //Wait till the execution is completed
    while (not(ExecThread.ExecutionFinished)) do
      Sleep(100);

    Result := ExecThread.Result;

    ExecTime := ExecThread.ExecutionTime;
  finally
    ExecThread.Free;
  end;}

  StartTime := Now;

  //Search for the function and call it with a list of params
  Result := myx_grt_function_get_and_call(Grt,
    ModulName, FunctionName, Ord(SearchParent),
    FunctionArgument, @error);

  ExecTime := Now - StartTime;

  //This will return a dict either with a key value or with
  //two keys error and stack

  //Check the result for error keys
  FunctionErrorString := myx_grt_function_check_error(Result,
    Ord(AllowNullAsResult));

  //Show error
  if (Error<>MYX_GRT_NO_ERROR) or
    (FunctionErrorString<>'') then
    raise EGrtError.Create(Ord(Error), FunctionErrorString);

  //If there was no error we have a value key
  //Return the contents of the key as result
  if (Result<>nil) then
    Result := myx_grt_dict_item_get_value(Result, 'value');

  if (MessageOutput<>nil) then
  begin
    //Check for messages
    PMsgs := myx_grt_get_msgs(Grt, 0);
    if (PMsgs<>nil)then
    begin
      try
        Msgs := TMYX_GRT_MSGS.Create(PMsgs);
        try
          for i:=0 to Msgs.msgs.Count-1 do
          begin
            if (Msgs.msgs[i].msg_type=1) then
              s := 'ERROR: '
            else
              s := '';

            MessageOutput.Add(s +
              WideStringReplace(
                WideStringReplace(
                  Msgs.msgs[i].msg, #13#10, #10, [rfReplaceAll], False),
                #10, #13#10, [rfReplaceAll], False));

            for j:=0 to Msgs.msgs[i].msg_detail.strings.Count-1 do
            begin
              MessageOutput.Add(
                WideStringReplace(
                  WideStringReplace(
                    Msgs.msgs[i].msg_detail.strings[j], #13#10, #10, [rfReplaceAll], False),
                  #10, #13#10, [rfReplaceAll], False));
            end;

            MessageOutput.Add('');
          end;
        finally
          Msgs.Free;
        end;
      finally
        myx_grt_free_msgs(PMsgs);
      end;
    end;

    MessageOutput.Add(
      _('Executed in ') +
        FormatDateTime('n:ss:zzz', ExecTime) + '.' + #13#10);
  end;
end;

// -----------------------------------------------------------------------------

function BuildGrtParamList(Params: array of const): Pointer;

var
  ParamList: Pointer;
  Param: Pointer;
  I: Integer;

begin
  ParamList := myx_grt_list_new(MYX_ANY_VALUE, '');

  for I:=0 to High(Params) do
  begin
    with Params[I] do
      case VType of
        vtInteger:
          Param := myx_grt_value_from_int(VInteger);
        vtExtended:
          Param := myx_grt_value_from_real(VExtended^);
        vtWideString:
          Param := myx_grt_value_from_string(WideString(VWideString));
        vtString:
          Param := myx_grt_value_from_string(VString^);
        vtAnsiString:
          Param := myx_grt_value_from_string(string(VAnsiString));
        vtPChar:
          Param := myx_grt_value_from_string(VPChar);
        vtPointer:
          Param := VPointer;
      else
        raise EInOutError.Create(_('BuildGrtParamList called with unsupported parameter type.'));
      end;

    myx_grt_list_item_add(ParamList, Param);
  end;

  Result := ParamList;
end;

// -----------------------------------------------------------------------------

function GetListMemberCount(Grt: Pointer; StructName: WideString;
  OnlyCheck: Boolean): Integer;

var
  PStruct,
    PMember: Pointer;
  i,
    count: integer;

begin
  PStruct := myx_grt_struct_get(Grt, StructName);

  count := 0;
  for i:=0 to myx_grt_struct_get_member_count_total_excluding_struct(
    Grt, PStruct, 'db.DatabaseObject')-1 do
  begin
    PMember := myx_grt_struct_get_member_by_index_total(
      Grt, PStruct, i);

    if (myx_grt_struct_member_get_type(PMember) = MYX_LIST_VALUE) and
      (myx_grt_struct_member_get_content_type(PMember) = MYX_DICT_VALUE) then
    begin
      inc(count);

      if (OnlyCheck) then
        break;
    end;
  end;

  Result := count;
end;

// -----------------------------------------------------------------------------

function GetListMember(Grt: Pointer; StructName: WideString;
  Index: Integer): Pointer;

var
  PStruct: Pointer;
  i,
    Count: integer;

begin
  Result := nil;
  PStruct := myx_grt_struct_get(Grt, StructName);

  count := 0;
  for i:=0 to myx_grt_struct_get_member_count_total(Grt, PStruct)-1 do
  begin
    Result := myx_grt_struct_get_member_by_index_total(
      Grt, PStruct, i);

    if (myx_grt_struct_member_get_type(Result) = MYX_LIST_VALUE) and
      (myx_grt_struct_member_get_content_type(Result) = MYX_DICT_VALUE) then
    begin
      inc(Count);

      if (Index = Count-1) then
        break;
    end;
  end;
end;

// -----------------------------------------------------------------------------

end.