Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
Dapper
Commits
b9c5d5d8
Commit
b9c5d5d8
authored
Nov 24, 2015
by
Nick Craver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Contrib: DRY code current keys implementation
parent
e40c7882
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
42 deletions
+19
-42
SqlMapperExtensions.Async.cs
Dapper.Contrib/SqlMapperExtensions.Async.cs
+3
-23
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+16
-19
No files found.
Dapper.Contrib/SqlMapperExtensions.Async.cs
View file @
b9c5d5d8
...
...
@@ -5,7 +5,6 @@
using
System.Reflection
;
using
System.Text
;
using
System.Threading.Tasks
;
using
Dapper
;
#if COREFX
...
...
@@ -18,10 +17,8 @@
namespace
Dapper.Contrib.Extensions
{
public
static
partial
class
SqlMapperExtensions
{
/// <summary>
/// Returns a single entity by a single id from table "Ts" asynchronously using .NET 4.5 Task. T must be of interface type.
/// Id must be marked with [Key] attribute.
...
...
@@ -39,18 +36,9 @@ public static partial class SqlMapperExtensions
string
sql
;
if
(!
GetQueries
.
TryGetValue
(
type
.
TypeHandle
,
out
sql
))
{
var
keys
=
KeyPropertiesCache
(
type
);
var
explicitKeys
=
ExplicitKeyPropertiesCache
(
type
);
if
(
keys
.
Count
>
1
||
explicitKeys
.
Count
>
1
)
throw
new
DataException
(
"Get<T> only supports an entity with a single [Key] or [ExplicitKey] property"
);
if
(!
keys
.
Any
()
&&
!
explicitKeys
.
Any
())
throw
new
DataException
(
"Get<T> only supports an entity with a [Key] or an [ExplicitKey] property"
);
var
key
=
keys
.
Any
()
?
keys
.
First
()
:
explicitKeys
.
First
();
var
key
=
GetSingleKey
<
T
>(
nameof
(
GetAsync
));
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
}
WHERE
{
key
.
Name
}
= @id"
;
GetQueries
[
type
.
TypeHandle
]
=
sql
;
}
...
...
@@ -58,7 +46,6 @@ public static partial class SqlMapperExtensions
var
dynParms
=
new
DynamicParameters
();
dynParms
.
Add
(
"@id"
,
id
);
if
(!
type
.
IsInterface
())
return
(
await
connection
.
QueryAsync
<
T
>(
sql
,
dynParms
,
transaction
,
commandTimeout
).
ConfigureAwait
(
false
)).
FirstOrDefault
();
...
...
@@ -99,15 +86,9 @@ public static partial class SqlMapperExtensions
string
sql
;
if
(!
GetQueries
.
TryGetValue
(
cacheType
.
TypeHandle
,
out
sql
))
{
var
keys
=
KeyPropertiesCache
(
type
);
if
(
keys
.
Count
>
1
)
throw
new
DataException
(
"Get<T> only supports an entity with a single [Key] property"
);
if
(!
keys
.
Any
())
throw
new
DataException
(
"Get<T> only supports en entity with a [Key] property"
);
GetSingleKey
<
T
>(
nameof
(
GetAll
));
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
;
GetQueries
[
cacheType
.
TypeHandle
]
=
sql
;
}
...
...
@@ -301,8 +282,7 @@ public static partial class SqlMapperExtensions
public
static
async
Task
<
bool
>
DeleteAllAsync
<
T
>(
this
IDbConnection
connection
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
where
T
:
class
{
var
type
=
typeof
(
T
);
var
name
=
GetTableName
(
type
);
var
statement
=
$"DELETE FROM
{
name
}
"
;
var
statement
=
"DELETE FROM "
+
GetTableName
(
type
);
var
deleted
=
await
connection
.
ExecuteAsync
(
statement
,
null
,
transaction
,
commandTimeout
).
ConfigureAwait
(
false
);
return
deleted
>
0
;
}
...
...
Dapper.Contrib/SqlMapperExtensions.cs
View file @
b9c5d5d8
...
...
@@ -127,6 +127,20 @@ private static bool IsWriteable(PropertyInfo pi)
return
writeAttribute
.
Write
;
}
private
static
PropertyInfo
GetSingleKey
<
T
>(
string
method
)
{
var
type
=
typeof
(
T
);
var
keys
=
KeyPropertiesCache
(
type
);
var
explicitKeys
=
ExplicitKeyPropertiesCache
(
type
);
var
keyCount
=
keys
.
Count
+
explicitKeys
.
Count
;
if
(
keyCount
>
1
)
throw
new
DataException
(
$"
{
method
}
<T> only supports an entity with a single [Key] or [ExplicitKey] property"
);
if
(
keyCount
==
0
)
throw
new
DataException
(
$"
{
method
}
<T> only supports an entity with a [Key] or an [ExplicitKey] property"
);
return
keys
.
Any
()
?
keys
.
First
()
:
explicitKeys
.
First
();
}
/// <summary>
/// Returns a single entity by a single id from table "Ts".
/// Id must be marked with [Key] attribute.
...
...
@@ -146,18 +160,9 @@ private static bool IsWriteable(PropertyInfo pi)
string
sql
;
if
(!
GetQueries
.
TryGetValue
(
type
.
TypeHandle
,
out
sql
))
{
var
keys
=
KeyPropertiesCache
(
type
);
var
explicitKeys
=
ExplicitKeyPropertiesCache
(
type
);
if
(
keys
.
Count
>
1
||
explicitKeys
.
Count
>
1
)
throw
new
DataException
(
"Get<T> only supports an entity with a single [Key] or [ExplicitKey] property"
);
if
(!
keys
.
Any
()
&&
!
explicitKeys
.
Any
())
throw
new
DataException
(
"Get<T> only supports an entity with a [Key] or an [ExplicitKey] property"
);
var
key
=
keys
.
Any
()
?
keys
.
First
()
:
explicitKeys
.
First
();
var
key
=
GetSingleKey
<
T
>(
nameof
(
Get
));
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
}
where
{
key
.
Name
}
= @id"
;
GetQueries
[
type
.
TypeHandle
]
=
sql
;
}
...
...
@@ -210,21 +215,13 @@ private static bool IsWriteable(PropertyInfo pi)
string
sql
;
if
(!
GetQueries
.
TryGetValue
(
cacheType
.
TypeHandle
,
out
sql
))
{
var
keys
=
KeyPropertiesCache
(
type
);
var
explicitKeys
=
ExplicitKeyPropertiesCache
(
type
);
if
(
keys
.
Count
>
1
||
explicitKeys
.
Count
>
1
)
throw
new
DataException
(
"Get<T> only supports an entity with a single [Key] or [ExplicitKey] property"
);
if
(!
keys
.
Any
()
&&
!
explicitKeys
.
Any
())
throw
new
DataException
(
"Get<T> only supports an entity with a [Key] or an [ExplicitKey] property"
);
GetSingleKey
<
T
>(
nameof
(
GetAll
));
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
;
GetQueries
[
cacheType
.
TypeHandle
]
=
sql
;
}
if
(!
type
.
IsInterface
())
return
connection
.
Query
<
T
>(
sql
,
null
,
transaction
,
commandTimeout
:
commandTimeout
);
var
result
=
connection
.
Query
(
sql
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment