Exemplo n.º 1
0
 private static IEnumerable<StoredProcedure> GetParamedDbos(string type ,XElement xml, DacpacExtractor extractor)
 {
     string xpath = "/DataSchemaModel/Model/Element[@Type='{0}']".FormatInvariantCulture(type);
     var elements = xml.XPathSelectElements(xpath);
     foreach (var element in elements)
     {
         var ret = new StoredProcedure
         {
             Name = element.GetAttributeString("Name"),
             Params =
                 element.XPathSelectElements(
                     "Relationship[@Name='Parameters']/Entry/Element[@Type='SqlSubroutineParameter']")
                     .Select(e => new EntityProperty() { Name = e.GetAttributeString("Name"), Type = e.XPathSelectElement("Relationship[@Name='Type']/Entry/Element[@Type='SqlTypeSpecifier' or @Type='SqlXmlTypeSpecifier']/Relationship[@Name='Type']/Entry/References").GetAttributeString("Name") })
                     .ToList()
         };
         yield return ret;
     }
 }
Exemplo n.º 2
0
 public string ParseDacpac(string dacpacFileName)
 {
     try
     {
         if (!Enabled)
         {
             return(null);
         }
         if (!File.Exists(dacpacFileName) && !ErrorIfDacpacNotFound)
         {
             return(null);
         }
         using (var stream = File.OpenRead(dacpacFileName))
         {
             using (var zip = new ZipArchive(stream, ZipArchiveMode.Read))
             {
                 var modelEntry = zip.GetEntry("model.xml");
                 using (var entryStream = modelEntry.Open())
                 {
                     var xml = XDocument.Load(entryStream);
                     if (xml.Root == null)
                     {
                         return(null);
                     }
                     foreach (var x in xml.Root.DescendantsAndSelf())
                     {
                         x.Name = x.Name.LocalName;
                         x.ReplaceAttributes((from xattrib in x.Attributes().Where(xa => !xa.IsNamespaceDeclaration) select new XAttribute(xattrib.Name.LocalName, xattrib.Value)));
                     }
                     var entities = Entity.GetEntities(xml.Root, this).ToList();
                     var methods  = Method.GetMethods(xml.Root, this, entities).ToList();
                     var config   = TemplatorConfig.DefaultInstance;
                     var parser   = new TemplatorParser(config);
                     foreach (var templatorKeyword in SqlToCsharpHelper.GetCustomizedTemplatorKeyword(this))
                     {
                         config.Keywords.AddOrOverwrite(templatorKeyword.Name, templatorKeyword);
                     }
                     if (!Directory.Exists(ModelOutPath))
                     {
                         Directory.CreateDirectory(ModelOutPath);
                     }
                     if (!Directory.Exists(DalOutPath))
                     {
                         Directory.CreateDirectory(DalOutPath);
                     }
                     var template = GetTemplate(Entity.Template);
                     foreach (var entity in entities)
                     {
                         var fileName = FileNameExtensionPrefix.IsNullOrWhiteSpace() ? entity.Name + ".cs" : "{0}.{1}.cs".FormatInvariantCulture(entity.Name, FileNameExtensionPrefix);
                         var json     = JsonConvert.SerializeObject(entity);
                         var input    = json.ParseJsonDict();
                         input.Add("Namespace", ModelNamespace);
                         input.Add("Usings", Usings.EmptyIfNull().Select(u => new Dictionary <string, object>()
                         {
                             { "Using", u }
                         }).Cast <object>().ToArray());
                         var file = parser.ParseText(template, input);
                         parser.StartOver();
                         using (var sw = new StreamWriter(Path.Combine(ModelOutPath, fileName)))
                         {
                             sw.Write(file);
                         }
                     }
                     template = GetTemplate(Method.Template);
                     var iTemplate = GetTemplate(Method.InterfaceTemplate);
                     var name      = FileNameExtensionPrefix.IsNullOrWhiteSpace() ? DataAccessClassName + ".cs" : "{0}.{1}.cs".FormatInvariantCulture(DataAccessClassName, FileNameExtensionPrefix);
                     using (var sw = new StreamWriter(Path.Combine(DalOutPath, name)))
                     {
                         using (var isw = new StreamWriter(Path.Combine(DalOutPath, "I" + name)))
                         {
                             var json = JsonConvert.SerializeObject(new
                             {
                                 NonQueryMethods      = methods.Where(m => m.IdentityColumns.IsNullOrEmpty() && m.Unique),
                                 ScalarMethods        = methods.Where(m => !m.IdentityColumns.IsNullOrEmpty()),
                                 UniqueMethods        = methods.Where(m => m.Unique),
                                 MultipleMethods      = methods.Where(m => !m.Unique),
                                 InlineTableFunctions = StoredProcedure.GetInlineTableValuedFunctions(xml.Root, this),
                                 TableFunctions       = StoredProcedure.GetlMultiStatementTableValuedFunctions(xml.Root, this),
                                 ScalarFunctions      = StoredProcedure.GetScalarFunctions(xml.Root, this),
                                 Sps = StoredProcedure.GetStoredProcedures(xml.Root, this),
                             });
                             var input = json.ParseJsonDict();
                             input.Add("DataAccessClassName", DataAccessClassName);
                             input.Add("DalNamespace", DalNamespace);
                             input.Add("HelperVersion", "1.0.0.4");
                             input.Add("Namespace", ModelNamespace);
                             input.Add("ClassAccess", ClassAccess ?? "public");
                             input.Add("Usings", Usings.EmptyIfNull().Select(u => new Dictionary <string, object>()
                             {
                                 { "Using", u }
                             }).Cast <object>().ToArray());
                             var file = parser.ParseText(template, input);
                             parser.StartOver();
                             sw.Write(file);
                             file = parser.ParseText(iTemplate, input);
                             parser.StartOver();
                             isw.Write(file);
                         }
                     }
                 }
             }
         }
     }
     catch (Exception)
     {
         return("Unexpected Error while generating code. " + ConfigErrorMessage);
     }
     return(null);
 }