Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.IO;
  7.  
  8. namespace insertyaka
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //Creation d'un fichier sql
  15.             StreamWriter sw = new StreamWriter(@"D:\Jennifer\cSharp\yaka\insert_yaka.sql");
  16.             String prop_sql= "";
  17.             String cat_sql="";
  18.             String desc_sql= "";
  19.             String art_sql= "";
  20.             String art_prop_sql= "";
  21.             String pack_sql= "";
  22.             String pack_prop_sql= "";
  23.             String comp_sql= "";
  24.  
  25.             //Chargement de la page XML
  26.             XmlDocument doc = new XmlDocument();
  27.             doc.Load(@"D:\Jennifer\cSharp\yaka\insertyaka\yakadb.xml");
  28.  
  29.             /***** TABLE PROPERTY ****/
  30.             XmlNodeList list = doc.DocumentElement.SelectNodes("//property/name/text()");
  31.  
  32.             /*HashSet permet d'avoir uniquement les différents*/
  33.             HashSet<string> uniqueprops = new HashSet<string>();
  34.             /*Dictionary est un tableau associatif*/
  35.             Dictionary<string, int> dico = new Dictionary<string, int>();
  36.  
  37.             /*Pour tous les nodes de property, je les ajoutes dans le hashset et c'est le hashset qui élimine les doublons*/
  38.             foreach (XmlNode p in list)
  39.             {
  40.                 //Console.WriteLine("Ajoute valeur de propriété : " + p.Value);
  41.                 uniqueprops.Add(p.Value);
  42.             }
  43.  
  44.             /*Je peux numéroter arbitrairement les property et je les mets dans un dico pour pouvoir retrouver l'association*/
  45.             int i = 0;
  46.             foreach (string item in uniqueprops)
  47.             {
  48.                 Console.WriteLine("Valeur dans le hashset : " + item);
  49.                 dico.Add(item, ++i);
  50.             }
  51.            
  52.             foreach (string item in dico.Keys)
  53.             {
  54.                 prop_sql += "INSERT INTO property(prop_id,prop_name) VALUES(" + dico[item] + ",'" + item + "');\n";
  55.             }
  56.            
  57.  
  58.             //***** TABLE CATEGORY *****//    
  59.             XmlNode categories = doc.DocumentElement.SelectSingleNode("categories"); //Single car il n'y a qu'une balise
  60.             XmlNodeList liste_categories = categories.SelectNodes("category");
  61.  
  62.             foreach (XmlNode categorie in liste_categories)
  63.             {
  64.                 String cat_id = categorie.SelectSingleNode("@id").Value;
  65.                 Console.WriteLine("id de la categorie:" + cat_id);
  66.                 String cat_name = categorie.SelectSingleNode("text()").Value;
  67.                 Console.WriteLine("nom de la categorie: " + cat_name);
  68.                 String cat_parent;
  69.  
  70.                 if (categorie.SelectSingleNode("@parent") == null)
  71.                 {
  72.                     cat_parent = "null";
  73.                 }
  74.                 else
  75.                 {
  76.                     cat_parent = categorie.SelectSingleNode("@parent").Value;
  77.                 }
  78.                 Console.WriteLine("Clé etrangère de la catégorie: " + cat_parent);
  79.  
  80.                 cat_sql += "INSERT INTO category(cat_id,cat_name,cat_parent,cat_deleted) VALUES (" + cat_id + ",'" + cat_name + "'," + cat_parent + ",0);\n";
  81.             }
  82.             XmlNode articles = doc.DocumentElement.SelectSingleNode("articles");
  83.            
  84.  
  85.  
  86.             /***** TABLE DE DESCRIPTION *****/
  87.             XmlNodeList types = articles.SelectNodes("type");    
  88.  
  89.             foreach (XmlNode type in types)
  90.             {
  91.                 String desc_id = type.SelectSingleNode("@id").Value;
  92.                 Console.WriteLine("Id de la description : " + desc_id);
  93.  
  94.                 String desc_name = type.SelectSingleNode("name/text()").Value;
  95.                 Console.WriteLine("Nom de la description : " + desc_name);
  96.  
  97.                 String desc_description = type.SelectSingleNode("description/text()").Value;
  98.                 Console.WriteLine("Description : " + desc_description);
  99.  
  100.                 String desc_shortdesc = type.SelectSingleNode("short-desc/text()").Value;
  101.                 Console.WriteLine("Raccourci description :" + desc_shortdesc);
  102.  
  103.                 String desc_image = type.SelectSingleNode("image/text()").Value;
  104.                 Console.WriteLine("image de la description : " + desc_image);
  105.  
  106.                 String desc_thumbnail = type.SelectSingleNode("thumb/text()").Value;
  107.                 Console.WriteLine("vignette : " + desc_thumbnail);
  108.  
  109.                 String desc_categorie_fk = type.SelectSingleNode("category_ref/@id").Value;
  110.                 Console.WriteLine("Clé etrangère categorie : " + desc_categorie_fk);
  111.  
  112.                 desc_sql += "INSERT INTO description(desc_id,desc_name,desc_description,desc_shortdesc,desc_image,desc_thumbnail,desc_category_fk,desc_deleted) VALUES (" + desc_id + ",'" + desc_name.Replace("'", "''") + "','" + desc_description.Replace("'", "''") + "','" + desc_shortdesc.Replace("'", "''") + "','" + desc_image + "','" + desc_thumbnail + "'," + desc_categorie_fk + ",0);\n";
  113.  
  114.  
  115.                 /***** TABLE ARTICLE *****/
  116.                 XmlNode sub_articles = type.SelectSingleNode("articles");
  117.  
  118.                 XmlNode compose = type.SelectSingleNode("composition");
  119.  
  120.                 if (sub_articles != null)
  121.                 {
  122.                     XmlNodeList article = sub_articles.SelectNodes("article");
  123.  
  124.                     foreach (XmlNode art_detail in article)
  125.                     {
  126.                         String art_id = art_detail.SelectSingleNode("@id").Value;
  127.                         Console.WriteLine("id de l'article :" + art_id);
  128.  
  129.                         String art_price = art_detail.SelectSingleNode("price/text()").Value;
  130.                         int prix = (int)(float.Parse(art_price));
  131.                         Console.WriteLine("Prix de l'article :" + prix);
  132.  
  133.                         art_sql += "INSERT INTO article(art_id,art_price,art_description_fk,art_promotion_fk,art_deleted) VALUES (" + art_id + ",'" + prix + "'," + desc_id + ",null,0);\n";
  134.  
  135.  
  136.                         /***** TABLE ARTICLE HAS PROPERTIES *****/
  137.                         XmlNode properties = art_detail.SelectSingleNode("properties");
  138.  
  139.                         if (properties != null)
  140.                         {
  141.                             XmlNodeList property = properties.SelectNodes("property");
  142.  
  143.                             foreach (XmlNode prop in property)
  144.                             {
  145.                                 String prop_name = prop.SelectSingleNode("name/text()").Value;
  146.                                 Console.WriteLine("Nom de la propriété: " + prop_name);
  147.  
  148.                                 String ap_value = prop.SelectSingleNode("value/text()").Value;
  149.                                 Console.WriteLine("Valeur de la propriété de l'article: " + ap_value);
  150.  
  151.                                 art_prop_sql += "INSERT INTO articleHasProperties(ap_article_fk,ap_property_fk,ap_value) VALUES (" + art_id + "," + dico[prop_name] + ",'" + ap_value + "');\n";
  152.                             }
  153.                         }
  154.                     }
  155.                 }
  156.  
  157.                 /***** TABLE PACK + COMPOSE *****/
  158.                 if (compose != null)
  159.                 {
  160.                     String pack_id = compose.SelectSingleNode("@id").Value;
  161.                     Console.WriteLine("Id du pack : " + pack_id);
  162.  
  163.                     XmlNodeList reference_liste = compose.SelectNodes("article_ref");
  164.  
  165.                     pack_sql += "INSERT INTO pack(pack_id,pack_description_fk,pack_promotion_fk,pack_deleted) VALUES (" + pack_id + "," + desc_id + ",null,0);\n";
  166.  
  167.                     foreach (XmlNode reference in reference_liste)
  168.                     {
  169.                         String comp_article_fk = reference.SelectSingleNode("@id").Value;
  170.                         Console.WriteLine("Clé etrangère de l'article:" + comp_article_fk);
  171.  
  172.                         String comp_quantity = reference.SelectSingleNode("@quantity").Value;
  173.                         Console.WriteLine("Quantité : " + comp_quantity);
  174.  
  175.                         comp_sql += "INSERT INTO compose(comp_pack_fk,comp_article_fk,comp_quantity) VALUES ("+pack_id + "," + comp_article_fk + "," + comp_quantity +");\n";
  176.  
  177.  
  178.                         /***** TABLE PACK HAS PROPERTIES *****/
  179.                         XmlNode properties = compose.SelectSingleNode("properties");
  180.  
  181.                         if (properties != null)
  182.                         {
  183.                             XmlNodeList property = properties.SelectNodes("property");
  184.  
  185.                             foreach (XmlNode prop in property)
  186.                             {
  187.                                 String prop_name = prop.SelectSingleNode("name/text()").Value;
  188.                                 Console.WriteLine("Nom de la propriété: " + prop_name);
  189.  
  190.                                 String pp_value = prop.SelectSingleNode("value/text()").Value;
  191.                                 Console.WriteLine("Valeur de la propriété de l'article: " + pp_value);
  192.  
  193.                                 pack_prop_sql += "INSERT INTO packHasProperties(pp_pack_fk,pp_property_fk,pp_value) VALUES (" + pack_id + "," + dico[prop_name] + ",'" + pp_value.Replace("'", "''") + "');\n";
  194.                             }
  195.                         }
  196.                     }
  197.                 }
  198.             }
  199.  
  200.             String sql = "";
  201.  
  202.             //Pour qu'il ne tient pas compte de l'auto-incrémentation
  203.             sql += "SET IDENTITY_INSERT property ON\n";
  204.                 sql += prop_sql;
  205.             sql += "SET IDENTITY_INSERT property OFF\n";
  206.  
  207.             sql += "SET IDENTITY_INSERT category ON\n";
  208.             sql += cat_sql;
  209.             sql += "SET IDENTITY_INSERT category OFF\n";
  210.  
  211.             sql += "SET IDENTITY_INSERT description ON\n";
  212.             sql += desc_sql;
  213.             sql += "SET IDENTITY_INSERT description OFF\n";
  214.  
  215.             sql += "SET IDENTITY_INSERT article ON\n";
  216.             sql += art_sql;
  217.             sql += "SET IDENTITY_INSERT article OFF\n";
  218.  
  219.             sql += "SET IDENTITY_INSERT pack ON\n";
  220.             sql += pack_sql;
  221.             sql += "SET IDENTITY_INSERT pack OFF\n";
  222.  
  223.             sql += comp_sql;
  224.            
  225.             sql += art_prop_sql;
  226.            
  227.             sql += pack_prop_sql;        
  228.            
  229.             sw.WriteLine(sql);
  230.             sw.Close();
  231.  
  232.         Console.ReadKey(false);
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement