public static IEnumerable<Entity> GetEntities(XElement xml, DacpacExtractor extractor) { const string xpath = "/DataSchemaModel/Model/Element[@Type='SqlTable']"; var elements = xml.XPathSelectElements(xpath); foreach (var e in elements) { var entity = new Entity {TableName = e.GetAttributeString("Name"), IncludeForeignKey = extractor.IncludeForeignKey}; foreach (var pe in e.XPathSelectElements("Relationship[@Name='Columns']/Entry/Element[@Type='SqlSimpleColumn']")) { var nullableNode = pe.XPathSelectElement("Property[@Name='IsNullable']"); var identityNode = pe.XPathSelectElement("Property[@Name='IsIdentity']"); var p = new EntityProperty() { Nullable = nullableNode == null || nullableNode.GetAttributeBool("Value"), Identity = identityNode != null && identityNode.GetAttributeBool("Value"), Name = pe.GetAttributeString("Name"), Type = pe.XPathSelectElement("Relationship[@Name='TypeSpecifier']/Entry/Element[@Type='SqlTypeSpecifier' or @Type='SqlXmlTypeSpecifier']/Relationship[@Name='Type']/Entry/References").GetAttributeString("Name"), }; var over = extractor.ColumnOverrides.EmptyIfNull().FirstOrDefault(o => o.Name == p.Name); if (over != null) { p.Type = over.Type ?? p.Type; p.Nullable = over.Nullable ?? p.Nullable; p.Attributes = over.Attributes; over.Name = null; } if (p.Type.ToLower() != "[timestamp]" && p.Type.ToLower() != "[rowversion]" && !extractor.ObjectsToIgnore.EmptyIfNull().Contains(p.Name)) { entity.Properties.Add(p); } } foreach (var c in extractor.ColumnOverrides.EmptyIfNull()) { if (c.Name != null && c.Type != null && c.Name.StartsWith(entity.TableName)) { entity.Properties.Add(c); } } var an = extractor.EntityOverrides.EmptyIfNull().FirstOrDefault(ae => ae.TableName == entity.TableName); if (an != null) { entity.Annotations = an.Annotations; entity.Name = an.Name; entity.IncludeForeignKey = an.IncludeForeignKey; } if (!extractor.ObjectsToIgnore.EmptyIfNull().Contains(entity.TableName)) { yield return entity; } } }
public void TestExtractDacpack() { var p = new DacpacExtractor(); using (var sw = new StreamWriter("dacpac.xml")) { p.PluralMappings = new[] { new DacpacExtractor.PluralMapping(){ EntityName = "TableAnother", PluralForm = "TableAnotheres" } ,new DacpacExtractor.PluralMapping(){ EntityName = "Table2", PluralForm = "Table2es" } }; p.ModelNamespace = "hehe"; var e = p.ToXElement(); sw.Write(e.ToString()); } }
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; } }
static void Main(string[] args) { var config = DacpacExtractor.ConfigFileName; if (args.Length == 2) { config = args[1]; } if (args.Length >= 1 && File.Exists(args[0])) { if (File.Exists(config)) { try { var xml = XDocument.Load(config); string error; var parser = DacpacExtractor.LoadFromXml(xml.Root, out error); if (parser == null) { Console.WriteLine(error); return; } parser.ParseDacpac(args[0]); } catch (Exception) { Console.WriteLine("Unexpected Error:"); throw; } } else { Console.WriteLine("Config file not exists"); ShowUsage(); } } else { ShowUsage(); } }
public static IEnumerable<StoredProcedure> GetStoredProcedures(XElement xml, DacpacExtractor extractor) { return GetParamedDbos("SqlProcedure", xml, extractor); }
public static IEnumerable<StoredProcedure> GetScalarFunctions(XElement xml, DacpacExtractor extractor) { return GetParamedDbos("SqlScalarFunction", xml, extractor); }
public static IEnumerable<StoredProcedure> GetlMultiStatementTableValuedFunctions(XElement xml, DacpacExtractor extractor) { return GetParamedDbos("SqlMultiStatementTableValuedFunction", xml, extractor); }
static void MapAsyncSettings(Method method, DacpacExtractor extractor) { var map = extractor.AsyncMappings.EmptyIfNull().FirstOrDefault(a => a.IndexName == method.KeyName); if (map != null) { method.ReadAsAsync = map.GetAsync; method.WriteAsAsync = map.SaveAsync; method.DeleteAsAsync = map.DeleteAsync; } }
public static IEnumerable<Method> GetMethods(XElement xml, DacpacExtractor extractor, IList<Entity> entities) { if (extractor.IncludeForeignKey) { var elements = xml.XPathSelectElements("/DataSchemaModel/Model/Element[@Type='SqlForeignKeyConstraint']"); foreach (var element in elements) { var foreignTable = element.XPathSelectElement("Relationship[@Name='ForeignTable']/Entry/References") .GetAttributeString("Name"); var ps = element.XPathSelectElements("Relationship[@Name='Columns']/Entry/References") .Select(e => new EntityProperty() { Name = e.GetAttributeString("Name") }).ToList(); for (int index = 0; index < ps.Count; index++) { var property = ps[index]; property.ForeignName = element.XPathSelectElement("Relationship[@Name='ForeignColumns']/Entry[" + (index + 1) + "]/References") .GetAttributeString("Name"); } var ret = new Method { TableName = element.XPathSelectElement("Relationship[@Name='DefiningTable']/Entry/References") .GetAttributeString("Name"), Params = ps, KeyName = element.GetAttributeString("Name"), ReadAsAsync = extractor.GetAsync, DeleteAsAsync = extractor.DeleteAsync, WriteAsAsync = extractor.SaveAsync }; if (!extractor.ObjectsToIgnore.EmptyIfNull().Contains(ret.TableName)) { SqlToCsharpHelper.GetType(entities, ret, false, foreignTable); MapAsyncSettings(ret, extractor); yield return ret; } } } if (extractor.IncludePrimaryKey) { var elements = xml.XPathSelectElements("/DataSchemaModel/Model/Element[@Type='SqlPrimaryKeyConstraint']"); foreach (var element in elements) { var ret = new Method { KeyName = element.GetAttributeString("Name"), TableName = element.XPathSelectElement("Relationship[@Name='DefiningTable']/Entry/References") .GetAttributeString("Name"), Unique = true, Params = element.XPathSelectElements( "Relationship[@Name='ColumnSpecifications']/Entry/Element[@Type='SqlIndexedColumnSpecification']/Relationship[@Name='Column']/Entry/References") .Select(e => new EntityProperty() { Name = e.GetAttributeString("Name") }) .ToList(), ReadAsAsync = extractor.GetAsync, DeleteAsAsync = extractor.DeleteAsync, WriteAsAsync = extractor.SaveAsync }; if (!extractor.ObjectsToIgnore.EmptyIfNull().Contains(ret.TableName)) { SqlToCsharpHelper.GetType(entities, ret, true, null); MapAsyncSettings(ret, extractor); yield return ret; } } } if (extractor.IncludeNonUniqueIndex || extractor.IncludeUniqueIndex) { var elements = xml.XPathSelectElements("/DataSchemaModel/Model/Element[@Type='SqlIndex']"); foreach (var element in elements) { var uniqueNode = element.XPathSelectElement("Property[@Name='IsUnique']"); var ret = new Method { TableName = element.XPathSelectElement("Relationship[@Name='IndexedObject']/Entry/References") .GetAttributeString("Name"), Unique = uniqueNode != null && uniqueNode.GetAttributeBool("Value"), Params = element.XPathSelectElements( "Relationship[@Name='ColumnSpecifications']/Entry/Element[@Type='SqlIndexedColumnSpecification']/Relationship[@Name='Column']/Entry/References") .Select(e => new EntityProperty() { Name = e.GetAttributeString("Name") }) .ToList(), KeyName = element.GetAttributeString("Name"), ReadAsAsync = extractor.GetAsync, DeleteAsAsync = extractor.DeleteAsync, WriteAsAsync = extractor.SaveAsync }; if (!extractor.ObjectsToIgnore.EmptyIfNull().Contains(ret.TableName)) { if ((extractor.IncludeNonUniqueIndex && !ret.Unique) || (extractor.IncludeUniqueIndex && ret.Unique)) { SqlToCsharpHelper.GetType(entities, ret, false, null); MapAsyncSettings(ret, extractor); yield return ret; } } } } }
public override bool Execute() { var m = new BuildMessageEventArgs("DbHelper code generating", "", "CpsDbHelperBuildTask", MessageImportance.Normal); BuildEngine.LogMessageEvent(m); var proj = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(ProjectPath).FirstOrDefault() ?? new Project(ProjectPath); var settingsFile = proj.GetItemsByEvaluatedInclude(DacpacExtractor.ConfigFileName).FirstOrDefault(); if (settingsFile != null) { var xml = XDocument.Load(settingsFile.UnevaluatedInclude); string error; var parser = DacpacExtractor.LoadFromXml(xml.Root, out error); if (parser == null) { var me = new BuildErrorEventArgs("DbHelper code generator", "", ProjectPath, 0, 0, 0, 0, error, DacpacExtractor.ConfigFileName, "CpsDbHelperCodeGenerator", DateTime.Now, MessageImportance.High); BuildEngine.LogErrorEvent(me); return(false); } if (Configuration != null && parser.EnabledInConfigurations != null && !parser.EnabledInConfigurations.Contains(Configuration)) { return(true); } if (!File.Exists(parser.DbProjectPath)) { var me = new BuildErrorEventArgs("DbHelper code generator", "", ProjectPath, 0, 0, 0, 0, "please config 'DbProjectPath' in '{0}'".FormatInvariantCulture(DacpacExtractor.ConfigFileName), DacpacExtractor.ConfigFileName, "CpsDbHelperCodeGenerator", DateTime.Now, MessageImportance.High); BuildEngine.LogErrorEvent(me); return(false); } var dbProj = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(Path.GetFullPath(parser.DbProjectPath)).FirstOrDefault() ?? new Project(Path.GetFullPath(parser.DbProjectPath)); if (dbProj.IsDirty) { dbProj.Build(); } var fileName = dbProj.GetPropertyValue("SqlTargetFile"); var dir = dbProj.GetPropertyValue("TargetDir"); var dacpac = Path.Combine(dir, fileName); if (File.Exists(dacpac)) { var errorMessage = parser.ParseDacpac(dacpac); if (errorMessage != null) { var me = new BuildErrorEventArgs("DbHelper code generator", "", ProjectPath, 0, 0, 0, 0, errorMessage, DacpacExtractor.ConfigFileName, "CpsDbHelperCodeGenerator", DateTime.Now, MessageImportance.High); BuildEngine.LogErrorEvent(me); return(false); } } else if (parser.ErrorIfDacpacNotFound) { var me = new BuildErrorEventArgs("DbHelper code generator", "", ProjectPath, 0, 0, 0, 0, "Cannot find dacpac file", DacpacExtractor.ConfigFileName, "CpsDbHelperCodeGenerator", DateTime.Now, MessageImportance.High); BuildEngine.LogErrorEvent(me); return(false); } else { var me = new BuildMessageEventArgs("Cannot find dacpac file, skip code generating", "", "CpsDbHelperBuildTask", MessageImportance.Normal); BuildEngine.LogMessageEvent(me); return(true); } } else { var me = new BuildErrorEventArgs("DbHelper code generator", "", ProjectPath, 0, 0, 0, 0, "Unable to find '{0}'".FormatInvariantCulture(), "CodeGeneratorSettings.xml", DacpacExtractor.ConfigFileName, DateTime.Now, MessageImportance.High); BuildEngine.LogErrorEvent(me); return(false); } return(true); }