-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathTotalRows.aspx.cs
More file actions
289 lines (253 loc) · 11.3 KB
/
Copy pathTotalRows.aspx.cs
File metadata and controls
289 lines (253 loc) · 11.3 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using MySqlConnector;
using System.IO;
namespace System.pages
{
public partial class TotalRows : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
void LoadData()
{
try
{
string databaseName = "";
string datadir = "";
StringBuilder sb = new StringBuilder();
DataTable dtTables = null;
long totalRows = 0;
long totalBytes = 0;
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
{
conn.Open();
databaseName = QueryExpress.ExecuteScalarStr(cmd, "select database();");
datadir = QueryExpress.ExecuteScalarStr(cmd, "SHOW GLOBAL VARIABLES LIKE 'datadir'", 1);
dtTables = QueryExpress.GetTable(cmd, $"SHOW FULL TABLES WHERE Table_type = 'BASE TABLE';");
dtTables.Columns.Add("total_rows", typeof(long));
foreach (DataRow dr in dtTables.Rows)
{
string tableName = dr[0] + "";
long totalRowsTable = QueryExpress.ExecuteScalarLong(cmd, $"select count(*) from `{QueryExpress.EscapeIdentifier(tableName)}`");
dr["total_rows"] = totalRowsTable;
totalRows += totalRowsTable;
}
}
// Calculate database size by examining files in the database directory
try
{
string databasePath = Path.Combine(datadir, databaseName);
sb.AppendLine($"<p>Database Folder: {databasePath}</p>");
if (Directory.Exists(databasePath))
{
DirectoryInfo dbDir = new DirectoryInfo(databasePath);
foreach (var file in dbDir.EnumerateFiles())
{
totalBytes += file.Length;
}
}
}
catch (Exception ex)
{
// Handle potential access issues
sb.AppendLine($"<!-- Error calculating database size: {ex.Message} -->");
}
// Build the output
sb.AppendLine($"<h3>Database Information</h3>");
sb.AppendLine($"<p><strong>Database Name:</strong> {databaseName}</p>");
sb.AppendLine($"<p><strong>Total Size:</strong> {FormatBytes(totalBytes)} ({totalBytes:N0} bytes)</p>");
sb.AppendLine($"<p><strong>Total Rows:</strong> {totalRows:N0}</p>");
// Generate HTML table with table information
sb.AppendLine("<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>");
sb.AppendLine("<thead>");
sb.AppendLine("<tr>");
sb.AppendLine("<th>Table Name</th>");
sb.AppendLine("<th>Total Rows</th>");
sb.AppendLine("</tr>");
sb.AppendLine("</thead>");
sb.AppendLine("<tbody>");
foreach (DataRow dr in dtTables.Rows)
{
string tableName = dr[0]?.ToString() ?? "";
long tableRows = Convert.ToInt64(dr["total_rows"]);
sb.AppendLine("<tr>");
sb.AppendLine($"<td>{System.Web.HttpUtility.HtmlEncode(tableName)}</td>");
sb.AppendLine($"<td>{tableRows:N0}</td>");
sb.AppendLine("</tr>");
}
sb.AppendLine("</tbody>");
sb.AppendLine("</table>");
ltResult.Text = sb.ToString();
}
catch (Exception ex)
{
ltResult.Text = $"Error: {ex.Message}";
}
}
void LoadAllDatabasesData()
{
try
{
string datadir = "";
StringBuilder sb = new StringBuilder();
DataTable dtDatabases = null;
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
{
conn.Open();
// Get the data directory
datadir = QueryExpress.ExecuteScalarStr(cmd, "SHOW GLOBAL VARIABLES LIKE 'datadir'", 1);
// Get list of all databases
dtDatabases = QueryExpress.GetTable(cmd, "SHOW DATABASES;");
// Add columns for our calculated data
dtDatabases.Columns.Add("total_rows", typeof(long));
dtDatabases.Columns.Add("total_bytes", typeof(long));
dtDatabases.Columns.Add("formatted_size", typeof(string));
foreach (DataRow dr in dtDatabases.Rows)
{
string databaseName = dr[0]?.ToString() ?? "";
long totalRows = 0;
long totalBytes = 0;
// Skip system databases if desired (optional)
if (databaseName.Equals("information_schema", StringComparison.OrdinalIgnoreCase) ||
databaseName.Equals("performance_schema", StringComparison.OrdinalIgnoreCase) ||
databaseName.Equals("mysql", StringComparison.OrdinalIgnoreCase) ||
databaseName.Equals("sys", StringComparison.OrdinalIgnoreCase))
{
dr["total_rows"] = 0;
dr["total_bytes"] = 0;
dr["formatted_size"] = "N/A (System DB)";
continue;
}
try
{
// Switch to the database
cmd.CommandText = $"USE `{QueryExpress.EscapeIdentifier(databaseName)}`;";
cmd.ExecuteNonQuery();
// Get all tables in this database
DataTable dtTables = QueryExpress.GetTable(cmd, "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE';");
// Count total rows in all tables
foreach (DataRow tableRow in dtTables.Rows)
{
string tableName = tableRow[0]?.ToString() ?? "";
if (!string.IsNullOrEmpty(tableName))
{
try
{
long tableRows = QueryExpress.ExecuteScalarLong(cmd, $"SELECT COUNT(*) FROM `{QueryExpress.EscapeIdentifier(tableName)}`;");
totalRows += tableRows;
}
catch (Exception tableEx)
{
// Skip tables that can't be counted (views, etc.)
continue;
}
}
}
// Calculate database size by examining files in the database directory
try
{
string databasePath = Path.Combine(datadir, databaseName);
if (Directory.Exists(databasePath))
{
DirectoryInfo dbDir = new DirectoryInfo(databasePath);
foreach (var file in dbDir.EnumerateFiles())
{
totalBytes += file.Length;
}
}
}
catch (Exception)
{
// Handle potential access issues silently
}
}
catch (Exception dbEx)
{
// Handle database access issues
totalRows = 0;
totalBytes = 0;
}
dr["total_rows"] = totalRows;
dr["total_bytes"] = totalBytes;
dr["formatted_size"] = FormatBytes(totalBytes);
}
}
// Build the output HTML
sb.AppendLine($@"
<h3>All Databases Information</h3>
<p><strong>Data Directory:</strong> {datadir}</p>
<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>
<thead>
<tr>
<th>Database Name</th>
<th>Total Rows</th>
<th>Size (Formatted)</th>
<th>Size (Bytes)</th>
</tr>
</thead>
<tbody>
");
foreach (DataRow dr in dtDatabases.Rows)
{
string databaseName = dr[0]?.ToString() ?? "";
long totalRows = Convert.ToInt64(dr["total_rows"]);
long totalBytes = Convert.ToInt64(dr["total_bytes"]);
string formattedSize = dr["formatted_size"]?.ToString() ?? "";
sb.AppendLine($@"
<tr>
<td>{System.Web.HttpUtility.HtmlEncode(databaseName)}</td>
<td>{totalRows:N0}</td>
<td>{System.Web.HttpUtility.HtmlEncode(formattedSize)}</td>
<td>{totalBytes:N0}</td>
</tr>
");
}
sb.AppendLine($@"
</tbody>
</table>
");
ltResult.Text = sb.ToString();
}
catch (Exception ex)
{
ltResult.Text = $"Error: {ex.Message}";
}
}
protected void btGetTotalRows_Click(object sender, EventArgs e)
{
LoadData();
}
string FormatBytes(long bytes)
{
if (bytes >= 1024 * 1024 * 1024) // GB
{
return $"{(bytes / (1024.0 * 1024.0 * 1024.0)):0.###}GB";
}
else if (bytes >= 1024 * 1024) // MB
{
return $"{(bytes / (1024.0 * 1024.0)):0.###}MB";
}
else if (bytes >= 1024) // KB
{
return $"{(bytes / 1024.0):0.###}KB";
}
else
{
return $"{bytes} bytes";
}
}
protected void btGetAllDatabases_Click(object sender, EventArgs e)
{
LoadAllDatabasesData();
}
}
}