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
c8ca11ef
Commit
c8ca11ef
authored
Jun 07, 2015
by
johandanforth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Contrib Insert support for short/int16 id/key Issue #196
parent
e408faba
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
13 deletions
+57
-13
Assert.cs
Dapper.Contrib.Tests/Assert.cs
+7
-0
Program.cs
Dapper.Contrib.Tests/Program.cs
+1
-0
Tests.cs
Dapper.Contrib.Tests/Tests.cs
+34
-6
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+15
-7
No files found.
Dapper.Contrib.Tests/Assert.cs
View file @
c8ca11ef
...
...
@@ -64,6 +64,13 @@ public static void IsNull(this object obj)
throw
new
ApplicationException
(
"Expected null"
);
}
}
public
static
void
IsNotNull
(
this
object
obj
)
{
if
(
obj
==
null
)
{
throw
new
ApplicationException
(
"Expected not null"
);
}
}
}
}
\ No newline at end of file
Dapper.Contrib.Tests/Program.cs
View file @
c8ca11ef
...
...
@@ -28,6 +28,7 @@ private static void Setup()
using
(
var
connection
=
new
SqlCeConnection
(
connectionString
))
{
connection
.
Open
();
connection
.
Execute
(
@" create table Stuff (TheId int IDENTITY(1,1) not null, Name nvarchar(100) not null, Created DateTime null) "
);
connection
.
Execute
(
@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "
);
connection
.
Execute
(
@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) "
);
connection
.
Execute
(
@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "
);
...
...
Dapper.Contrib.Tests/Tests.cs
View file @
c8ca11ef
...
...
@@ -33,6 +33,15 @@ public class Person
public
string
Name
{
get
;
set
;
}
}
[
Table
(
"Stuff"
)]
public
class
Stuff
{
[
Key
]
public
short
TheId
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
DateTime
?
Created
{
get
;
set
;
}
}
[
Table
(
"Automobiles"
)]
public
class
Car
{
...
...
@@ -71,6 +80,30 @@ private IDbConnection GetConnection()
return
connection
;
}
public
void
ShortIdentity
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
id
=
connection
.
Insert
(
new
Stuff
()
{
Name
=
"First item"
});
id
.
IsEqualTo
(
1
);
var
item
=
connection
.
Get
<
Stuff
>(
1
);
item
.
TheId
.
IsEqualTo
((
short
)
1
);
}
}
public
void
NullDateTime
()
{
using
(
var
connection
=
GetOpenConnection
())
{
connection
.
Insert
(
new
Stuff
()
{
Name
=
"First item"
});
connection
.
Insert
(
new
Stuff
()
{
Name
=
"Second item"
,
Created
=
DateTime
.
Now
});
var
stuff
=
connection
.
Query
<
Stuff
>(
"select * from stuff"
).
ToList
();
stuff
.
First
().
Created
.
IsNull
();
stuff
.
Last
().
Created
.
IsNotNull
();
}
}
public
void
TableName
()
{
using
(
var
connection
=
GetOpenConnection
())
...
...
@@ -99,11 +132,6 @@ public void TestSimpleGet()
public
void
TestClosedConnection
()
{
//using (var connection = GetOpenConnection())
//{
// connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0);
//}
using
(
var
connection
=
GetConnection
())
{
connection
.
Insert
(
new
User
{
Name
=
"Adama"
,
Age
=
10
}).
IsMoreThan
(
0
);
...
...
Dapper.Contrib/SqlMapperExtensions.cs
View file @
c8ca11ef
...
...
@@ -634,21 +634,29 @@ public partial class SqlServerAdapter : ISqlAdapter
public
int
Insert
(
IDbConnection
connection
,
IDbTransaction
transaction
,
int
?
commandTimeout
,
String
tableName
,
string
columnList
,
string
parameterList
,
IEnumerable
<
PropertyInfo
>
keyProperties
,
object
entityToInsert
)
{
//TODO: Append a select identity at end if command?
//TODO: Create specific sqladapter for sqlce
var
cmd
=
String
.
Format
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
connection
.
Execute
(
cmd
,
entityToInsert
,
transaction
,
commandTimeout
);
//
NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
//
TODO: use IDENT_CURRENT('tablename') or IDENT_SCOPE or SCOPE_IDENTITY() - create specific sqlce adapter with @@IDENTITY
var
r
=
connection
.
Query
(
"select @@IDENTITY id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
var
id
=
(
int
)
r
.
First
().
id
;
var
id
=
r
.
First
().
id
;
var
propertyInfos
=
keyProperties
as
PropertyInfo
[]
??
keyProperties
.
ToArray
();
if
(
propertyInfos
.
Any
())
propertyInfos
.
First
().
SetValue
(
entityToInsert
,
id
,
null
);
return
id
;
{
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
,
(
int
)
id
,
null
);
}
return
(
int
)
id
;
}
}
public
partial
class
PostgresAdapter
:
ISqlAdapter
{
public
int
Insert
(
IDbConnection
connection
,
IDbTransaction
transaction
,
int
?
commandTimeout
,
String
tableName
,
string
columnList
,
string
parameterList
,
IEnumerable
<
PropertyInfo
>
keyProperties
,
object
entityToInsert
)
...
...
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