示例#1
0
 /// <summary>
 /// Asynchronously executes INSERT statement generated by specified table name and dictionary values.
 /// </summary>
 public Task <int> InsertAsync(string tableName, IDictionary <string, object> data)
 {
     if (data == null)
     {
         throw new ArgumentNullException($"{nameof(data)}");
     }
     return(InsertInternalAsync(tableName, DataHelper.GetChangeset(data)));
 }
示例#2
0
 /// <summary>
 /// Asynchronously executes UPDATE statement generated by specified <see cref="Query"/> and POCO model.
 /// </summary>
 public Task <int> UpdateAsync(Query query, object pocoModel)
 {
     if (pocoModel == null)
     {
         throw new ArgumentNullException($"{nameof(pocoModel)}");
     }
     return(UpdateInternalAsync(query, DataHelper.GetChangeset(pocoModel, null)));
 }
示例#3
0
 /// <summary>
 /// Asynchronously executes UPDATE statement generated by specified <see cref="Query"/> and dictionary values.
 /// </summary>
 public Task <int> UpdateAsync(Query query, IDictionary <string, object> data)
 {
     if (data == null)
     {
         throw new ArgumentNullException($"{nameof(data)}");
     }
     return(UpdateInternalAsync(query, DataHelper.GetChangeset(data)));
 }
示例#4
0
        /// <summary>
        /// Asynchronously executes INSERT statement generated by specified table name and POCO model.
        /// </summary>
        public async Task <int> InsertAsync(string tableName, object pocoModel)
        {
            if (pocoModel == null)
            {
                throw new ArgumentNullException($"{nameof(pocoModel)}");
            }
            if (tableName == null)
            {
                throw new ArgumentNullException($"{nameof(tableName)}");
            }

            CancellationToken cancel = CancellationToken.None;
            var isClosedConn         = Connection.State == ConnectionState.Closed;

            if (isClosedConn)
            {
                await Connection.OpenAsync(cancel).ConfigureAwait(false);
            }
            int affected = 0;

            try {
                affected = await InsertInternalAsync(tableName, DataHelper.GetChangeset(pocoModel, null)).ConfigureAwait(false);

                var autoIncrementCol = FindAutoIncrementColumn(pocoModel);
                if (autoIncrementCol != null)
                {
                    var insertedId = await CommandBuilder.DbFactory.GetInsertIdAsync(Connection, cancel).ConfigureAwait(false);

                    if (insertedId != null)
                    {
                        autoIncrementCol.SetValue(pocoModel, insertedId);
                    }
                }
            } finally {
                if (isClosedConn)
                {
                    Connection.Close();
                }
            }

            return(affected);
        }
示例#5
0
        /// <summary>
        /// Executes INSERT statement generated by specified table name and annotated POCO model.
        /// </summary>
        /// <param name="tableName">table name</param>
        /// <param name="pocoModel">POCO model with public properties that match table columns.</param>
        /// <returns>Number of inserted data records.</returns>
        public int Insert(string tableName, object pocoModel)
        {
            if (pocoModel == null)
            {
                throw new ArgumentNullException($"{nameof(pocoModel)}");
            }
            int affected = 0;

            DataHelper.EnsureConnectionOpen(Connection, () => {
                affected             = InsertInternal(tableName, DataHelper.GetChangeset(pocoModel, null));
                var autoIncrementCol = FindAutoIncrementColumn(pocoModel);
                if (autoIncrementCol != null)
                {
                    var insertedId = CommandBuilder.DbFactory.GetInsertId(Connection);
                    if (insertedId != null)
                    {
                        autoIncrementCol.SetValue(pocoModel, insertedId);
                    }
                }
            });
            return(affected);
        }
示例#6
0
 public static IDbCommand GetInsertCommand(this IDbCommandBuilder cmdBuilder, string table, object poco)
 {
     return(cmdBuilder.GetInsertCommand(table, DataHelper.GetChangeset(poco, null)));
 }
示例#7
0
 public static IDbCommand GetInsertCommand(this IDbCommandBuilder cmdBuilder, string table, IDictionary <string, object> data)
 {
     return(cmdBuilder.GetInsertCommand(table, DataHelper.GetChangeset(data)));
 }
示例#8
0
 public static IDbCommand GetUpdateCommand(this IDbCommandBuilder cmdBuilder, Query q, object poco)
 {
     return(cmdBuilder.GetUpdateCommand(q, DataHelper.GetChangeset(poco, null)));
 }
示例#9
0
 public static IDbCommand GetUpdateCommand(this IDbCommandBuilder cmdBuilder, Query q, IDictionary <string, object> data)
 {
     return(cmdBuilder.GetUpdateCommand(q, DataHelper.GetChangeset(data)));
 }