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
d34b6bbf
Commit
d34b6bbf
authored
May 07, 2014
by
Marc Gravell
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #106 from aggieben/master
add ComputedAttribute to allow support for computed columns on Insert
parents
efd44601
67026a0d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
7 deletions
+26
-7
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+26
-7
No files found.
Dapper.Contrib/SqlMapperExtensions.cs
View file @
d34b6bbf
...
...
@@ -23,6 +23,7 @@ public interface IProxy
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
>>
ComputedProperties
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
IEnumerable
<
PropertyInfo
>>();
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>
GetQueries
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>();
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>
TypeTableName
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>();
...
...
@@ -30,7 +31,19 @@ public interface IProxy
{
"sqlconnection"
,
new
SqlServerAdapter
()},
{
"npgsqlconnection"
,
new
PostgresAdapter
()}
};
private
static
IEnumerable
<
PropertyInfo
>
ComputedPropertiesCache
(
Type
type
)
{
IEnumerable
<
PropertyInfo
>
pi
;
if
(
ComputedProperties
.
TryGetValue
(
type
.
TypeHandle
,
out
pi
))
{
return
pi
;
}
var
computedProperties
=
TypePropertiesCache
(
type
).
Where
(
p
=>
p
.
GetCustomAttributes
(
true
).
Any
(
a
=>
a
is
ComputedAttribute
)).
ToList
();
ComputedProperties
[
type
.
TypeHandle
]
=
computedProperties
;
return
computedProperties
;
}
private
static
IEnumerable
<
PropertyInfo
>
KeyPropertiesCache
(
Type
type
)
{
...
...
@@ -175,22 +188,23 @@ private static string GetTableName(Type type)
var
allProperties
=
TypePropertiesCache
(
type
);
var
keyProperties
=
KeyPropertiesCache
(
type
);
var
allPropertiesExceptKey
=
allProperties
.
Except
(
keyProperties
);
var
computedProperties
=
ComputedPropertiesCache
(
type
);
var
allPropertiesExceptKeyAndComputed
=
allProperties
.
Except
(
keyProperties
.
Union
(
computedProperties
));
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
.
Count
();
i
++)
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
AndComputed
.
Count
();
i
++)
{
var
property
=
allPropertiesExceptKey
.
ElementAt
(
i
);
var
property
=
allPropertiesExceptKey
AndComputed
.
ElementAt
(
i
);
sbColumnList
.
AppendFormat
(
"[{0}]"
,
property
.
Name
);
if
(
i
<
allPropertiesExceptKey
.
Count
()
-
1
)
if
(
i
<
allPropertiesExceptKey
AndComputed
.
Count
()
-
1
)
sbColumnList
.
Append
(
", "
);
}
var
sbParameterList
=
new
StringBuilder
(
null
);
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
.
Count
();
i
++)
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
AndComputed
.
Count
();
i
++)
{
var
property
=
allPropertiesExceptKey
.
ElementAt
(
i
);
var
property
=
allPropertiesExceptKey
AndComputed
.
ElementAt
(
i
);
sbParameterList
.
AppendFormat
(
"@{0}"
,
property
.
Name
);
if
(
i
<
allPropertiesExceptKey
.
Count
()
-
1
)
if
(
i
<
allPropertiesExceptKey
AndComputed
.
Count
()
-
1
)
sbParameterList
.
Append
(
", "
);
}
ISqlAdapter
adapter
=
GetFormatter
(
connection
);
...
...
@@ -472,6 +486,11 @@ public WriteAttribute(bool write)
}
public
bool
Write
{
get
;
private
set
;
}
}
[
AttributeUsage
(
AttributeTargets
.
Property
)]
public
class
ComputedAttribute
:
Attribute
{
}
}
public
interface
ISqlAdapter
...
...
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