forked from CodeSpartan/MMOKitPersistenceServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
183 lines (159 loc) · 4.83 KB
/
ExtensionMethods.cs
File metadata and controls
183 lines (159 loc) · 4.83 KB
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
using Microsoft.Data.Sqlite;
using MySqlConnector;
using System.Data;
using System.Data.Common;
using System.Text;
namespace PersistenceServer
{
public static class ExtensionMethods
{
public static string ReadMmoString(this BinaryReader reader)
{
try
{
int strLen = reader.ReadInt32();
var stringbytes = reader.ReadBytes(strLen);
string result = Encoding.UTF8.GetString(stringbytes);
return result;
}
catch {
return "";
}
}
/*
* DataTable extensions
*/
public static bool HasRows(this DataTable dt)
{
return dt.Rows.Count > 0;
}
public static void PrintColumnNames(this DataTable dt)
{
List<string> columns = new();
foreach(DataColumn column in dt.Columns)
{
columns.Add(column.ColumnName);
}
Console.WriteLine(string.Join(", ", columns));
}
public static int? GetBigInt(this DataTable dt, int row, string column)
{
return (int?)dt.Rows[row].Field<long?>(column);
}
public static int? GetBigInt(this DataTable dt, int row, int column)
{
return (int?)dt.Rows[row].Field<long?>(column);
}
public static int? GetInt(this DataTable dt, int row, string column)
{
// annoyingly enough, sqlite's integer is of different (8 bytes) size from mysql's (4 bytes)
try
{
return dt.Rows[row].Field<int?>(column);
}
catch
{
return dt.GetBigInt(row, column);
}
}
public static int? GetInt(this DataTable dt, int row, int column)
{
// annoyingly enough, sqlite's integer is of different (8 bytes) size from mysql's (4 bytes)
try
{
return dt.Rows[row].Field<int?>(column);
}
catch
{
return dt.GetBigInt(row, column);
}
}
public static string? GetString(this DataTable dt, int row, string column)
{
return dt.Rows[row].Field<string?>(column);
}
public static string? GetString(this DataTable dt, int row, int column)
{
return dt.Rows[row].Field<string?>(column);
}
public static byte[] GetBinaryArray(this DataTable dt, int row, string column)
{
return (byte[])dt.Rows[row][column];
}
public static byte[] GetBinaryArray(this DataTable dt, int row, int column)
{
return (byte[])dt.Rows[row][column];
}
/*
* ~DataTable
*/
/*
* DataRow extensions
*/
public static string? GetString(this DataRow dr, string column)
{
return dr.Field<string?>(column);
}
public static string? GetString(this DataRow dr, int column)
{
return dr.Field<string?>(column);
}
public static int? GetBigInt(this DataRow dr, string column)
{
return (int?)dr.Field<long?>(column);
}
public static int? GetBigInt(this DataRow dr, int column)
{
return (int?)dr.Field<long?>(column);
}
public static int? GetInt(this DataRow dr, string column)
{
try
{
return dr.Field<int?>(column);
}
catch
{
return dr.GetBigInt(column);
}
}
public static int? GetInt(this DataRow dr, int column)
{
try
{
return dr.Field<int?>(column);
}
catch
{
return dr.GetBigInt(column);
}
}
/*
* ~DataRow
*/
public static void AddParam(this DbCommand? cmd, string param, object? value)
{
if (cmd is MySqlCommand mysqlCmd)
{
mysqlCmd.Parameters.AddWithValue(param, value);
return;
}
if (cmd is SqliteCommand sqliteCmd)
{
sqliteCmd.Parameters.AddWithValue(param, value);
return;
}
throw new Exception("AddParam is not defined for your Sql Type");
}
// not tested
//public static bool GetBool(this DataTable dt, int row, string column)
//{
// return dt.Rows[row].Field<bool>(column);
//}
// not tested
//public static float GetFloat(this DataTable dt, int row, string column)
//{
// return dt.Rows[row].Field<float>(column);
//}
}
}