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
726af71d
Commit
726af71d
authored
Mar 11, 2012
by
Sam
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #25 from greygeek/
d93b0991
Update object's key(s) after insert and array parameter support.
parents
f97241e5
d93b0991
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
52 deletions
+107
-52
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+23
-8
SqlMapper.cs
Dapper/SqlMapper.cs
+84
-44
No files found.
Dapper.Contrib/SqlMapperExtensions.cs
View file @
726af71d
...
...
@@ -486,7 +486,15 @@ public int Insert(IDbConnection connection, IDbTransaction transaction, int? com
//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
;
int
id
=
0
;
foreach
(
var
p
in
keyProperties
)
{
var
value
=
((
IDictionary
<
string
,
object
>)
r
.
First
())[
p
.
Name
.
ToLower
()];
p
.
SetValue
(
entityToInsert
,
value
,
null
);
if
(
id
==
0
)
id
=
Convert
.
ToInt32
(
value
);
}
return
id
;
}
}
...
...
@@ -497,15 +505,22 @@ public int Insert(IDbConnection connection, IDbTransaction transaction, int? com
StringBuilder
sb
=
new
StringBuilder
();
sb
.
AppendFormat
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
// If no primary key then safe to assume a join table with not too much data to return
if
(!
keyProperties
.
Any
())
sb
.
Append
(
" RETURNING *"
);
else
{
sb
.
Append
(
" RETURNING "
);
bool
first
=
true
;
foreach
(
var
property
in
keyProperties
)
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
...
...
Dapper/SqlMapper.cs
View file @
726af71d
...
...
@@ -1145,6 +1145,15 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
var
count
=
0
;
if
(
list
!=
null
)
{
if
(
FeatureSupport
.
Get
(
command
.
Connection
).
Arrays
)
{
var
arrayParm
=
command
.
CreateParameter
();
arrayParm
.
Value
=
list
;
arrayParm
.
ParameterName
=
namePrefix
;
command
.
Parameters
.
Add
(
arrayParm
);
}
else
{
bool
isString
=
value
is
IEnumerable
<
string
>;
bool
isDbString
=
value
is
IEnumerable
<
DbString
>;
...
...
@@ -1157,7 +1166,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
if
(
isString
)
{
listParam
.
Size
=
4000
;
if
(
item
!=
null
&&
((
string
)
item
).
Length
>
4000
)
if
(
item
!=
null
&&
((
string
)
item
).
Length
>
4000
)
{
listParam
.
Size
=
-
1
;
}
...
...
@@ -1191,6 +1200,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
});
}
}
}
}
...
...
@@ -2202,4 +2212,34 @@ public void AddParameter(IDbCommand command, string name)
command
.
Parameters
.
Add
(
param
);
}
}
/// <summary>
/// Handles variances in features per DBMS
/// </summary>
public
class
FeatureSupport
{
/// <summary>
/// Dictionary of supported features index by connection type name
/// </summary>
private
static
readonly
Dictionary
<
string
,
FeatureSupport
>
FeatureList
=
new
Dictionary
<
string
,
FeatureSupport
>()
{
{
"sqlserverconnection"
,
new
FeatureSupport
{
Arrays
=
false
}},
{
"npgsqlconnection"
,
new
FeatureSupport
{
Arrays
=
true
}}
};
/// <summary>
/// Gets the featureset based on the passed connection
/// </summary>
public
static
FeatureSupport
Get
(
IDbConnection
connection
)
{
string
name
=
connection
.
GetType
().
Name
.
ToLower
();
FeatureSupport
features
;
return
FeatureList
.
TryGetValue
(
name
,
out
features
)
?
features
:
FeatureList
.
Values
.
First
();
}
/// <summary>
/// True if the db supports array columns e.g. Postgresql
/// </summary>
public
bool
Arrays
{
get
;
set
;
}
}
}
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