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
edd142d4
Commit
edd142d4
authored
Jan 23, 2012
by
Sam Saffron
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:SamSaffron/dapper-dot-net
parents
a7c0a64b
bce25285
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
20 deletions
+99
-20
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+99
-20
No files found.
Dapper.Contrib/SqlMapperExtensions.cs
View file @
edd142d4
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
using
System.Reflection.Emit
;
using
System.Reflection.Emit
;
using
System.Threading
;
using
System.Threading
;
using
System.Runtime.CompilerServices
;
using
System.Runtime.CompilerServices
;
using
Dapper
;
namespace
Dapper.Contrib.Extensions
namespace
Dapper.Contrib.Extensions
{
{
...
@@ -25,6 +26,11 @@ public interface IProxy
...
@@ -25,6 +26,11 @@ public interface IProxy
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>
GetQueries
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>();
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>
GetQueries
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>();
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>
TypeTableName
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>();
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>
TypeTableName
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
string
>();
private
static
readonly
Dictionary
<
string
,
ISqlAdapter
>
AdapterDictionary
=
new
Dictionary
<
string
,
ISqlAdapter
>()
{
{
"sqlconnection"
,
new
SqlServerAdapter
()},
{
"npgsqlconnection"
,
new
PostgresAdapter
()}
};
private
static
IEnumerable
<
PropertyInfo
>
KeyPropertiesCache
(
Type
type
)
private
static
IEnumerable
<
PropertyInfo
>
KeyPropertiesCache
(
Type
type
)
{
{
...
@@ -57,12 +63,23 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type)
...
@@ -57,12 +63,23 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type)
return
pis
;
return
pis
;
}
}
var
properties
=
type
.
GetProperties
();
var
properties
=
type
.
GetProperties
()
.
Where
(
IsWriteable
)
;
TypeProperties
[
type
.
TypeHandle
]
=
properties
;
TypeProperties
[
type
.
TypeHandle
]
=
properties
;
return
properties
;
return
properties
;
}
}
/// <summary>
public
static
bool
IsWriteable
(
PropertyInfo
pi
)
{
object
[]
attributes
=
pi
.
GetCustomAttributes
(
typeof
(
WriteAttribute
),
false
);
if
(
attributes
.
Length
==
1
)
{
WriteAttribute
write
=
(
WriteAttribute
)
attributes
[
0
];
return
write
.
Write
;
}
return
true
;
}
/// <summary>
/// Returns a single entity by a single id from table "Ts". T must be of interface type.
/// Returns a single entity by a single id from table "Ts". T must be of interface type.
/// Id must be marked with [Key] attribute.
/// Id must be marked with [Key] attribute.
/// Created entity is tracked/intercepted for changes and used by the Update() extension.
/// Created entity is tracked/intercepted for changes and used by the Update() extension.
...
@@ -148,41 +165,37 @@ private static string GetTableName(Type type)
...
@@ -148,41 +165,37 @@ private static string GetTableName(Type type)
/// <param name="entityToInsert">Entity to insert</param>
/// <param name="entityToInsert">Entity to insert</param>
/// <returns>Identity of inserted entity</returns>
/// <returns>Identity of inserted entity</returns>
public
static
long
Insert
<
T
>(
this
IDbConnection
connection
,
T
entityToInsert
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
where
T
:
class
public
static
long
Insert
<
T
>(
this
IDbConnection
connection
,
T
entityToInsert
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
where
T
:
class
{
{
var
type
=
typeof
(
T
);
var
type
=
typeof
(
T
);
var
name
=
GetTableName
(
type
);
var
name
=
GetTableName
(
type
);
var
sb
=
new
StringBuilder
(
null
);
var
sbColumnList
=
new
StringBuilder
(
null
);
sb
.
AppendFormat
(
"insert into {0} ("
,
name
);
var
allProperties
=
TypePropertiesCache
(
type
);
var
allProperties
=
TypePropertiesCache
(
type
);
var
keyProperties
=
KeyPropertiesCache
(
type
);
var
keyProperties
=
KeyPropertiesCache
(
type
);
var
allPropertiesExceptKey
=
allProperties
.
Except
(
keyProperties
);
var
allPropertiesExceptKey
=
allProperties
.
Except
(
keyProperties
);
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
.
Count
();
i
++)
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
.
Count
();
i
++)
{
{
var
property
=
allPropertiesExceptKey
.
ElementAt
(
i
);
var
property
=
allPropertiesExceptKey
.
ElementAt
(
i
);
sb
.
Append
(
property
.
Name
);
sbColumnList
.
Append
(
property
.
Name
);
if
(
i
<
allPropertiesExceptKey
.
Count
()
-
1
)
if
(
i
<
allPropertiesExceptKey
.
Count
()
-
1
)
sb
.
Append
(
", "
);
sbColumnList
.
Append
(
", "
);
}
}
sb
.
Append
(
") values ("
);
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
.
Count
();
i
++)
var
sbParameterList
=
new
StringBuilder
(
null
);
for
(
var
i
=
0
;
i
<
allPropertiesExceptKey
.
Count
();
i
++)
{
{
var
property
=
allPropertiesExceptKey
.
ElementAt
(
i
);
var
property
=
allPropertiesExceptKey
.
ElementAt
(
i
);
sb
.
AppendFormat
(
"@{0}"
,
property
.
Name
);
sb
ParameterList
.
AppendFormat
(
"@{0}"
,
property
.
Name
);
if
(
i
<
allPropertiesExceptKey
.
Count
()
-
1
)
if
(
i
<
allPropertiesExceptKey
.
Count
()
-
1
)
sb
.
Append
(
", "
);
sb
ParameterList
.
Append
(
", "
);
}
}
sb
.
Append
(
") "
);
ISqlAdapter
adapter
=
GetFormatter
(
connection
);
connection
.
Execute
(
sb
.
ToString
(),
entityToInsert
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
int
id
=
adapter
.
Insert
(
connection
,
transaction
,
commandTimeout
,
name
,
sbColumnList
.
ToString
(),
sbParameterList
.
ToString
(),
keyProperties
,
entityToInsert
);
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
return
id
;
var
r
=
connection
.
Query
(
"select @@IDENTITY id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
return
(
int
)
r
.
First
().
id
;
}
}
/// <summary>
/// <summary>
...
@@ -203,7 +216,7 @@ private static string GetTableName(Type type)
...
@@ -203,7 +216,7 @@ private static string GetTableName(Type type)
var
type
=
typeof
(
T
);
var
type
=
typeof
(
T
);
var
keyProperties
=
KeyPropertiesCache
(
type
);
var
keyProperties
=
KeyPropertiesCache
(
type
);
if
(
keyProperties
.
Count
()
==
0
)
if
(
!
keyProperties
.
Any
()
)
throw
new
ArgumentException
(
"Entity must have at least one [Key] property"
);
throw
new
ArgumentException
(
"Entity must have at least one [Key] property"
);
var
name
=
GetTableName
(
type
);
var
name
=
GetTableName
(
type
);
...
@@ -264,8 +277,15 @@ private static string GetTableName(Type type)
...
@@ -264,8 +277,15 @@ private static string GetTableName(Type type)
return
deleted
>
0
;
return
deleted
>
0
;
}
}
public
static
ISqlAdapter
GetFormatter
(
IDbConnection
connection
)
{
string
name
=
connection
.
GetType
().
Name
.
ToLower
();
if
(!
AdapterDictionary
.
ContainsKey
(
name
))
return
new
SqlServerAdapter
();
return
AdapterDictionary
[
name
];
}
class
ProxyGenerator
class
ProxyGenerator
{
{
private
static
readonly
Dictionary
<
Type
,
object
>
TypeCache
=
new
Dictionary
<
Type
,
object
>();
private
static
readonly
Dictionary
<
Type
,
object
>
TypeCache
=
new
Dictionary
<
Type
,
object
>();
...
@@ -440,4 +460,63 @@ public class KeyAttribute : Attribute
...
@@ -440,4 +460,63 @@ public class KeyAttribute : Attribute
{
{
}
}
[
AttributeUsage
(
AttributeTargets
.
Property
)]
public
class
WriteAttribute
:
Attribute
{
public
WriteAttribute
(
bool
write
)
{
Write
=
write
;
}
public
bool
Write
{
get
;
private
set
;
}
}
}
}
public
interface
ISqlAdapter
{
int
Insert
(
IDbConnection
connection
,
IDbTransaction
transaction
,
int
?
commandTimeout
,
String
tableName
,
string
columnList
,
string
parameterList
,
IEnumerable
<
PropertyInfo
>
keyProperties
,
object
entityToInsert
);
}
public
class
SqlServerAdapter
:
ISqlAdapter
{
public
int
Insert
(
IDbConnection
connection
,
IDbTransaction
transaction
,
int
?
commandTimeout
,
String
tableName
,
string
columnList
,
string
parameterList
,
IEnumerable
<
PropertyInfo
>
keyProperties
,
object
entityToInsert
)
{
string
cmd
=
String
.
Format
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
connection
.
Execute
(
cmd
,
entityToInsert
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
var
r
=
connection
.
Query
(
"select @@IDENTITY id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
return
(
int
)
r
.
First
().
id
;
}
}
public
class
PostgresAdapter
:
ISqlAdapter
{
public
int
Insert
(
IDbConnection
connection
,
IDbTransaction
transaction
,
int
?
commandTimeout
,
String
tableName
,
string
columnList
,
string
parameterList
,
IEnumerable
<
PropertyInfo
>
keyProperties
,
object
entityToInsert
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
AppendFormat
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
sb
.
Append
(
" RETURNING "
);
bool
first
=
true
;
foreach
(
var
property
in
keyProperties
)
{
if
(!
first
)
sb
.
Append
(
", "
);
first
=
false
;
sb
.
Append
(
property
.
Name
);
}
var
results
=
connection
.
Query
(
sb
.
ToString
(),
entityToInsert
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
// Return the key by assinging the corresponding property in the object - by product is that it supports compound primary keys
int
id
=
0
;
foreach
(
var
p
in
keyProperties
)
{
var
value
=
((
IDictionary
<
string
,
object
>)
results
.
First
())[
p
.
Name
.
ToLower
()];
p
.
SetValue
(
entityToInsert
,
value
,
null
);
if
(
id
==
0
)
id
=
Convert
.
ToInt32
(
value
);
}
return
id
;
}
}
\ No newline at end of file
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