From a9bd85230cdb5883171b94d778f345cc8778c1fa Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 15 Jan 2020 23:58:37 +0000 Subject: [PATCH] --- server/AyaNova/biz/ObjectFields.cs | 101 ++++++++++++++++++++++++- server/AyaNova/biz/SqlSelectBuilder.cs | 30 ++++++-- 2 files changed, 124 insertions(+), 7 deletions(-) diff --git a/server/AyaNova/biz/ObjectFields.cs b/server/AyaNova/biz/ObjectFields.cs index ea1b2453..0fd976a8 100644 --- a/server/AyaNova/biz/ObjectFields.cs +++ b/server/AyaNova/biz/ObjectFields.cs @@ -9,8 +9,12 @@ namespace AyaNova.Biz // This contains all the fields that are: // - Customizable on forms // - In grid list templates - //In addition it serves as a source for valid object keys in AvailableObjectKeys, and a source of database column names (tolower) - // + //In addition it serves as a source for valid object keys in AvailableObjectKeys + //SQL code uses this: + // - a source of database column names and ID column names + // - Essentially this replaces all the attribute decoration in v7 and the individual classes with objects + + public static class ObjectFields { @@ -53,6 +57,97 @@ namespace AyaNova.Biz List l = new List(); switch (key) { + /* + TODO: going to need some SQL hints added here + - SqlIDColumn indicating what the ID column is to fetch the underlying value from + - SqlColumn indicating an alternative column name as known to the sql server if it varies from the propertyname (in lowercase) + Maybe decorate with an INTERNAL ONLY bool so that we see it at the server but not at the client or when fetched as possible fields + hypothetical to help develop this: + Client and head office list: + {key=ClientName, propertyname=Name, idcolumn=id,} + + In v7 this is what the attribute decorations looked like: + [SqlColumnNameAttribute("aClient.aName", "aClient.aID")] + public GridNameValueCellItem LT_O_Client + { + get + { + return mClient; + } + } + + [SqlColumnNameAttribute("aHeadOffice.aName","aHeadOffice.aID")] + public GridNameValueCellItem LT_O_HeadOffice + { + get + { + return mHeadOffice; + } + } + + And the query (from clientlistdetailed) + string q = + "SELECT ~MAXRECS~ ACLIENT.aID, ACLIENT.aName, ACLIENT.aModified, " + + " ACLIENT.AACTIVE, ACLIENT.aUsesBanking, " + + " ACLIENT.aBillHeadOffice, ACLIENT.aWebAddress, " + + " ACLIENT.aDispatchZoneID, ACLIENT.aClientGroupID, " + + " ACLIENT.aLastWorkorderID, " + + " ACLIENT.aLastServiceDate, (SELECT TOP 1 aSBANK.aHoursBalance " + + "FROM aServiceBank aSBANK WHERE aSBANK.AAPPLIESTOROOTOBJECTID " + + "= ACLIENT.aID ORDER " + + "BY aSBANK.aCreated DESC) AS HOURSBAL, (SELECT TOP " + + "1 aSBANK.aIncidentsBalance FROM aServiceBank aSBANK " + + "WHERE aSBANK.AAPPLIESTOROOTOBJECTID = ACLIENT.aID " + + "ORDER BY aSBANK.aCreated DESC) AS INCIDENTSBAL, " + + " (SELECT TOP 1 aSBANK.aCurrencyBalance FROM " + + "aServiceBank aSBANK WHERE aSBANK.AAPPLIESTOROOTOBJECTID " + + "= ACLIENT.aID ORDER BY aSBANK.aCreated DESC) " + + "AS CURRENCYBAL, " + + " ACLIENT.AACCOUNTNUMBER, aHeadOffice.aName " + + "AS aHeadOfficeName, aHeadOffice.aID " + + "AS aHeadOfficeID, aDispatchZone.aName AS aDispatchZoneName, " + + " ACLIENT.aRegionID, aRegion.aName " + + "AS aRegionName, aClientGroup.aName AS " + + "aClientGroupName, aPHYSICAL.aDeliveryAddress, " + + "aPHYSICAL.aCity, aPHYSICAL.aStateProv, aPHYSICAL.aCountry, " + + " aPHYSICAL.aPostal, aPHYSICAL.AADDRESSTYPE, " + + " aPHYSICAL.aLongitude, aPHYSICAL.aLatitude, " + + " aWorkorderService.aServiceNumber, " + + " ACLIENT.aContractID, aContract.aName AS " + + "aContractName, ACLIENT.aContractExpires, aPHYSICAL.aCountryCode, " + + " aPOSTAL.AADDRESSTYPE AS aPAddressType, " + + " aPOSTAL.aDeliveryAddress AS aPDeliveryAddress, " + + " aPOSTAL.aCity AS aPCity, aPOSTAL.aStateProv " + + "AS aPStateProv, aPOSTAL.aCountryCode AS aPCountryCode, " + + " aPOSTAL.aCountry AS aPCountry, aPOSTAL.aPostal " + + "AS aPPostal, ACLIENT.aCustom1, ACLIENT.aCustom2, " + + " ACLIENT.aCustom3, ACLIENT.aCustom4, " + + " ACLIENT.aCustom5, ACLIENT.aCustom6, " + + " ACLIENT.aCustom7, ACLIENT.aCustom8, ACLIENT.aCustom9, " + + " ACLIENT.aCustom0, ACLIENT.aTechNotes, " + + " ACLIENT.aNotes, ACLIENT.aPopUpNotes, " + + " ACLIENT.ACONTACT, ACLIENT.AEMAIL, ACLIENT.APHONE1, ACLIENT.APHONE2, ACLIENT.APHONE3, ACLIENT.APHONE4, ACLIENT.APHONE5, ACLIENT.ACONTACTNOTES " + + + + + "FROM " + + " ACLIENT ACLIENT " + + " LEFT OUTER JOIN ADISPATCHZONE ON (ACLIENT.ADISPATCHZONEID=ADISPATCHZONE.AID) " + + " LEFT OUTER JOIN ACLIENTGROUP ON (ACLIENT.ACLIENTGROUPID=ACLIENTGROUP.AID) " + + " LEFT OUTER JOIN AADDRESS aPHYSICAL ON (ACLIENT.AID=aPHYSICAL.AROOTOBJECTID) " + + " LEFT OUTER JOIN AADDRESS aPOSTAL ON (ACLIENT.AID=aPOSTAL.AROOTOBJECTID) " + + " LEFT OUTER JOIN ACONTRACT ON (ACLIENT.ACONTRACTID=ACONTRACT.AID) " + + " LEFT OUTER JOIN AHEADOFFICE ON (ACLIENT.AHEADOFFICEID=AHEADOFFICE.AID) " + + " LEFT OUTER JOIN AWORKORDERSERVICE ON (ACLIENT.ALASTWORKORDERID=AWORKORDERSERVICE.AWORKORDERID) " + + " LEFT OUTER JOIN AREGION ON (ACLIENT.AREGIONID=AREGION.AID) " + + + "WHERE (aPHYSICAL.AADDRESSTYPE " + + "IS NULL OR aPHYSICAL.AADDRESSTYPE " + + "= 2) AND (aPOSTAL.AADDRESSTYPE IS NULL OR aPOSTAL.AADDRESSTYPE " + + "= 1) \r\n "; + + + */ case WIDGET_KEY: #region WIDGET_KEY l.Add(new ObjectField { Key = "WidgetName", PropertyName = "Name", DataType = (int)AyaDataType.Text, Hideable = false }); @@ -147,6 +242,8 @@ namespace AyaNova.Biz { //parse the template var jtemplate = JObject.Parse(template); + + //get the fields list var fields = ObjectFieldsList(ObjectKey); //convert to strings (https://stackoverflow.com/a/33836599/8939) diff --git a/server/AyaNova/biz/SqlSelectBuilder.cs b/server/AyaNova/biz/SqlSelectBuilder.cs index 01d8fd75..74dce8e5 100644 --- a/server/AyaNova/biz/SqlSelectBuilder.cs +++ b/server/AyaNova/biz/SqlSelectBuilder.cs @@ -11,18 +11,38 @@ namespace AyaNova.Biz { public static class SqlSelectBuilder { - - public static string Build(AyaNova.Models.DataFilter dataFilter) + //Build the SELECT portion of a list query based on the template, mini or full and the object key in question + public static string Build(string objectKey, string template, bool mini) { - if (string.IsNullOrWhiteSpace(dataFilter.Sort)) + //parse the template + var jtemplate = JObject.Parse(template); + + //get the fields list + var FieldsList = ObjectFields.ObjectFieldsList(objectKey); + + //convert to strings array (https://stackoverflow.com/a/33836599/8939) + string[] templateFieldList; + if (mini) { - return DefaultGetManyOrderBy(); + templateFieldList = ((JArray)jtemplate["mini"]).ToObject(); } + else + { + templateFieldList = ((JArray)jtemplate["full"]).ToObject(); + } + StringBuilder sb = new StringBuilder(); + sb.Append("SELECT "); + + foreach (string ColumnName in templateFieldList) + { + sb.Append(ColumnName); + } + //iterate the datafilter and concatenate a sql query from it var SortArray = JArray.Parse(dataFilter.Sort); for (int i = 0; i < SortArray.Count; i++) @@ -31,7 +51,7 @@ namespace AyaNova.Biz var SortItem = SortArray[i]; var fld = SortItem["fld"].Value(); var dir = SortItem["dir"].Value(); - + sb.Append(" "); sb.Append(fld); sb.Append(" ");