/// <summary> /// Converts the first column of the first row of the <see cref="DbDataReader"/> to an object in an asynchronous way. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <returns>An instance of extracted object as value result.</returns> public async Task <TResult> ScalarAsync <TResult>() { var value = default(TResult); // Only if there are record if (await m_reader.ReadAsync()) { // TODO: This can be compiled expression using the 'Get<Type>()' method value = ObjectConverter.ToType <TResult>(m_reader[0]); } // Move to next result await NextResultAsync(); // Return the result return(value); }
/// <summary> /// Inserts a new data in the database in an asynchronous way. /// </summary> /// <typeparam name="TEntity">The type of the object (whether a data entity or a dynamic).</typeparam> /// <typeparam name="TResult">The target type of the result.</typeparam> /// <param name="connection">The connection object to be used.</param> /// <param name="tableName">The name of the target table to be used.</param> /// <param name="entity">The data entity or dynamic object to be inserted.</param> /// <param name="fields">The mapping list of <see cref="Field"/> objects to be used.</param> /// <param name="commandTimeout">The command timeout in seconds to be used.</param> /// <param name="transaction">The transaction to be used.</param> /// <param name="trace">The trace object to be used.</param> /// <param name="statementBuilder">The statement builder object to be used.</param> /// <param name="skipIdentityCheck">True to skip the identity check.</param> /// <returns>The value of the identity field if present, otherwise, the value of primary field.</returns> internal async static Task <TResult> InsertAsyncInternalBase <TEntity, TResult>(this IDbConnection connection, string tableName, TEntity entity, IEnumerable <Field> fields = null, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null, bool skipIdentityCheck = false) where TEntity : class { // Variables needed var dbSetting = connection.GetDbSetting(); // Get the database fields var dbFields = await DbFieldCache.GetAsync(connection, tableName, transaction); // Get the function var callback = new Func <InsertExecutionContext <TEntity> >(() => { // Variables needed var identity = (Field)null; var inputFields = (IEnumerable <DbField>)null; var identityDbField = dbFields?.FirstOrDefault(f => f.IsIdentity); // Set the identity field if (skipIdentityCheck == false) { identity = IdentityCache.Get <TEntity>()?.AsField(); if (identity == null && identityDbField != null) { identity = FieldCache.Get <TEntity>().FirstOrDefault(field => string.Equals(field.Name.AsUnquoted(true, dbSetting), identityDbField.Name.AsUnquoted(true, dbSetting), StringComparison.OrdinalIgnoreCase)); } } // Filter the actual properties for input fields inputFields = dbFields? .Where(dbField => dbField.IsIdentity == false) .Where(dbField => fields.FirstOrDefault(field => string.Equals(field.Name.AsUnquoted(true, dbSetting), dbField.Name.AsUnquoted(true, dbSetting), StringComparison.OrdinalIgnoreCase)) != null) .AsList(); // Variables for the entity action var identityPropertySetter = (Action <TEntity, object>)null; // Get the identity setter if (skipIdentityCheck == false && identity != null) { identityPropertySetter = FunctionCache.GetDataEntityPropertyValueSetterFunction <TEntity>(identity); } // Identify the requests var insertRequest = (InsertRequest)null; // Create a different kind of requests if (typeof(TEntity) == typeof(object)) { insertRequest = new InsertRequest(tableName, connection, transaction, fields, statementBuilder); } else { insertRequest = new InsertRequest(typeof(TEntity), connection, transaction, fields, statementBuilder); } // Return the value return(new InsertExecutionContext <TEntity> { CommandText = CommandTextCache.GetInsertText(insertRequest), InputFields = inputFields, ParametersSetterFunc = FunctionCache.GetDataEntityDbCommandParameterSetterFunction <TEntity>( string.Concat(typeof(TEntity).FullName, ".", tableName, ".Insert"), inputFields?.AsList(), null, dbSetting), IdentityPropertySetterFunc = identityPropertySetter }); }); // Get the context var context = InsertExecutionContextCache <TEntity> .Get(tableName, fields, callback); // Before Execution if (trace != null) { var cancellableTraceLog = new CancellableTraceLog(context.CommandText, entity, null); trace.BeforeInsert(cancellableTraceLog); if (cancellableTraceLog.IsCancelled) { if (cancellableTraceLog.IsThrowException) { throw new CancelledExecutionException(context.CommandText); } return(default(TResult)); } context.CommandText = (cancellableTraceLog.Statement ?? context.CommandText); entity = (TEntity)(cancellableTraceLog.Parameter ?? entity); } // Before Execution Time var beforeExecutionTime = DateTime.UtcNow; // Execution variables var result = default(TResult); // Create the command using (var command = (DbCommand)(await connection.EnsureOpenAsync()).CreateCommand(context.CommandText, CommandType.Text, commandTimeout, transaction)) { // Set the values context.ParametersSetterFunc(command, entity); // Actual Execution result = ObjectConverter.ToType <TResult>(command.ExecuteScalar()); // Get explicity if needed if (Equals(result, default(TResult)) == true && dbSetting.IsMultiStatementExecutable == false) { result = ObjectConverter.ToType <TResult>(connection.GetDbHelper().GetScopeIdentity(connection, transaction)); } // Set the return value if (Equals(result, default(TResult)) == false) { context.IdentityPropertySetterFunc?.Invoke(entity, result); } } // After Execution if (trace != null) { trace.AfterInsert(new TraceLog(context.CommandText, entity, result, DateTime.UtcNow.Subtract(beforeExecutionTime))); } // Return the result return(result); }
/// <summary> /// Gets the long value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override long GetInt64(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <long>(GetValue(i))); }
/// <summary> /// Gets the string value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override string GetString(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <string>(GetValue(i))); }
/// <summary> /// Gets the float value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override float GetFloat(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <float>(GetValue(i))); }
/// <summary> /// Gets the int value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override int GetInt32(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <int>(GetValue(i))); }
/// <summary> /// Gets the double value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override double GetDouble(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <double>(GetValue(i))); }
/// <summary> /// Gets the decimal value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override decimal GetDecimal(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <decimal>(GetValue(i))); }
/// <summary> /// Gets the date time value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override DateTime GetDateTime(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <DateTime>(GetValue(i))); }
/// <summary> /// Gets the char value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override char GetChar(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <char>(GetValue(i))); }
/// <summary> /// Gets the boolean value from the defined property index. /// </summary> /// <param name="i">The index of the property.</param> /// <returns>The value from the property index.</returns> public override bool GetBoolean(int i) { ThrowExceptionIfNotAvailable(); return(ObjectConverter.ToType <bool>(GetValue(i))); }