|
4 | 4 | using System.Data.SqlClient; |
5 | 5 | using System.Diagnostics; |
6 | 6 | using System.Linq; |
| 7 | +using System.Runtime.CompilerServices; |
7 | 8 | using System.Text; |
8 | 9 | using System.Threading.Tasks; |
9 | 10 |
|
@@ -105,18 +106,18 @@ public static bool Save<T>(SqlConnection connection, T entity, string[] keys = n |
105 | 106 |
|
106 | 107 | #endregion Save Methods |
107 | 108 |
|
108 | | - #region Load Methods |
109 | | - |
110 | | - #region Connection Depending |
111 | | - /// <summary> |
112 | | - /// Load single entity from database |
113 | | - /// </summary> |
114 | | - /// <typeparam name="T">entity type</typeparam> |
115 | | - /// <param name="connection">connection to database</param> |
116 | | - /// <param name="keyValue"></param> |
117 | | - /// <param name="identity">coloumn in database the represents the id</param> |
118 | | - /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
119 | | - /// <returns>found entity of the specified type or null if nothing is found or caused an exception</returns> |
| 109 | + #region Load Methods |
| 110 | + |
| 111 | + #region Connection Depending |
| 112 | + /// <summary> |
| 113 | + /// Load single entity from database |
| 114 | + /// </summary> |
| 115 | + /// <typeparam name="T">entity type</typeparam> |
| 116 | + /// <param name="connection">connection to database</param> |
| 117 | + /// <param name="keyValue"></param> |
| 118 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 119 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 120 | + /// <returns>found entity of the specified type or null if nothing is found or caused an exception</returns> |
120 | 121 | public static T Load<T>(SqlConnection connection, object keyValue, string identity = "Id", |
121 | 122 | string tableName = null) where T : class |
122 | 123 | { |
@@ -555,6 +556,186 @@ public static T LoadByQuery<T>(string connString, string query, Dictionary<strin |
555 | 556 | } |
556 | 557 | #endregion Load |
557 | 558 |
|
| 559 | + #region Delete Methods |
| 560 | + |
| 561 | + /// <summary> |
| 562 | + /// Delete single entity from database |
| 563 | + /// </summary> |
| 564 | + /// <param name="connection">Current active connection/param> |
| 565 | + /// <param name="entity">the single entity that needs to be deleted from database</param> |
| 566 | + /// <param name="keys"></param> |
| 567 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 568 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 569 | + /// <typeparam name="T">entity type</typeparam> |
| 570 | + /// <returns>true or false for success status</returns> |
| 571 | + public static bool Delete<T>(SqlConnection connection, T entity, string[] keys = null, string identity = "Id", |
| 572 | + string tableName = null) where T : class |
| 573 | + { |
| 574 | + try |
| 575 | + { |
| 576 | + ValidateConnection(connection); |
| 577 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 578 | + return entityHandler.DeleteEntity(entity, tableName ?? typeof(T).Name, keys); |
| 579 | + } |
| 580 | + catch (Exception ex) |
| 581 | + { |
| 582 | + Debug.WriteLine(ex.ToString()); |
| 583 | + return false; |
| 584 | + |
| 585 | + } |
| 586 | + } |
| 587 | + |
| 588 | + /// <summary> |
| 589 | + /// Delete multiple entities from the database |
| 590 | + /// </summary> |
| 591 | + /// <param name="connection">Current active connection</param> |
| 592 | + /// <param name="entities">list of entities that needs to be deleted from database</param> |
| 593 | + /// <param name="keys"></param> |
| 594 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 595 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 596 | + /// <typeparam name="T">entity type</typeparam> |
| 597 | + /// <returns>true or false for success status</returns> |
| 598 | + public static bool Delete<T>(SqlConnection connection, List<T> entities, string[] keys = null, string identity = "Id", |
| 599 | + string tableName = null) where T : class |
| 600 | + { |
| 601 | + try |
| 602 | + { |
| 603 | + ValidateConnection(connection); |
| 604 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 605 | + return entityHandler.DeleteEntities(entities, tableName ?? typeof(T).Name, keys); |
| 606 | + |
| 607 | + } |
| 608 | + catch (Exception ex) |
| 609 | + { |
| 610 | + Debug.WriteLine(ex.ToString()); |
| 611 | + return false; |
| 612 | + |
| 613 | + } |
| 614 | + } |
| 615 | + |
| 616 | + /// <summary> |
| 617 | + /// Delete items from table with from filter |
| 618 | + /// </summary> |
| 619 | + /// <param name="connection">Current active connection</param> |
| 620 | + /// <param name="table">name of table to delete from</param> |
| 621 | + /// <param name="filter"></param> |
| 622 | + /// <returns>number of affected rows</returns> |
| 623 | + public static int DeleteFromTable(SqlConnection connection, string table, Dictionary<string, object> filter) |
| 624 | + { |
| 625 | + try |
| 626 | + { |
| 627 | + ValidateConnection(connection); |
| 628 | + |
| 629 | + GenericSQLEntityHandler entityHandler = new GenericSQLEntityHandler(connection); |
| 630 | + int affectedRows; |
| 631 | + entityHandler.DeleteViaConditions(table, filter, out affectedRows); |
| 632 | + return affectedRows; |
| 633 | + |
| 634 | + } |
| 635 | + catch (Exception ex) |
| 636 | + { |
| 637 | + Debug.WriteLine(ex.ToString()); |
| 638 | + return -1; |
| 639 | + |
| 640 | + } |
| 641 | + } |
| 642 | + |
| 643 | + /// <summary> |
| 644 | + /// Deletes every thing from the specified table in database |
| 645 | + /// </summary> |
| 646 | + /// <param name="connection">Currennt active connection</param> |
| 647 | + /// <param name="table"></param> |
| 648 | + /// <returns>number of affected rows</returns> |
| 649 | + public static int DeleteAllFromTable(SqlConnection connection, string table) |
| 650 | + { |
| 651 | + try |
| 652 | + { |
| 653 | + ValidateConnection(connection); |
| 654 | + return DeleteFromTable(connection, table, null); |
| 655 | + } |
| 656 | + catch (Exception ex) |
| 657 | + { |
| 658 | + Debug.WriteLine(ex.ToString()); |
| 659 | + return -1; |
| 660 | + |
| 661 | + } |
| 662 | + } |
| 663 | + |
| 664 | + |
| 665 | + /// <summary> |
| 666 | + /// Delete single entity from database |
| 667 | + /// </summary> |
| 668 | + /// <param name="connString">connection string</param> |
| 669 | + /// <param name="entity">the single entity that needs to be deleted from database</param> |
| 670 | + /// <param name="keys"></param> |
| 671 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 672 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 673 | + /// <typeparam name="T">entity type</typeparam> |
| 674 | + /// <returns>true or false for success status</returns> |
| 675 | + public static bool Delete<T>(string connString, T entity, string[] keys = null, string identity = "Id", string tableName = null) where T : class |
| 676 | + { |
| 677 | + if (keys == null || keys.Length == 0) |
| 678 | + keys = new[] { identity }; |
| 679 | + |
| 680 | + using (SqlConnection connection = new SqlConnection(connString)) |
| 681 | + { |
| 682 | + connection.Open(); |
| 683 | + return Delete(connection, entity, keys, identity, tableName); |
| 684 | + } |
| 685 | + } |
| 686 | + |
| 687 | + /// <summary> |
| 688 | + /// Delete multiple entities from the database |
| 689 | + /// </summary> |
| 690 | + /// <param name="connString">connection string</param> |
| 691 | + /// <param name="entities">list of entities that needs to be deleted from database</param> |
| 692 | + /// <param name="keys"></param> |
| 693 | + /// <param name="identity">coloumn in database the represents the id</param> |
| 694 | + /// <param name="tableName">name of sqltable - if null, will use entitytype name as tablename</param> |
| 695 | + /// <typeparam name="T">entity type</typeparam> |
| 696 | + /// <returns>true or false for success status</returns> |
| 697 | + public static bool Delete<T>(string connString, List<T> entities, string[] keys = null, string identity = "Id", string tableName = null) where T : class |
| 698 | + { |
| 699 | + if (keys == null || keys.Length == 0) |
| 700 | + keys = new[] { identity }; |
| 701 | + |
| 702 | + using (SqlConnection connection = new SqlConnection(connString)) |
| 703 | + { |
| 704 | + connection.Open(); |
| 705 | + return Delete(connection, entities, keys, identity, tableName); |
| 706 | + } |
| 707 | + } |
| 708 | + |
| 709 | + |
| 710 | + /// <summary> |
| 711 | + /// Delete items from table with from filter |
| 712 | + /// </summary> |
| 713 | + /// <param name="connString">connection string</param> |
| 714 | + /// <param name="table">name of table to delete from</param> |
| 715 | + /// <param name="filter"></param> |
| 716 | + /// <returns>number of affected rows</returns> |
| 717 | + public static int DeleteFromTable(string connString, string table, Dictionary<string, object> filter) |
| 718 | + { |
| 719 | + using (SqlConnection connection = new SqlConnection(connString)) |
| 720 | + { |
| 721 | + connection.Open(); |
| 722 | + return DeleteFromTable(connection, table, filter); |
| 723 | + } |
| 724 | + } |
| 725 | + |
| 726 | + /// <summary> |
| 727 | + /// Deletes every thing from the specified table in database |
| 728 | + /// </summary> |
| 729 | + /// <param name="connString">connection string</param> |
| 730 | + /// <param name="table"></param> |
| 731 | + /// <returns>number of affected rows</returns> |
| 732 | + public static int DeleteAllFromTable(string connString, string table) |
| 733 | + { |
| 734 | + return DeleteFromTable(connString, table, null); |
| 735 | + } |
| 736 | + |
| 737 | + #endregion Delete Methods |
| 738 | + |
558 | 739 | #region Private methods |
559 | 740 | private static void ValidateConnection(SqlConnection connection) |
560 | 741 | { |
|
0 commit comments