Example #1
0
        /// <inheritdoc/>
        /// <exception cref="OperationException">
        /// <paramref name="databaseContext"/> does not have a <see cref="IDatabaseContext.Connection"/> of type <see cref="SqlConnection"/>.
        /// </exception>
        public IDbCommand Create <T>(IDatabaseContext databaseContext, string commandText, string parameterName, IEnumerable <T> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentException($"'{nameof(commandText)}' cannot be null or whitespace.", nameof(commandText));
            }

            if (string.IsNullOrWhiteSpace(parameterName))
            {
                throw new ArgumentException($"'{nameof(parameterName)}' cannot be null or whitespace.", nameof(parameterName));
            }

            if (databaseContext.Connection is not SqlConnection sqlConnection)
            {
                throw new OperationException(databaseContext, ErrorMessages.DatabaseContextConnectionIsNotSqlConnection);
            }

            var       sqlCommand = new SqlCommand(commandText, sqlConnection);
            DataTable dataTable  = null;

            try
            {
                databaseContext.AssociateTransaction(sqlCommand);
                var dataTableLoader = new DataTableLoader <T>(databaseContext.RepositoryAdapter.DefinitionProvider);
                dataTable = dataTableLoader.Load(items);
                var tableParameter = sqlCommand.Parameters.AddWithValue(
                    $"{databaseContext.RepositoryAdapter.NameQualifier.AddParameterPrefix(parameterName)}",
                    dataTable);

                tableParameter.SqlDbType = SqlDbType.Structured;
                tableParameter.TypeName  = typeof(T).GetCustomAttribute <TableTypeAttribute>()?.TypeName ?? typeof(T).Name;
            }
            catch
            {
                dataTable?.Dispose();
                sqlCommand.Dispose();
                throw;
            }

            sqlCommand.Disposed += (_, _) => dataTable.Dispose();
            return(sqlCommand);
        }
Example #2
0
        /// <inheritdoc />
        /// <exception cref="OperationException">
        /// <paramref name="databaseContext"/> does not have a <see cref="IDatabaseContext.Connection"/> of type <see cref="SqlConnection"/>.
        /// </exception>
        public IDbCommand Create <T>(IDatabaseContext databaseContext, string commandText, string parameterName, IEnumerable <T> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (databaseContext.Connection is not SqlConnection sqlConnection)
            {
                throw new OperationException(databaseContext.Connection, ErrorMessages.DatabaseContextConnectionIsNotSqlConnection);
            }

            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentException($"'{nameof(commandText)}' cannot be null or whitespace.", nameof(commandText));
            }

            if (string.IsNullOrWhiteSpace(parameterName))
            {
                throw new ArgumentException($"'{nameof(parameterName)}' cannot be null or whitespace.", nameof(parameterName));
            }

            var sqlCommand = new SqlCommand(commandText, sqlConnection);

            try
            {
                databaseContext.AssociateTransaction(sqlCommand);
                var serializationOptions = new JsonSerializerOptions();
                serializationOptions.Converters.Add(new MoneyConverter());

                var tableParameter = sqlCommand.Parameters.AddWithValue(
                    $"{databaseContext.RepositoryAdapter.NameQualifier.AddParameterPrefix(parameterName)}",
                    JsonSerializer.Serialize(items.ToList(), serializationOptions));

                tableParameter.SqlDbType = SqlDbType.NVarChar;
                tableParameter.Size      = 4000;
            }
            catch
            {
                sqlCommand.Dispose();
                throw;
            }

            return(sqlCommand);
        }
        /// <exception cref="OperationException">
        /// <paramref name="databaseContext"/> does not have a <see cref="IDatabaseContext.Connection"/> of the type <see cref="NpgsqlConnection"/>.
        /// </exception>
        /// <inheritdoc />
        public IDbCommand Create <T>(IDatabaseContext databaseContext, string commandText, string parameterName, IEnumerable <T> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (databaseContext.Connection is not NpgsqlConnection sqlConnection)
            {
                throw new OperationException(databaseContext.Connection, $"This operation requires a connection type of {nameof(NpgsqlConnection)}.");
            }

            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentException($"'{nameof(commandText)}' cannot be null or whitespace.", nameof(commandText));
            }

            if (string.IsNullOrWhiteSpace(parameterName))
            {
                throw new ArgumentException($"'{nameof(parameterName)}' cannot be null or whitespace.", nameof(parameterName));
            }

            var npgsqlCommand = new NpgsqlCommand(commandText, sqlConnection);

            try
            {
                databaseContext.AssociateTransaction(npgsqlCommand);

                var parameter = new NpgsqlParameter(parameterName, NpgsqlDbType.Jsonb)
                {
                    Value = items
                };

                npgsqlCommand.Parameters.Add(parameter);
            }
            catch
            {
                npgsqlCommand.Dispose();
                throw;
            }

            return(npgsqlCommand);
        }