Дополнительный модуль, в котором сосредоточены основные SQL запросы. Имена некоторых полей берутся из ObjectInspector.
Из специфичного для MySQL инструкция Insert… Select… для копирования узлов
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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
unit uSQLQueries; //Здесь будут храниться шаблоны запросов SQL //Они будут изменяться из ObjectInspector interface uses System.SysUtils, System.Classes,vcl.dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQLDef, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, FireDAC.Phys.MySQL, Data.DB, FireDAC.Comp.Client,uDBConnectionBPL; type TSQLQueries=class(TComponent) private {Собственно SQL запросы дерева находятся в этих полях} {Их можно было поправить из ObjectInspector, в поздних версиях убрал эту возможность} FInsertFolderFile:TstringList; FinsertNode:TStringList; FMaxID:TStringList; FSelectIDisFolder:TStringList; FSelectAllChildren:TStringList; // <<Неполный запрос, остальная часть проставляется в конкретной // процедуре //Удаление узлов из БД //Модуль C:\Users\YellowFriend\Desktop\DBTReeViewSVN2\uCopyCutDelete\uDeleteThread.pas FDeleteNodesFromDB:TStringList; //Копирование и вставка узла FCopyRecordInDB:TStringList; //Обновление родителя FUpdateIdParent:TstringList; //Обновление галочек FUpdateChecked:TStringList; //Select по id FSelectByID:TStringList; //Проверка на папку FCheckIsFolder:TStringList; //Поиск родителя FSelectParent:TStringlist; //Поиск по имени FSearch:TStringList; //Редактирование названия узла FEdit:TStringList; //Обновление индекса FUpdateIndex:TStringlist; //Получение id FSelectID:TStringList; //Обновление родителя в базе FEditParentKey:TStringList; //Параметры соединения, для получения имени базы FDBConnectionParams:TDBConnectionParams; FDBTable:TDBTable; //Выборка только тех, кто с галочками FSelectAllChecked:TStringList; //Удаление всех галочек из базы FClearAllCheckBoxes:TStringList; //FSelectAll FSelectAll:TstringList; //Геттеры function GetInsertFolderFile:TstringList; function GetInsertNode:TStringList; function GetMaxID:TStringList; function GetSelectIDisFolder:TStringList; function GetSelectAllChildren:TStringList; function GetUpdateChecked:TStringList; function GetDeleteNodesFromDB:TStringList; function GetCopyRecordInDB:TStringList; function GetUpdateIdParent:TstringList; function GetSelectByID:TStringList; function GetCheckIsFolder:TStringList; function GetSelectParent:TStringlist; function GetSearch:TStringList; function GetEdit:TStringList; function GetUpdateIndex:TStringList; function GetSelectID:TStringList; function GetEditParentKey:TStringList; function GetClearAllCheckBoxes:TStringList; function GetSelectAllChecked:TStringList; function GetSelectAll:TstringList; //Сеттеры procedure SetInsertFolderFile(Value:TstringList); procedure SetInsertNode(Value:TStringList); procedure SetMaxID(Value:TStringList); procedure SetSelectIDisFolder(Value:TStringList); procedure SetSelectAllChildren(Value:TstringList); procedure SetUpdateChecked(Value:TstringList); procedure SetDeleteNodesFromDB(Value:TStringList); procedure SetCopyRecordInDB(Value:TStringList); procedure SetUpdateIdParent(Value:TstringList); procedure SetSelectByID(Value:TStringList); procedure SetCheckIsFolder(Value:TStringList); procedure SetSelectParent(Value:TStringlist); procedure SetSearch(Value:TStringList); procedure SetEdit(Value:TStringList); procedure SetUpdateIndex(Value:TStringList); procedure SetSelectID(Value:TStringList); procedure SetEditParentKey(Value:TStringList); procedure SetClearAllCheckBoxes(Value:TStringList); procedure SetSelectAllChecked(Value:TStringList); procedure SetSelectAll(Value:TStringList); public constructor Create(AOwner:TComponent); override; destructor Destroy; override; published // << Убираем из published, так как менять будем через //DBTreeViewConnParams и DBTReeViewTable в ObjectInspector property InsertFolderFile:TstringList read GetInsertFolderFile write SetInsertFolderFile; property InsertNode:TStringList read GetInsertNode write SetInsertNode; property MaxID:TStringList read GetMaxID write SetMaxId; property SelectIDisFolder:Tstringlist read GetSelectIDisFolder write SetSelectIDisFolder; property SelectAllChildren:TStringList read GetSelectAllChildren write SetSelectAllChildren; property UpdateChecked:TStringList read GetUpdateChecked write SetUpdateChecked; property DeleteNodesFromDB:TStringList read GetDeleteNodesFromDB write SetDeleteNodesFromDB; property CopyRecordInDB:TStringList read GetCopyRecordInDB write SetCopyRecordInDB; property UpdateIdParent:TstringList read GetUpdateIdParent write SetUpdateIdParent; property SelectByID:TStringList read GetSelectByID write SetSelectByID; property CheckIsFolder:TStringList read GetCheckIsFolder write SetCheckIsFolder; property SelectParent:TStringList read GetSelectParent write SetSelectParent; property Search:TStringList read GetSearch write SetSearch; property Edit:TStringList read GetEdit write SetEdit; property UpdateIndex:TStringList read GetUpdateIndex write SetUpdateIndex; property SelectID:TStringList read GetSelectID write SetSelectID; property EditParentKey:TStringList read GetEditParentKey write SetEditParentKey; property ClearAllCheckBoxes:TStringList read GetClearAllCheckBoxes write SetClearAllCheckBoxes; property SelectAllChecked:TstringList read GetSelectAllChecked write SetSelectAllChecked; property SelectAll:TStringLIst read FSelectAll write FSelectAll; property DBConnectionParams:TDBConnectionParams read FDBConnectionParams write FDBConnectionParams; property DBTableName:TDBTable read FDBTable write FDBTable; end; implementation uses uTreeView; { TSQLQueries } constructor TSQLQueries.Create(AOwner: TComponent); var DBName:string; TableName:string; DBNamePlusTable:string; //`IdParent`, `Name`, `Index`,`isChecked`,`Level`,`IsFolder` idField,idParent,NameField,IndexField,isChecked,Level,IsFolder,IsExpanded:string; begin inherited; Self.Name:='SQLQueries'; //------------------------------------------------------------------------------ // Переменные для подстановки в запросы, их значения берем из ObjectInspector //------------------------------------------------------------------------------ DBNamePlusTable:= '`' +DBTreeViewGlobal.DBTreeViewConnParams.DataBase +'`'+'.'+'`' +DBTreeViewGlobal.DBTreeViewTable.TableName+'`'; //Поля таблицы из ObjectInspector idField:=DBTreeViewGlobal.DBTreeViewTable.id; idParent:=DBTreeViewGlobal.DBTreeViewTable.idParent; NameField:=DBTreeViewGlobal.DBTreeViewTable.NodeName; IndexField:=DBTreeViewGlobal.DBTreeViewTable.IndexField; isChecked:=DBTreeViewGlobal.DBTreeViewTable.IsChecked; Level:=DBTreeViewGlobal.DBTreeViewTable.Level; IsFolder:=DBTreeViewGlobal.DBTreeViewTable.IsFolder; IsExpanded:=DBTreeViewGlobal.DBTreeViewTable.IsExpanded; idField:='`'+idField+'`'; idParent:='`'+idParent+'`'; NameField:='`'+NameField+'`'; IndexField:='`'+IndexField+'`'; isChecked:='`'+isChecked+'`'; Level:='`'+Level+'`'; IsFolder:='`'+IsFolder+'`'; IsExpanded:='`'+IsExpanded+'`'; //Вставка папки или файла // InsertFolderFile:=TStringList.Create; FInsertFolderFile:=TStringlist.Create; FInsertFolderFile.Clear; FInsertFolderFile.Text:= 'INSERT INTO '+DBNamePlusTable+' ('+IdParent+','+NameField+', '+IndexField+ ', '+isChecked+', '+Level+','+IsFolder+')'+ ' VALUES (:idParent, :name, :index,:isChecked,:Level,:IsFolder);'; { //Без параметров FInsertFolderFile.Text:= 'INSERT INTO '+DBNamePlusTable+' (`idParent`,`Name`,`Index`,`isChecked`,`Level`,`IsFolder`) '+ ' VALUES (:idParent, :name, :index,:isChecked,:Level,:IsFolder);'; } //Вставка произвольного узла // InsertNode:=TStringList.Create; FInsertNode:=Tstringlist.Create; FInsertNode.Clear; FInsertNode.Text:= 'INSERT INTO '+DBNamePlusTable+' ('+IdParent+','+NameField+', '+IndexField+ ', '+isChecked+', '+Level+')'+ ' VALUES (:idParent, :name, :index,:isChecked,:Level);'; { // Без параметров ( 'INSERT INTO '+DBNamePlusTable+' (`IdParent`, `Name`, `Index`,`isChecked`,`Level`)'+ 'VALUES (:idParent, :name, :index,:isChecked,:Level);' ); } {Получение максимального ID} FMaxID:=TstringList.Create; FMaxID.Clear; FMaxID.Text:='SELECT Max('+idField+') as newkey FROM '+DBNamePlusTable+';'; // Без параметров {FMaxID.Text:='SELECT Max(id) as newkey FROM '+DBNamePlusTable+';';} {Проверка папка это или файл по базе} FSelectIDisFolder:=TStringList.Create; FSelectIDisFolder.Clear; FSelectIDisFolder.Text:='SELECT '+idField+','+IsFolder+' FROM '+DBNamePlusTable+';'; // Без параметров {FSelectIDisFolder.Text:='SELECT id,IsFolder FROM '+DBNamePlusTable+';';} {Получение всех детей узла} //Внимание! Запрос неполный - остальная часть проставляется в конкретной процедуре, например {SQL.Text:='SELECT * FROM treeview_db.tree where IdParent='+NodeID.ToString;} FSelectAllChildren:=TStringList.Create; FSelectAllChildren.Clear; FSelectAllChildren.Text:='SELECT * FROM '+DBNamePlusTable+' where '+IdParent+'='; // Без параметров {'SELECT * FROM treeview_db.tree where IdParent=} {Поиск родителя} FSelectParent:=TstringList.Create; FSelectParent.Clear; FSelectParent.Text:='Select * from '+DBNamePlusTable+' where '+idField+'='; {Обновление галочек} FUpdateChecked:=TStringList.Create; FUpdateChecked.Clear; FUpdateChecked.Text:='UPDATE '+DBNamePlusTable+' SET '+IsChecked+'=:IsChecked WHERE '+idField+'=:id;'; {Удаление узла из БД} FDeleteNodesFromDB:=TstringList.Create; FDeleteNodesFromDB.Clear; FDeleteNodesFromDB.Text:='DELETE FROM '+DBNamePlusTable+' WHERE '+idField+' =:id;'; {Копирование и вставка узла в БД INSERT SELECT} FCopyRecordInDB:=TstringList.Create; FCopyRecordInDB.Clear; FCopyRecordInDB.Text:= 'INSERT INTO '+DBNamePlusTable+ '('+idParent+','+ NameField+','+ IndexField+','+ isChecked+','+ IsExpanded+','+ Level+','+ IsFolder+')'+ ' SELECT '+ IdParent+','+ NameField+','+ IndexField+','+ isChecked+','+ IsExpanded+','+ Level+','+ IsFolder+ ' from ' +DBNamePlusTable+' where '+idField+'=:id' ; { // Без параметров FCopyRecordInDB.Text:= 'INSERT INTO '+DBNamePlusTable+ '(`IdParent`,'+ '`Name`,'+ '`Index`,'+ '`isChecked`,'+ '`IsExpanded`,'+ '`Level`,'+ '`IsFolder`)'+ 'SELECT'+ '`IdParent`,'+ '`Name`,'+ '`Index`,'+ '`isChecked`,'+ '`IsExpanded`,'+ '`Level`,'+ '`IsFolder`'+ ' from ' +DBNamePlusTable+' where id=:id' ; } {Обновление родителя} // UPDATE `treeview_db`.`tree` SET `IdParent`=:idParent WHERE `id`=:id; FUpdateIdParent:=TstringList.Create; FUpdateIdParent.Clear; FUpdateIdParent.Text:='UPDATE '+DBNamePlusTable+' SET '+idParent+'=:idParent WHERE '+idField+'=:id;'; // Без параметров {FUpdateIdParent.Text:='UPDATE '+DBNamePlusTable+' SET `IdParent`=:idParent WHERE `id`=:id;';} {Select by ID} FSelectByID:=TstringList.Create; FSelectByID.Clear; FSelectByID.Text:= 'SELECT * FROM '+DBNamePlusTable+' where '+idField+'=:id;'; {Проверка на папку} FCheckIsFolder:=Tstringlist.Create; FCheckIsFolder.Clear; FCheckIsFolder.Text:='Select * from '+DBNamePlusTable+' where '+idField+'='; {Поиск по имени} FSearch:=TstringList.Create; FSearch.Clear; FSearch.Text:='SELECT '+NameField+' FROM '+DBNamePlusTable+' where '+NameField+' like '; // Без параметров ////'SELECT name FROM treeview_db.tree where Name like '+'''%'+name+'%'''; FEdit:=TStringList.Create; FEdit.Clear; FEdit.Text:='UPDATE '+DBNamePlusTable+' SET '+NameField+' =:Name WHERE '+idField+'=:id'; {Обновление индекса} FUpdateIndex:=TstringList.Create; FUpdateIndex.Clear; FUpdateIndex.Text:='UPDATE '+DBNamePlusTable+' SET '+indexField+'=:Index WHERE '+IdField+'=:id;'; {SelectID} FSelectID:=TStringLIst.Create; FSelectID.Clear; FSelectID.Text:='SELECT '+idField+' FROM '+DBNamePlusTable+';'; {SelectALL order ByParent} FSelectAll:=TStringList.Create; FSelectAll.Clear; FSelectAll.Text:='SELECT*FROM '+DBNamePlusTable+' order by '+indexField+';'; {EditParentKey} FEditParentKey:=TstringList.Create; FEditParentKey.Clear; FEditParentKey.Text:='UPDATE '+DBNamePlusTable+' SET '+idParent+'=:idParent WHERE '+idField+'=:id;'; {Выборка всех отмеченных галочками} FSelectAllChecked:=TStringList.Create; FSelectAllChecked.Clear; FSelectAllChecked.Text:='SELECT * FROM '+DBNamePlusTable+' where '+IsChecked+'=''1'';'; {ClearAllCheckBoxes} FClearAllCheckBoxes:=TStringList.Create; FClearAllCheckBoxes.Clear; FClearAllCheckBoxes.Text:='UPDATE '+DBNamePlusTable+' SET '+isChecked+'=''0'' where '+idField+'=:id;' {UPDATE `treeview_db`.`tree` SET `IsChecked`='0' WHERE `id`='87078'; } end; destructor TSQLQueries.Destroy; begin FreeAndNil(FInsertFolderFile); FreeAndNil(FInsertNode); FreeAndNil(FMaxID); FreeAndNil(FSelectIDisFolder); FreeAndNil(FSelectAllChildren); FreeAndNil(FUpdateChecked); FreeAndNil(FDeleteNodesFromDB); FreeAndNil(FCopyRecordInDB); FreeAndNil(FUpdateIdParent); FreeAndNIl(FSelectByID); FreeAndNil(FCheckIsFolder); FreeAndNIl(FSelectParent); FreeAndNil(FSearch); FreeAndNil(FEdit); FreeAndNil(FUpdateIndex); FreeAndNil(FSelectID); FreeAndNil(FEditParentKey); FreeAndNil(FClearAllCheckBoxes); FreeAndNil(FSelectAllChecked); FreeAndNil(FSelectAll); inherited; end; //-----------------Геттеры---------------------------- function TSQLQueries.GetSearch: TStringList; begin Result:=FSearch; end; function TSQLQueries.GetSelectAll: TstringList; begin Result:=FSelectAll; end; function TSQLQueries.GetSelectAllChecked: TStringList; begin Result:=FSelectAllChecked; end; function TSQLQueries.GetSelectAllChildren: TStringList; begin Result:=FSelectAllChildren; end; function TSQLQueries.GetSelectByID: TStringList; begin Result:=FSelectByID; end; function TSQLQueries.GetSelectID: TStringList; begin Result:=FSelectID; end; function TSQLQueries.GetSelectIDisFolder: TStringList; begin Result:=FSelectIDisFolder; end; function TSQLQueries.GetSelectParent: TStringlist; begin Result:=FSelectParent; end; function TSQLQueries.GetUpdateChecked: TStringList; begin Result:=FUpdateChecked; end; function TSQLQueries.GetUpdateIdParent: TstringList; begin Result:=FUpdateIdParent; end; function TSQLQueries.GetUpdateIndex: TStringList; begin Result:=FUpdateIndex; end; function TSQLQueries.GetCheckIsFolder: TStringList; begin Result:=FCheckIsFolder; end; function TSQLQueries.GetClearAllCheckBoxes: TStringList; begin Result:=FClearAllCheckBoxes; end; function TSQLQueries.GetCopyRecordInDB: TStringList; begin Result:=FCopyRecordInDB; end; function TSQLQueries.GetDeleteNodesFromDB: TStringList; begin Result:=FDeleteNodesFromDB; end; function TSQLQueries.GetEdit: TStringList; begin Result:=FEdit; end; function TSQLQueries.GetEditParentKey: TStringList; begin Result:=FEditParentKey; end; function TSQLQueries.GetInsertFolderFile: TstringList; begin Result:=FInsertFolderFile; end; function TSQLQueries.GetInsertNode: TStringList; begin Result:=FInsertNode; end; function TSQLQueries.GetMaxID: TStringList; begin Result:=FMaxID; end; //-----------------Сеттеры------------------------------ procedure TSQLQueries.SetSearch(Value: TStringList); begin if not Assigned(Value) then exit; FSearch.Assign(Value); end; procedure TSQLQueries.SetSelectAll(Value: TStringList); begin if not Assigned(Value) then exit; FSelectAll.Assign(Value); end; procedure TSQLQueries.SetSelectAllChecked(Value: TStringList); begin if not Assigned(Value) then exit; FSelectAllChecked.Assign(Value); end; procedure TSQLQueries.SetSelectAllChildren(Value: TstringList); begin if not Assigned(Value) then exit; FSelectAllChildren.Assign(Value); end; procedure TSQLQueries.SetSelectByID(Value: TStringList); begin if not Assigned(Value) then exit; FSelectByID.Assign(Value); end; procedure TSQLQueries.SetSelectID(Value: TStringList); begin if not Assigned(Value) then exit; FSelectID.Assign(Value); end; procedure TSQLQueries.SetSelectIDisFolder(Value: TStringList); begin if not Assigned(Value) then exit; FSelectIDisFolder.Assign(Value); end; procedure TSQLQueries.SetSelectParent(Value: TStringlist); begin if not Assigned(Value) then exit; FSelectParent.Assign(Value); end; procedure TSQLQueries.SetUpdateChecked(Value: TstringList); begin if not Assigned(Value) then exit; FUpdateChecked.Assign(Value); end; procedure TSQLQueries.SetUpdateIdParent(Value: TstringList); begin if not Assigned(Value) then exit; FUpdateIdParent.Assign(Value); end; procedure TSQLQueries.SetUpdateIndex(Value: TStringList); begin if not Assigned(Value) then exit; FUpdateIndex.Assign(Value); end; procedure TSQLQueries.SetCheckIsFolder(Value: TStringList); begin if not Assigned(Value) then exit; FCheckIsFolder.Assign(Value); end; procedure TSQLQueries.SetClearAllCheckBoxes(Value: TStringList); begin if not Assigned(Value) then exit; FClearAllCheckBoxes.Assign(Value); end; procedure TSQLQueries.SetCopyRecordInDB(Value: TStringList); begin if not Assigned(Value) then exit; FCopyRecordInDB.Assign(Value); end; procedure TSQLQueries.SetDeleteNodesFromDB(Value: TStringList); begin if not Assigned(Value) then exit; FDeleteNodesFromDB.Assign(Value); end; procedure TSQLQueries.SetEdit(Value: TStringList); begin if not Assigned(Value) then exit; FEdit.Assign(Value); end; procedure TSQLQueries.SetEditParentKey(Value: TStringList); begin if not Assigned(Value) then exit; FEditParentKey.Assign(Value); end; procedure TSQLQueries.SetInsertFolderFile(Value: TstringList); begin //FInsertFolderFile:=Value; // << Так неправильно - затирание ссылки if not Assigned(Value) then exit; FInsertFolderFile.Assign(Value); end; procedure TSQLQueries.SetInsertNode(Value: TStringList); begin if not Assigned(Value) then exit; FInsertNode.Assign(Value); end; procedure TSQLQueries.SetMaxID(Value: TStringList); begin if not Assigned(Value) then exit; FMaxID.Assign(Value); end; end. |