Commit 4f95c64d authored by Jovan Popovic's avatar Jovan Popovic

Merge remote-tracking branch 'refs/remotes/StackExchange/master'

parents 01acca6c f0bbbab8
...@@ -429,4 +429,26 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran ...@@ -429,4 +429,26 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
return id; return id;
} }
} }
public partial class FbAdapter
{
public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
{
var cmd = $"insert into {tableName} ({columnList}) values ({parameterList})";
await connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout);
var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();
var keyName = propertyInfos.First().Name;
var r = await connection.QueryAsync($"SELECT FIRST 1 {keyName} ID FROM {tableName} ORDER BY {keyName} DESC", transaction: transaction, commandTimeout: commandTimeout);
var id = r.First().ID;
if (id == null) return 0;
if (!propertyInfos.Any()) return Convert.ToInt32(id);
var idp = propertyInfos.First();
idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null);
return Convert.ToInt32(id);
}
}
#endif #endif
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"description": "The official collection of get, insert, update and delete helpers for dapper.net. Also handles lists of entities and optional \"dirty\" tracking of interface-based entities.", "description": "The official collection of get, insert, update and delete helpers for dapper.net. Also handles lists of entities and optional \"dirty\" tracking of interface-based entities.",
"version": "1.50.2-*", "version": "1.50.2-*",
"title": "Dapper.Contrib", "title": "Dapper.Contrib",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
"Dapper": { "Dapper": {
"version": "1.50.2-*", "version": "1.50.2-*",
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"description": "Extension handlers for entity framework", "description": "Extension handlers for entity framework",
"version": "1.50.2-*", "version": "1.50.2-*",
"title": "Dapper entity framework type handlers (with a strong name)", "title": "Dapper entity framework type handlers (with a strong name)",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
"Dapper.StrongName": { "Dapper.StrongName": {
"version": "1.50.2-*", "version": "1.50.2-*",
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"description": "Extension handlers for entity framework", "description": "Extension handlers for entity framework",
"version": "1.50.2-*", "version": "1.50.2-*",
"title": "Dapper entity framework type handlers", "title": "Dapper entity framework type handlers",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
"Dapper": { "Dapper": {
"version": "1.50.2-*", "version": "1.50.2-*",
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"description": "The Dapper SqlBuilder component, for building SQL queries dynamically.", "description": "The Dapper SqlBuilder component, for building SQL queries dynamically.",
"version": "1.50.2-*", "version": "1.50.2-*",
"title": "Dapper SqlBuilder component", "title": "Dapper SqlBuilder component",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
"Dapper": { "Dapper": {
"version": "1.50.2-*", "version": "1.50.2-*",
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
"description": "A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc..", "description": "A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc..",
"version": "1.50.2-*", "version": "1.50.2-*",
"title": "Dapper dot net (strong named)", "title": "Dapper dot net (strong named)",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
}, },
"buildOptions": { "buildOptions": {
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"description": "Dapper Contrib Test Suite", "description": "Dapper Contrib Test Suite",
"title": "Dapper.Tests.Contrib", "title": "Dapper.Tests.Contrib",
"version": "1.0.0", "version": "1.0.0",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
"Dapper": { "Dapper": {
"target": "project" "target": "project"
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"description": "Dapper Core Test Suite", "description": "Dapper Core Test Suite",
"title": "Dapper.Tests", "title": "Dapper.Tests",
"version": "1.0.0-*", "version": "1.0.0-*",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
"Dapper": { "Dapper": {
"target": "project" "target": "project"
......
...@@ -14,8 +14,8 @@ ...@@ -14,8 +14,8 @@
"version": "1.50.2-*", "version": "1.50.2-*",
"authors": [ "Sam Saffron", "Marc Gravell", "Nick Craver" ], "authors": [ "Sam Saffron", "Marc Gravell", "Nick Craver" ],
"description": "A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc..", "description": "A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc..",
"title": "Dapper dot net", "title": "Dapper",
"copyright": "2016 Stack Exchange, Inc.", "copyright": "2017 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
}, },
"buildOptions": { "buildOptions": {
......
...@@ -374,6 +374,50 @@ Query<Thing>("select * from Thing where Name = @Name", new {Name = new DbString ...@@ -374,6 +374,50 @@ Query<Thing>("select * from Thing where Name = @Name", new {Name = new DbString
On SQL Server it is crucial to use the unicode when querying unicode and ansi when querying non unicode. On SQL Server it is crucial to use the unicode when querying unicode and ansi when querying non unicode.
Type Switching Per Row
---------------------
Usually you'll want to treat all rows from a given table as the same data type. However, there are some circumstances where it's useful to be able to parse different rows as different data types. This is where `IDataReader.GetRowParser` comes in handy.
Imagine you have a database table named "Shapes" with the columns: `Id`, `Type`, and `Data`, and you want to parse its rows into `Circle`, `Square`, or `Triangle` objects based on the value of the Type column.
```csharp
var shapes = new List<IShape>();
using (var reader = connection.ExecuteReader("select * from Shapes"))
{
// Generate a row parser for each type you expect.
// The generic type <IShape> is what the parser will return.
// The argument (typeof(*)) is the concrete type to parse.
var circleParser = reader.GetRowParser<IShape>(typeof(Circle));
var squareParser = reader.GetRowParser<IShape>(typeof(Square));
var triangleParser = reader.GetRowParser<IShape>(typeof(Triangle));
var typeColumnIndex = reader.GetOrdinal("Type");
while (reader.Read())
{
IShape shape;
var type = (ShapeType)reader.GetInt32(typeColumnIndex);
switch (type)
{
case ShapeType.Circle:
shape = circleParser(reader);
break;
case ShapeType.Square:
shape = squareParser(reader);
break;
case ShapeType.Triangle:
shape = triangleParser(reader);
break;
default:
throw new NotImplementedException();
}
shapes.Add(shape);
}
}
```
Limitations and caveats Limitations and caveats
--------------------- ---------------------
Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. The objects it stores are never flushed. If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues. We may convert the dictionaries to an LRU Cache. Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. The objects it stores are never flushed. If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues. We may convert the dictionaries to an LRU Cache.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment