private string Process(T state, StringBuilder builder, QuerySource query, string alias)
        {
            string name;

            if (query.TableName != null)
            {
                EscapeName(builder, query.TableName, true);
                name = alias ?? query.TableName;
            }
            else
            {
                builder.Append("(");
                var subName = Process(state, builder, query.TableQuery);
                builder.Append(")");
                alias = alias ?? subName;
                name  = alias;
            }

            if (!StencilUtils.IsNullOrWhiteSpace(alias))
            {
                builder.Append(" AS ");
                EscapeName(builder, alias, false);
            }
            return(name);
        }
 public void Process(T state, StringBuilder builder, Column column)
 {
     column.Value.Visit(state, builder, this);
     if (!StencilUtils.IsNullOrWhiteSpace(column.Alias))
     {
         builder.Append(" AS ");
         EscapeName(builder, column.Alias, false);
     }
 }
        public static FieldMetadata?NewFieldMetadata(PropertyInfo info)
        {
            var reflection = StencilContext.ReflectionServices;

            if (reflection == null)
            {
                throw new MissingReflectionException();
            }
            var attribute = reflection.GetCustomAttribute <DatabaseFieldAttribute>(info);

            if (attribute == null)
            {
                return(null);
            }
            return(new FieldMetadata()
            {
                PropertyInfo = info,
                ColumnName = StencilUtils.IsNullOrWhiteSpace(attribute.ColumnName) ? info.Name : attribute.ColumnName,
                DataType = attribute.DataType,
                DefaultValue = attribute.DefaultValue,
                Width = attribute.Width,
                CanBeNull = attribute.CanBeNull,
                Id = attribute.Id,
                GeneratedId = attribute.GeneratedId,
                GeneratedIdSequence = attribute.GeneratedIdSequence,
                Foreign = attribute.Foreign,
                UnknownEnumName = attribute.UnknownEnumName,
                ThrowIfNull = attribute.ThrowIfNull,
                Persisted = attribute.Persisted,
                Format = attribute.Format,
                Unique = attribute.Unique,
                UniqueCombo = attribute.UniqueCombo,
                Index = attribute.Index,
                UniqueIndex = attribute.UniqueIndex,
                IndexName = attribute.IndexName,
                UniqueIndexName = attribute.UniqueIndexName,
                AllowGeneratedIdInsert = attribute.AllowGeneratedIdInsert,
                ColumnDefinition = attribute.ColumnDefinition,
                Version = attribute.Version,
                ForeignColumnName = attribute.ForeignColumnName,
            });
        }
        public static TableMetadata?NewTableMetadata(Type type)
        {
            var reflection = StencilContext.ReflectionServices;

            if (reflection == null)
            {
                throw new MissingReflectionException();
            }
            var table = reflection.GetCustomAttribute <DatabaseTableAttribute>(type, false);

            if (table == null)
            {
                return(null);
            }
            IDictionary <string, FieldMetadata> fields = new Dictionary <string, FieldMetadata>();
            IDictionary <string, FieldMetadata> keys   = new Dictionary <string, FieldMetadata>();

            foreach (PropertyInfo info in reflection.GetProperties(type))
            {
                var newField = FieldMetadata.NewFieldMetadata(info);
                if (newField == null || StencilUtils.IsNullOrWhiteSpace(newField.Value.ColumnName))
                {
                    continue;
                }
                var field = newField.Value;
                fields[field.ColumnName] = field;
                if (field.Id)
                {
                    keys[field.ColumnName] = field;
                }
            }
            return(new TableMetadata
            {
                TableName = StencilUtils.IsNullOrWhiteSpace(table.TableName) ? type.Name : table.TableName,
                Fields = fields,
                Keys = keys
            });
        }