public virtual string GetColumnList()
        {
            if (QueryLite.FieldList == null || QueryLite.FieldList.Count == 0 || QueryLite.FieldList[0] == "*")
            {
                return("*");
            }

            StringBuilder sb         = new StringBuilder();
            bool          firstTime  = true;
            var           properties = QueryLite.EntityType.GetEntityMetadata().Properties;
            string        startQuote = QueryLite.DataService.EntityLiteProvider.StartQuote;
            string        endQuote   = QueryLite.DataService.EntityLiteProvider.EndQuote;

            foreach (var fieldName in QueryLite.FieldList)
            {
                if (firstTime)
                {
                    firstTime = false;
                }
                else
                {
                    sb.Append(", ");
                }
                var propertyMetadata = properties[fieldName];
                if (propertyMetadata.IsLocalizedFiled)
                {
                    propertyMetadata = properties[CurrentLanguageService.GetSufixedLocalizedFieldName(fieldName)];
                }
                sb.Append(startQuote).Append(propertyMetadata.SqlField.ColumnName).Append(endQuote);
            }
            return(sb.ToString());
        }
        private PropertyMetadata GetPropertyMetadata(Type entityType, string fieldName)
        {
            PropertyMetadata propertyMetadata;
            EntityMetadata   entityMetadata = entityType.GetEntityMetadata();

            if (entityMetadata == null)
            {
                throw new InvalidOperationException("Entity " + entityType.Name + " has no metadata");
            }
            if (!entityMetadata.Properties.TryGetValue(fieldName, out propertyMetadata))
            {
                throw new ArgumentException("Field " + fieldName + " cannot be used in a filter because it is not a property of " + entityType.Name);
            }
            if (propertyMetadata.IsLocalizedFiled)
            {
                fieldName = CurrentLanguageService.GetSufixedLocalizedFieldName(fieldName);

                if (string.IsNullOrEmpty(fieldName))
                {
                    throw new InvalidOperationException("Cannot sort by localized property " + fieldName + " because no field name has been found for the current language: " + CurrentLanguageService.CurrentLanguageCode);
                }
                if (!entityMetadata.Properties.TryGetValue(fieldName, out propertyMetadata))
                {
                    throw new ArgumentException("Field " + fieldName + " cannot be used in a filter because it is not a property of " + entityType.Name);
                }
            }
            if (propertyMetadata.SqlField == null)
            {
                throw new ArgumentException("Field " + fieldName + " cannot be used in a filter because it has no metadata");
            }
            return(propertyMetadata);
        }
Esempio n. 3
0
        public virtual string[] GetColumns(Type entityType, IList <string> propertyNames)
        {
            if (propertyNames == null || propertyNames.Count == 0 || propertyNames[0] == "*")
            {
                return(new string[] { "*" });
            }
            if (entityType == null)
            {
                throw new ArgumentNullException(nameof(entityType));
            }
            var    properties = entityType.GetEntityMetadata().Properties;
            string startQuote = QueryLite.DataService.EntityLiteProvider.StartQuote;
            string endQuote   = QueryLite.DataService.EntityLiteProvider.EndQuote;
            var    columns    = new string[propertyNames.Count];

            for (int i = 0; i < columns.Length; i++)
            {
                var fieldName = propertyNames[i];
                if (!properties.TryGetValue(fieldName, out var propertyMetadata))
                {
                    var message = string.Format(CultureInfo.InvariantCulture, "property '{0}' not found in type '{1}'", fieldName, entityType.Name);
                    throw new KeyNotFoundException(message);
                }
                if (propertyMetadata.IsLocalizedFiled)
                {
                    propertyMetadata = properties[CurrentLanguageService.GetSufixedLocalizedFieldName(fieldName)];
                }
                columns[i] = startQuote + propertyMetadata.SqlField.ColumnName + endQuote;
            }
            return(columns);
        }
Esempio n. 4
0
        protected virtual bool GenerateOrderByForOneField(StringBuilder commandText, bool firstTime, SortDescriptor sortDescriptor)
        {
            if (commandText == null)
            {
                throw new ArgumentNullException(nameof(commandText));
            }
            if (sortDescriptor == null)
            {
                throw new ArgumentNullException(nameof(sortDescriptor));
            }
            PropertyMetadata propertyMetadata = null;
            var entityMetadata = QueryLite.EntityType.GetEntityMetadata();

            if (entityMetadata == null)
            {
                throw new InvalidOperationException("Entity " + QueryLite.EntityType.Name + " has no metadata");
            }
            entityMetadata.Properties.TryGetValue(sortDescriptor.FieldName, out propertyMetadata);

            if (propertyMetadata == null || (propertyMetadata.SqlField == null && !propertyMetadata.IsLocalizedFiled))
            {
                throw new ArgumentException(sortDescriptor.FieldName + " cannot be used in the order by clasuse because it is not a property of " + QueryLite.EntityType.Name + " or it has no metadata and is not a localizable field");
            }

            if (firstTime)
            {
                firstTime = false;
            }
            else
            {
                commandText.Append(", ");
            }

            string fieldName = sortDescriptor.FieldName;

            if (propertyMetadata.IsLocalizedFiled)
            {
                fieldName = CurrentLanguageService.GetSufixedLocalizedFieldName(sortDescriptor.FieldName);

                if (string.IsNullOrEmpty(fieldName))
                {
                    throw new InvalidOperationException("Cannot sort by localized property " + sortDescriptor.FieldName + " because no field name has been found for the current language: " + CurrentLanguageService.CurrentLanguageCode);
                }

                if (!entityMetadata.Properties.TryGetValue(fieldName, out propertyMetadata) || propertyMetadata.SqlField == null || string.IsNullOrEmpty(propertyMetadata.SqlField.ColumnName))
                {
                    throw new InvalidOperationException("Cannot sort by localized property " + sortDescriptor.FieldName + " because no field name has been found for the property: " + fieldName);
                }
            }
            string columnName = propertyMetadata.SqlField.ColumnName;

            commandText.Append(this.QueryLite.DataService.EntityLiteProvider.StartQuote).Append(columnName).Append(this.QueryLite.DataService.EntityLiteProvider.EndQuote).Append(sortDescriptor.SortOrder == SortOrder.Descending ? " DESC" : string.Empty);
            return(firstTime);
        }
Esempio n. 5
0
            private int GetFieldIndex(string fieldName)
            {
                int fieldIndex = 0;

                if (!readerFields.TryGetValue(fieldName, out fieldIndex) && !pascalReaderFields.TryGetValue(fieldName, out fieldIndex))
                {
                    string localizedFieldName = CurrentLanguageService.GetSufixedLocalizedFieldName(fieldName);
                    if (!readerFields.TryGetValue(localizedFieldName, out fieldIndex) && !pascalReaderFields.TryGetValue(localizedFieldName, out fieldIndex))
                    {
                        throw new InvalidOperationException(string.Format("Field {0} not found in data reader", fieldName));
                    }
                }
                return(fieldIndex);
            }