Changeset 22 for trunk/DbEngines/UEngineXML.pas
- Timestamp:
- Mar 24, 2018, 12:33:48 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DbEngines/UEngineXML.pas
r21 r22 30 30 procedure LoadFromFile(FileName: string); 31 31 procedure SaveToFile(FileName: string); 32 procedure Expect(var Source: string; Text: string); 33 function CheckNext(var Source: string; Text: string): Boolean; 32 34 function GetNextPart(var Text: string; Separator: string = ' '): string; 33 35 protected … … 304 306 end; 305 307 308 procedure TDatabaseXML.Expect(var Source: string; Text: string); 309 var 310 Found: string; 311 begin 312 Found := GetNextPart(Source); 313 if Found <> Text then 314 raise Exception.Create('Expected ' + Text + ' but ' + Found + ' found.'); 315 end; 316 317 function TDatabaseXML.CheckNext(var Source: string; Text: string): Boolean; 318 var 319 Found: string; 320 SourceCopy: string; 321 begin 322 SourceCopy := Source; 323 Found := GetNextPart(SourceCopy); 324 if Found = Text then begin 325 Source := SourceCopy; 326 Result := True; 327 end else Result := False; 328 end; 329 306 330 function TDatabaseXML.GetNextPart(var Text: string; Separator: string = ' '): string; 307 331 begin … … 324 348 I: Integer; 325 349 F: Integer; 350 Row: TRecord; 351 WhereName: string; 352 WhereValue: string; 353 InsertValues: TStringList; 354 Field: TField; 355 FieldIndex: Integer; 356 NewValue: TValue; 357 ValueIndex: Integer; 326 358 begin 327 359 Command := GetNextPart(Text); 328 360 if Command = 'SELECT' then begin 329 361 Columns := GetNextPart(Text); 330 Command := GetNextPart(Text); 331 if Command = 'FROM' then begin 332 TableName := GetNextPart(Text); 333 end else raise Exception.Create('No table specified with FROM'); 362 Expect(Text, 'FROM'); 363 TableName := GetNextPart(Text); 334 364 Table := Tables.SearchByName(TableName); 335 365 if Assigned(Table) then begin … … 350 380 end else raise Exception.Create('Table ' + TableName + ' not found.'); 351 381 end else 352 if Command = 'CREATE' then begin 353 Command := GetNextPart(Text); 354 if Command = 'TABLE' then begin 355 TableName := GetNextPart(Text); 382 if Command = 'INSERT' then begin 383 InsertValues := TStringList.Create; 384 Expect(Text, 'INTO'); 385 TableName := GetNextPart(Text); 386 Expect(Text, '('); 387 InsertValues.Add(GetNextPart(Text) + InsertValues.NameValueSeparator); 388 while CheckNext(Text, ',') do begin 389 InsertValues.Add(GetNextPart(Text) + InsertValues.NameValueSeparator); 390 end; 391 Expect(Text, ')'); 392 Expect(Text, 'VALUES'); 393 Expect(Text, '('); 394 ValueIndex := 0; 395 InsertValues.ValueFromIndex[ValueIndex] := GetNextPart(Text); 396 Inc(ValueIndex); 397 while CheckNext(Text, ',') do begin 398 InsertValues.ValueFromIndex[ValueIndex] := GetNextPart(Text); 399 Inc(ValueIndex); 400 end; 401 Expect(Text, ')'); 402 if TableName = 'Model' then begin 356 403 Table := TTable.Create; 357 Table.Name := TableName; 404 Table.Name := InsertValues.Values['Name']; 405 Table.Caption := InsertValues.Values['Caption']; 358 406 Table.DbClient := Self; 359 Tables.Add(Table);; 360 end else raise Exception.Create('TABLE keyword expected'); 407 Tables.Add(Table); 408 end else begin 409 Table := Tables.SearchByName(TableName); 410 if Assigned(Table) then begin 411 Row := TRecord.Create; 412 Row.Parent := Table; 413 Row.InitValues; 414 for ValueIndex := 0 to InsertValues.Count - 1 do begin 415 Field := Table.Fields.SearchByName(InsertValues.Names[ValueIndex]); 416 FieldIndex := Table.Fields.IndexOf(Field); 417 TValue(Row.Values[FieldIndex]).SetString(InsertValues.ValueFromIndex[ValueIndex]); 418 end; 419 Table.Records.Add(Row); 420 end else raise Exception.Create(Format('Table %s not found', [TableName])); 421 end; 422 InsertValues.Free; 361 423 end else 362 if Command = 'DROP' then begin 363 Command := GetNextPart(Text); 364 if Command = 'TABLE' then begin 365 TableName := GetNextPart(Text); 424 if Command = 'DELETE' then begin 425 Expect(Text, 'FROM'); 426 TableName := GetNextPart(Text); 427 if CheckNext(Text, 'WHERE') then begin 428 WhereName := GetNextPart(Text); 429 Command := GetNextPart(Text); 430 if Command = '=' then begin 431 WhereValue := GetNextPart(Text); 432 end else raise Exception.Create('Expression error'); 433 end; 434 if TableName = 'Model' then begin 435 Table := Tables.SearchByName(WhereValue); 436 if Assigned(Table) then begin 437 Tables.Remove(Table); 438 end else raise Exception.Create(Format('Table %s not found', [whereValue])); 439 end else begin 366 440 Table := Tables.SearchByName(TableName); 367 if Assigned(Table) then Tables.Remove(Table) 368 else raise Exception.Create('Table ' + TableName + ' not found'); 369 end else raise Exception.Create('TABLE keyword expected'); 441 if Assigned(Table) then begin 442 Row := Table.Records.SearchByValue(WhereName, WhereValue); 443 if Assigned(Row) then begin 444 Table.Records.Remove(Row); 445 end else raise Exception.Create('Row not found'); 446 end else raise Exception.Create(Format('Table %s not found', [TableName])); 447 end; 370 448 end else 371 449 raise Exception.Create('Unsupported SQL command ' + Command); … … 380 458 begin 381 459 FileName := TDbConnectParamsXml(ConnectProfile.Params).FileName; 460 Tables.DbClient := Self; 382 461 if FileExists(FileName) then 383 462 LoadFromFile(FileName);
Note:
See TracChangeset
for help on using the changeset viewer.