Created
January 5, 2012 12:51
-
-
Save seraphy/1565132 to your computer and use it in GitHub Desktop.
C#4.0でのXMLの書き出し、LinqToXMLを使った読み込み、およびコマンド引数のワイルドカード展開などメモ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.IO; | |
| using System.Xml; | |
| using System.Xml.Linq; | |
| namespace TableColumnDefLib | |
| { | |
| public static class ArgumentUtils | |
| { | |
| public static string[] ExpandFilePaths(string[] args) | |
| { | |
| var fileList = new HashSet<string>(); | |
| Array.ForEach(args, (arg) => { | |
| var substitutedArg = System.Environment.ExpandEnvironmentVariables(arg); | |
| var dirPart = Path.GetDirectoryName(substitutedArg); | |
| if (dirPart.Length == 0) | |
| { | |
| dirPart = "."; | |
| } | |
| var filePart = Path.GetFileName(substitutedArg); | |
| Array.ForEach( | |
| Directory.GetFiles(dirPart, filePart), | |
| (filepath) => { fileList.Add(filepath); } | |
| ); | |
| }); | |
| return fileList.ToArray(); | |
| } | |
| } | |
| public class ColumnDef | |
| { | |
| public int Order { set; get; } | |
| public string ColumnName { get; set; } | |
| public string ColumnNamePhysical { get; set; } | |
| public string DBType { get; set; } | |
| public string Length { set; get; } | |
| public bool NotNull { set; get; } | |
| public int? PrimaryOrder { set; get; } | |
| public override string ToString() | |
| { | |
| return "(" + Order + ": " + ColumnName + "/" + ColumnNamePhysical + " : " | |
| + DBType + "(" + Length + ") " | |
| + (PrimaryOrder.HasValue ? "PK" : (NotNull ? "NotNull" : "Nullable")) + ")"; | |
| } | |
| } | |
| public class PrimaryKey | |
| { | |
| public string Name { set; get; } | |
| private SortedDictionary<int, ColumnDef> _primaryColumns = new SortedDictionary<int,ColumnDef>(); | |
| public SortedDictionary<int, ColumnDef> Columns | |
| { | |
| get | |
| { | |
| return _primaryColumns; | |
| } | |
| } | |
| public override string ToString() | |
| { | |
| var buf = new StringBuilder(); | |
| buf.Append("(PrimaryKey: " + Name); | |
| foreach (var col in _primaryColumns.Values) | |
| { | |
| buf.Append(col.ToString()); | |
| } | |
| buf.Append(")"); | |
| return buf.ToString(); | |
| } | |
| } | |
| public class TableDef | |
| { | |
| public string ObjType { get; set; } | |
| public string TableName { get; set; } | |
| public string TableNamePhysical { get; set; } | |
| private SortedDictionary<int, ColumnDef> _columnDef = new SortedDictionary<int, ColumnDef>(); | |
| public SortedDictionary<int, ColumnDef> ColumnDefs | |
| { | |
| get | |
| { | |
| return _columnDef; | |
| } | |
| } | |
| public PrimaryKey PrimaryKey { set; get; } | |
| public override string ToString() | |
| { | |
| var buf = new StringBuilder(); | |
| buf.Append("(" + ObjType + ":" + TableName + "/" + TableNamePhysical + " "); | |
| foreach (var col in _columnDef.Values) | |
| { | |
| buf.Append(col.ToString()); | |
| } | |
| buf.Append(")"); | |
| return buf.ToString(); | |
| } | |
| } | |
| public static class TableDefIO | |
| { | |
| public static void OutputXML(List<TableDef> results, Stream stm) | |
| { | |
| var settings = new XmlWriterSettings(); | |
| settings.Indent = true; | |
| settings.Encoding = new UTF8Encoding(false); // Suppress BOM | |
| using (XmlWriter wr = XmlWriter.Create(stm, settings)) | |
| { | |
| wr.WriteStartDocument(); | |
| var tableMap = new SortedDictionary<string, TableDef>(); | |
| results.ForEach(tableDef => { tableMap.Add(tableDef.TableNamePhysical, tableDef); }); | |
| wr.WriteStartElement("Tables"); | |
| foreach (TableDef tableDef in tableMap.Values) | |
| { | |
| wr.WriteStartElement("Table"); | |
| string TableNamePhysical = tableDef.TableNamePhysical; | |
| if (TableNamePhysical.Length > 30) | |
| { | |
| System.Console.Error.WriteLine("テーブルの物理名が30文字を超えています:" + TableNamePhysical); | |
| } | |
| wr.WriteAttributeString("Name", TableNamePhysical); | |
| wr.WriteAttributeString("Comment", tableDef.TableName); | |
| var primaryKeys = new List<ColumnDef>(); | |
| wr.WriteStartElement("Columns"); | |
| foreach (ColumnDef columnDef in tableDef.ColumnDefs.Values) | |
| { | |
| wr.WriteStartElement("Column"); | |
| string ColumnNamePhysical = columnDef.ColumnNamePhysical; | |
| if (ColumnNamePhysical.Length > 30) | |
| { | |
| System.Console.Error.WriteLine("カラムの物理名が30文字を超えています:" + ColumnNamePhysical); | |
| } | |
| wr.WriteAttributeString("Name", columnDef.ColumnNamePhysical); | |
| wr.WriteAttributeString("Comment", columnDef.ColumnName); | |
| wr.WriteAttributeString("Order", columnDef.Order.ToString()); | |
| wr.WriteAttributeString("DBType", columnDef.DBType ?? ""); | |
| wr.WriteAttributeString("Length", columnDef.Length ?? ""); | |
| wr.WriteAttributeString("NotNull", columnDef.NotNull ? "true" : "false"); | |
| if (columnDef.PrimaryOrder.HasValue) | |
| { | |
| primaryKeys.Add(columnDef); | |
| } | |
| wr.WriteEndElement(); | |
| } | |
| wr.WriteEndElement(); | |
| if (primaryKeys.Count > 0) | |
| { | |
| string pkName = tableDef.TableNamePhysical; | |
| if (pkName.Length > 27) | |
| { | |
| pkName = pkName.Substring(0, 27); | |
| } | |
| pkName = pkName + "_PK"; | |
| primaryKeys.Sort((x, y) => { return x.PrimaryOrder.Value - y.PrimaryOrder.Value; }); | |
| wr.WriteStartElement("PrimaryKey"); | |
| wr.WriteAttributeString("Name", pkName); | |
| wr.WriteStartElement("Columns"); | |
| foreach (ColumnDef columnDef in primaryKeys) | |
| { | |
| wr.WriteStartElement("Column"); | |
| wr.WriteAttributeString("Order", columnDef.PrimaryOrder.Value.ToString()); | |
| wr.WriteAttributeString("Name", columnDef.ColumnNamePhysical); | |
| wr.WriteEndElement(); | |
| } | |
| wr.WriteEndElement(); | |
| wr.WriteEndElement(); | |
| } | |
| wr.WriteEndElement(); | |
| } | |
| wr.WriteEndElement(); | |
| wr.WriteEndDocument(); | |
| } | |
| } | |
| public static void ReadXML(List<TableDef> results, Stream stm) | |
| { | |
| XDocument doc = XDocument.Load(stm); | |
| XElement root = doc.Element("Tables"); | |
| if (root == null) | |
| { | |
| System.Console.Error.WriteLine("XMLのルート要素はTablesでなければなりません。"); | |
| return; | |
| } | |
| var tables = from t in root.Elements("Table") select t; | |
| foreach (var tableDef in tables) | |
| { | |
| var tbl = new TableDef(); | |
| results.Add(tbl); | |
| string TableNamePhysical = tableDef.Attribute("Name").Value; | |
| string TableName = tableDef.Attribute("Comment").Value; | |
| tbl.ObjType = "TABLE"; | |
| tbl.TableName = TableName; | |
| tbl.TableNamePhysical = TableNamePhysical; | |
| var cols = tbl.ColumnDefs; | |
| var colsMap = new Dictionary<string, ColumnDef>(); | |
| var columns = from c in tableDef.Elements("Columns").Elements("Column") select c; | |
| foreach (var columnDef in columns) | |
| { | |
| string ColumnNamePhysical = (columnDef.Attribute("Name").Value).ToUpper(); | |
| string ColumnName = columnDef.Attribute("Comment").Value; | |
| string ColumnOrder = columnDef.Attribute("Order").Value; | |
| string DBType = columnDef.Attribute("DBType").Value; | |
| string Length = columnDef.Attribute("Length").Value; | |
| string NotNull = columnDef.Attribute("NotNull").Value; | |
| var col = new ColumnDef(); | |
| col.ColumnName = ColumnName; | |
| col.ColumnNamePhysical = ColumnNamePhysical; | |
| col.DBType = DBType; | |
| col.Length = Length; | |
| col.NotNull = (NotNull == null) ? false : (NotNull.ToLower() == "true"); | |
| int order = -1; | |
| int.TryParse(ColumnOrder, out order); | |
| col.Order = order; | |
| cols.Add(order, col); | |
| colsMap.Add(ColumnNamePhysical, col); | |
| } | |
| PrimaryKey pk = null; | |
| var primaryKeyElm = tableDef.Element("PrimaryKey"); | |
| if (primaryKeyElm != null) | |
| { | |
| pk = new PrimaryKey(); | |
| tbl.PrimaryKey = pk; | |
| string pkName = primaryKeyElm.Attribute("Name").Value; | |
| pk.Name = pkName; | |
| var primaries = from p in primaryKeyElm.Elements("Columns").Elements("Column") select p; | |
| foreach (var columnDef in primaries) | |
| { | |
| string ColumnNamePhysical = (columnDef.Attribute("Name").Value).ToUpper(); | |
| string PrimaryOrder = columnDef.Attribute("Order").Value; | |
| int order = int.Parse(PrimaryOrder); | |
| ColumnDef col = colsMap[ColumnNamePhysical]; | |
| pk.Columns.Add(order, col); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment