Skip to content

Commit

Permalink
Move out the DbCommandExtensions class to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Oct 17, 2024
1 parent 9c6c893 commit fd76906
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 67 deletions.
86 changes: 86 additions & 0 deletions src/Hangfire.SqlServer/DbCommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// This file is part of Hangfire. Copyright © 2024 Hangfire OÜ.
//
// Hangfire is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3
// of the License, or any later version.
//
// Hangfire is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Data;
using System.Data.Common;
using Hangfire.Annotations;

namespace Hangfire.SqlServer;

internal static class DbCommandExtensions
{
public static DbCommand Create(
[NotNull] this DbConnection connection,
[NotNull] string text,
CommandType type = CommandType.Text,
int? timeout = null)
{
if (connection == null) throw new ArgumentNullException(nameof(connection));
if (text == null) throw new ArgumentNullException(nameof(text));

var command = connection.CreateCommand();
command.CommandType = type;
command.CommandText = text;

if (timeout.HasValue)
{
command.CommandTimeout = timeout.Value;
}

return command;
}

public static DbCommand AddParameter(
[NotNull] this DbCommand command,
[NotNull] string parameterName,
[CanBeNull] object value,
DbType dbType,
[CanBeNull] int? size = null)
{
if (command == null) throw new ArgumentNullException(nameof(command));
if (parameterName == null) throw new ArgumentNullException(nameof(parameterName));

var parameter = command.CreateParameter();
parameter.ParameterName = parameterName;
parameter.DbType = dbType;
parameter.Value = value ?? DBNull.Value;

if (size.HasValue) parameter.Size = size.Value;

command.Parameters.Add(parameter);
return command;
}

public static DbCommand AddReturnParameter(
[NotNull] this DbCommand command,
string parameterName,
out DbParameter parameter,
DbType dbType,
int? size = null)
{
if (command == null) throw new ArgumentNullException(nameof(command));

parameter = command.CreateParameter();
parameter.ParameterName = parameterName;
parameter.DbType = dbType;
parameter.Direction = ParameterDirection.ReturnValue;

if (size.HasValue) parameter.Size = size.Value;

command.Parameters.Add(parameter);
return command;
}
}
67 changes: 0 additions & 67 deletions src/Hangfire.SqlServer/SqlCommandBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,10 @@

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Hangfire.Annotations;

namespace Hangfire.SqlServer
{
internal static class DbCommandExtensions
{
public static DbCommand Create(
[NotNull] this DbConnection connection,
[NotNull] string text,
CommandType type = CommandType.Text,
int? timeout = null)
{
if (connection == null) throw new ArgumentNullException(nameof(connection));
if (text == null) throw new ArgumentNullException(nameof(text));

var command = connection.CreateCommand();
command.CommandType = type;
command.CommandText = text;

if (timeout.HasValue)
{
command.CommandTimeout = timeout.Value;
}

return command;
}

public static DbCommand AddParameter(
[NotNull] this DbCommand command,
[NotNull] string parameterName,
[CanBeNull] object value,
DbType dbType,
[CanBeNull] int? size = null)
{
if (command == null) throw new ArgumentNullException(nameof(command));
if (parameterName == null) throw new ArgumentNullException(nameof(parameterName));

var parameter = command.CreateParameter();
parameter.ParameterName = parameterName;
parameter.DbType = dbType;
parameter.Value = value ?? DBNull.Value;

if (size.HasValue) parameter.Size = size.Value;

command.Parameters.Add(parameter);
return command;
}

public static DbCommand AddReturnParameter(
[NotNull] this DbCommand command,
string parameterName,
out DbParameter parameter,
DbType dbType,
int? size = null)
{
if (command == null) throw new ArgumentNullException(nameof(command));

parameter = command.CreateParameter();
parameter.ParameterName = parameterName;
parameter.DbType = dbType;
parameter.Direction = ParameterDirection.ReturnValue;

if (size.HasValue) parameter.Size = size.Value;

command.Parameters.Add(parameter);
return command;
}
}

internal sealed class SqlCommandBatch : IDisposable
{
private readonly List<DbCommand> _commandList = new List<DbCommand>();
Expand Down

0 comments on commit fd76906

Please sign in to comment.