Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
beb675d1
D
Dapper
项目概览
int
/
Dapper
11 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dapper
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
beb675d1
编写于
6月 06, 2015
作者:
J
johandanforth
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
GetDatebaseType delegate for Contrib Insert
上级
63460c60
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
61 addition
and
19 deletion
+61
-19
Dapper.Contrib.Tests/Tests.cs
Dapper.Contrib.Tests/Tests.cs
+28
-4
Dapper.Contrib/SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+33
-15
未找到文件。
Dapper.Contrib.Tests/Tests.cs
浏览文件 @
beb675d1
...
...
@@ -197,13 +197,37 @@ public void InsertGetUpdate()
connection
.
Query
<
User
>(
"select * from Users"
).
Count
().
IsEqualTo
(
0
);
connection
.
Update
(
notrackedUser
).
IsEqualTo
(
false
);
//returns false, user not found
//insert with custom sqladapter
connection
.
Insert
(
new
User
{
Name
=
"Adam"
,
Age
=
10
},
sqlAdapter
:
new
SqlServerAdapter
()).
IsMoreThan
(
0
);
}
}
public
void
InsertWithCustomDbType
()
{
SqlMapperExtensions
.
GetDatabaseType
=
(
conn
)
=>
"SQLiteConnection"
;
bool
sqliteCodeCalled
=
false
;
using
(
var
connection
=
GetOpenConnection
())
{
connection
.
DeleteAll
<
User
>();
connection
.
Get
<
User
>(
3
).
IsNull
();
try
{
var
id
=
connection
.
Insert
(
new
User
{
Name
=
"Adam"
,
Age
=
10
});
}
catch
(
SqlCeException
ex
)
{
sqliteCodeCalled
=
ex
.
Message
.
IndexOf
(
"last_insert_rowid"
,
StringComparison
.
InvariantCultureIgnoreCase
)
>=
0
;
}
catch
(
Exception
)
{
}
}
SqlMapperExtensions
.
GetDatabaseType
=
null
;
if
(!
sqliteCodeCalled
)
{
throw
new
Exception
(
"Was expecting sqlite code to be called"
);
}
}
public
void
GetAll
()
{
...
...
Dapper.Contrib/SqlMapperExtensions.cs
浏览文件 @
beb675d1
...
...
@@ -23,6 +23,7 @@ public interface IProxy //must be kept public
{
bool
IsDirty
{
get
;
set
;
}
}
public
delegate
string
GetDatabaseTypeDelegate
(
IDbConnection
connection
);
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
>>();
...
...
@@ -170,7 +171,7 @@ private static bool IsWriteable(PropertyInfo pi)
public
static
IEnumerable
<
T
>
GetAll
<
T
>(
this
IDbConnection
connection
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
where
T
:
class
{
var
type
=
typeof
(
T
);
var
cacheType
=
typeof
(
List
<
T
>);
var
cacheType
=
typeof
(
List
<
T
>);
string
sql
;
if
(!
GetQueries
.
TryGetValue
(
cacheType
.
TypeHandle
,
out
sql
))
...
...
@@ -184,7 +185,7 @@ private static bool IsWriteable(PropertyInfo pi)
var
name
=
GetTableName
(
type
);
// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql
=
"select * from "
+
name
;
sql
=
"select * from "
+
name
;
GetQueries
[
cacheType
.
TypeHandle
]
=
sql
;
}
...
...
@@ -226,14 +227,14 @@ private static string GetTableName(Type type)
return
name
;
}
/// <summary>
/// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list.
/// </summary>
/// <param name="connection">Open SqlConnection</param>
/// <param name="entityToInsert">Entity to insert, can be list of entities</param>
/// <returns>Identity of inserted entity, or number of inserted rows if inserting a list</returns>
public
static
long
Insert
<
T
>(
this
IDbConnection
connection
,
T
entityToInsert
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
,
ISqlAdapter
sqlAdapter
=
null
)
where
T
:
class
public
static
long
Insert
<
T
>(
this
IDbConnection
connection
,
T
entityToInsert
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
where
T
:
class
{
var
isList
=
false
;
...
...
@@ -271,9 +272,8 @@ private static string GetTableName(Type type)
if
(!
isList
)
//single entity
{
if
(
sqlAdapter
==
null
)
sqlAdapter
=
GetFormatter
(
connection
);
return
sqlAdapter
.
Insert
(
connection
,
transaction
,
commandTimeout
,
name
,
sbColumnList
.
ToString
(),
var
adapter
=
GetFormatter
(
connection
);
return
adapter
.
Insert
(
connection
,
transaction
,
commandTimeout
,
name
,
sbColumnList
.
ToString
(),
sbParameterList
.
ToString
(),
keyProperties
,
entityToInsert
);
}
...
...
@@ -386,9 +386,27 @@ private static string GetTableName(Type type)
return
deleted
>
0
;
}
/// <summary>
/// Specifies a custom callback that detects the database type instead of relying on the default strategy (the name of the connection type object).
/// 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
)
{
var
name
=
connection
.
GetType
().
Name
.
ToLower
();
string
name
;
var
getDatabaseType
=
GetDatabaseType
;
if
(
getDatabaseType
!=
null
)
{
name
=
getDatabaseType
(
connection
);
if
(
name
!=
null
)
name
=
name
.
ToLower
();
}
else
{
name
=
connection
.
GetType
().
Name
.
ToLower
();
}
return
!
AdapterDictionary
.
ContainsKey
(
name
)
?
new
SqlServerAdapter
()
:
AdapterDictionary
[
name
];
...
...
@@ -552,8 +570,8 @@ public TableAttribute(string tableName)
Name
=
tableName
;
}
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnusedAutoPropertyAccessor.Global
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public
string
Name
{
get
;
set
;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录