Added a new extension to see if a row exists by it's id. Bumped package version to 1.1.3

This commit is contained in:
MattMo 2023-06-23 09:58:23 -07:00
parent 926fff38fd
commit 8cf3b2eac0
2 changed files with 51 additions and 3 deletions

View File

@ -1,10 +1,8 @@
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace MontoyaTech.MySqlPlus
{
@ -211,6 +209,56 @@ namespace MontoyaTech.MySqlPlus
command.CommandText = $"SELECT * FROM `{(rowAttribute == null || string.IsNullOrWhiteSpace(rowAttribute.Name) ? rowType.Name : rowAttribute.Name)}`";
}
/// <summary>
/// Setups this MySqlCommand to see if a row exists by an id from the db.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="command"></param>
/// <param name="id"></param>
public static void Exists<T>(this MySqlCommand command, ulong id)
{
//Get the type of T
var rowType = typeof(T);
//Get the row information.
var rowAttribute = rowType.GetCustomAttribute<MySqlRow>();
//Get the id field
if (!rowType.GetMySqlPrimaryKey(out FieldInfo primaryField, out MySqlColumn primaryColumn))
throw new Exception("Failed to find the primary key for the given row.");
//Set the command text.
command.CommandText = $"SELECT EXISTS(SELECT 1 FROM `{(rowAttribute == null || string.IsNullOrWhiteSpace(rowAttribute.Name) ? rowType.Name : rowAttribute.Name)}` WHERE `{(string.IsNullOrWhiteSpace(primaryColumn.Name) ? primaryField.Name : primaryColumn.Name)}`=@id LIMIT 1)";
//Add the id parameter.
command.Parameters.AddWithValue("@id", id);
}
/// <summary>
/// Setups this MySqlCommand to see if a row exists by an id from the db.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="command"></param>
/// <param name="id"></param>
public static void Exists<T>(this MySqlCommand command, string id)
{
//Get the type of T
var rowType = typeof(T);
//Get the row information.
var rowAttribute = rowType.GetCustomAttribute<MySqlRow>();
//Get the id field
if (!rowType.GetMySqlPrimaryKey(out FieldInfo primaryField, out MySqlColumn primaryColumn))
throw new Exception("Failed to find the primary key for the given row.");
//Set the command text.
command.CommandText = $"SELECT EXISTS(SELECT 1 FROM `{(rowAttribute == null || string.IsNullOrWhiteSpace(rowAttribute.Name) ? rowType.Name : rowAttribute.Name)}` WHERE `{(string.IsNullOrWhiteSpace(primaryColumn.Name) ? primaryField.Name : primaryColumn.Name)}`=@id LIMIT 1)";
//Add the id parameter.
command.Parameters.AddWithValue("@id", id);
}
/// <summary>
/// Setups this MySqlCommand to delete a row from the db by it's id from an instance of the row.
/// </summary>

View File

@ -7,7 +7,7 @@
<AssemblyName>MontoyaTech.MySqlPlus</AssemblyName>
<RootNamespace>MontoyaTech.MySqlPlus</RootNamespace>
<Title>MontoyaTech.MySqlPlus</Title>
<Version>1.1.2</Version>
<Version>1.1.3</Version>
<Company>MontoyaTech</Company>
<Description>A simple C# library to help work with MySql.</Description>
<Copyright>MontoyaTech 2023</Copyright>