-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLOperations.cs
114 lines (101 loc) · 3.94 KB
/
SQLOperations.cs
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
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
public static class SQLOperations {
public static MySqlH Sql;
public static bool ThrowExceptions = false;
public static bool MessageExceptions = true;
/// <summary>
/// Gets the 'MySqlH' instante from 'App.Current.Properties["sql"]' and executes an action. If the code fails, a MessageBox is shown.
/// </summary>
/// <param name="title">The MessageBox title.</param>
/// <param name="action">The action to execute the MySqlH instance.</param>
private static void SQL(string title, Action<MySqlH> action, Action<Exception> onError = null, bool tryAgain = false) {
int a = 0;
A:
try {
Sql.Connection.Open();
action.Invoke(Sql);
} catch (Exception ex) {
if (onError != null) onError.Invoke(ex);
if (tryAgain && a == 0) {
a += 1;
goto A;
}
if (MessageExceptions) MessageBox.Show($"Classe: {ex.GetType()}\nMensagem: {ex.Message}", title, MessageBoxButton.OK, MessageBoxImage.Error);
if (ThrowExceptions) throw ex;
} finally {
if (Sql != null && Sql.Connection != null) Sql.Connection.Close();
}
}
public static void Connection(string title, Action<MySqlConnection> action) {
SQL(title, (sql) => {
try {
sql.Connection.Open();
action.Invoke(sql.Connection);
} catch (Exception ex) {
throw ex;
} finally {
if (sql != null && sql.Connection != null) sql.Connection.Close();
}
});
}
public static void NonQuery(string title, string nonQuery) {
SQL(title, (sql) => {
sql.NonQuery(nonQuery);
});
}
public static void NonQuery(string title, MySqlCommand c, Action<Exception> onError = null, bool tryAgain = false) {
SQL(title, (sql) => {
sql.NonQuery(c);
}, onError, tryAgain);
}
public static void NonQuery(string title, Func<MySqlCommand, MySqlCommand> action, Action<Exception> onError = null, bool tryAgain = false) {
SQL(title, (sql) => {
var c = new MySqlCommand();
c = action.Invoke(c);
sql.NonQuery(c);
}, onError, tryAgain);
}
public static void QueryR(string title, MySqlCommand command, Action<MySqlDataReader> action, Action<Exception> onError = null, bool tryAgain = false) {
SQL(title, (sql) => {
sql.QueryR(command, action);
}, onError, tryAgain);
}
public static void QueryRLoop(string title, string command, Action<MySqlDataReader> action, Action<Exception> onError = null, bool tryAgain = false) {
QueryRLoop(title, new MySqlCommand(command), action, onError, tryAgain);
}
public static void QueryRLoop(string title, MySqlCommand c, Action<MySqlDataReader> action, Action<Exception> onError = null, bool tryAgain = false) {
QueryR(title, c, (r) => {
while (r.Read()) {
action.Invoke(r);
}
}, onError, tryAgain);
}
public static void Command(string title, Action<MySqlCommand> action) {
SQL(title, (sql) => {
var c = new MySqlCommand();
c.Connection = sql.Connection;
try {
c.Connection.Open();
action.Invoke(c);
} catch (Exception ex) {
throw ex;
} finally {
c.Connection.Close();
}
});
}
public static void DataAdapter(string title, string query, Action<MySqlDataAdapter> action) {
Command(title, (co) => {
var da = new MySqlDataAdapter();
co.CommandText = query;
da.SelectCommand = co;
action.Invoke(da);
});
}
}