Implemented the MySqlColumn converter and added more example code. Improved documentation. Added new DeleteAll function.
This commit is contained in:
@ -20,18 +20,51 @@ namespace MontoyaTech.MySqlPlus.Example
|
||||
[MySqlColumn("year")]
|
||||
public uint Year = 0;
|
||||
|
||||
//[MySqlColumn("dateCreated", typeof(DateTime2UnixConverter))]
|
||||
//public DateTime DateCreated = DateTime.UtcNow;
|
||||
[MySqlColumn("dateCreated", typeof(DateTime2UnixConverter))]
|
||||
public DateTime DateCreated = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public class DateTime2UnixConverter
|
||||
public class DateTime2UnixConverter : MySqlColumnConverter
|
||||
{
|
||||
public object ConvertFrom(object input)
|
||||
{
|
||||
if (input is ulong unix)
|
||||
{
|
||||
if (unix == 0)
|
||||
return DateTime.MinValue;
|
||||
|
||||
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unix).ToUniversalTime();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("Unsupported input object to convert from unix.");
|
||||
}
|
||||
}
|
||||
|
||||
public object ConvertTo(object input)
|
||||
{
|
||||
if (input is DateTime dateTime)
|
||||
{
|
||||
if (dateTime == DateTime.MinValue)
|
||||
return 0;
|
||||
|
||||
if (dateTime == DateTime.MaxValue)
|
||||
return ulong.MaxValue;
|
||||
|
||||
return (ulong)(dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))).TotalSeconds;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("Unsupported input object to convert to unix.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var session = new MySqlSession("server=db.zone2d.com;user=root;database=zone2d;port=3306;password=-+W6!?Kv-6wDL2Vj5f=kC^Q&;SslMode=Required");
|
||||
var session = new MySqlSession("");
|
||||
|
||||
session.DeleteAll<Car>();
|
||||
|
||||
session.Insert(new Car() { Make = "Chevy", Model = "Camaro", Year = 2011 });
|
||||
|
||||
@ -40,7 +73,7 @@ namespace MontoyaTech.MySqlPlus.Example
|
||||
var cars = session.GetAll<Car>();
|
||||
|
||||
foreach (var car in cars)
|
||||
Console.WriteLine($"Make: {car.Make}, Model: {car.Model}, Year: {car.Year}");
|
||||
Console.WriteLine($"Make: {car.Make}, Model: {car.Model}, Year: {car.Year}, DateCreated: {car.DateCreated}");
|
||||
|
||||
cars[0].Make = "test";
|
||||
|
||||
|
Reference in New Issue
Block a user