Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b89dfb8

Browse files
committed
Add db.CreateParam() helper for creating detached Db params
1 parent 3a51fff commit b89dfb8

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,5 +1705,41 @@ public class OrmLiteDataParameter : IDbDataParameter
17051705
public byte Scale { get; set; }
17061706
public int Size { get; set; }
17071707
}
1708+
1709+
public static class DbDataParameterExtensions
1710+
{
1711+
public static IDbDataParameter CreateParam(this IDbConnection db,
1712+
string name,
1713+
object value=null,
1714+
DbType? dbType=null,
1715+
bool? isNullable=null,
1716+
byte? precision=null,
1717+
byte? scale=null,
1718+
int? size=null)
1719+
{
1720+
var dialectProvider = db.GetDialectProvider();
1721+
1722+
var to = new OrmLiteDataParameter
1723+
{
1724+
ParameterName = dialectProvider.GetParam(name),
1725+
Value = value,
1726+
};
1727+
1728+
var valueType = value != null ? value.GetType() : typeof(string);
1729+
if (dbType != null)
1730+
to.DbType = dbType.GetValueOrDefault(dialectProvider.GetColumnDbType(valueType));
1731+
1732+
to.IsNullable = isNullable.GetValueOrDefault(value == null || !valueType.IsNullableType());
1733+
1734+
if (precision != null)
1735+
to.Precision = precision.Value;
1736+
if (scale != null)
1737+
to.Scale = scale.Value;
1738+
if (size != null)
1739+
to.Size = size.Value;
1740+
1741+
return to;
1742+
}
1743+
}
17081744
}
17091745

src/ServiceStack.OrmLite/OrmLiteReadApiAsync.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ public static Task<T> SingleFmtAsync<T>(this IDbConnection dbConn, string sqlFor
183183
return dbConn.Exec(dbCmd => dbCmd.SingleWhereAsync<T>(name, value, token));
184184
}
185185

186+
public static T ScalarAsync<T>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams)
187+
{
188+
if (sqlParams != null)
189+
dbCmd.SetParameters(sqlParams);
190+
191+
return dbCmd.Scalar<T>(sql);
192+
}
193+
186194
/// <summary>
187195
/// Returns a single scalar value using an SqlExpression. E.g:
188196
/// <para>db.Column&lt;int&gt;(db.From&lt;Persion&gt;().Select(x => Sql.Count("*")).Where(q => q.Age > 40))</para>

0 commit comments

Comments
 (0)