Commit 62cb035d authored by johandanforth's avatar johandanforth

Custom table name mapper for Contrib

parent beb675d1
...@@ -31,6 +31,7 @@ private static void Setup() ...@@ -31,6 +31,7 @@ private static void Setup()
using (var connection = new SqlCeConnection(connectionString)) using (var connection = new SqlCeConnection(connectionString))
{ {
connection.Open(); 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 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 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) "); 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() ...@@ -28,6 +28,7 @@ private static void Setup()
using (var connection = new SqlCeConnection(connectionString)) using (var connection = new SqlCeConnection(connectionString))
{ {
connection.Open(); 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 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 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) "); 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 ...@@ -27,6 +27,12 @@ public class User : IUser
public int Age { get; set; } public int Age { get; set; }
} }
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
[Table("Automobiles")] [Table("Automobiles")]
public class Car public class Car
{ {
...@@ -202,7 +208,7 @@ public void InsertGetUpdate() ...@@ -202,7 +208,7 @@ public void InsertGetUpdate()
public void InsertWithCustomDbType() public void InsertWithCustomDbType()
{ {
SqlMapperExtensions.GetDatabaseType = (conn) => "SQLiteConnection"; SqlMapperExtensions.GetDatabaseType = connection => "SQLiteConnection";
bool sqliteCodeCalled = false; bool sqliteCodeCalled = false;
using (var connection = GetOpenConnection()) using (var connection = GetOpenConnection())
...@@ -229,6 +235,31 @@ public void InsertWithCustomDbType() ...@@ -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() public void GetAll()
{ {
const int numberOfEntities = 100; const int numberOfEntities = 100;
......
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
...@@ -23,7 +22,14 @@ public interface IProxy //must be kept public ...@@ -23,7 +22,14 @@ public interface IProxy //must be kept public
{ {
bool IsDirty { get; set; } bool IsDirty { get; set; }
} }
public interface ITableNameMapper
{
string GetTableName(Type type);
}
public delegate string GetDatabaseTypeDelegate(IDbConnection connection); 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>> KeyProperties = new ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>();
private static readonly ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>> TypeProperties = 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) ...@@ -208,22 +214,35 @@ private static bool IsWriteable(PropertyInfo pi)
return list; 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) private static string GetTableName(Type type)
{ {
string name; 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 //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 var tableattr = type.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic;
dynamic;
if (tableattr != null) if (tableattr != null)
name = tableattr.Name; 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; return name;
} }
...@@ -391,14 +410,14 @@ private static string GetTableName(Type type) ...@@ -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. /// Please note that this callback is global and will be used by all the calls that require a database specific adapter.
/// </summary> /// </summary>
public static GetDatabaseTypeDelegate GetDatabaseType; public static GetDatabaseTypeDelegate GetDatabaseType;
private static ISqlAdapter GetFormatter(IDbConnection connection) private static ISqlAdapter GetFormatter(IDbConnection connection)
{ {
string name; string name;
var getDatabaseType = GetDatabaseType; if (GetDatabaseType != null)
if (getDatabaseType != null)
{ {
name = getDatabaseType(connection); name = GetDatabaseType(connection);
if (name != null) if (name != null)
name = name.ToLower(); 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