Ejemplo n.º 1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            PrimaryKeyFieldSchema field2 = obj as PrimaryKeyFieldSchema;

            if ((object)field2 == null)
            {
                return(false);
            }

            // Kolla igenom alla egenskaper och jämför dem
            if (IsIdentity != field2.IsIdentity)
            {
                return(false);
            }

            return(base.Equals(obj));
        }
Ejemplo n.º 2
0
        private void ParseXml(string xmlString)
        {
            XmlReader reader = XmlReader.Create(new StringReader(xmlString));

            reader.ReadToFollowing("Database");
            if (reader.MoveToAttribute("Version"))
            {
                int version;
                if (int.TryParse(reader.Value, out version))
                {
                    Version = version;
                }
                else
                {
                    throw new DatabaseSchemaException("Database version tag is unreadable.");
                }
            }
            else
            {
                throw new DatabaseSchemaException("Database tag is missing its version attribute.");
            }

            while (reader.ReadToFollowing("Table"))
            {
                string             tableName  = "";
                bool               isView     = false;
                List <FieldSchema> fields     = new List <FieldSchema>();
                List <UniqueKey>   uniqueKeys = new List <UniqueKey>();

                while (reader.MoveToNextAttribute())
                {
                    string attributeName = reader.Name;

                    switch (attributeName)
                    {
                    case "Name":
                        tableName = reader.Value;
                        break;

                    case "IsView":
                        isView = (reader.Value == "true");
                        break;

                    default:
                        throw new NotImplementedException("'" + attributeName + "' on table '" + tableName + "' is not a valid table attribute.");
                    }
                }

                if (tableName == "")
                {
                    throw new DatabaseSchemaException("Table must have a name.");
                }

                while (reader.Read() && reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.EndElement)
                {
                    ;
                }

                if (reader.NodeType != XmlNodeType.EndElement && !reader.EOF)
                {
                    if (reader.Name == "Fields")
                    {
                        while (reader.Read() && reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.EndElement)
                        {
                            ;
                        }

                        while (reader.NodeType != XmlNodeType.EndElement && !reader.EOF)
                        {
                            FieldSchema field;

                            if (reader.Name == "PrimaryKeyField")
                            {
                                string fieldName  = "";
                                string fieldType  = "";
                                bool   allowNull  = false;
                                bool   isIdentity = false;

                                while (reader.MoveToNextAttribute())
                                {
                                    string attributeName = reader.Name;

                                    switch (attributeName)
                                    {
                                    case "Name":
                                        fieldName = reader.Value;
                                        break;

                                    case "Type":
                                        fieldType = reader.Value;
                                        break;

                                    case "AllowNull":
                                        allowNull = Convert.ToBoolean(reader.Value);
                                        break;

                                    case "IsIdentity":
                                        isIdentity = Convert.ToBoolean(reader.Value);
                                        break;

                                    default:
                                        throw new NotImplementedException("'" + attributeName + "' on field '" + fieldName + "' on table '" + tableName + "' is not a valid primary key field attribute.");
                                    }
                                }

                                if (fieldName == "")
                                {
                                    throw new DatabaseSchemaException("Primary key field for table '" + tableName + "' must have a name.");
                                }
                                if (fieldType == "")
                                {
                                    throw new DatabaseSchemaException("Primary key field '" + fieldName + "' on table '" + tableName + "' must have a type.");
                                }

                                field = new PrimaryKeyFieldSchema(tableName, fieldName, fieldType, allowNull, isIdentity);
                            }
                            else if (reader.Name == "ForeignKeyField")
                            {
                                string     fieldName       = "";
                                string     fieldType       = "";
                                string     targetTableName = "";
                                string     targetFieldName = "";
                                bool       allowNull       = false;
                                List <int> uniqueKeyGroups = new List <int>();

                                while (reader.MoveToNextAttribute())
                                {
                                    string attributeName = reader.Name;

                                    switch (attributeName)
                                    {
                                    case "Name":
                                        fieldName = reader.Value;
                                        break;

                                    case "Type":
                                        fieldType = reader.Value;
                                        break;

                                    case "TargetTable":
                                        targetTableName = reader.Value;
                                        break;

                                    case "TargetField":
                                        targetFieldName = reader.Value;
                                        break;

                                    case "AllowNull":
                                        allowNull = Convert.ToBoolean(reader.Value);
                                        break;

                                    case "UniqueKeyGroup":
                                        string[] keyGroups = reader.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                                        foreach (string sKeyGroup in keyGroups)
                                        {
                                            uniqueKeyGroups.Add(int.Parse(sKeyGroup));
                                        }
                                        break;

                                    default:
                                        throw new NotImplementedException("'" + attributeName + "' on field '" + fieldName + "' on table '" + tableName + "' is not a valid foreign key field attribute.");
                                    }
                                }


                                if (fieldName == "")
                                {
                                    throw new DatabaseSchemaException("Foreign key field for table '" + tableName + "' must have a name.");
                                }
                                if (fieldType == "")
                                {
                                    throw new DatabaseSchemaException("Foreign key field '" + fieldName + "' on table '" + tableName + "' must have a type.");
                                }
                                if (targetTableName == "")
                                {
                                    throw new DatabaseSchemaException("Foreign key field '" + fieldName + "' on table '" + tableName + "' must have a target table.");
                                }
                                if (targetFieldName == "")
                                {
                                    throw new DatabaseSchemaException("Foreign key field '" + fieldName + "' on table '" + tableName + "' must have a target field.");
                                }

                                field = new ForeignKeyFieldSchema(tableName, fieldName, fieldType, targetTableName, targetFieldName, allowNull);

                                if (uniqueKeyGroups.Count > 0)
                                {
                                    foreach (int uniqueKeyGroup in uniqueKeyGroups)
                                    {
                                        AddToUniqueKeys(uniqueKeys, tableName, uniqueKeyGroup, field);
                                    }
                                }
                            }
                            else if (reader.Name == "Field")
                            {
                                string     fieldName       = "";
                                string     fieldType       = "";
                                bool       allowNull       = false;
                                List <int> uniqueKeyGroups = new List <int>();

                                while (reader.MoveToNextAttribute())
                                {
                                    string attributeName = reader.Name;

                                    switch (attributeName)
                                    {
                                    case "Name":
                                        fieldName = reader.Value;
                                        break;

                                    case "Type":
                                        fieldType = reader.Value;
                                        break;

                                    case "AllowNull":
                                        allowNull = Convert.ToBoolean(reader.Value);
                                        break;

                                    case "UniqueKeyGroup":
                                        string[] keyGroups = reader.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                                        foreach (string sKeyGroup in keyGroups)
                                        {
                                            uniqueKeyGroups.Add(int.Parse(sKeyGroup));
                                        }
                                        break;

                                    default:
                                        throw new NotImplementedException("'" + attributeName + "' on field '" + fieldName + "' on table '" + tableName + "' is not a valid field attribute.");
                                    }
                                }

                                if (fieldName == "")
                                {
                                    throw new DatabaseSchemaException("Field on table '" + tableName + "' must have a name.");
                                }
                                if (fieldType == "")
                                {
                                    throw new DatabaseSchemaException("Field '" + fieldName + "' table '" + tableName + "' must have a type.");
                                }

                                field = new FieldSchema(tableName, fieldName, fieldType, allowNull);

                                if (uniqueKeyGroups.Count > 0)
                                {
                                    foreach (int uniqueKeyGroup in uniqueKeyGroups)
                                    {
                                        AddToUniqueKeys(uniqueKeys, tableName, uniqueKeyGroup, field);
                                    }
                                }
                            }
                            else if (reader.Name == "ViewField")
                            {
                                string fieldName = "";
                                string fieldType = "";
                                bool   allowNull = false;

                                while (reader.MoveToNextAttribute())
                                {
                                    string attributeName = reader.Name;

                                    switch (attributeName)
                                    {
                                    case "Name":
                                        fieldName = reader.Value;
                                        break;

                                    case "Type":
                                        fieldType = reader.Value;
                                        break;

                                    case "AllowNull":
                                        allowNull = Convert.ToBoolean(reader.Value);
                                        break;

                                    default:
                                        throw new NotImplementedException("'" + attributeName + "' on field '" + fieldName + "' on table '" + tableName + "' is not a valid field attribute.");
                                    }
                                }

                                if (fieldName == "")
                                {
                                    throw new DatabaseSchemaException("Field on table '" + tableName + "' must have a name.");
                                }
                                if (fieldType == "")
                                {
                                    throw new DatabaseSchemaException("Field '" + fieldName + "' table '" + tableName + "' must have a type.");
                                }

                                field = new ViewFieldSchema(tableName, fieldName, fieldType, allowNull);
                            }
                            else
                            {
                                throw new DatabaseSchemaException("When initiating table '" + tableName + "', tags '<PrimaryKeyField>, <ForeighKeyField> or <Field>' was expected, but got tag '<" + reader.Name + ">'");
                            }

                            fields.Add(field);

                            while (reader.Read() && reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.EndElement)
                            {
                                ;
                            }
                        }
                    }
                    else
                    {
                        throw new DatabaseSchemaException("When initiating table '" + tableName + "', tag '<Fields>' was expected, but got tag '<" + reader.Name + ">'");
                    }

                    Tables.Add(new TableSchema(tableName, isView, fields.ToArray(), uniqueKeys.ToArray()));
                }
            }
        }