public virtual void CacheInfoFromModel(NamedSmoObject smoObject) { SmoObject = smoObject; NodeValue = smoObject.Name; ScriptSchemaObjectBase schemaBasecObject = smoObject as ScriptSchemaObjectBase; ObjectMetadata = new Metadata.Contracts.ObjectMetadata(); ObjectMetadata.Name = smoObject.Name; try { if (smoObject.Urn != null) { ObjectMetadata.MetadataTypeName = smoObject.Urn.Type; } } catch { //Ignore the exception, sometimes the urn returns exception and I' not sure why } if (schemaBasecObject != null) { ObjectMetadata.Schema = schemaBasecObject.Schema; if (!string.IsNullOrEmpty(ObjectMetadata.Schema)) { NodeValue = $"{ObjectMetadata.Schema}.{smoObject.Name}"; } } }
private IEnumerable <string> ForEachStatement(ScriptSchemaObjectBase o) { foreach (var statement in this.scripter.Script(new Urn[] { o.Urn })) { yield return(statement); } }
private bool GenerateDbObject(Scripter scripter, ScriptSchemaObjectBase e, DirectoryInfo targetDir, string locationPart) { var name = e.Name; var schema = e.Schema; var urn = new[] { e.Urn }; var table = e as Table; var view = e as View; if ((table != null && table.IsSystemObject) || (view != null && view.IsSystemObject) ) { return(false); } var sc = scripter.Script(urn); var sb = new StringBuilder(); foreach (var st in sc) { sb.Append(" "); sb.Append(st); } var value = sb.ToString().Trim(new[] { '\r', '\n' }); var location = $"{targetDir.FullName}\\{locationPart}\\{schema}.{name}.sql"; System.IO.File.WriteAllText(location, value); return(true); }
public DatabaseSearchResult(ScriptSchemaObjectBase result, SqlConnectionInfo connection, Database db) { DataBase = db; this.result = result; this.connection = connection; if (result is Table) { ObjectType = ObjType.Table; } else if (result is StoredProcedure) { ObjectType = ObjType.StoredProc; } else if (result is View) { ObjectType = ObjType.View; } else if (result is UserDefinedFunction) { ObjectType = ObjType.Func; } else { throw new NotImplementedException("Unknown object type " + result.GetType().Name); } SearchName = SchemaAndName.ToLower(); }
public ScriptableObject(IScriptingSource scriptingSource, ScriptSchemaObjectBase schemaObject) { _scriptingSource = scriptingSource; _schemaObject = schemaObject; _table = _schemaObject as Table; _view = _schemaObject as View; _sproc = _schemaObject as StoredProcedure; _udf = _schemaObject as UserDefinedFunction; }
public ScriptableObject(IScriptingSource scriptingSource, ScriptSchemaObjectBase schemaObject) { _scriptingSource = scriptingSource; _schemaObject = schemaObject; _table = _schemaObject as Table; _view = _schemaObject as View; _sproc = _schemaObject as StoredProcedure; _udf = _schemaObject as UserDefinedFunction; }
private XmlNode ExtractObject(ScriptSchemaObjectBase obj) { bool ext = true; ext &= Constants.ObjectTypeMap.ContainsKey(obj.GetType()); // Avoid metadata tables ext &= String.Compare(obj.Schema, Constants.SchemaMeta, true) != 0; // Avoid system views and tables ext &= String.Compare(obj.Schema, Constants.SchemaSys, true) != 0; ext &= String.Compare(obj.Schema, Constants.SchemaInfoSch, true) != 0; if (ext) { XmlNode node = xml.CreateElement(Constants.ObjectTypeMap[obj.GetType()].ToString().ToLower()); node.Attributes.Append(xml.CreateAttribute(Constants.AttributeName)).Value = String.Format("[{0}].[{1}]", obj.Schema, obj.Name); foreach (ExtendedProperty p in ((IExtendedProperties)obj).ExtendedProperties) { string pname = p.Name.Replace(Constants.SchemaMeta.ToLowerInvariant() + ".", String.Empty); if (Constants.ObjectElements.Contains(pname)) { node.AppendChild(xml.CreateElement(pname)).InnerText = p.Value.ToString(); } else { // Assume all the rest is attribute node.Attributes.Append(xml.CreateAttribute(pname)).Value = p.Value.ToString(); } } ExtractColumnsAndParameters(obj, node); return(node); } else { return(null); } }
private void GenerateScript(ScriptSchemaObjectBase scriptObject, ScriptingOptions scriptOptions, DirectoryInfo path) { var tableScript = ((IScriptable)scriptObject).Script(scriptOptions); var sqlBuilder = new StringBuilder(); foreach (string str in tableScript) { var script = FixScript(str); if (!string.IsNullOrEmpty(script)) { sqlBuilder.AppendLine(script); sqlBuilder.AppendLine("---"); } } var filename = $"{scriptObject.Schema}.{scriptObject.Name}.sql"; var sqlPath = Path.Combine(path.FullName, filename); Log.Information("Generating script for: {schema}.{table} -> {path}", scriptObject.Schema, scriptObject.Name, sqlPath); File.WriteAllText(sqlPath, sqlBuilder.ToString()); }
private void ExtractColumnsAndParameters(ScriptSchemaObjectBase obj, XmlNode node) { List<NamedSmoObject> colpar = new List<NamedSmoObject>(); if (obj is Table) { colpar.AddRange(((Table)obj).Columns.Cast<NamedSmoObject>()); } else if (obj is View) { colpar.AddRange(((View)obj).Columns.Cast<NamedSmoObject>()); } else if (obj is StoredProcedure) { colpar.AddRange(((StoredProcedure)obj).Parameters.Cast<NamedSmoObject>()); } else if (obj is UserDefinedFunction) { colpar.AddRange(((UserDefinedFunction)obj).Parameters.Cast<NamedSmoObject>()); colpar.AddRange(((UserDefinedFunction)obj).Columns.Cast<NamedSmoObject>()); } foreach (NamedSmoObject cp in colpar) { ParameterType pt = Constants.ParameterTypeMap[cp.GetType()]; XmlElement pn = xml.CreateElement(pt.ToString().ToLowerInvariant()); pn.Attributes.Append(xml.CreateAttribute(Constants.AttributeName)).Value = String.Format("[{0}]", cp.Name); foreach (ExtendedProperty p in ((IExtendedProperties)obj).ExtendedProperties) { string pname = p.Name.Replace(Constants.SchemaMeta.ToLowerInvariant() + ".", String.Empty); pn.Attributes.Append(xml.CreateAttribute(pname)).Value = p.Value.ToString(); } node.AppendChild(pn); } }
private void ExtractColumnsAndParameters(ScriptSchemaObjectBase obj, XmlNode node) { List <NamedSmoObject> colpar = new List <NamedSmoObject>(); if (obj is Table) { colpar.AddRange(((Table)obj).Columns.Cast <NamedSmoObject>()); } else if (obj is View) { colpar.AddRange(((View)obj).Columns.Cast <NamedSmoObject>()); } else if (obj is StoredProcedure) { colpar.AddRange(((StoredProcedure)obj).Parameters.Cast <NamedSmoObject>()); } else if (obj is UserDefinedFunction) { colpar.AddRange(((UserDefinedFunction)obj).Parameters.Cast <NamedSmoObject>()); colpar.AddRange(((UserDefinedFunction)obj).Columns.Cast <NamedSmoObject>()); } foreach (NamedSmoObject cp in colpar) { ParameterType pt = Constants.ParameterTypeMap[cp.GetType()]; XmlElement pn = xml.CreateElement(pt.ToString().ToLowerInvariant()); pn.Attributes.Append(xml.CreateAttribute(Constants.AttributeName)).Value = String.Format("[{0}]", cp.Name); foreach (ExtendedProperty p in ((IExtendedProperties)obj).ExtendedProperties) { string pname = p.Name.Replace(Constants.SchemaMeta.ToLowerInvariant() + ".", String.Empty); pn.Attributes.Append(xml.CreateAttribute(pname)).Value = p.Value.ToString(); } node.AppendChild(pn); } }
public static string GetScriptFileName(ScriptSchemaObjectBase parentObject, NamedSmoObject subObject = null) { var scriptFileName = FixUpFileName(parentObject.Schema) + "." + FixUpFileName(parentObject.Name); if (subObject != null) { scriptFileName += "." + FixUpFileName(subObject.Name); } scriptFileName += ".sql"; return scriptFileName; }
private XmlNode ExtractObject(ScriptSchemaObjectBase obj) { bool ext = true; ext &= Constants.ObjectTypeMap.ContainsKey(obj.GetType()); // Avoid metadata tables ext &= String.Compare(obj.Schema, Constants.SchemaMeta, true) != 0; // Avoid system views and tables ext &= String.Compare(obj.Schema, Constants.SchemaSys, true) != 0; ext &= String.Compare(obj.Schema, Constants.SchemaInfoSch, true) != 0; if (ext) { XmlNode node = xml.CreateElement(Constants.ObjectTypeMap[obj.GetType()].ToString().ToLower()); node.Attributes.Append(xml.CreateAttribute(Constants.AttributeName)).Value = String.Format("[{0}].[{1}]", obj.Schema, obj.Name); foreach (ExtendedProperty p in ((IExtendedProperties)obj).ExtendedProperties) { string pname = p.Name.Replace(Constants.SchemaMeta.ToLowerInvariant() + ".", String.Empty); if (Constants.ObjectElements.Contains(pname)) { node.AppendChild(xml.CreateElement(pname)).InnerText = p.Value.ToString(); } else { // Assume all the rest is attribute node.Attributes.Append(xml.CreateAttribute(pname)).Value = p.Value.ToString(); } } ExtractColumnsAndParameters(obj, node); return node; } else { return null; } }
public static string ScriptFileName(this ScriptSchemaObjectBase scriptObject) { return(scriptObject.Schema.Replace('\\', '.') + '.' + scriptObject.Name); }
public void Add(Database d, ScriptSchemaObjectBase obj, SqlConnectionInfo connectionInfo) { var searchResult = new DatabaseSearchResult(obj, connectionInfo, d); dictionary[searchResult.SearchName] = searchResult; }