private async Task <object> InsertAsyncImp <T>(PocoData pocoData, string tableName, string primaryKeyName, bool autoIncrement, T poco, bool sync) { if (!OnInsertingInternal(new InsertContext(poco, tableName, autoIncrement, primaryKeyName))) { return(0); } try { OpenSharedConnectionInternal(); var preparedInsert = InsertStatements.PrepareInsertSql(this, pocoData, tableName, primaryKeyName, autoIncrement, poco); using (var cmd = CreateCommand(_sharedConnection, preparedInsert.Sql, preparedInsert.Rawvalues.ToArray())) { // Assign the Version column InsertStatements.AssignVersion(poco, preparedInsert); object id; if (!autoIncrement) { _ = sync ? ExecuteNonQueryHelper(cmd) : await ExecuteNonQueryHelperAsync(cmd).ConfigureAwait(false); id = InsertStatements.AssignNonIncrementPrimaryKey(primaryKeyName, poco, preparedInsert); } else { id = sync ? _dbType.ExecuteInsert(this, cmd, primaryKeyName, preparedInsert.PocoData.TableInfo.UseOutputClause, poco, preparedInsert.Rawvalues.ToArray()) : await _dbType.ExecuteInsertAsync(this, cmd, primaryKeyName, preparedInsert.PocoData.TableInfo.UseOutputClause, poco, preparedInsert.Rawvalues.ToArray()).ConfigureAwait(false); InsertStatements.AssignPrimaryKey(primaryKeyName, poco, id, preparedInsert); } return(id); } } catch (Exception x) { OnExceptionInternal(x); throw; } finally { CloseSharedConnectionInternal(); } }
private async Task <int> InsertBatchAsyncImp <T>(IEnumerable <T> pocos, BatchOptions options, bool sync) { options = options ?? new BatchOptions(); var result = 0; try { OpenSharedConnectionInternal(); PocoData pd = null; foreach (var batchedPocos in pocos.Chunkify(options.BatchSize)) { var preparedInserts = batchedPocos.Select(x => { if (pd == null) { pd = PocoDataFactory.ForType(x.GetType()); } return(InsertStatements.PrepareInsertSql(this, pd, pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, pd.TableInfo.AutoIncrement, x)); }).ToArray(); var sql = new Sql(); foreach (var preparedInsertSql in preparedInserts) { sql.Append(preparedInsertSql.Sql + options.StatementSeperator, preparedInsertSql.Rawvalues.ToArray()); } using (var cmd = CreateCommand(_sharedConnection, sql.SQL, sql.Arguments)) { result += sync ? ExecuteNonQueryHelper(cmd) : await ExecuteNonQueryHelperAsync(cmd).ConfigureAwait(false); } } } catch (Exception x) { OnExceptionInternal(x); throw; } finally { CloseSharedConnectionInternal(); } return(result); }
/// <summary> /// Performs an SQL Insert /// </summary> /// <param name="tableName">The name of the table to insert into</param> /// <param name="primaryKeyName">The name of the primary key column of the table</param> /// <param name="autoIncrement">True if the primary key is automatically allocated by the DB</param> /// <param name="poco">The POCO object that specifies the column values to be inserted</param> /// <returns>The auto allocated primary key of the new record, or null for non-auto-increment tables</returns> /// <remarks>Inserts a poco into a table. If the poco has a property with the same name /// as the primary key the id of the new record is assigned to it. Either way, /// the new id is returned.</remarks> public virtual async Task <object> InsertAsync <T>(string tableName, string primaryKeyName, bool autoIncrement, T poco) { if (!OnInserting(new InsertContext(poco, tableName, autoIncrement, primaryKeyName))) { return(0); } try { OpenSharedConnectionInternal(); var preparedInsert = InsertStatements.PrepareInsertSql(this, tableName, primaryKeyName, autoIncrement, poco); using (var cmd = CreateCommand(_sharedConnection, preparedInsert.Sql, preparedInsert.Rawvalues.ToArray())) { // Assign the Version column InsertStatements.AssignVersion(poco, preparedInsert); object id; if (!autoIncrement) { await _dbType.ExecuteNonQueryAsync(this, cmd).ConfigureAwait(false); id = InsertStatements.AssignNonIncrementPrimaryKey(primaryKeyName, poco, preparedInsert); } else { id = await _dbType.ExecuteInsertAsync(this, cmd, primaryKeyName, poco, preparedInsert.Rawvalues.ToArray()).ConfigureAwait(false); InsertStatements.AssignPrimaryKey(primaryKeyName, poco, id, preparedInsert); } return(id); } } catch (Exception x) { OnException(x); throw; } finally { CloseSharedConnectionInternal(); } }