protected EntityGeneralPipeline(Schema schema, Entity entityDefine, string connectString, PipelineMode mode = PipelineMode.Insert)
        {
            Mode          = mode;
            ConnectString = connectString;

            Schema  = GenerateSchema(schema);
            Columns = entityDefine.Fields.Where(f => f.DataType != null).ToList();
            var primary = entityDefine.Primary;

            if (primary != null)
            {
                foreach (var p in primary)
                {
                    var col = Columns.FirstOrDefault(c => c.Name == p);
                    if (col == null)
                    {
                        throw new SpiderExceptoin("Columns set as primary is not a property of your entity.");
                    }
                    else
                    {
                        Primary.Add(col);
                    }
                }
            }

            if (mode == PipelineMode.Update && entityDefine.Updates != null)
            {
                foreach (var column in entityDefine.Updates)
                {
                    var col = Columns.FirstOrDefault(c => c.Name == column);
                    if (col == null)
                    {
                        throw new SpiderExceptoin("Columns set as update is not a property of your entity.");
                    }
                    else
                    {
                        UpdateColumns.Add(col);
                    }
                }
                if (UpdateColumns == null || UpdateColumns.Count == 0)
                {
                    UpdateColumns = Columns;
                    UpdateColumns.RemoveAll(c => Primary.Contains(c));
                }
                if (Primary == null || Primary.Count == 0)
                {
                    throw new SpiderExceptoin("Do you forget set the Primary in IndexesAttribute for your entity class.");
                }
            }

            AutoIncrement = entityDefine.AutoIncrement;

            if (entityDefine.Indexes != null)
            {
                foreach (var index in entityDefine.Indexes)
                {
                    List <string> tmpIndex = new List <string>();
                    foreach (var i in index)
                    {
                        var col = Columns.FirstOrDefault(c => c.Name == i);
                        if (col == null)
                        {
                            throw new SpiderExceptoin("Columns set as index is not a property of your entity.");
                        }
                        else
                        {
                            tmpIndex.Add(col.Name);
                        }
                    }
                    if (tmpIndex.Count != 0)
                    {
                        Indexs.Add(tmpIndex);
                    }
                }
            }
            if (entityDefine.Uniques != null)
            {
                foreach (var unique in entityDefine.Uniques)
                {
                    List <string> tmpUnique = new List <string>();
                    foreach (var i in unique)
                    {
                        var col = Columns.FirstOrDefault(c => c.Name == i);
                        if (col == null)
                        {
                            throw new SpiderExceptoin("Columns set as unique is not a property of your entity.");
                        }
                        else
                        {
                            tmpUnique.Add(col.Name);
                        }
                    }
                    if (tmpUnique.Count != 0)
                    {
                        Uniques.Add(tmpUnique);
                    }
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// 判断某一列是否在主键中
 /// </summary>
 /// <param name="column">列</param>
 /// <returns></returns>
 public bool IsPrimary(string column)
 {
     return(Primary != null && Primary.Contains(column));
 }
        public override void InitiEntity(EntityMetadata metadata)
        {
            if (metadata.Schema == null)
            {
                Spider.Log("Schema is necessary", LogLevel.Warn);
                IsEnabled = false;
                return;
            }
            Schema = GenerateSchema(metadata.Schema);
            foreach (var f in metadata.Entity.Fields)
            {
                if (!string.IsNullOrEmpty(((Field)f).DataType))
                {
                    Columns.Add((Field)f);
                }
            }
            if (Columns.Count == 0)
            {
                Spider.Log("Columns is necessary", LogLevel.Warn);
                IsEnabled = false;
                return;
            }
            var primary = metadata.Primary;

            if (primary != null)
            {
                foreach (var p in primary)
                {
                    var col = Columns.FirstOrDefault(c => c.Name == p);
                    if (col == null)
                    {
                        throw new SpiderException("Columns set as primary is not a property of your entity.");
                    }
                    else
                    {
                        Primary.Add(col);
                    }
                }
            }

            if (Mode == PipelineMode.Update)
            {
                if (Primary == null || Primary.Count == 0)
                {
                    throw new SpiderException("Set Primary in the Indexex attribute.");
                }

                if (metadata.Updates != null && metadata.Updates.Count > 0)
                {
                    foreach (var column in metadata.Updates)
                    {
                        var col = Columns.FirstOrDefault(c => c.Name == column);
                        if (col == null)
                        {
                            throw new SpiderException("Columns set as update is not a property of your entity.");
                        }
                        else
                        {
                            UpdateColumns.Add(col);
                        }
                    }

                    UpdateColumns.RemoveAll(c => Primary.Contains(c));

                    if (UpdateColumns.Count == 0)
                    {
                        throw new SpiderException("There is no column need update.");
                    }
                }
                else
                {
                    UpdateColumns = Columns;
                    UpdateColumns.RemoveAll(c => Primary.Contains(c));

                    if (UpdateColumns.Count == 0)
                    {
                        throw new SpiderException("There is no column need update.");
                    }
                }
            }

            AutoIncrement = metadata.AutoIncrement;

            if (metadata.Indexes != null)
            {
                foreach (var index in metadata.Indexes)
                {
                    List <string> tmpIndex = new List <string>();
                    foreach (var i in index)
                    {
                        var col = Columns.FirstOrDefault(c => c.Name == i);
                        if (col == null)
                        {
                            throw new SpiderException("Columns set as index is not a property of your entity.");
                        }
                        else
                        {
                            tmpIndex.Add(col.Name);
                        }
                    }
                    if (tmpIndex.Count != 0)
                    {
                        Indexs.Add(tmpIndex);
                    }
                }
            }
            if (metadata.Uniques != null)
            {
                foreach (var unique in metadata.Uniques)
                {
                    List <string> tmpUnique = new List <string>();
                    foreach (var i in unique)
                    {
                        var col = Columns.FirstOrDefault(c => c.Name == i);
                        if (col == null)
                        {
                            throw new SpiderException("Columns set as unique is not a property of your entity.");
                        }
                        else
                        {
                            tmpUnique.Add(col.Name);
                        }
                    }
                    if (tmpUnique.Count != 0)
                    {
                        Uniques.Add(tmpUnique);
                    }
                }
            }
        }
Exemple #4
0
        protected EntityGeneralPipeline(Schema schema, JObject entityDefine, string connectString, PipelineMode mode = PipelineMode.Insert)
        {
            Mode          = mode;
            ConnectString = connectString;

            Schema  = GenerateSchema(schema);
            Columns = entityDefine.SelectTokens("$.Fields[*]").Select(j => j.ToObject <Column>()).Where(c => !string.IsNullOrEmpty(c.DataType)).ToList();
            var primary = entityDefine.SelectToken("$.Primary")?.ToObject <List <string> >();

            if (primary != null)
            {
                foreach (var p in primary)
                {
                    var col = Columns.FirstOrDefault(c => c.Name == p);
                    if (col == null)
                    {
                        throw new SpiderExceptoin("Columns set as primary is not a property of your entity.");
                    }
                    else
                    {
                        Primary.Add(col);
                    }
                }
            }

            if (mode == PipelineMode.Update)
            {
                var tmpUpdateColumns = entityDefine.SelectTokens("$.Update[*]").Select(u => u.ToString()).ToList();
                foreach (var column in tmpUpdateColumns)
                {
                    var col = Columns.FirstOrDefault(c => c.Name == column);
                    if (col == null)
                    {
                        throw new SpiderExceptoin("Columns set as update is not a property of your entity.");
                    }
                    else
                    {
                        UpdateColumns.Add(col);
                    }
                }
                if (UpdateColumns == null || UpdateColumns.Count == 0)
                {
                    UpdateColumns = Columns;
                    UpdateColumns.RemoveAll(c => Primary.Contains(c));
                }
                if (Primary == null || Primary.Count == 0)
                {
                    throw new SpiderExceptoin("Do you forget set the Primary in IndexesAttribute for your entity class.");
                }
            }

            AutoIncrement = entityDefine.SelectToken("$.AutoIncrement")?.ToString();

            foreach (var index in entityDefine.SelectTokens("$.Indexs[*]"))
            {
                List <string> tmpIndex = new List <string>();
                foreach (var i in index.ToObject <List <string> >())
                {
                    var col = Columns.FirstOrDefault(c => c.Name == i);
                    if (col == null)
                    {
                        throw new SpiderExceptoin("Columns set as index is not a property of your entity.");
                    }
                    else
                    {
                        tmpIndex.Add(col.Name);
                    }
                }
                if (tmpIndex.Count != 0)
                {
                    Indexs.Add(tmpIndex);
                }
            }

            foreach (var unique in entityDefine.SelectTokens("$.Uniques[*]"))
            {
                List <string> tmpUnique = new List <string>();
                foreach (var i in unique.ToObject <List <string> >())
                {
                    var col = Columns.FirstOrDefault(c => c.Name == i);
                    if (col == null)
                    {
                        throw new SpiderExceptoin("Columns set as unique is not a property of your entity.");
                    }
                    else
                    {
                        tmpUnique.Add(col.Name);
                    }
                }
                if (tmpUnique.Count != 0)
                {
                    Uniques.Add(tmpUnique);
                }
            }
        }