Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions Dapper.SimpleCRUD/SimpleCRUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,13 @@ private static void BuildUpdateSet<T>(T entityToUpdate, StringBuilder masterSb)
for (var i = 0; i < nonIdProps.Length; i++)
{
var property = nonIdProps[i];
var apParamPrefix = "@";
if (_dialect == Dialect.Oracle)
{
apParamPrefix = ":";
}

sb.AppendFormat("{0} = @{1}", GetColumnName(property), property.Name);
sb.AppendFormat("{0} = " + apParamPrefix + "{1}", GetColumnName(property), property.Name);
if (i < nonIdProps.Length - 1)
sb.AppendFormat(", ");
}
Expand Down Expand Up @@ -744,6 +749,7 @@ private static void BuildWhere<TEntity>(StringBuilder sb, IEnumerable<PropertyIn
for (var i = 0; i < propertyInfos.Count(); i++)
{
var useIsNull = false;
var defaultWhereAttributeStatement = "{0} = @{1}";

//match up generic properties to source entity properties to allow fetching of the column attribute
//the anonymous object used for search doesn't have the custom attributes attached to them so this allows us to build the correct where clause
Expand All @@ -762,8 +768,14 @@ private static void BuildWhere<TEntity>(StringBuilder sb, IEnumerable<PropertyIn
break;
}
}

if (_dialect == Dialect.Oracle)
{
defaultWhereAttributeStatement = defaultWhereAttributeStatement.Replace("@", ":");
}

sb.AppendFormat(
useIsNull ? "{0} is null" : "{0} = @{1}",
useIsNull ? "{0} is null" : defaultWhereAttributeStatement,
GetColumnName(propertyToUse),
propertyToUse.Name);

Expand Down Expand Up @@ -799,7 +811,13 @@ private static void BuildInsertValues<T>(StringBuilder masterSb)

if (property.Name.Equals("Id", StringComparison.OrdinalIgnoreCase) && property.GetCustomAttributes(true).All(attr => attr.GetType().Name != typeof(RequiredAttribute).Name) && property.PropertyType != typeof(Guid)) continue;

sb.AppendFormat("@{0}", property.Name);
var apParamPrefix = "@";
if (_dialect == Dialect.Oracle)
{
apParamPrefix = ":";
}

sb.AppendFormat(apParamPrefix + "{0}", property.Name);
if (i < props.Count() - 1)
sb.Append(", ");
}
Expand Down Expand Up @@ -1078,9 +1096,16 @@ public virtual string ResolveColumnName(PropertyInfo propertyInfo)
if (columnattr != null)
{
columnName = Encapsulate(columnattr.Name);

if (Debugger.IsAttached)
Trace.WriteLine(String.Format("Column name for type overridden from {0} to {1}", propertyInfo.Name, columnName));
}

if (_dialect == Dialect.Oracle)
{
columnName = columnName.ToUpper();
}

return columnName;
}
}
Expand Down
26 changes: 13 additions & 13 deletions Dapper.SimpleCRUD/SimpleCRUDAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ public static async Task<T> GetAsync<T>(this IDbConnection connection, object id
//create a new empty instance of the type to get the base properties
BuildSelect(sb, GetScaffoldableProperties<T>().ToArray());
sb.AppendFormat(" from {0} where ", name);
BuildWhere<T>(sb, idProps);

for (var i = 0; i < idProps.Count; i++)
var dynamicParameters = new DynamicParameters();
foreach (var property in idProps)
{
if (i > 0)
sb.Append(" and ");
sb.AppendFormat("{0} = @{1}", GetColumnName(idProps[i]), idProps[i].Name);
}
var value = id;
if (_dialect == Dialect.Oracle)
{
if (Guid.TryParse(value.ToString(), out var result))
{
value = value.ToString();
}
}

var dynParms = new DynamicParameters();
if (idProps.Count == 1)
dynParms.Add("@" + idProps.First().Name, id);
else
{
foreach (var prop in idProps)
dynParms.Add("@" + prop.Name, id.GetType().GetProperty(prop.Name).GetValue(id, null));
dynamicParameters.Add(property.Name, value);
}

if (Debugger.IsAttached)
Trace.WriteLine(String.Format("Get<{0}>: {1} with Id: {2}", currenttype, sb, id));

var query = await connection.QueryAsync<T>(sb.ToString(), dynParms, transaction, commandTimeout);
var query = await connection.QueryAsync<T>(sb.ToString(), dynamicParameters, transaction, commandTimeout);
return query.FirstOrDefault();
}

Expand Down