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
3ad263d2
Commit
3ad263d2
authored
Oct 14, 2015
by
johandanforth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes for #336 escape column names
parent
2f7125a0
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
147 additions
and
56 deletions
+147
-56
SqlMapperExtensionsAsync.cs
Dapper.Contrib NET45/SqlMapperExtensionsAsync.cs
+43
-25
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+104
-31
No files found.
Dapper.Contrib NET45/SqlMapperExtensionsAsync.cs
View file @
3ad263d2
...
...
@@ -305,14 +305,12 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
if
(
first
==
null
||
first
.
id
==
null
)
return
0
;
var
id
=
(
int
)
first
.
id
;
var
propertyInfos
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(!
propertyInfos
.
Any
())
return
id
;
var
pi
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(!
pi
.
Any
())
return
id
;
var
idp
=
pi
.
First
();
idp
.
SetValue
(
entityToInsert
,
Convert
.
ChangeType
(
id
,
idp
.
PropertyType
),
null
);
var
idProperty
=
propertyInfos
.
First
();
if
(
idProperty
.
PropertyType
.
Name
==
"Int16"
)
//for short id/key types issue #196
idProperty
.
SetValue
(
entityToInsert
,
(
Int16
)
id
,
null
);
else
idProperty
.
SetValue
(
entityToInsert
,
id
,
null
);
return
id
;
}
}
...
...
@@ -323,20 +321,39 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
{
var
cmd
=
String
.
Format
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
await
connection
.
ExecuteAsync
(
cmd
,
entityToInsert
,
transaction
,
commandTimeout
).
ConfigureAwait
(
false
);
var
r
=
await
connection
.
QueryAsync
<
dynamic
>(
"select @@IDENTITY id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
).
ConfigureAwait
(
false
);
var
r
=
(
await
connection
.
QueryAsync
<
dynamic
>(
"select @@IDENTITY id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
).
ConfigureAwait
(
false
)).
ToList
();
if
(
r
.
First
()
==
null
||
r
.
First
().
id
==
null
)
return
0
;
var
id
=
(
int
)
r
.
First
().
id
;
var
pi
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(!
pi
.
Any
())
return
id
;
var
idp
=
pi
.
First
();
idp
.
SetValue
(
entityToInsert
,
Convert
.
ChangeType
(
id
,
idp
.
PropertyType
),
null
);
return
id
;
}
}
public
partial
class
MySqlAdapter
:
ISqlAdapter
{
public
async
Task
<
int
>
InsertAsync
(
IDbConnection
connection
,
IDbTransaction
transaction
,
int
?
commandTimeout
,
string
tableName
,
string
columnList
,
string
parameterList
,
IEnumerable
<
PropertyInfo
>
keyProperties
,
object
entityToInsert
)
{
var
cmd
=
String
.
Format
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
await
connection
.
ExecuteAsync
(
cmd
,
entityToInsert
,
transaction
,
commandTimeout
).
ConfigureAwait
(
false
);
var
r
=
await
connection
.
QueryAsync
<
dynamic
>(
"select LAST_INSERT_ID()"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
).
ConfigureAwait
(
false
);
var
id
=
r
.
First
().
id
;
if
(
id
==
null
)
return
0
;
var
keyPropertyInfos
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(
keyPropertyInfos
.
Any
())
{
var
idProperty
=
keyPropertyInfos
.
First
();
if
(
idProperty
.
PropertyType
.
Name
==
"Int16"
)
//for short id/key types issue #196
idProperty
.
SetValue
(
entityToInsert
,
(
Int16
)
id
,
null
);
else
idProperty
.
SetValue
(
entityToInsert
,
(
int
)
id
,
null
);
}
return
(
int
)
id
;
var
pi
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(!
pi
.
Any
())
return
id
;
var
idp
=
pi
.
First
();
idp
.
SetValue
(
entityToInsert
,
Convert
.
ChangeType
(
id
,
idp
.
PropertyType
),
null
);
return
id
;
}
}
...
...
@@ -385,15 +402,16 @@ public partial class SQLiteAdapter
public
async
Task
<
int
>
InsertAsync
(
IDbConnection
connection
,
IDbTransaction
transaction
,
int
?
commandTimeout
,
String
tableName
,
string
columnList
,
string
parameterList
,
IEnumerable
<
PropertyInfo
>
keyProperties
,
object
entityToInsert
)
{
var
cmd
=
String
.
Format
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
var
cmd
=
String
.
Format
(
"insert into {0} ({1}) values ({2}); select last_insert_rowid() id"
,
tableName
,
columnList
,
parameterList
);
var
multi
=
await
connection
.
QueryMultipleAsync
(
cmd
,
entityToInsert
,
transaction
,
commandTimeout
);
await
connection
.
ExecuteAsync
(
cmd
,
entityToInsert
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
).
ConfigureAwait
(
false
);
var
id
=
(
int
)
multi
.
Read
().
First
().
id
;
var
pi
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(!
pi
.
Any
())
return
id
;
var
idp
=
pi
.
First
();
idp
.
SetValue
(
entityToInsert
,
Convert
.
ChangeType
(
id
,
idp
.
PropertyType
),
null
);
var
r
=
await
connection
.
QueryAsync
<
dynamic
>(
"select last_insert_rowid() id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
).
ConfigureAwait
(
false
);
var
id
=
(
int
)
r
.
First
().
id
;
var
propertyInfos
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(
propertyInfos
.
Any
())
propertyInfos
.
First
().
SetValue
(
entityToInsert
,
id
,
null
);
return
id
;
}
}
Dapper.Contrib/SqlMapperExtensions.cs
View file @
3ad263d2
This diff is collapsed.
Click to expand it.
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