Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Apr 11, 2024
2 parents 1cf47f8 + 2f51392 commit fc2ca21
Show file tree
Hide file tree
Showing 8 changed files with 1,149 additions and 1,023 deletions.
4 changes: 2 additions & 2 deletions FetchXmlBuilder/Converters/CSharpCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private string GetLinkEntitiesLbL(DataCollection<LinkEntity> linkEntities, strin
foreach (var link in linkEntities)
{
var linkcode = string.Empty;
var linkname = GetVarName(string.IsNullOrEmpty(link.EntityAlias) ? LineStart + "_" + link.LinkToEntityName : link.EntityAlias);
var linkname = GetVarName(string.IsNullOrEmpty(link.EntityAlias) || link.LinkToEntityName == link.EntityAlias ? LineStart + "_" + link.LinkToEntityName : link.EntityAlias);
if (settings.IncludeComments)
{
linkcode += $"//{CRLF}// Add link-entity {linkname}{CRLF}";
Expand Down Expand Up @@ -1435,4 +1435,4 @@ internal static string CapitSchemaName(this AttributeMetadata attribute, QExFlav
return attribute.SchemaName;
}
}
}
}
12 changes: 6 additions & 6 deletions FetchXmlBuilder/Converters/ODataCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static string ConvertToOData2(FetchType fetch, FetchXmlBuilder sender)
}
var organizationServiceUrl = sender.ConnectionDetail.OrganizationDataServiceUrl;
var url = organizationServiceUrl;
var entity = fetch.Items.Where(i => i is FetchEntityType).FirstOrDefault() as FetchEntityType;
var entity = fetch.Item;
if (entity == null)
{
throw new Exception("Fetch must contain entity definition");
Expand Down Expand Up @@ -100,7 +100,7 @@ private static string GetExpand(FetchEntityType entity, FetchXmlBuilder sender,
{
foreach (FetchLinkEntityType linkitem in linkitems)
{
if (linkitem.linktype == "outer")
if (linkitem.linktype == FetchLinkEntityTypeLinktype.outer)
{
throw new Exception("OData queries do not support outer joins");
}
Expand Down Expand Up @@ -197,7 +197,7 @@ private static string GetFilter(FetchEntityType entity, filter filteritem, Fetch
{
if (item is condition)
{
result += GetCondition(entity, item as condition, sender);
result += GetCondition(entity, item as FetchConditionType, sender);
}
else if (item is filter)
{
Expand All @@ -216,7 +216,7 @@ private static string GetFilter(FetchEntityType entity, filter filteritem, Fetch
return result;
}

private static string GetCondition(FetchEntityType entity, condition condition, FetchXmlBuilder sender)
private static string GetCondition(FetchEntityType entity, FetchConditionType condition, FetchXmlBuilder sender)
{
var result = "";
if (!string.IsNullOrEmpty(condition.attribute))
Expand Down Expand Up @@ -410,7 +410,7 @@ private static RelationshipMetadataBase LinkItemToRelation(string entityname, Fe
if (linkitems.Count == 1)
{
var nextlink = (FetchLinkEntityType)linkitems[0];
if (nextlink.linktype == "outer")
if (nextlink.linktype == FetchLinkEntityTypeLinktype.outer)
{
throw new Exception("OData queries do not support outer joins");
}
Expand All @@ -432,7 +432,7 @@ private static RelationshipMetadataBase LinkItemToRelation(string entityname, Fe
if (linkitems.Count == 1)
{
var nextlink = (FetchLinkEntityType)linkitems[0];
if (nextlink.linktype == "outer")
if (nextlink.linktype == FetchLinkEntityTypeLinktype.outer)
{
throw new Exception("OData queries do not support outer joins");
}
Expand Down
24 changes: 12 additions & 12 deletions FetchXmlBuilder/Converters/SQLQueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static string GetSQLQuery(FetchType fetch, FetchXmlBuilder fxb)
{
aliasmap = new Dictionary<string, string>();
var sql = new StringBuilder();
var entity = fetch.Items.FirstOrDefault(i => i is FetchEntityType) as FetchEntityType;
var entity = fetch.Item;
if (entity == null)
{
throw new Exception("Fetch must contain entity definition");
Expand All @@ -35,7 +35,7 @@ internal static string GetSQLQuery(FetchType fetch, FetchXmlBuilder fxb)
selectcols = GetSelect(entity);
ordercols = GetOrder(entity.Items.Where(i => i is FetchOrderType).ToList(), string.Empty);
var join = GetJoin(entity.Items.Where(i => i is FetchLinkEntityType).ToList(), entity.name, fxb);
var where = GetWhere(entity.name, string.Empty, entity.Items.Where(i => i is filter && ((filter)i).Items != null && ((filter)i).Items.Length > 0).ToList(), fxb);
var where = GetWhere(entity.name, string.Empty, entity.Items.Where(i => i is FetchFilterType && ((FetchFilterType)i).Items != null && ((FetchFilterType)i).Items.Length > 0).ToList(), fxb);
sql.AppendLine(string.Join(", ", selectcols));
sql.AppendLine($"FROM {entity.name}");
if (join != null && join.Count > 0)
Expand Down Expand Up @@ -80,7 +80,7 @@ private static List<string> GetJoin(List<object> linkentities, string entityalia
foreach (FetchLinkEntityType linkitem in linkentities)
{
var join = new StringBuilder();
if (linkitem.linktype == "outer")
if (linkitem.linktype == FetchLinkEntityTypeLinktype.outer)
{
join.Append("LEFT OUTER ");
}
Expand All @@ -92,7 +92,7 @@ private static List<string> GetJoin(List<object> linkentities, string entityalia
join.Append($"JOIN {linkitem.name} {linkalias} ON {linkalias}.{linkitem.from} = {entityalias}.{linkitem.to}");
if (linkitem.Items != null)
{
var linkwhere = GetWhere(linkitem.name, linkalias, linkitem.Items.Where(i => i is filter && ((filter)i).Items != null && ((filter)i).Items.Length > 0).ToList(), fxb);
var linkwhere = GetWhere(linkitem.name, linkalias, linkitem.Items.Where(i => i is FetchFilterType && ((FetchFilterType)i).Items != null && ((FetchFilterType)i).Items.Length > 0).ToList(), fxb);
if (!string.IsNullOrEmpty(linkwhere))
{
join.Append($" AND {linkwhere} ");
Expand Down Expand Up @@ -144,35 +144,35 @@ private static string GetWhere(string entityname, string entityalias, List<objec
var resultList = new StringBuilder();
if (filteritems.Count > 0)
{
foreach (filter filteritem in filteritems)
foreach (FetchFilterType filteritem in filteritems)
{
resultList.Append(GetFilter(entityname, entityalias, filteritem, fxb));
}
}
return resultList.ToString();
}

private static string GetFilter(string entityname, string entityalias, filter filteritem, FetchXmlBuilder fxb)
private static string GetFilter(string entityname, string entityalias, FetchFilterType filteritem, FetchXmlBuilder fxb)
{
var result = "";
if (filteritem.Items == null || filteritem.Items.Length == 0)
{
return "";
}
var logical = filteritem.type == filterType.or ? " OR " : " AND ";
var logical = filteritem.type == FetchFilterTypeType.or ? " OR " : " AND ";
if (filteritem.Items.Length > 1)
{
result = "(";
}
foreach (var item in filteritem.Items)
{
if (item is condition)
if (item is FetchConditionType)
{
result += GetCondition(entityname, entityalias, item as condition, fxb);
result += GetCondition(entityname, entityalias, item as FetchConditionType, fxb);
}
else if (item is filter)
else if (item is FetchFilterType)
{
result += GetFilter(entityname, entityalias, item as filter, fxb);
result += GetFilter(entityname, entityalias, item as FetchFilterType, fxb);
}
result += logical;
}
Expand All @@ -187,7 +187,7 @@ private static string GetFilter(string entityname, string entityalias, filter fi
return result;
}

private static string GetCondition(string entityname, string entityalias, condition condition, FetchXmlBuilder fxb)
private static string GetCondition(string entityname, string entityalias, FetchConditionType condition, FetchXmlBuilder fxb)
{
var result = new StringBuilder();
if (!string.IsNullOrEmpty(entityalias))
Expand Down
Loading

0 comments on commit fc2ca21

Please sign in to comment.