Пример #1
0
        public override void Assign(DatabaseObjectInfo source)
        {
            base.Assign(source);
            var src = (TableInfo)source;

            if (src.PrimaryKey != null)
            {
                PrimaryKey = src.PrimaryKey.ClonePrimaryKey(this);
            }
            foreach (var fk in src.ForeignKeys)
            {
                ForeignKeys.Add(fk.CloneForeignKey(this));
            }
            foreach (var ix in src.Indexes)
            {
                Indexes.Add(ix.CloneIndex(this));
            }
            foreach (var ix in src.Uniques)
            {
                Uniques.Add(ix.CloneUnique(this));
            }
            foreach (var ch in src.Checks)
            {
                Checks.Add(ch.CloneCheck(this));
            }
        }
Пример #2
0
        protected EntityGeneralPipeline(Schema schema, JObject entityDefine, string connectString)
        {
#if NET_CORE
            Logger.Info($"Db ConnectString: {connectString}", true);
#endif

            ConnectString = connectString;

            Schema  = GenerateSchema(schema);
            Columns = entityDefine.SelectTokens("$.Fields[*]").Select(j => j.ToObject <Column>()).ToList();

            Primary       = entityDefine.SelectToken("$.Primary").ToObject <List <string> >();
            AutoIncrement = entityDefine.SelectToken("$.AutoIncrement")?.ToString();
            foreach (var index in entityDefine.SelectTokens("$.Indexs[*]"))
            {
                Indexs.Add(index.ToObject <List <string> >());
            }

            foreach (var index in entityDefine.SelectTokens("$.Uniques[*]"))
            {
                Uniques.Add(index.ToObject <List <string> >());
            }

            Type = GenerateType(schema, Columns);
            Logger.Info($"TYPE-> Name: {Type.Name} Properties: [{Type.GetProperties().Select(p=>p.Name)}]");
        }
Пример #3
0
        public void Add(string command)
        {
            if (Uniques.Add(command))
            {
                Commands.Add(command);

                var span = command.AsSpan();
                ReadOnlySpan <char> rSpan         = "-r:".AsSpan();
                ReadOnlySpan <char> referenceSpan = "--reference".AsSpan();

                if (span.StartsWith(rSpan))
                {
                    References.Add(span.TrimStart(rSpan).ToString());
                }
                else if (span.StartsWith(referenceSpan))
                {
                    References.Add(span.TrimStart(referenceSpan).ToString());
                }
            }
        }
        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);
                    }
                }
            }
        }
        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);
                    }
                }
            }
        }
Пример #6
0
        public void AddConstraint(ConstraintInfo cnt, bool reuseGrouId = false)
        {
            var primaryKeyInfo = cnt as PrimaryKeyInfo;

            if (primaryKeyInfo != null)
            {
                PrimaryKey = primaryKeyInfo.ClonePrimaryKey(this);
                if (!reuseGrouId)
                {
                    PrimaryKey.GroupId = Guid.NewGuid().ToString();
                }
                PrimaryKey.OwnerTable = this;
            }
            var foreignKeyInfo = cnt as ForeignKeyInfo;

            if (foreignKeyInfo != null)
            {
                var fknew = foreignKeyInfo.CloneForeignKey(this);
                if (!reuseGrouId)
                {
                    fknew.GroupId = Guid.NewGuid().ToString();
                }
                ForeignKeys.Add(fknew);
                fknew.OwnerTable = this;
            }
            var indexInfo = cnt as IndexInfo;

            if (indexInfo != null)
            {
                var ixnew = indexInfo.CloneIndex(this);
                if (!reuseGrouId)
                {
                    ixnew.GroupId = Guid.NewGuid().ToString();
                }
                Indexes.Add(ixnew);
                ixnew.OwnerTable = this;
            }
            var uniqueInfo = cnt as UniqueInfo;

            if (uniqueInfo != null)
            {
                var uqnew = uniqueInfo.CloneUnique(this);
                if (!reuseGrouId)
                {
                    uqnew.GroupId = Guid.NewGuid().ToString();
                }
                Uniques.Add(uqnew);
                uqnew.OwnerTable = this;
            }
            var checkInfo = cnt as CheckInfo;

            if (checkInfo != null)
            {
                var chnew = checkInfo.CloneCheck(this);
                if (!reuseGrouId)
                {
                    chnew.GroupId = Guid.NewGuid().ToString();
                }
                Checks.Add(chnew);
                chnew.OwnerTable = this;
            }
        }
Пример #7
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);
                }
            }
        }