Exemplo n.º 1
0
        protected override SqlCommand[] BuildCreateTableCommands(ISyntaxProvider syntax, string tableName, IList <IProperty> properties)
        {
            var sb = new StringBuilder();

            sb.Append($"create table main.{Delimit(syntax, tableName)}\n(\n");

            var count = properties.Count;
            var last  = count - 1;

            for (var i = 0; i < count; i++)
            {
                AppendFieldToBuilder(sb, syntax, properties[i]);

                if (i != last)
                {
                    sb.Append(",");
                }

                sb.AppendLine();
            }

            sb.Append(");\n");

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 2
0
 public DbToolSyntaxParser(ISyntaxProvider syntaxProvider)
 {
     _tags = new List<Tag>();
     _suggestions = new List<Suggestion>();
     _syntaxProvider = syntaxProvider;
     _logger = DebugLogger.Instance;
 }
        protected override SqlCommand[] BuildAddFieldCommands(ISyntaxProvider syntax, string tableName, IList <IProperty> properties)
        {
            var sb = new StringBuilder();

            sb.Append($"alter table {Delimit(syntax, tableName)}");

            var count = properties.Count;
            var last  = count - 1;

            for (var i = 0; i < count; i++)
            {
                sb.Append(" add column ");

                AppendFieldToBuilder(sb, syntax, properties[i]);

                if (i != last)
                {
                    sb.Append(",");
                }

                sb.AppendLine();
            }

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 4
0
 public SyntaxParser(ISyntaxProvider syntaxProvider, ICSharpEvaluator cSharpEvaluator)
 {
     _tags            = new List <Tag>();
     _suggestions     = new List <Suggestion>();
     _syntaxProvider  = syntaxProvider;
     _cSharpEvaluator = cSharpEvaluator;
 }
Exemplo n.º 5
0
        internal static Expression GetInsertExpression(ISyntaxProvider syntax, Expression instance)
        {
            InsertCommandExpression insertExp;
            var entityType = instance.Type;
            Func <TableExpression, IEnumerable <ColumnAssignment> > func;

            if (instance is ParameterExpression parExp)
            {
                func = new Func <TableExpression, IEnumerable <ColumnAssignment> >(t => GetInsertArguments(syntax, t, parExp));
            }
            else
            {
                var entity = instance.As <ConstantExpression>().Value as IEntity;
                func = new Func <TableExpression, IEnumerable <ColumnAssignment> >(t => GetInsertArguments(syntax, t, entity));
            }

            var metadata = EntityMetadataUnity.GetEntityMetadata(entityType);
            var table    = new TableExpression(new TableAlias(), metadata.TableName, entityType);

            insertExp = new InsertCommandExpression(table, func(table))
            {
                WithAutoIncrement = !string.IsNullOrEmpty(syntax.IdentitySelect) && HasAutoIncrement(instance.Type)
            };

            return(insertExp);
        }
Exemplo n.º 6
0
 internal EntityQueryBuilder(ISyntaxProvider syntax, EntityPersistentEnvironment environment, Type entityType)
 {
     m_builder     = new StringBuilder();
     m_syntax      = syntax;
     m_environment = environment;
     m_entityType  = entityType;
     m_metadata    = EntityMetadataUnity.GetEntityMetadata(m_entityType);
 }
Exemplo n.º 7
0
 /// <summary>
 /// 初始化 <see cref="EntityTreeRepository&lt;TEntity&gt;"/> 类的新实例。
 /// </summary>
 /// <param name="repository"></param>
 /// <param name="context"></param>
 public EntityTreeRepository(EntityRepository <TEntity> repository, InternalContext context)
 {
     this.repository = repository;
     entityType      = typeof(TEntity);
     metadata        = EntityMetadataUnity.GetEntityMetadata(entityType);
     metaTree        = metadata.EntityTree;
     database        = context.Database;
     syntax          = database.Provider.GetService <ISyntaxProvider>();
 }
Exemplo n.º 8
0
        private static bool InternalCreate(EntityMetadata metadata, ISyntaxProvider syntax, InternalContext context)
        {
            //表的名称
            var tableName = metadata.TableName;

            SqlCommand sql = BuildTableScript(metadata.EntityType, syntax, tableName);

            return(context.Database.ExecuteNonQuery(sql) > 0);
        }
Exemplo n.º 9
0
 /// <summary>
 /// 初始化 <see cref="EntityTreeRepository{TEntity}"/> 类的新实例。
 /// </summary>
 /// <param name="repository"></param>
 /// <param name="service"></param>
 public EntityTreeRepository(IRepository <TEntity> repository, IContextService service)
 {
     this.repository = repository;
     entityType      = typeof(TEntity);
     metadata        = EntityMetadataUnity.GetEntityMetadata(entityType);
     metaTree        = metadata.EntityTree;
     database        = service.Database;
     syntax          = database.Provider.GetService <ISyntaxProvider>();
     options         = service.InitializeContext.Options;
 }
Exemplo n.º 10
0
        /// <summary>
        /// 获取主键值的表达式。
        /// </summary>
        /// <param name="syntax"></param>
        /// <param name="table"></param>
        /// <param name="parExp"></param>
        /// <param name="entity"></param>
        /// <param name="property"></param>
        /// <returns></returns>
        private static Expression GetPrimaryValueExpression(ISyntaxProvider syntax, TableExpression table, ParameterExpression parExp, IEntity entity, IProperty property)
        {
            //如果不支持自增量,则使用 IGenerateProvider生成
            if (property.Info.GenerateType == IdentityGenerateType.Generator ||
                (property.Info.GenerateType == IdentityGenerateType.AutoIncrement &&
                 string.IsNullOrEmpty(syntax.IdentitySelect)))
            {
                return(new GeneratorExpression(table, parExp ?? (Expression)Expression.Constant(entity), property));
            }

            return(null);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 返回插入具体实体时的 <see cref="ColumnAssignment"/> 集合。
        /// </summary>
        /// <param name="syntax"></param>
        /// <param name="table"></param>
        /// <param name="entity">具体的实体。</param>
        /// <returns></returns>
        private static IEnumerable <ColumnAssignment> GetInsertArguments(ISyntaxProvider syntax, TableExpression table, IEntity entity)
        {
            var properties = GetModifiedProperties(entity);

            var assignments = properties
                              .Select(m => new ColumnAssignment(
                                          (ColumnExpression)GetMemberExpression(table, m),
                                          Expression.Constant(GetConvertableValue(entity, m))
                                          )).ToList();

            assignments.AddRange(GetAssignmentsForPrimaryKeys(syntax, table, null, entity));

            return(assignments);
        }
Exemplo n.º 12
0
        /// <summary>
        /// 将字段生成添加到 builder 中。
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="syntax"></param>
        /// <param name="property"></param>
        protected virtual void AppendFieldToBuilder(StringBuilder builder, ISyntaxProvider syntax, IProperty property)
        {
            builder.AppendFormat(" {0}", Quote(syntax, property.Info.FieldName));

            //数据类型及长度精度等
            builder.AppendFormat(" {0}", syntax.Column((DbType)property.Info.DataType,
                                                       property.Info.Length,
                                                       property.Info.Precision,
                                                       property.Info.Scale));

            //主键
            if (property.Info.IsPrimaryKey)
            {
                ProcessPrimaryKeyField(builder, syntax, property);
            }

            //自增
            if (property.Info.GenerateType == IdentityGenerateType.AutoIncrement &&
                !string.IsNullOrEmpty(syntax.IdentityColumn))
            {
                builder.AppendFormat(" {0}", syntax.IdentityColumn);
            }

            //不可空
            if (!property.Info.IsNullable)
            {
                builder.AppendFormat(" not null");
            }

            //默认值
            if (!PropertyValue.IsEmpty(property.Info.DefaultValue))
            {
                if (property.Type == typeof(string))
                {
                    builder.AppendFormat(" default '{0}'", property.Info.DefaultValue);
                }
                else if (property.Type.IsEnum)
                {
                    builder.AppendFormat(" default {0}", (int)property.Info.DefaultValue);
                }
                else if (property.Type == typeof(bool) || property.Type == typeof(bool?))
                {
                    builder.AppendFormat(" default {0}", (bool)property.Info.DefaultValue ? 1 : 0);
                }
                else
                {
                    builder.AppendFormat(" default {0}", property.Info.DefaultValue);
                }
            }
        }
Exemplo n.º 13
0
        protected override SqlCommand[] BuildAddFieldCommands(ISyntaxProvider syntax, EntityMetadata metadata, IProperty[] properties)
        {
            var sb    = new StringBuilder();
            var count = properties.Length;

            for (var i = 0; i < count; i++)
            {
                sb.AppendFormat("alter table {0} add ", Quote(syntax, metadata.TableName));

                AppendFieldToBuilder(sb, syntax, properties[i]);

                sb.AppendLine(";");
            }

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 14
0
        protected override SqlCommand[] BuildAddFieldCommands(ISyntaxProvider syntax, string tableName, IList <IProperty> properties)
        {
            var sb    = new StringBuilder();
            var count = properties.Count;

            for (var i = 0; i < count; i++)
            {
                sb.AppendFormat("alter table {0} add ", Quote(syntax, tableName));

                AppendFieldToBuilder(sb, syntax, properties[i]);

                sb.AppendLine(";");
            }

            return(new SqlCommand[] { sb.ToString() });
        }
        protected override SqlCommand[] BuildCreateTableCommands(ISyntaxProvider syntax, string tableName, IList <IProperty> properties)
        {
            var sb = new StringBuilder();

            sb.Append($"create table {Delimit(syntax, tableName)}\n(\n");

            var count = properties.Count;
            var last  = count - 1;

            for (var i = 0; i < count; i++)
            {
                AppendFieldToBuilder(sb, syntax, properties[i]);

                if (i != last)
                {
                    sb.Append(",");
                }

                sb.AppendLine();
            }

            //主键
            var primaryPeoperties = properties.Where(s => s.Info.IsPrimaryKey).ToArray();

            if (primaryPeoperties.Length > 0)
            {
                sb.AppendLine(",");
                sb.Append("primary key (");

                for (int i = 0, n = primaryPeoperties.Length; i < n; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(Delimit(syntax, primaryPeoperties[i].Info.FieldName));
                }

                sb.Append(")");
            }

            sb.Append(");\n");

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 16
0
        protected override SqlCommand[] BuildCreateTableCommands(ISyntaxProvider syntax, EntityMetadata metadata, IList <IProperty> properties)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("create table {0}\n(\n", Quote(syntax, metadata.TableName));

            var count = properties.Count;

            for (var i = 0; i < count; i++)
            {
                AppendFieldToBuilder(sb, syntax, properties[i]);

                if (i != count - 1)
                {
                    sb.Append(",");
                }

                sb.AppendLine();
            }

            //主键
            var primaryPeoperties = properties.Where(s => s.Info.IsPrimaryKey).ToArray();

            if (primaryPeoperties.Length > 0)
            {
                sb.AppendLine(",");
                sb.Append("primary key (");

                for (var i = 0; i < primaryPeoperties.Length; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(Quote(syntax, primaryPeoperties[i].Info.FieldName));
                }

                sb.Append(")");
            }

            sb.Append(");\n");

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 17
0
        private static IEnumerable <ColumnAssignment> GetAssignmentsForPrimaryKeys(ISyntaxProvider syntax, TableExpression table, ParameterExpression parExp, IEntity entity)
        {
            var entityType  = parExp != null ? parExp.Type : entity.EntityType;
            var assignments = new List <ColumnAssignment>();

            foreach (var p in PropertyUnity.GetPrimaryProperties(entityType))
            {
                var pvExp = GetPrimaryValueExpression(syntax, table, parExp, entity, p);

                if (pvExp != null)
                {
                    var columnExp = (ColumnExpression)GetMemberExpression(table, p);
                    assignments.Add(new ColumnAssignment(columnExp, pvExp));
                }
            }

            return(assignments);
        }
Exemplo n.º 18
0
 /// <summary>
 /// 添加或替换集合中的参数。
 /// </summary>
 /// <param name="syntax"></param>
 /// <param name="mapping">名称和类型的映射字典。</param>
 /// <param name="parameters"><see cref="DbCommand"/> 中的参数集合。</param>
 /// <param name="data">数组的数组。</param>
 /// <param name="parFunc">创建 <see cref="DbParameter"/> 对象的函数。</param>
 private void AddOrReplayParameters(ISyntaxProvider syntax, IEnumerable <PropertyFieldMapping> mapping, DbParameterCollection parameters, object[][] data, Func <DbParameter> parFunc)
 {
     mapping.ForEach((m, i) =>
     {
         if (parameters.Contains(m.FieldName))
         {
             parameters[m.FieldName].Value = data[i];
         }
         else
         {
             var parameter           = parFunc();
             parameter.ParameterName = m.FieldName;
             parameter.Direction     = ParameterDirection.Input;
             parameter.DbType        = m.FieldType;
             parameter.Value         = data[i];
             parameters.Add(parameter);
         }
     });
 }
Exemplo n.º 19
0
        /// <summary>
        /// 返回插入具体实体时的 <see cref="ColumnAssignment"/> 集合。
        /// </summary>
        /// <param name="syntax"></param>
        /// <param name="table"></param>
        /// <param name="parExp"></param>
        /// <returns></returns>
        private static IEnumerable <ColumnAssignment> GetInsertArguments(ISyntaxProvider syntax, TableExpression table, ParameterExpression parExp)
        {
            IEnumerable <IProperty> properties    = null;
            List <string>           modifiedNames = null;

            if ((modifiedNames = GetReferenceModifiedProperties()) != null)
            {
                properties = GetModifiedProperties(table.Type, modifiedNames);
            }

            var assignments = properties
                              .Select(m => new ColumnAssignment(
                                          (ColumnExpression)GetMemberExpression(table, m),
                                          Expression.MakeMemberAccess(parExp, m.Info.ReflectionInfo)
                                          )).ToList();

            assignments.AddRange(GetAssignmentsForPrimaryKeys(syntax, table, parExp, null));

            return(assignments);
        }
Exemplo n.º 20
0
        protected override SqlCommand[] BuildAddFieldCommands(ISyntaxProvider syntax, EntityMetadata metadata, IList <IProperty> properties)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("alter table {0} add (", Quote(syntax, metadata.TableName));

            var count = properties.Count;

            for (var i = 0; i < count; i++)
            {
                AppendFieldToBuilder(sb, syntax, properties[i]);

                if (i != count - 1)
                {
                    sb.Append(",");
                }
            }

            sb.Append(")");

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 21
0
        /// <summary>
        /// 使用引号标识符格式化表名称或列名称。
        /// </summary>
        /// <param name="syntax"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string FormatByQuote(ISyntaxProvider syntax, string name)
        {
            if (syntax == null)
            {
                return(name);
            }

            var section = ConfigurationUnity.GetSection <GlobalConfigurationSection>();

            if ((section == null ||
                 (!section.Options.AttachQuote && name.IndexOf(' ') == -1) || name.Length == 0 || syntax.Quote == null || syntax.Quote.Length != 2))
            {
                return(name);
            }

            if (name.Length > 1 &&
                (name[0].ToString() == syntax.Quote[0] || name[name.Length - 1].ToString() == syntax.Quote[1]))
            {
                return(name);
            }

            return(string.Format("{0}{1}{2}", syntax.Quote[0], name, syntax.Quote[1]));
        }
Exemplo n.º 22
0
        /// <summary>
        /// 添加或替换集合中的参数。
        /// </summary>
        /// <param name="syntax"></param>
        /// <param name="mapping">名称和类型的映射字典。</param>
        /// <param name="parameters"><see cref="DbCommand"/> 中的参数集合。</param>
        /// <param name="data">数组的数组。</param>
        /// <param name="parFunc">创建 <see cref="DbParameter"/> 对象的函数。</param>
        private void AddOrReplayParameters(ISyntaxProvider syntax, IEnumerable <PropertyFieldMapping> mapping, DbParameterCollection parameters, object[][] data, Func <DbParameter> parFunc)
        {
            var i = 0;

            foreach (var kvp in mapping)
            {
                if (parameters.Contains(kvp.FieldName))
                {
                    parameters[kvp.FieldName].Value = data[i];
                }
                else
                {
                    var parameter = parFunc();
                    parameter.ParameterName = kvp.FieldName;
                    parameter.Direction     = ParameterDirection.Input;
                    parameter.DbType        = kvp.FieldType;
                    parameter.Value         = data[i];
                    parameters.Add(parameter);
                }

                i++;
            }
        }
Exemplo n.º 23
0
        protected override SqlCommand[] BuildCreateTableCommands(ISyntaxProvider syntax, string tableName, IList <IProperty> properties)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("create table {0}\n(\n", Quote(syntax, tableName));

            var count = properties.Count;

            for (var i = 0; i < count; i++)
            {
                AppendFieldToBuilder(sb, syntax, properties[i]);

                if (i != count - 1)
                {
                    sb.Append(",");
                }

                sb.AppendLine();
            }

            sb.Append(")\n");

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 24
0
        protected override SqlCommand[] BuildCreateTableCommands(ISyntaxProvider syntax, EntityMetadata metadata, IProperty[] properties)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("create table main.{0}\n(\n", Quote(syntax, metadata.TableName));

            var count = properties.Length;

            for (var i = 0; i < count; i++)
            {
                AppendFieldToBuilder(sb, syntax, properties[i]);

                if (i != count - 1)
                {
                    sb.Append(",");
                }

                sb.AppendLine();
            }

            sb.Append(");\n");

            return(new SqlCommand[] { sb.ToString() });
        }
 public ConvertResourceValueToSelectedOptionAction(ISyntaxProvider syntaxProvider)
 {
     _syntaxProvider = syntaxProvider;
 }
Exemplo n.º 26
0
 public ProgramBuilder(MethodBody body, ISyntaxProvider syntaxProvider)
 {
     this.body           = body ?? throw new ArgumentNullException(nameof(body));
     this.syntaxProvider = syntaxProvider ?? throw new ArgumentNullException(nameof(syntaxProvider));
 }
 public ConvertPropertyValueToInnerText(ISyntaxProvider syntaxProvider)
 {
     _syntaxProvider = syntaxProvider;
 }
 public ValueToConditionalAttribute(ISyntaxProvider syntaxProvider)
 {
     _syntaxProvider = syntaxProvider;
 }
 public CreateUriActionModifier(ISyntaxProvider syntaxProvider)
 {
     _syntaxProvider = syntaxProvider;
 }
Exemplo n.º 30
0
 /// <summary>
 /// 构建创建表的语句。
 /// </summary>
 /// <param name="syntax"></param>
 /// <param name="metadata"></param>
 /// <returns></returns>
 protected abstract SqlCommand[] BuildCreateTableCommands(ISyntaxProvider syntax, EntityMetadata metadata);
 public PropertyPathActionModifier(ISyntaxProvider syntaxProvider)
 {
     _syntaxProvider = syntaxProvider;
 }
Exemplo n.º 32
0
        protected override SqlCommand[] BuildCreateTableCommands(ISyntaxProvider syntax, EntityMetadata metadata)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("create table {0}\n(\n", metadata.TableName);

            //获取实体类型中所有可持久化的属性,不包含引用类型的属性
            var properties        = PropertyUnity.GetPersistentProperties(metadata.EntityType).ToArray();
            var primaryPeoperties = PropertyUnity.GetPrimaryProperties(metadata.EntityType).ToArray();
            var count             = properties.Length;

            for (var i = 0; i < count; i++)
            {
                var property = properties[i];
                sb.AppendFormat(" {0}", property.Info.FieldName);

                //数据类型及长度精度等
                sb.AppendFormat(" {0}", syntax.Column((DbType)property.Info.DataType,
                                                      property.Info.Length,
                                                      property.Info.Precision,
                                                      property.Info.Scale));

                //自增
                if (property.Info.GenerateType == IdentityGenerateType.AutoIncrement &&
                    !string.IsNullOrEmpty(syntax.IdentityColumn))
                {
                    sb.AppendFormat(" {0}", syntax.IdentityColumn);
                }

                //不可空
                if (!property.Info.IsNullable)
                {
                    sb.AppendFormat(" not null");
                }

                //默认值
                if (!PropertyValue.IsEmpty(property.Info.DefaultValue))
                {
                    if (property.Type == typeof(string))
                    {
                        sb.AppendFormat(" default '{0}'", property.Info.DefaultValue);
                    }
                    else if (property.Type.IsEnum)
                    {
                        sb.AppendFormat(" default {0}", (int)property.Info.DefaultValue);
                    }
                    else if (property.Type == typeof(bool) || property.Type == typeof(bool?))
                    {
                        sb.AppendFormat(" default {0}", (bool)property.Info.DefaultValue ? 1 : 0);
                    }
                    else
                    {
                        sb.AppendFormat(" default {0}", property.Info.DefaultValue);
                    }
                }

                if (i != count - 1)
                {
                    sb.Append(",");
                }

                sb.AppendLine();
            }

            //主键
            if (primaryPeoperties.Length > 0)
            {
                sb.Append(",");
                sb.AppendFormat("primary key (", metadata.TableName);

                for (var i = 0; i < primaryPeoperties.Length; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(primaryPeoperties[i].Info.FieldName);
                }

                sb.Append(")");
            }

            sb.Append(");\n");

            return(new SqlCommand[] { sb.ToString() });
        }
Exemplo n.º 33
0
 /// <summary>
 /// 处理主键。
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="syntax"></param>
 /// <param name="property"></param>
 protected virtual void ProcessPrimaryKeyField(StringBuilder builder, ISyntaxProvider syntax, IProperty property)
 {
 }
Exemplo n.º 34
0
 protected string Quote(ISyntaxProvider syntax, string name)
 {
     return(DbUtility.FormatByQuote(syntax, name));
 }
Exemplo n.º 35
0
 protected abstract SqlCommand[] BuildAddFieldCommands(ISyntaxProvider syntax, EntityMetadata metadata, IProperty[] properties);
Exemplo n.º 36
0
 public PropertyPathActionModifier(ISyntaxProvider syntaxProvider)
 {
     _syntaxProvider = syntaxProvider;
 }