Commit 62cb035d authored by johandanforth's avatar johandanforth

Custom table name mapper for Contrib

parent beb675d1
......@@ -31,6 +31,7 @@ private static void Setup()
using (var connection = new SqlCeConnection(connectionString))
{
connection.Open();
connection.Execute(@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) ");
connection.Execute(@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) ");
......
......@@ -28,6 +28,7 @@ private static void Setup()
using (var connection = new SqlCeConnection(connectionString))
{
connection.Open();
connection.Execute(@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) ");
connection.Execute(@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) ");
......
......@@ -27,6 +27,12 @@ public class User : IUser
public int Age { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
[Table("Automobiles")]
public class Car
{
......@@ -202,7 +208,7 @@ public void InsertGetUpdate()
public void InsertWithCustomDbType()
{
SqlMapperExtensions.GetDatabaseType = (conn) => "SQLiteConnection";
SqlMapperExtensions.GetDatabaseType = connection => "SQLiteConnection";
bool sqliteCodeCalled = false;
using (var connection = GetOpenConnection())
......@@ -229,6 +235,31 @@ public void InsertWithCustomDbType()
}
}
public void InsertWithCustomTableNameMapper()
{
SqlMapperExtensions.TableNameMapper = (type) =>
{
switch (type.Name)
{
case "Person":
return "People";
default:
var name = type.Name + "s";
if (type.IsInterface && name.StartsWith("I"))
name = name.Substring(1);
return name;
}
};
using (var connection = GetOpenConnection())
{
var id = connection.Insert(new Person() { Name = "Mr Mapper" });
id.IsEqualTo(1);
var people = connection.GetAll<Person>();
}
}
public void GetAll()
{
const int numberOfEntities = 100;
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
......@@ -23,7 +22,14 @@ public interface IProxy //must be kept public
{
bool IsDirty { get; set; }
}
public interface ITableNameMapper
{
string GetTableName(Type type);
}
public delegate string GetDatabaseTypeDelegate(IDbConnection connection);
public delegate string TableNameMapperDelegate(Type type);
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> KeyProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> TypeProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
......@@ -208,22 +214,35 @@ private static bool IsWriteable(PropertyInfo pi)
return list;
}
/// <summary>
/// Specify a custom table name mapper based on the POCO type name
/// </summary>
public static TableNameMapperDelegate TableNameMapper;
private static string GetTableName(Type type)
{
string name;
if (!TypeTableName.TryGetValue(type.TypeHandle, out name))
if (TypeTableName.TryGetValue(type.TypeHandle, out name)) return name;
if (TableNameMapper != null)
{
name = TableNameMapper(type);
}
else
{
name = type.Name + "s";
if (type.IsInterface && name.StartsWith("I"))
name = name.Substring(1);
//NOTE: This as dynamic trick should be able to handle both our own Table-attribute as well as the one in EntityFramework
var tableattr = type.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as
dynamic;
var tableattr = type.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic;
if (tableattr != null)
name = tableattr.Name;
TypeTableName[type.TypeHandle] = name;
else
{
name = type.Name + "s";
if (type.IsInterface && name.StartsWith("I"))
name = name.Substring(1);
}
}
TypeTableName[type.TypeHandle] = name;
return name;
}
......@@ -391,14 +410,14 @@ private static string GetTableName(Type type)
/// Please note that this callback is global and will be used by all the calls that require a database specific adapter.
/// </summary>
public static GetDatabaseTypeDelegate GetDatabaseType;
private static ISqlAdapter GetFormatter(IDbConnection connection)
{
string name;
var getDatabaseType = GetDatabaseType;
if (getDatabaseType != null)
if (GetDatabaseType != null)
{
name = getDatabaseType(connection);
name = GetDatabaseType(connection);
if (name != null)
name = name.ToLower();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment