-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathMySqlDateTimeTester.aspx.cs
More file actions
768 lines (663 loc) · 32.7 KB
/
Copy pathMySqlDateTimeTester.aspx.cs
File metadata and controls
768 lines (663 loc) · 32.7 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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace System.pages
{
public partial class MySqlDateTimeTester : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btTestMicroseconds_Click(object sender, EventArgs e)
{
TimePrecisionTester t = new TimePrecisionTester(config.ConnString);
string result = t.RunTest();
ltResult.Text = result;
}
protected void btTestFull_Click(object sender, EventArgs e)
{
MySqlDateTimeTestHelper t = new MySqlDateTimeTestHelper(config.ConnString);
string output = t.RunTest();
ltResult.Text = output;
}
}
public class TimePrecisionTester
{
private StringBuilder output;
private string connectionString;
public TimePrecisionTester(string connStr = null)
{
connectionString = connStr;
output = new StringBuilder();
}
public string RunTest(string connStr = null)
{
if (!string.IsNullOrEmpty(connStr))
connectionString = connStr;
if (string.IsNullOrEmpty(connectionString))
{
return "Error: Connection string not provided";
}
output.Clear();
Random rd = new Random();
string dbName = $"test_mysqldatetime_{rd.Next(1000,9999)}";
try
{
using (var conn = new MySqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
using (var mb = new MySqlBackup(cmd))
{
conn.Open();
cmd.CommandText = $"DROP DATABASE IF EXISTS `{dbName}`";
cmd.ExecuteNonQuery();
cmd.CommandText = $"CREATE DATABASE IF NOT EXISTS `{dbName}`";
cmd.ExecuteNonQuery();
cmd.CommandText = $"USE `{dbName}`";
cmd.ExecuteNonQuery();
AppendLine("=== SETTING TIMEZONE CONSISTENCY ===");
cmd.CommandText = "SET time_zone = '+00:00'";
cmd.ExecuteNonQuery();
AppendLine("Timezone set to UTC (+00:00)");
AppendLine();
// Step 1: Create specialized table for TIME precision testing
AppendLine("=== CREATING TEST TABLE ===");
cmd.CommandText = @"
DROP TABLE IF EXISTS time_precision_test;
CREATE TABLE time_precision_test (
id INT PRIMARY KEY,
time_col_0 TIME,
time_col_3 TIME(3),
time_col_6 TIME(6)
) ENGINE=InnoDB;
";
cmd.ExecuteNonQuery();
AppendLine("Table created successfully");
AppendLine();
// Step 2: Insert test data with microsecond precision
AppendLine("=== INSERTING TEST DATA ===");
cmd.CommandText = @"
INSERT INTO time_precision_test VALUES
(1, '14:30:45', '14:30:45.123', '14:30:45.123456'),
(2, '23:59:59', '23:59:59.999', '23:59:59.999999'),
(3, '00:00:00', '00:00:00.000', '00:00:00.000000');
";
cmd.ExecuteNonQuery();
AppendLine("Test data inserted successfully");
AppendLine();
// Step 3: Use MySqlDataReader to fetch and analyze the data types
cmd.CommandText = "SELECT * FROM time_precision_test ORDER BY id";
using (var reader = cmd.ExecuteReader())
{
// Get column schema information
var schemaTable = reader.GetSchemaTable();
AppendLine("=== COLUMN SCHEMA ANALYSIS ===");
foreach (DataRow row in schemaTable.Rows)
{
string columnName = row["ColumnName"].ToString();
string dataType = row["DataType"].ToString();
string providerType = row["ProviderType"].ToString();
AppendLine($"Column: {columnName}");
AppendLine($" .NET Type: {dataType}");
AppendLine($" Provider Type: {providerType}");
AppendLine();
}
AppendLine("=== DATA VALUES ANALYSIS ===");
while (reader.Read())
{
int id = reader.GetInt32(0);
AppendLine($"Row {id}:");
for (int i = 1; i < reader.FieldCount; i++)
{
string columnName = reader.GetName(i);
object value = reader.GetValue(i);
AppendLine($" {columnName}:");
AppendLine($" Value: {value}");
AppendLine($" Type: {value?.GetType()?.FullName ?? "NULL"}");
// Special handling for different types
if (value is TimeSpan ts)
{
AppendLine($" TimeSpan Details:");
AppendLine($" TotalHours: {ts.TotalHours}");
AppendLine($" Hours: {ts.Hours}");
AppendLine($" Minutes: {ts.Minutes}");
AppendLine($" Seconds: {ts.Seconds}");
AppendLine($" Milliseconds: {ts.Milliseconds}");
AppendLine($" Ticks: {ts.Ticks}");
AppendLine($" Microseconds: {ts.Ticks / 10}");
AppendLine($" Microseconds (mod 1M): {(ts.Ticks / 10) % 1000000}");
}
else if (value is MySqlDateTime mdt)
{
AppendLine($" MySqlDateTime Details:");
AppendLine($" Hour: {mdt.Hour}");
AppendLine($" Minute: {mdt.Minute}");
AppendLine($" Second: {mdt.Second}");
AppendLine($" Microsecond: {mdt.Microsecond}");
AppendLine($" IsValidDateTime: {mdt.IsValidDateTime}");
}
else if (value is DateTime dt)
{
AppendLine($" DateTime Details:");
AppendLine($" TimeOfDay: {dt.TimeOfDay}");
AppendLine($" Ticks: {dt.Ticks}");
}
AppendLine();
}
AppendLine("------------------------");
}
}
// Step 4: Test MySqlBackup export behavior
AppendLine("=== TESTING MYSQLBACKUP EXPORT ===");
// Export the table
string exportedSql = mb.ExportToString();
AppendLine("Exported SQL:");
AppendLine(exportedSql);
AppendLine();
// Look for INSERT statements specifically
string[] lines = exportedSql.Split('\n');
foreach (string line in lines)
{
if (line.Trim().StartsWith("INSERT INTO") && line.Contains("time_precision_test"))
{
AppendLine("INSERT Statement Found:");
AppendLine(line);
AppendLine();
break;
}
}
// Step 5: Test individual column metadata
AppendLine("=== COLUMN METADATA ANALYSIS ===");
cmd.CommandText = @"
SELECT COLUMN_NAME, DATA_TYPE, DATETIME_PRECISION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'time_precision_test'
AND TABLE_SCHEMA = DATABASE()
ORDER BY ORDINAL_POSITION
";
using (var metaReader = cmd.ExecuteReader())
{
while (metaReader.Read())
{
AppendLine($"Column: {metaReader.GetString(0)}");
AppendLine($" Data Type: {metaReader.GetString(1)}");
AppendLine($" Precision: {(metaReader.IsDBNull(2) ? "NULL" : metaReader.GetValue(2).ToString())}");
AppendLine();
}
}
// Step 6: Test field type information
AppendLine("=== FIELD TYPE INFORMATION ===");
TestFieldTypes(cmd);
// Cleanup
AppendLine("=== CLEANUP ===");
cmd.CommandText = "DROP TABLE time_precision_test";
cmd.ExecuteNonQuery();
AppendLine("Test table dropped successfully");
}
}
catch (Exception ex)
{
AppendLine($"ERROR: {ex.Message}");
AppendLine($"Stack Trace: {ex.StackTrace}");
}
return output.ToString();
}
private void TestFieldTypes(MySqlCommand cmd)
{
cmd.CommandText = "SELECT time_col_6 FROM time_precision_test WHERE id = 1";
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
var fieldType = reader.GetFieldType(0);
var dataTypeName = reader.GetDataTypeName(0);
AppendLine($"TIME(6) Column Analysis:");
AppendLine($" Field Type: {fieldType}");
AppendLine($" Data Type Name: {dataTypeName}");
// Get the actual value and analyze it
object value = reader.GetValue(0);
AppendLine($" Actual Value: {value}");
AppendLine($" Actual Type: {value?.GetType()?.FullName}");
if (value is TimeSpan ts)
{
AppendLine($" TimeSpan Ticks: {ts.Ticks}");
AppendLine($" TimeSpan Microseconds: {ts.Ticks / 10}");
AppendLine($" TimeSpan String: {ts}");
}
}
}
}
private void AppendLine(string text = "")
{
output.AppendLine(text);
}
}
public class MySqlDateTimeTestHelper
{
private StringBuilder output;
private string connectionString;
public MySqlDateTimeTestHelper(string connStr = null)
{
connectionString = connStr;
output = new StringBuilder();
}
public string RunTest(string connStr = null)
{
if (!string.IsNullOrEmpty(connStr))
connectionString = connStr;
if (string.IsNullOrEmpty(connectionString))
{
return "Error: Connection string not provided";
}
output.Clear();
Random rd = new Random();
string dbName = $"test_mysqldatetime_{rd.Next(1000, 9999)}";
try
{
using (var conn = new MySqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
using (var mb = new MySqlBackup(cmd))
{
conn.Open();
cmd.CommandText = $"DROP DATABASE IF EXISTS `{dbName}`";
cmd.ExecuteNonQuery();
cmd.CommandText = $"CREATE DATABASE IF NOT EXISTS `{dbName}`";
cmd.ExecuteNonQuery();
cmd.CommandText = $"USE `{dbName}`";
cmd.ExecuteNonQuery();
// adjust the session time zone to UTC to export actual value without time zone offset
cmd.CommandText = "SET time_zone = '+00:00'";
cmd.ExecuteNonQuery();
// Step 1: Create comprehensive test table
AppendLine("=== CREATING COMPREHENSIVE TEST TABLE ===");
cmd.CommandText = @"
DROP TABLE IF EXISTS datetime_precision_test;
CREATE TABLE datetime_precision_test (
id INT PRIMARY KEY,
-- DATE columns
date_col DATE,
date_null DATE,
-- TIME columns with different precisions
time_col_0 TIME,
time_col_3 TIME(3),
time_col_6 TIME(6),
time_null_0 TIME,
time_null_3 TIME(3),
time_null_6 TIME(6),
-- DATETIME columns with different precisions
datetime_col_0 DATETIME,
datetime_col_3 DATETIME(3),
datetime_col_6 DATETIME(6),
datetime_null_0 DATETIME,
datetime_null_3 DATETIME(3),
datetime_null_6 DATETIME(6),
-- TIMESTAMP columns with different precisions
timestamp_col_0 TIMESTAMP,
timestamp_col_3 TIMESTAMP(3),
timestamp_col_6 TIMESTAMP(6),
timestamp_null_0 TIMESTAMP NULL,
timestamp_null_3 TIMESTAMP(3) NULL,
timestamp_null_6 TIMESTAMP(6) NULL
) ENGINE=InnoDB;
";
cmd.ExecuteNonQuery();
AppendLine("Comprehensive test table created successfully");
AppendLine();
// Step 2: Insert comprehensive test data
AppendLine("=== INSERTING COMPREHENSIVE TEST DATA ===");
cmd.CommandText = @"
INSERT INTO datetime_precision_test VALUES
(
1,
-- DATE columns
'2024-01-15', NULL,
-- TIME columns (non-null)
'14:30:45', '14:30:45.123', '14:30:45.123456',
NULL, NULL, NULL,
-- DATETIME columns (non-null)
'2024-01-15 14:30:45', '2024-01-15 14:30:45.123', '2024-01-15 14:30:45.123456',
NULL, NULL, NULL,
-- TIMESTAMP columns (non-null) ✅ SAFE VALUES
'2024-01-15 14:30:45', '2024-01-15 14:30:45.123', '2024-01-15 14:30:45.123456',
NULL, NULL, NULL
),
(
2,
-- DATE columns
'2023-12-31', NULL,
-- TIME columns (edge case: 23:59:59)
'23:59:59', '23:59:59.999', '23:59:59.999999',
NULL, NULL, NULL,
-- DATETIME columns (edge case: year-end)
'2023-12-31 23:59:59', '2023-12-31 23:59:59.999', '2023-12-31 23:59:59.999999',
NULL, NULL, NULL,
-- TIMESTAMP columns (edge case: year-end) ✅ SAFE VALUES
'2023-12-31 15:59:59', '2023-12-31 15:59:59.999', '2023-12-31 15:59:59.999999',
NULL, NULL, NULL
),
(
3,
-- DATE columns
'1000-01-01', NULL,
-- TIME columns (edge case: midnight)
'00:00:00', '00:00:00.000', '00:00:00.000000',
NULL, NULL, NULL,
-- DATETIME columns (edge case: minimum date)
'1000-01-01 00:00:00', '1000-01-01 00:00:00.000', '1000-01-01 00:00:00.000000',
NULL, NULL, NULL,
-- TIMESTAMP columns ✅ SAFE MINIMUM (accounting for +08:00 timezone)
'1970-01-01 08:30:01', '1970-01-01 08:30:01.000', '1970-01-01 08:30:01.000000',
NULL, NULL, NULL
),
(
4,
-- DATE columns
'9999-12-31', NULL,
-- TIME columns (edge case: negative time)
'-838:59:59', '-838:59:59.000', '-838:59:59.000000',
NULL, NULL, NULL,
-- DATETIME columns (edge case: maximum date)
'9999-12-31 23:59:59', '9999-12-31 23:59:59.999', '9999-12-31 23:59:59.999999',
NULL, NULL, NULL,
-- TIMESTAMP columns ✅ SAFE MAXIMUM
'2038-01-19 02:14:06', '2038-01-19 02:14:06.999', '2038-01-19 02:14:06.999999',
NULL, NULL, NULL
);
";
cmd.ExecuteNonQuery();
AppendLine("Comprehensive test data inserted successfully");
AppendLine();
// Step 3: Analyze column schema
cmd.CommandText = "SELECT * FROM datetime_precision_test ORDER BY id";
using (var reader = cmd.ExecuteReader())
{
var schemaTable = reader.GetSchemaTable();
AppendLine("=== COMPREHENSIVE COLUMN SCHEMA ANALYSIS ===");
foreach (DataRow row in schemaTable.Rows)
{
string columnName = row["ColumnName"].ToString();
string dataType = row["DataType"].ToString();
string providerType = row["ProviderType"].ToString();
bool allowDBNull = (bool)row["AllowDBNull"];
AppendLine($"Column: {columnName}");
AppendLine($" .NET Type: {dataType}");
AppendLine($" Provider Type: {providerType}");
AppendLine($" Allow Null: {allowDBNull}");
AppendLine();
}
AppendLine("=== COMPREHENSIVE DATA VALUES ANALYSIS ===");
while (reader.Read())
{
int id = reader.GetInt32(0);
AppendLine($"=== ROW {id} ANALYSIS ===");
for (int i = 1; i < reader.FieldCount; i++)
{
string columnName = reader.GetName(i);
object value = reader.GetValue(i);
bool isNull = reader.IsDBNull(i);
AppendLine($"Column: {columnName}");
AppendLine($" Is Null: {isNull}");
if (!isNull)
{
AppendLine($" Value: '{value}'");
AppendLine($" Type: {value?.GetType()?.FullName}");
// Detailed analysis based on type
if (value is TimeSpan ts)
{
AppendLine($" TimeSpan Analysis:");
AppendLine($" TotalHours: {ts.TotalHours}");
AppendLine($" Hours: {ts.Hours}");
AppendLine($" Minutes: {ts.Minutes}");
AppendLine($" Seconds: {ts.Seconds}");
AppendLine($" Milliseconds: {ts.Milliseconds}");
AppendLine($" Ticks: {ts.Ticks}");
AppendLine($" Microseconds: {ts.Ticks / 10}");
AppendLine($" Microseconds (fractional): {(ts.Ticks / 10) % 1000000}");
AppendLine($" String representation: {ts}");
}
else if (value is MySqlDateTime mdt)
{
AppendLine($" MySqlDateTime Analysis:");
AppendLine($" Year: {mdt.Year}");
AppendLine($" Month: {mdt.Month}");
AppendLine($" Day: {mdt.Day}");
AppendLine($" Hour: {mdt.Hour}");
AppendLine($" Minute: {mdt.Minute}");
AppendLine($" Second: {mdt.Second}");
AppendLine($" Microsecond: {mdt.Microsecond}");
AppendLine($" IsValidDateTime: {mdt.IsValidDateTime}");
AppendLine($" String representation: {mdt}");
if (mdt.IsValidDateTime)
{
try
{
DateTime dt = mdt.GetDateTime();
AppendLine($" As DateTime: {dt}");
AppendLine($" DateTime Ticks: {dt.Ticks}");
}
catch (Exception ex)
{
AppendLine($" GetDateTime() Error: {ex.Message}");
}
}
}
else if (value is DateTime dt)
{
AppendLine($" DateTime Analysis:");
AppendLine($" Date: {dt.Date}");
AppendLine($" TimeOfDay: {dt.TimeOfDay}");
AppendLine($" Ticks: {dt.Ticks}");
AppendLine($" Millisecond: {dt.Millisecond}");
AppendLine($" String representation: {dt}");
AppendLine($" ISO format: {dt:yyyy-MM-dd HH:mm:ss.ffffff}");
}
else
{
AppendLine($" Generic Value Analysis:");
AppendLine($" ToString(): {value}");
}
}
else
{
AppendLine($" Value: NULL");
}
AppendLine();
}
AppendLine("=" + new string('=', 50));
}
}
// Step 4: Test MySqlBackup export behavior
AppendLine("=== TESTING MYSQLBACKUP EXPORT BEHAVIOR ===");
string exportedSql = mb.ExportToString();
AppendLine("Looking for INSERT statements...");
string[] lines = exportedSql.Split('\n');
bool foundInsert = false;
foreach (string line in lines)
{
if (line.Trim().StartsWith("INSERT INTO") && line.Contains("datetime_precision_test"))
{
AppendLine("INSERT Statement Found:");
AppendLine(line);
AppendLine();
foundInsert = true;
}
}
if (!foundInsert)
{
AppendLine("No INSERT statements found. Full export:");
AppendLine(exportedSql);
}
// Step 5: Column metadata analysis
AppendLine("=== COLUMN METADATA FROM INFORMATION_SCHEMA ===");
cmd.CommandText = @"
SELECT
COLUMN_NAME,
DATA_TYPE,
DATETIME_PRECISION,
IS_NULLABLE,
COLUMN_DEFAULT,
COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'datetime_precision_test'
AND TABLE_SCHEMA = DATABASE()
ORDER BY ORDINAL_POSITION
";
using (var metaReader = cmd.ExecuteReader())
{
while (metaReader.Read())
{
AppendLine($"Column: {metaReader.GetString("COLUMN_NAME")}");
AppendLine($" Data Type: {metaReader.GetString("DATA_TYPE")}");
AppendLine($" Column Type: {metaReader.GetString("COLUMN_TYPE")}");
// ✅ FIX: Use column names directly with proper null checking
string precision = metaReader["DATETIME_PRECISION"] == DBNull.Value ? "NULL" : metaReader["DATETIME_PRECISION"].ToString();
string nullable = metaReader["IS_NULLABLE"] == DBNull.Value ? "NULL" : metaReader["IS_NULLABLE"].ToString();
string defaultVal = metaReader["COLUMN_DEFAULT"] == DBNull.Value ? "NULL" : metaReader["COLUMN_DEFAULT"].ToString();
AppendLine($" Precision: {precision}");
AppendLine($" Nullable: {nullable}");
AppendLine($" Default: {defaultVal}");
AppendLine();
}
}
// Step 6: Test individual data type handling
AppendLine("=== INDIVIDUAL TYPE TESTING ===");
TestSpecificTypes(cmd);
// Step 7: Test edge cases
AppendLine("=== EDGE CASE TESTING ===");
TestEdgeCases(cmd);
// Cleanup
AppendLine("=== CLEANUP ===");
cmd.CommandText = "DROP TABLE datetime_precision_test";
cmd.ExecuteNonQuery();
AppendLine("Test table dropped successfully");
}
}
catch (Exception ex)
{
AppendLine($"ERROR: {ex.Message}");
}
return output.ToString();
}
private void TestSpecificTypes(MySqlCommand cmd)
{
// Test TIME(6) specifically
AppendLine("--- TIME(6) Specific Test ---");
cmd.CommandText = "SELECT time_col_6 FROM datetime_precision_test WHERE id = 1";
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
var fieldType = reader.GetFieldType(0);
var dataTypeName = reader.GetDataTypeName(0);
object value = reader.GetValue(0);
AppendLine($"TIME(6) Field Analysis:");
AppendLine($" Field Type: {fieldType}");
AppendLine($" Data Type Name: {dataTypeName}");
AppendLine($" Value: {value}");
AppendLine($" .NET Type: {value?.GetType()?.FullName}");
if (value is TimeSpan ts)
{
AppendLine($" TimeSpan Details:");
AppendLine($" Original: {ts}");
AppendLine($" Ticks: {ts.Ticks}");
AppendLine($" Microseconds: {ts.Ticks / 10}");
AppendLine($" Fractional Microseconds: {(ts.Ticks / 10) % 1000000}");
}
}
}
AppendLine();
// Test DATETIME(6) specifically
AppendLine("--- DATETIME(6) Specific Test ---");
cmd.CommandText = "SELECT datetime_col_6 FROM datetime_precision_test WHERE id = 1";
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
var fieldType = reader.GetFieldType(0);
var dataTypeName = reader.GetDataTypeName(0);
object value = reader.GetValue(0);
AppendLine($"DATETIME(6) Field Analysis:");
AppendLine($" Field Type: {fieldType}");
AppendLine($" Data Type Name: {dataTypeName}");
AppendLine($" Value: {value}");
AppendLine($" .NET Type: {value?.GetType()?.FullName}");
if (value is DateTime dt)
{
AppendLine($" DateTime Details:");
AppendLine($" Original: {dt}");
AppendLine($" Ticks: {dt.Ticks}");
AppendLine($" Microseconds: {dt.Ticks / 10}");
}
else if (value is MySqlDateTime mdt)
{
AppendLine($" MySqlDateTime Details:");
AppendLine($" Microsecond: {mdt.Microsecond}");
}
}
}
AppendLine();
}
private void TestEdgeCases(MySqlCommand cmd)
{
// Test NULL values
AppendLine("--- NULL Values Test ---");
cmd.CommandText = "SELECT time_null_6, datetime_null_6 FROM datetime_precision_test WHERE id = 1";
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
AppendLine($"NULL TIME(6): IsDBNull = {reader.IsDBNull(0)}, Value = {(reader.IsDBNull(0) ? "NULL" : reader.GetValue(0))}");
AppendLine($"NULL DATETIME(6): IsDBNull = {reader.IsDBNull(1)}, Value = {(reader.IsDBNull(1) ? "NULL" : reader.GetValue(1))}");
}
}
AppendLine();
// Test negative TIME values
AppendLine("--- Negative TIME Values Test ---");
cmd.CommandText = "SELECT time_col_6 FROM datetime_precision_test WHERE id = 4";
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
object value = reader.GetValue(0);
AppendLine($"Negative TIME Value: {value}");
AppendLine($"Type: {value?.GetType()?.FullName}");
if (value is TimeSpan ts)
{
AppendLine($"TimeSpan: {ts}");
AppendLine($"TotalHours: {ts.TotalHours}");
AppendLine($"IsNegative: {ts < TimeSpan.Zero}");
}
}
}
AppendLine();
// Test precision variations
AppendLine("--- Precision Variations Test ---");
cmd.CommandText = "SELECT time_col_0, time_col_3, time_col_6 FROM datetime_precision_test WHERE id = 1";
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
for (int i = 0; i < 3; i++)
{
object value = reader.GetValue(i);
string colName = reader.GetName(i);
AppendLine($"{colName}: {value} (Type: {value?.GetType()?.Name})");
if (value is TimeSpan ts)
{
AppendLine($" Microseconds: {(ts.Ticks / 10) % 1000000}");
}
}
}
}
}
private void AppendLine(string text = "")
{
output.AppendLine(text);
}
}
}