Beispiel #1
0
        public void StoreKeys(IEnumerable <string> keys, IDbTransaction dbTransaction = default)
        {
            keys = keys ?? throw new ArgumentNullException(nameof(keys));

            string sql = $@"
INSERT INTO {_boost.FullTableName} ({FIELD_LIST}) VALUES({FIELD_PARAM_LIST});";
            Action <IDbConnection, IDbTransaction> exec = (conn, tx) =>
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                foreach (var key in keys)
                {
                    var paramLst = new List <SqlParameter>
                    {
                        new SqlParameter("@Id", Snowflake.Default().NextId()),
                        new SqlParameter("@Version", _redboxOptions.Version),
                        new SqlParameter("@Key", key),
                        new SqlParameter("@Retries", "0"),
                        new SqlParameter("@AddedAt", DateTime.Now),
                        new SqlParameter("@ExpiresAt", DBNull.Value),
                        new SqlParameter("@State", KeyState.Waiting)
                    };
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = sql;
                        paramLst.ForEach(p => cmd.Parameters.Add(p));
                        cmd.Transaction = dbTransaction;
                        cmd.ExecuteNonQuery();
                    }
                }
            };

            if (dbTransaction == null)
            {
                using (var conn = new SqlConnection(_options.ConnectionString))
                {
                    exec(conn, null);
                }
            }
            else
            {
                var conn = dbTransaction.Connection;
                exec(conn, dbTransaction);
            }
        }