mirror of
https://github.com/mm201/pkmn-classic-framework.git
synced 2026-04-24 07:16:48 -05:00
Toying with the idea of using a DbParameterFactory to further database engine agnosticism in the project.
This commit is contained in:
parent
8d02425230
commit
e0740dcd6c
30
library/Data/DbParameterFactory.cs
Normal file
30
library/Data/DbParameterFactory.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace PkmnFoundations.Data
|
||||
{
|
||||
public class DbParameterFactory
|
||||
{
|
||||
public DbParameterFactory(DbConnection db)
|
||||
{
|
||||
if (db is MySqlConnection) m_parameter_factory = new MySqlParameterFactory();
|
||||
else throw new NotSupportedException();
|
||||
}
|
||||
|
||||
private DbParameterFactoryBase m_parameter_factory;
|
||||
|
||||
public DbParameter CreateParameter(String name, object value, Type t)
|
||||
{
|
||||
return m_parameter_factory.CreateParameter(name, value, t);
|
||||
}
|
||||
|
||||
public DbParameter CloneParameter(DbParameter orig)
|
||||
{
|
||||
return m_parameter_factory.CloneParameter(orig);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
library/Data/DbParameterFactoryBase.cs
Normal file
19
library/Data/DbParameterFactoryBase.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace PkmnFoundations.Data
|
||||
{
|
||||
internal abstract class DbParameterFactoryBase
|
||||
{
|
||||
internal DbParameterFactoryBase()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal abstract DbParameter CreateParameter(String name, object value, Type t);
|
||||
internal abstract DbParameter CloneParameter(DbParameter orig);
|
||||
}
|
||||
}
|
||||
69
library/Data/MySqlParameterFactory.cs
Normal file
69
library/Data/MySqlParameterFactory.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace PkmnFoundations.Data
|
||||
{
|
||||
internal class MySqlParameterFactory : DbParameterFactoryBase
|
||||
{
|
||||
internal MySqlParameterFactory()
|
||||
: base()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal override System.Data.Common.DbParameter CreateParameter(string name, object value, Type t)
|
||||
{
|
||||
MySqlDbType ? dbType = GetDbType(t);
|
||||
|
||||
if (dbType == null)
|
||||
return new MySqlParameter(name, value);
|
||||
else
|
||||
{
|
||||
MySqlParameter result = new MySqlParameter(name, (MySqlDbType)dbType);
|
||||
result.Value = value;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<Type, MySqlDbType> m_dbtype_map = new Dictionary<Type, MySqlDbType>()
|
||||
{
|
||||
{typeof(bool), MySqlDbType.Byte},
|
||||
{typeof(sbyte), MySqlDbType.Byte},
|
||||
{typeof(short), MySqlDbType.Int16},
|
||||
{typeof(int), MySqlDbType.Int32},
|
||||
{typeof(long), MySqlDbType.Int64},
|
||||
{typeof(byte), MySqlDbType.UByte},
|
||||
{typeof(ushort), MySqlDbType.UInt16},
|
||||
{typeof(uint), MySqlDbType.UInt32},
|
||||
{typeof(ulong), MySqlDbType.UInt64},
|
||||
{typeof(float), MySqlDbType.Float},
|
||||
{typeof(double), MySqlDbType.Double},
|
||||
{typeof(decimal), MySqlDbType.Decimal},
|
||||
{typeof(DateTime), MySqlDbType.DateTime},
|
||||
{typeof(TimeSpan), MySqlDbType.Time},
|
||||
{typeof(String), MySqlDbType.VarChar},
|
||||
{typeof(Guid), MySqlDbType.Guid},
|
||||
};
|
||||
|
||||
private MySqlDbType GetDbType(Type t)
|
||||
{
|
||||
if (m_dbtype_map.ContainsKey(t)) return m_dbtype_map[t];
|
||||
if (t.BaseType == typeof(Enum)) return MySqlDbType.Int32; // fixme: wrong for enums inheriting from other types
|
||||
return MySqlDbType.Blob;
|
||||
}
|
||||
|
||||
internal override System.Data.Common.DbParameter CloneParameter(System.Data.Common.DbParameter orig)
|
||||
{
|
||||
if (orig == null) throw new ArgumentNullException();
|
||||
if (!(orig is MySqlParameter)) throw new ArgumentException();
|
||||
|
||||
MySqlParameter p = (MySqlParameter)orig;
|
||||
MySqlParameter param = new MySqlParameter(p.ParameterName, (MySqlDbType)p.DbType, p.Size, p.Direction, p.IsNullable, p.Precision, p.Scale, p.SourceColumn, p.SourceVersion, p.Value);
|
||||
param.DbType = p.DbType;
|
||||
return param;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -52,6 +52,9 @@
|
|||
<Compile Include="Data\DatabaseExtender.cs" />
|
||||
<Compile Include="Data\DataMysql.cs" />
|
||||
<Compile Include="Data\DataSqlite.cs" />
|
||||
<Compile Include="Data\DbParameterFactory.cs" />
|
||||
<Compile Include="Data\DbParameterFactoryBase.cs" />
|
||||
<Compile Include="Data\MySqlParameterFactory.cs" />
|
||||
<Compile Include="Pokedex\Ability.cs" />
|
||||
<Compile Include="Pokedex\Evolution.cs" />
|
||||
<Compile Include="Pokedex\FormStats.cs" />
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user