internal ResultSerialData Fetch(string entityTypeName, QueryObject queryObject)
        {
            var queries          = Utils.GetQueryResult(entityTypeName, queryObject, this.dialect, this.metadataSrv);
            var resultSerialData = new ResultSerialData();

            foreach (var et in queries)
            {
                if (!string.IsNullOrEmpty(et.EntityTypeName))
                {
                    var result = this.databaseOperations.ExecuteQuery(et.QueryText);
                    if (et.EntityTypeName == entityTypeName)
                    {
                        resultSerialData.Items          = result; //.OfType<Dto>().ToList();
                        resultSerialData.EntityTypeName = entityTypeName;
                    }
                    else
                    {
                        if (resultSerialData.RelatedItems == null)
                        {
                            resultSerialData.RelatedItems = new Dictionary <string, IEnumerable <Dto> >();
                        }
                        resultSerialData.RelatedItems.Add(et.EntityTypeName, result);
                    }
                }
                else
                {
                    var result = this.databaseOperations.CountQuery(et.QueryText);
                    resultSerialData.TotalCount = result;
                }
            }
            return(resultSerialData);
        }
        public static ResultSingleSerialData ToSingle(this ResultSerialData resultSerialData)
        {
            var resultSingleSerialData = new ResultSingleSerialData()
            {
                Item           = resultSerialData.Items.Count() > 0 ? resultSerialData.Items.FirstOrDefault() : null,
                EntityTypeName = resultSerialData.EntityTypeName,
                RelatedItems   = resultSerialData.RelatedItems
            };

            return(resultSingleSerialData);
        }
        /**
         * Insert single entity
         */
        public ResultSingleSerialData InsertItem(string entityTypeName, Dto dto)
        {
            ResultSerialData resultSerialDataOriginal = null;

            if (Utils.KeyPresent(entityTypeName, dto, this.metadataSrv))
            {
                resultSerialDataOriginal = this.dataAdapterRead.FetchOne(entityTypeName, dto, null);
            }
            if (resultSerialDataOriginal != null && resultSerialDataOriginal.Items.Count() > 0)
            {
                throw new DtosException(code: 409, message: "Conflict");
            }
            else
            {
                var resultSerialData = this.dataAdapterCud.InsertEntity(entityTypeName, dto);
                return(resultSerialData.ToSingle());
            }
        }
        /**
         * Insert multiple entities
         */
        public List <ResultSingleSerialData> InsertItems(string entityTypeName, IEnumerable <Dto> dtos)
        {
            ResultSerialData resultSerialDataOriginal = null;

            if (Utils.KeysPresent(entityTypeName, dtos, this.metadataSrv))
            {
                resultSerialDataOriginal = this.dataAdapterRead.FetchMany(entityTypeName, dtos, null);
            }
            if (resultSerialDataOriginal != null && resultSerialDataOriginal.Items.Count() > 0)
            {
                throw new DtosException(code: 409, message: "Conflict");
            }
            else
            {
                var entityInserts = new List <ResultSingleSerialData>();
                foreach (var dto in dtos)
                {
                    var resultSerialData = this.dataAdapterCud.InsertEntity(entityTypeName, dto);
                    entityInserts.Add(resultSerialData.ToSingle());
                }
                return(entityInserts);
            }
        }