2009-05-15 3 views
2

この質問の終わりに、フォーマットとしてXMLとしてデータベース定義を保存しています。私が抱えている問題は、スキーマのリスト、それらのスキーマのテーブル、それらのテーブルの列(すべて関連情報付き)です。私の現在のコード(サンプルXMLの下に含まれています)は、ネストを完全に無視し、複数のスキーマのために各テーブル/カラムを複数回返します。LinqからDDLを生成するためのXML

サンプルXML:

<schemas> 
    <schema> 
     <name>schema_name1</name> 
     <tables> 
      <table> 
       <name>table_name2</name> 
       <comment>comment string2</comment> 
       <type>innodb2</type> 
       <columns> 
        <column> 
         <name>column_name3</name> 
         <type>data_type3</type> 
         <size>3</size> 
         <nullable>not null3</nullable> 
         <comment>comment string3</comment> 
        </column> 
        <column> 
         <name>column_name4</name> 
         <type>data_type4</type> 
         <size>4</size> 
         <nullable>not null4</nullable> 
         <comment>comment string4</comment> 
        </column> 
       </columns> 
      </table> 
     </tables> 
    </schema> 
    <schema> 
     <name>schema_name5</name> 
     <tables> 
      <table> 
       <name>table_name6</name> 
       <comment>comment string6</comment> 
       <type>innodb6</type> 
       <columns> 
        <column> 
         <name>column_name7</name> 
         <type>data_type7</type> 
         <size>7</size> 
         <nullable>not null7</nullable> 
         <comment>comment string7</comment> 
        </column> 
       </columns> 
      </table> 
     </tables> 
    </schema> 
</schemas> 

C#コード:

XDocument xml_input = XDocument.Load(FILE_IN); 
    string column_create = ""; 
    //build a list of all schemas in xml 
    var schemas = from s in xml_input.Descendants("schema") 
        select new 
        { 
         name = s.Element("name").Value 
        }; 
    //loop through all schemas 
    foreach (var s in schemas) 
    { 
     //write the schema creation lines 
     Console.WriteLine("DROP SCHEMA IF EXISTS " + s.name + ";"); 
     Console.WriteLine("CREATE SCHEMA " + s.name + ";"); 
     //build a list of all tables in schema 
     var tables = from t in xml_input.Descendants("schema") 
             .Descendants("table") 
        select new 
        { 
         name = t.Element("name").Value, 
         comment = t.Element("comment").Value, 
         type = t.Element("type").Value 
        }; 
     //loop through all tables in schema 
     foreach (var t in tables) 
     { 
      //write the beginning of the table creation lines 
      Console.WriteLine("CREATE TABLE " + s.name + "." + t.name + " ("); 
      //build a list of all columns in the schema 
      var columns = from c in xml_input.Descendants("schema") 
              .Descendants("table") 
              .Descendants("column") 
          select new 
          { 
           name = c.Element("name").Value, 
           type = c.Element("type").Value, 
           size = c.Element("size").Value, 
           comment = c.Element("comment").Value 
          }; 
      //loop through all columns in table 
      foreach (var c in columns) 
      { 
       //build the column creation line 
       column_create = c.name + " " + c.type; 
       if (c.size != null) 
       { 
        column_create += "(" + c.size + ")"; 
       } 
       if (c.comment != null) 
       { 
        column_create += " COMMENT '" + c.comment + "'"; 
       } 
       column_create += ", "; 
       //write the column creation line 
       Console.WriteLine(column_create); 
      } 
      //write the end of the table creation lines 
      Console.WriteLine(")"); 
      if (t.comment != null) 
      { 
       Console.WriteLine("COMMENT '" + t.comment + "'"); 
      } 
      if (t.type != null) 
      { 
       Console.WriteLine("TYPE = " + t.type); 
      } 
      Console.WriteLine(";"); 
     } 
    } 

の入れ子構造を保持する方法上の任意のアイデア?私はまた、オプションのXML要素(すべてのデータ型には当てはまらないテーブルコメントやサイズフィールドなど)の処理に問題があります。

ありがとうございます!

答えて

1

ネストされた構造を保存する方法は次のとおりです。 XElementを匿名型に追加して、ネストされたクエリのソースとして使用します。

 <column> 
     <name>column_name4</name> 
     <type>data_type4</type> 
     <size>4</size> 
     <nullable>not null4</nullable> 
     <comment/> 
     </column> 

これはあなたのクエリで空の文字列を返します。オプションの要素に対処する

XDocument xml_input = XDocument.Load(FILE_IN); 
     string column_create = ""; 
     //build a list of all schemas in xml 
     var schemas = from s in xml_input.Descendants("schema") 
         select new 
         { 
          schema = s, 
          name = s.Element("name").Value 
         }; 
     //loop through all schemas 
     foreach (var s in schemas) 
     { 
      //write the schema creation lines 
      Console.WriteLine("DROP SCHEMA IF EXISTS " + s.name + ";"); 
      Console.WriteLine("CREATE SCHEMA " + s.name + ";"); 
      //build a list of all tables in schema 
      var tables = from t in s.schema.Descendants("table") 
         select new 
         { 
          table = t, 
          name = t.Element("name").Value, 
          comment = t.Element("comment").Value, 
          type = t.Element("type").Value 
         }; 
      //loop through all tables in schema 
      foreach (var t in tables) 
      { 
       //write the beginning of the table creation lines 
       Console.WriteLine("CREATE TABLE " + s.name + "." + t.name + " ("); 
       //build a list of all columns in the schema 
       var columns = from c in t.table.Descendants("column") 
           select new 
           { 
            name = c.Element("name").Value, 
            type = c.Element("type").Value, 
            size = c.Element("size").Value, 
            comment = c.Element("comment").Value 
           }; 
       //loop through all columns in table 
       foreach (var c in columns) 
       { 
        //build the column creation line 
        column_create = c.name + " " + c.type; 
        if (c.size != null) 
        { 
         column_create += "(" + c.size + ")"; 
        } 
        if (c.comment != null) 
        { 
         column_create += " COMMENT '" + c.comment + "'"; 
        } 
        column_create += ", "; 
        //write the column creation line 
        Console.WriteLine(column_create); 
       } 
       //write the end of the table creation lines 
       Console.WriteLine(")"); 
       if (t.comment != null) 
       { 
        Console.WriteLine("COMMENT '" + t.comment + "'"); 
       } 
       if (t.type != null) 
       { 
        Console.WriteLine("TYPE = " + t.type); 
       } 
       Console.WriteLine(";"); 
      } 
     } 

一つの方法は、この列のコメントのように値がないとき、あなたのXMLが空の要素を含有しているのですですので、コードを次のように変更してください:

if (!string.IsNullOrEmpty(c.comment)) 
    { 
     column_create += " COMMENT '" + c.comment + "'"; 
    } 
+0

これは素晴らしいことです。魅力的な作品! – Mike

関連する問題