You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OMS.NET/DbClass/MapData.cs

155 lines
8.3 KiB
C#

using MySql.Data.MySqlClient;
namespace OMS.NET.DbClass
{
public class MapData
{
#region base
public long Id { get; set; }
public string Type { get; set; }
public string Points { get; set; }
public string Point { get; set; }
public string? Color { get; set; }
public int Phase { get; set; }
public int? Width { get; set; }
public string? ChildRelations { get; set; }
public string? FatherRelations { get; set; }
public string? ChildNodes { get; set; }
public string? FatherNode { get; set; }
public string? Details { get; set; }
public string? Custom { get; set; }
public MapData()
{
this.Id = -1;
this.Type = "";
this.Points = "";
this.Point = "";
this.Phase = 1;
}
#endregion
public static void Add(MapData mapData)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"INSERT INTO map_0_data (type, points, point, color, phase, width, child_relations, father_relations, child_nodes, father_node, details, custom)
VALUES (@Type, @Points, @Point, @Color, @Phase, @Width, @ChildRelations, @FatherRelations, @ChildNodes, @FatherNode, @Details, @Custom)";
using MySqlCommand command = new(query, connection);
command.Parameters.AddWithValue("@Type", mapData.Type);
command.Parameters.AddWithValue("@Points", mapData.Points);
command.Parameters.AddWithValue("@Point", mapData.Point);
command.Parameters.AddWithValue("@Color", mapData.Color ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Phase", mapData.Phase);
command.Parameters.AddWithValue("@Width", mapData.Width ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@ChildRelations", mapData.ChildRelations ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@FatherRelations", mapData.FatherRelations ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@ChildNodes", mapData.ChildNodes ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@FatherNode", mapData.FatherNode ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Details", mapData.Details ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Custom", mapData.Custom ?? (object)DBNull.Value);
command.ExecuteNonQuery();
mapData.Id = command.LastInsertedId;
}
public static int Update(MapData mapData)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"UPDATE map_0_data SET type = @Type, points = @Points, point = @Point, color = @Color, phase = @Phase, width = @Width,
child_relations = @ChildRelations, father_relations = @FatherRelations, child_nodes = @ChildNodes, father_node = @FatherNode,
details = @Details, custom = @Custom WHERE id = @Id";
using MySqlCommand command = new(query, connection);
command.Parameters.AddWithValue("@Id", mapData.Id);
command.Parameters.AddWithValue("@Type", mapData.Type);
command.Parameters.AddWithValue("@Points", mapData.Points);
command.Parameters.AddWithValue("@Point", mapData.Point);
command.Parameters.AddWithValue("@Color", mapData.Color ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Phase", mapData.Phase);
command.Parameters.AddWithValue("@Width", mapData.Width ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@ChildRelations", mapData.ChildRelations ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@FatherRelations", mapData.FatherRelations ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@ChildNodes", mapData.ChildNodes ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@FatherNode", mapData.FatherNode ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Details", mapData.Details ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Custom", mapData.Custom ?? (object)DBNull.Value);
return command.ExecuteNonQuery();
}
public static void Delete(long id)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"DELETE FROM map_0_data WHERE id = @Id";
using MySqlCommand command = new(query, connection);
command.Parameters.AddWithValue("@Id", id);
command.ExecuteNonQuery();
}
public static MapData? Get(long id)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"SELECT id, type, points, point, color, phase, width, child_relations, father_relations, child_nodes, father_node, details, custom
FROM map_0_data WHERE id = @Id";
using MySqlCommand command = new(query, connection);
command.Parameters.AddWithValue("@Id", id);
using MySqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
return new MapData
{
Id = reader.GetInt64("id"),
Type = reader.GetString("type"),
Points = reader.GetString("points"),
Point = reader.GetString("point"),
Color = reader["color"] as string,
Phase = reader.GetInt32("phase"),
Width = reader["width"] as int?,
ChildRelations = reader["child_relations"] as string,
FatherRelations = reader["father_relations"] as string,
ChildNodes = reader["child_nodes"] as string,
FatherNode = reader["father_node"] as string,
Details = reader["details"] as string,
Custom = reader["custom"] as string
};
}
return null;
}
public static List<MapData> GetMapDataList()
{
List<MapData> mapDataList = new();
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"SELECT * FROM map_0_data";
using var command = new MySqlCommand(query, connection);
using var reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
var mapData = new MapData
{
Id = reader.GetInt64("id"),
Type = reader.GetString("type"),
Points = reader.GetString("points"),
Point = reader.GetString("point"),
Color = reader.IsDBNull(reader.GetOrdinal("color")) ? null : reader.GetString("color"),
Phase = reader.GetInt32("phase"),
Width = reader.IsDBNull(reader.GetOrdinal("width")) ? (int?)null : reader.GetInt32("width"),
ChildRelations = reader.IsDBNull(reader.GetOrdinal("child_relations")) ? null : reader.GetString("child_relations"),
FatherRelations = reader.IsDBNull(reader.GetOrdinal("father_relations")) ? null : reader.GetString("father_relations"),
ChildNodes = reader.IsDBNull(reader.GetOrdinal("child_nodes")) ? null : reader.GetString("child_nodes"),
FatherNode = reader.IsDBNull(reader.GetOrdinal("father_node")) ? null : reader.GetString("father_node"),
Details = reader.IsDBNull(reader.GetOrdinal("details")) ? null : reader.GetString("details"),
Custom = reader.IsDBNull(reader.GetOrdinal("custom")) ? null : reader.GetString("custom")
};
mapDataList.Add(mapData);
}
}
return mapDataList;
}
}
}