Commit 3ad263d2 authored by johandanforth's avatar johandanforth

Fixes for #336 escape column names

parent 2f7125a0
...@@ -305,14 +305,12 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran ...@@ -305,14 +305,12 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
if (first == null || first.id == null) return 0; if (first == null || first.id == null) return 0;
var id = (int)first.id; var id = (int)first.id;
var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); var pi = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();
if (!propertyInfos.Any()) return id; 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; return id;
} }
} }
...@@ -323,20 +321,39 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran ...@@ -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); var cmd = String.Format("insert into {0} ({1}) values ({2})", tableName, columnList, parameterList);
await connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout).ConfigureAwait(false); 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; var id = r.First().id;
if (id == null) return 0; if (id == null) return 0;
var keyPropertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); var pi = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();
if (keyPropertyInfos.Any()) if (!pi.Any()) return id;
{
var idProperty = keyPropertyInfos.First(); var idp = pi.First();
if (idProperty.PropertyType.Name == "Int16") //for short id/key types issue #196 idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null);
idProperty.SetValue(entityToInsert, (Int16)id, null);
else return id;
idProperty.SetValue(entityToInsert, (int)id, null);
}
return (int)id;
} }
} }
...@@ -385,15 +402,16 @@ public partial class SQLiteAdapter ...@@ -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) 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; return id;
} }
} }
This diff is collapsed.
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