public int Insert(IDbConnection connection, IDbTransaction transaction, int?commandTimeout, string tableName, string columnList, string parameterList, IEnumerable <PropertyInfo> keyProperties, ColumNameMapperDelegate columnNameMapper, object entityToInsert) { var cmd = $"INSERT INTO {tableName} ({columnList}) VALUES ({parameterList}); SELECT last_insert_rowid() id"; var multi = connection.QueryMultiple(cmd, entityToInsert, transaction, commandTimeout); var id = (int)multi.Read().First().id; var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); if (!propertyInfos.Any()) { return(id); } var idProperty = propertyInfos.First(); idProperty.SetValue(entityToInsert, Convert.ChangeType(id, idProperty.PropertyType), null); return(id); }
public int Insert(IDbConnection connection, IDbTransaction transaction, int?commandTimeout, string tableName, string columnList, string parameterList, IEnumerable <PropertyInfo> keyProperties, ColumNameMapperDelegate columnNameMapper, object entityToInsert) { var cmd = $"insert into {tableName} ({columnList}) values ({parameterList})"; connection.Execute(cmd, entityToInsert, transaction, commandTimeout); var r = connection.Query("Select LAST_INSERT_ID() id", transaction: transaction, commandTimeout: commandTimeout); var id = r.First().id; if (id == null) { return(0); } var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); if (!propertyInfos.Any()) { return(Convert.ToInt32(id)); } var idp = propertyInfos.First(); idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null); return(Convert.ToInt32(id)); }
public int Insert(IDbConnection connection, IDbTransaction transaction, int?commandTimeout, string tableName, string columnList, string parameterList, IEnumerable <PropertyInfo> keyProperties, ColumNameMapperDelegate columnNameMapper, object entityToInsert) { var 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 var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); if (!propertyInfos.Any()) { sb.Append(" RETURNING *"); } else { sb.Append(" RETURNING "); var first = true; foreach (var property in propertyInfos) { if (!first) { sb.Append(", "); } first = false; sb.AppendFormat("\"{0}\"", columnNameMapper(property)); } } var results = connection.Query(sb.ToString(), entityToInsert, transaction, commandTimeout: commandTimeout).ToList(); // Return the key by assinging the corresponding property in the object - by product is that it supports compound primary keys var id = 0; foreach (var p in propertyInfos) { var value = ((IDictionary <string, object>)results.First())[columnNameMapper(p)]; p.SetValue(entityToInsert, value, null); if (id == 0) { id = Convert.ToInt32(value); } } return(id); }
public int Insert(IDbConnection connection, IDbTransaction transaction, int?commandTimeout, string tableName, string columnList, string parameterList, IEnumerable <PropertyInfo> keyProperties, ColumNameMapperDelegate columnNameMapper, object entityToInsert) { var cmd = $"INSERT INTO {tableName} ({columnList}) VALUES ({parameterList})"; connection.Execute(cmd, entityToInsert, transaction, commandTimeout); var r = connection.Query("SELECT @@IDENTITY [id]", transaction: transaction, commandTimeout: commandTimeout).ToList(); if (r.First().id == null) { return(0); } var id = (int)r.First().id; var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); if (!propertyInfos.Any()) { return(id); } var idProperty = propertyInfos.First(); idProperty.SetValue(entityToInsert, Convert.ChangeType(id, idProperty.PropertyType), null); return(id); }