Exemplo n.º 1
0
        /// <summary>
        /// 添加或者更新指定文件中的数据库连接信息。
        /// </summary>
        /// <param name="item">数据库连接信息</param>
        /// <param name="file">保存到的文件路径</param>
        public static void SaveOrUpdate(ConnectedDatabase item, string file = null)
        {
            if (item != null)
            {
                var collections = GetConnectedDatabases(file);
                var xdoument = CreateXDocument(file) ?? XDocument.Parse("<root></root>");

                if (collections[item.Type, item.Name] != null)
                {
                    var elements = (from x in xdoument.Descendants("database")
                                    where
                                        x.Attribute("type").Value == item.Type.ToString() && x.Attribute("name").Value == item.Name
                                    select x);

                    foreach (var element in elements)
                    {
                        element.Element("logon").Attribute("server").Value = item.ServerUri;
                        element.Element("logon").Attribute("account").Value = item.Account;
                        element.Element("logon").Attribute("password").Value = item.Password;
                    }
                }
                else
                {
                    var element = new XElement("database");
                    var logonElement = new XElement("logon");

                    element.SetAttributeValue("name", item.Name);
                    element.SetAttributeValue("type", item.Type);
                    logonElement.SetAttributeValue("server", item.ServerUri);
                    logonElement.SetAttributeValue("account", item.Account);
                    logonElement.SetAttributeValue("password", item.Password);

                    element.Add(logonElement);

                    if (xdoument.Root.LastNode != null)
                    {
                        xdoument.Root.LastNode.AddAfterSelf(element);
                    }
                    else
                    {
                        xdoument.Root.Add(element);
                    }
                }

                xdoument.Save(GetFullFilePath(file));
            }
        }
Exemplo n.º 2
0
        public static XDocument ToXml(this DbTable table, ConnectedDatabase database, string rootNamespace, string author, DateTime buildDate, string copyrightOwner)
        {
            var xdocument = new XDocument(new XElement("root"));

            xdocument.Root.Add(new XElement("author", author));
            xdocument.Root.Add(new XElement("rootNamespace", rootNamespace));
            xdocument.Root.Add(new XElement("buildDate", buildDate.ToString("yyyy-MM-dd")));
            xdocument.Root.Add(new XElement("copyright", copyrightOwner));

            if (table != null && database != null)
            {
                var tableElement = new XElement("table");
                var dbTypeMapping = ServiceLocator.Current.GetInstance<DbTypeMapping>(database.Type.ToString());

                var arrays = table.Name.Split('.');

                tableElement.SetAttributeValue("table", table.Name.Contains(".") ? $"{arrays[0]}.{arrays[1]}" : $"{table.Name}");

                if (database.Type == DatabaseType.MSSQL)
                {
                    tableElement.SetAttributeValue("name", table.Name.Contains(".") ? $"[{arrays[0]}].[{arrays[1]}]" : $"[{table.Name}]");
                }
                else
                {
                    tableElement.SetAttributeValue("name", table.Name.Contains(".") ? $"{arrays[0]}.{arrays[1]}" : $"{table.Name}");
                }

                tableElement.SetAttributeValue("namespace", table.Namespace);
                tableElement.SetAttributeValue("isView", table.IsView);
                tableElement.SetAttributeValue("moduleName", table.ModuleName ?? string.Empty);
                tableElement.SetAttributeValue("moduleDescription", table.ModuleDescription ?? string.Empty);
                tableElement.SetAttributeValue("className", table.ClassName);
                tableElement.SetAttributeValue("camelClassName", table.ClassName.CamelNaming());
                tableElement.SetAttributeValue("pluralClassName", table.ClassName.PluralClassName());
                tableElement.SetAttributeValue("description", table.Description);
                tableElement.SetAttributeValue("hasCreate", table.HasCreate);
                tableElement.SetAttributeValue("hasUpdate", table.HasUpdate);
                tableElement.SetAttributeValue("hasCreateOrUpdate", table.HasCreateOrUpdate);
                tableElement.SetAttributeValue("hasRemove", table.HasRemove);
                tableElement.SetAttributeValue("hasSingleData", table.HasSingleData);
                tableElement.SetAttributeValue("hasSearchData", table.HasSearchData);

                foreach (var column in table.Columns)
                {
                    var columnElement = new XElement("column");

                    columnElement.SetAttributeValue("name", column.Name);
                    columnElement.SetAttributeValue("sqlType", column.SqlType);
                    columnElement.SetAttributeValue("length", column.Length);
                    columnElement.SetAttributeValue("nullable", column.Nullable);
                    columnElement.SetAttributeValue("basicType", dbTypeMapping.GetBasicType("C#", column.SqlType));
                    columnElement.SetAttributeValue("isPrimaryKey", column.IsPrimaryKey);
                    columnElement.SetAttributeValue("isIdentity", column.IsIdentity);
                    columnElement.SetAttributeValue("description", column.Description);
                    columnElement.SetAttributeValue("propertyName", column.PropertyName);
                    columnElement.SetAttributeValue("fieldName", column.FieldName);
                    columnElement.SetAttributeValue("isKeyWordSearch", column.IsKeyWordSearch);
                    columnElement.SetAttributeValue("isSearchCriteria", column.IsSearchCriteria);

                    tableElement.Add(columnElement);
                }

                xdocument.Root.Add(tableElement);
            }

            return xdocument;
        }