Exemplo n.º 1
0
        protected void SaveObjects(ManagementSession session)
        {
            var concreteCompositeType = new List <IClass>(this.database.MetaPopulation.Classes);

            concreteCompositeType.Sort(MetaObjectComparer.ById);
            foreach (var type in concreteCompositeType)
            {
                var atLeastOne = false;

                var sql = "SELECT " + this.database.Schema.ObjectId + "\n";
                sql += "FROM " + this.database.Schema.Table(type.ExclusiveClass) + "\n";
                sql += "WHERE " + this.database.Schema.TypeId + "=" + this.database.Schema.TypeId.Param.InvocationName + "\n";
                sql += "ORDER BY " + this.database.Schema.ObjectId;

                using (var command = session.CreateCommand(sql))
                {
                    command.AddInParameter(this.database.Schema.TypeId.Param.Name, type.Id);

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (atLeastOne == false)
                            {
                                atLeastOne = true;

                                this.writer.WriteStartElement(Serialization.ObjectType);
                                this.writer.WriteAttributeString(Serialization.Id, type.Id.ToString());
                            }
                            else
                            {
                                this.writer.WriteString(Serialization.ObjectsSplitter);
                            }

                            var objectId = long.Parse(reader[0].ToString());
                            this.writer.WriteString(objectId.ToString());
                        }

                        reader.Close();
                    }
                }

                if (atLeastOne)
                {
                    this.writer.WriteEndElement();
                }
            }
        }
Exemplo n.º 2
0
        protected virtual void LoadObjectsPostProcess(ManagementSession session)
        {
            foreach (var type in this.database.MetaPopulation.Composites)
            {
                if (type is IClass)
                {
                    var sql = new StringBuilder();
                    sql.Append("INSERT INTO " + this.database.Schema.Objects + " (" + this.database.Schema.ObjectId + "," + this.database.Schema.TypeId + "," + this.database.Schema.CacheId + ")\n");
                    sql.Append("SELECT " + this.database.Schema.ObjectId + "," + this.database.Schema.TypeId + ", " + Reference.InitialVersion + "\n");
                    sql.Append("FROM " + this.database.Schema.Table(type.ExclusiveClass));

                    lock (this)
                    {
                        using (var command = session.CreateCommand(sql.ToString()))
                        {
                            command.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        protected void SaveRelations(ManagementSession session)
        {
            var exclusiveLeafClassesByObjectType = new Dictionary <IObjectType, HashSet <IClass> >();

            var relations = new List <IRelationType>(this.database.MetaPopulation.RelationTypes);

            relations.Sort(MetaObjectComparer.ById);

            foreach (var relation in relations)
            {
                var associationType = relation.AssociationType;

                if (associationType.ObjectType.ExistClass)
                {
                    var roleType = relation.RoleType;

                    var sql = string.Empty;
                    if (roleType.ObjectType is IUnit)
                    {
                        HashSet <IClass> exclusiveLeafClasses;
                        if (!exclusiveLeafClassesByObjectType.TryGetValue(associationType.ObjectType, out exclusiveLeafClasses))
                        {
                            exclusiveLeafClasses = new HashSet <IClass>();
                            foreach (var concreteClass in associationType.ObjectType.Classes)
                            {
                                exclusiveLeafClasses.Add(concreteClass.ExclusiveClass);
                            }

                            exclusiveLeafClassesByObjectType[associationType.ObjectType] = exclusiveLeafClasses;
                        }

                        var first = true;
                        foreach (var exclusiveLeafClass in exclusiveLeafClasses)
                        {
                            if (first)
                            {
                                first = false;
                            }
                            else
                            {
                                sql += "UNION\n";
                            }

                            sql += "SELECT " + this.database.Schema.ObjectId + " As " + this.database.Schema.AssociationId + ", " + this.database.Schema.Column(roleType) + " As " + this.database.Schema.RoleId + "\n";
                            sql += "FROM " + this.database.Schema.Table(exclusiveLeafClass) + "\n";
                            sql += "WHERE " + this.database.Schema.Column(roleType) + " IS NOT NULL\n";
                        }

                        sql += "ORDER BY " + this.database.Schema.AssociationId;
                    }
                    else
                    {
                        if ((roleType.IsMany && associationType.IsMany) || !relation.ExistExclusiveClasses)
                        {
                            sql += "SELECT " + this.database.Schema.AssociationId + "," + this.database.Schema.RoleId + "\n";
                            sql += "FROM " + this.database.Schema.Table(relation) + "\n";
                            sql += "ORDER BY " + this.database.Schema.AssociationId + "," + this.database.Schema.RoleId;
                        }
                        else
                        {
                            // use foreign keys
                            if (roleType.IsOne)
                            {
                                sql += "SELECT " + this.database.Schema.ObjectId + " As " + this.database.Schema.AssociationId + ", " + this.database.Schema.Column(roleType) + " As " + this.database.Schema.RoleId + "\n";
                                sql += "FROM " + this.database.Schema.Table(associationType.ObjectType.ExclusiveClass) + "\n";
                                sql += "WHERE " + this.database.Schema.Column(roleType) + " IS NOT NULL\n";
                                sql += "ORDER BY " + this.database.Schema.AssociationId;
                            }
                            else
                            {
                                // role.Many
                                var compositeType = (IComposite)roleType.ObjectType;
                                sql += "SELECT " + this.database.Schema.Column(associationType) + " As " + this.database.Schema.AssociationId + ", " + this.database.Schema.ObjectId + " As " + this.database.Schema.RoleId + "\n";
                                sql += "FROM " + this.database.Schema.Table(compositeType.ExclusiveClass) + "\n";
                                sql += "WHERE " + this.database.Schema.Column(associationType) + " IS NOT NULL\n";
                                sql += "ORDER BY " + this.database.Schema.AssociationId + "," + this.database.Schema.RoleId;
                            }
                        }
                    }

                    using (var command = session.CreateCommand(sql))
                    {
                        using (var reader = command.ExecuteReader())
                        {
                            if (roleType.IsMany)
                            {
                                using (var relationTypeManyXmlWriter = new RelationTypeManyXmlWriter(relation, this.writer))
                                {
                                    while (reader.Read())
                                    {
                                        var a = long.Parse(reader[0].ToString());
                                        var r = long.Parse(reader[1].ToString());
                                        relationTypeManyXmlWriter.Write(a, r);
                                    }

                                    relationTypeManyXmlWriter.Close();
                                }
                            }
                            else
                            {
                                using (var relationTypeOneXmlWriter = new RelationTypeOneXmlWriter(relation, this.writer))
                                {
                                    while (reader.Read())
                                    {
                                        var a = long.Parse(reader[0].ToString());

                                        if (roleType.ObjectType is IUnit)
                                        {
                                            var unitType    = (IUnit)roleType.ObjectType;
                                            var unitTypeTag = unitType.UnitTag;
                                            var r           = command.GetValue(reader, unitTypeTag, 1);
                                            var content     = Serialization.WriteString(unitTypeTag, r);
                                            relationTypeOneXmlWriter.Write(a, content);
                                        }
                                        else
                                        {
                                            var r = reader[1];
                                            relationTypeOneXmlWriter.Write(a, XmlConvert.ToString(long.Parse(r.ToString())));
                                        }
                                    }

                                    relationTypeOneXmlWriter.Close();
                                }
                            }
                        }
                    }
                }
            }
        }