private void PrintTableReferenceRecurse(TableReference aTableReference) { if (aTableReference.GetType() == typeof(NamedTableReference)) { NamedTableReference aNamedTableReference = (NamedTableReference)aTableReference; Identifier aAliasIdentifier = aNamedTableReference.Alias; SchemaObjectName aSchemaObjectName = aNamedTableReference.SchemaObject; AddLogText(string.Format("Table Reference Server.Database.Schema.Base={0}.{1}.{2}.{3}", (aSchemaObjectName.ServerIdentifier != null) ? aSchemaObjectName.ServerIdentifier.Value : "", (aSchemaObjectName.DatabaseIdentifier != null) ? aSchemaObjectName.DatabaseIdentifier.Value : "", (aSchemaObjectName.SchemaIdentifier != null) ? aSchemaObjectName.SchemaIdentifier.Value : "", (aSchemaObjectName.BaseIdentifier != null) ? aSchemaObjectName.BaseIdentifier.Value : "") ); if (aAliasIdentifier != null) { AddLogText(string.Format("Table Reference Alias:{0}", aAliasIdentifier.Value)); } AddTableReference(aSchemaObjectName, aAliasIdentifier); } if (aTableReference.GetType() == typeof(QualifiedJoin)) { QualifiedJoin aQualifiedJoin = (QualifiedJoin)aTableReference; AddLogText(string.Format("Table Reference QualifiedJoinType ={0}", aQualifiedJoin.QualifiedJoinType.ToString())); PrintTableReferenceRecurse(aQualifiedJoin.FirstTableReference); PrintTableReferenceRecurse(aQualifiedJoin.SecondTableReference); } if (aTableReference.GetType() == typeof(JoinTableReference)) { JoinTableReference aJoinTableReference = (JoinTableReference)aTableReference; PrintTableReferenceRecurse(aJoinTableReference.FirstTableReference); PrintTableReferenceRecurse(aJoinTableReference.SecondTableReference); } }
public SchemaObjectName GenerateStoredProcedureName(string procedureName, Table table, QuoteType quoteType = QuoteType.NotQuoted) { SchemaObjectName objectName = new SchemaObjectName(); objectName.Identifiers.AddRange(ScriptFactory.Identifier(table.Schema, quoteType), ScriptFactory.Identifier(procedureName, quoteType)); return(objectName); }
/// <summary> /// Create a SqlName from a schemaObjectname. /// </summary> /// <param name="name">the name to convert</param> /// <returns>a SqlName or null</returns> public static SqlName From(SchemaObjectName name) { if (name is null) { return(null); } SqlName result = null; if (name.ServerIdentifier != null) { result = new SqlName(result, name.ServerIdentifier.Value, ObjectLevel.Server); } if ((name.DatabaseIdentifier != null) || (result != null)) { result = new SqlName(result, (name.DatabaseIdentifier?.Value) ?? string.Empty, ObjectLevel.Database); } if ((name.SchemaIdentifier != null) || (result != null)) { result = new SqlName(result, (name.SchemaIdentifier?.Value) ?? string.Empty, ObjectLevel.Schema); } if ((name.BaseIdentifier != null) || (result != null)) { result = new SqlName(result, (name.BaseIdentifier?.Value) ?? string.Empty, ObjectLevel.Object); } return(result); }
public ProcedureBuilder(string testSchema, string testName, SchemaObjectName procUnderTest) { _testProcedure.StatementList = new StatementList(); CreateTestProcedureDefinition(testSchema, testName); CreateExecForProcUnderTest(procUnderTest); }
public static string SchemaObjectName(SchemaObjectName sobj) { var sb = new StringBuilder(128); var empty = true; if (sobj.ServerIdentifier != null) { sb.Append(sobj.ServerIdentifier.Value); empty = false; } if (sobj.DatabaseIdentifier != null) { if (!empty) { sb.Append('.'); } sb.Append(sobj.DatabaseIdentifier.Value); empty = false; } if (sobj.BaseIdentifier == null) return sb.ToString(); if (!empty) { sb.Append('.'); } sb.Append(sobj.BaseIdentifier.Value); return sb.ToString(); }
public static SchemaObjectName ToSchemaObjectName(this ObjectIdentifier src) { var name = new SchemaObjectName(); var items = src.Parts.Count; if (items == 0) { throw new NameConversionException("Didn't find any name parts on ObjectIdentifier"); } if (items == 1) { name.Identifiers.Add(src.Parts[0].ToIdentifier()); return name; } if (items == 2) { name.Identifiers.Add(src.Parts[0].ToIdentifier()); name.Identifiers.Add(src.Parts[1].ToIdentifier()); return name; } name.Identifiers.Add(src.Parts[items - 2].ToIdentifier()); name.Identifiers.Add(src.Parts[items - 1].ToIdentifier()); return name; }
public static SchemaObjectName ToSchemaObjectName(this string src) { var name = new SchemaObjectName(); name.Identifiers.Add(src.ToIdentifier()); return(name); }
/// <summary> /// A SqlObject which contains information about the name and schema of the given object full name. /// </summary> /// <param name="fullName">Full name of object, including schema (if it exists).</param> /// <exception cref="InvalidOperationException">If the name can't be parsed</exception> public SqlObject(string fullName) { var parser = new TSql150Parser(false); var stringReader = new StringReader(fullName); SchemaObjectName tree = parser.ParseSchemaObjectName(stringReader, out IList <ParseError> errors); if (errors.Count > 0) { string errorMessages = "Encountered error(s) while parsing schema and object name:\n"; foreach (ParseError err in errors) { errorMessages += $"{err.Message}\n"; } throw new InvalidOperationException(errorMessages); } var visitor = new TSqlObjectFragmentVisitor(); tree.Accept(visitor); this.Schema = visitor.schemaName; this.QuotedSchema = this.Schema == SCHEMA_NAME_FUNCTION ? this.Schema : this.Schema.AsSingleQuotedString(); this.Name = visitor.objectName; this.QuotedName = this.Name.AsSingleQuotedString(); this.FullName = this.Schema == SCHEMA_NAME_FUNCTION ? this.Name : $"{this.Schema}.{this.Name}"; this.QuotedFullName = this.FullName.AsSingleQuotedString(); this.BracketQuotedFullName = this.Schema == SCHEMA_NAME_FUNCTION?this.Name.AsBracketQuotedString() : $"{this.Schema.AsBracketQuotedString()}.{this.Name.AsBracketQuotedString()}"; }
public TestClassEnumerator(string path) { var tSQLtName = new SchemaObjectName(); tSQLtName.Identifiers.Add(new Identifier() { Value = "tSQLt" }); tSQLtName.Identifiers.Add(new Identifier() { Value = "TestClass" }); var model = Model.Get(path); var extendedProperties = model.GetObjects <TSqlExtendedProperty>(DacQueryScopes.UserDefined).Where(s => s.Name.EqualsName(tSQLtName)); foreach (var prop in extendedProperties) { Console.WriteLine(prop.Name); } //_tables = dacTables.Select(t => new TableDescriptor(t)).ToList(); Model.Close(path); }
public static string SchemaObjectName(SchemaObjectName sobj) { var sb = new StringBuilder(128); var empty = true; if (sobj.ServerIdentifier != null) { sb.Append(sobj.ServerIdentifier.Value); empty = false; } if (sobj.DatabaseIdentifier != null) { if (!empty) { sb.Append('.'); } sb.Append(sobj.DatabaseIdentifier.Value); empty = false; } if (sobj.BaseIdentifier == null) { return(sb.ToString()); } if (!empty) { sb.Append('.'); } sb.Append(sobj.BaseIdentifier.Value); return(sb.ToString()); }
public void ConfigReplaceTests() { SchemaObjectName so = new SchemaObjectName("Customer.tbl_Addresses"); var AliasName1 = Configuration.ReplaceEx("{SCHEMANAME-L}", so); Assert.True(AliasName1.Equals("customer"), "Customer should be all lower case"); var AliasName2 = Configuration.ReplaceEx("{SCHEMANAME-U}", so); Assert.True(AliasName2.Equals("CUSTOMER"), "Customer should be all lower case"); var AliasName3 = Configuration.ReplaceEx("{OBJECTNAME-L}", so); Assert.True(AliasName3.Equals("tbl_addresses"), "tbl_Addresses should be all lower case"); var AliasName4 = Configuration.ReplaceEx("{OBJECTNAME-U}", so); Assert.True(AliasName4.Equals("TBL_ADDRESSES"), "tbl_Addresses should be all lower case"); var AliasName5 = Configuration.ReplaceEx("{SCHEMANAME-L}{OBJECTNAME-U}", so); Assert.True(AliasName5.Equals("customerTBL_ADDRESSES"), "customer should be all lower and tbl_Addresses should be all upper case"); var AliasName6 = Configuration.ReplaceEx("{SCHEMANAME-P}--{OBJECTNAME-P|X'Tbl_'}", so); Assert.True(AliasName6.Equals("Customer--Addresses"), "customer should be all lower and tbl_Addresses should be all upper case"); var AliasName7 = Configuration.ReplaceEx("{SCHEMANAME-P}{OBJECTNAME-P|R'Tbl_'=>'XXX'}", so); Assert.True(AliasName7.Equals("CustomerXXXAddresses"), "customer should be all lower and tbl_Addresses should be all upper case"); }
public SchemaObjectName GenerateSchemaAndIdentifierName(string schemaName, string identifierName, QuoteType quoteType = QuoteType.NotQuoted) { SchemaObjectName objectName = new SchemaObjectName(); objectName.Identifiers.AddRange(ScriptFactory.Identifier(schemaName, quoteType), ScriptFactory.Identifier(identifierName, quoteType)); return(objectName); }
/// <summary> /// The CreateExecuteSql method "wraps" the provided statement script in an "sp_executesql" statement /// Examples of statements that must be so wrapped include: stored procedures, views, and functions /// </summary> private static ExecuteStatement CreateExecuteSql(string statementScript) { // define a new Exec statement ExecuteStatement executeSp = new ExecuteStatement(); ExecutableProcedureReference spExecute = new ExecutableProcedureReference(); executeSp.ExecuteSpecification = new ExecuteSpecification { ExecutableEntity = spExecute }; // define the name of the procedure that you want to execute, in this case sp_executesql SchemaObjectName procName = new SchemaObjectName(); procName.Identifiers.Add(CreateIdentifier("sp_executesql", QuoteType.NotQuoted)); ProcedureReference procRef = new ProcedureReference { Name = procName }; spExecute.ProcedureReference = new ProcedureReferenceName { ProcedureReference = procRef }; // add the script parameter, constructed from the provided statement script ExecuteParameter scriptParam = new ExecuteParameter(); spExecute.Parameters.Add(scriptParam); scriptParam.ParameterValue = new StringLiteral { Value = statementScript }; scriptParam.Variable = new VariableReference { Name = "@stmt" }; return(executeSp); }
public bool IsIgnoredEntity(string entityNameToCheck) { var schemaObjectName = new SchemaObjectName(entityNameToCheck); var ignoreEntity = false; var configEntityFound = Entities.Find(e => e.Name == entityNameToCheck); if (configEntityFound == null) { configEntityFound = Entities.Find(e => e.Name == schemaObjectName.AsFullName()); } if (configEntityFound != null) { ignoreEntity = configEntityFound.Ignore; } if (!ignoreEntity) { foreach (var entity in this.Entities) { if (entity.Name.Contains(@"*")) //contains wildcard? { var isMatched = Regex.IsMatch(schemaObjectName.AsFullName(), "^" + Regex.Escape(entity.Name).Replace("\\?", ".").Replace("\\*", ".*") + "$"); if (isMatched) { ignoreEntity = entity.Ignore; } if (ignoreEntity) { break; } } } } return(ignoreEntity); }
/// <summary> /// A Replacement that is geared to specific templates and some simple string maniplation capabilities /// /// You can use the following variables here {SCHEMANAME}, {OBJECTNAME} with replace patterns after the instructions, for example: /// Lets say the Schema name is "CUSTOMER" and the Object name is "tbl_Address" /// You have the following one letter codes to modify the string after the name seperated by "|" /// U=Upper Case, L = Lower Case, P=Proper Case, X'<String to remove>'= Clear String, R'Old string'=>'New String' /// After Filtering, the following patterns will yield that following names: /// "{SCHEMANAME}{OBJECTNAME}" = "CUSTOMERtbl_Address" /// "{SCHEMANAME}{OBJECTNAME-U}" = "CUSTOMERTBL_ADDRESS" /// "{SCHEMANAME-L}{OBJECTNAME-L|X'tbl_'}" = "customeraddress" /// "{SCHEMANAME-P}{OBJECTNAME-P|X'tbl_'}" ="CustomerAddress" /// </summary> /// <param name="TemplatePattern">The template pattern which can be {##-U|L|P|X''|R''=>''} where ## can be SCHEMANAME or OBJECTNAME</param> /// <param name="schemaObjectName">Name of the SchemaObjectName that will rename the string.</param> /// <returns>The String replace with formatted</returns> public static string ReplaceEx(string TemplatePattern, SchemaObjectName schemaObjectName) { var returnString = TemplatePattern; var TemplatePatternArr = TemplatePattern.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < TemplatePatternArr.Count(); i++) { TemplatePatternArr[i] = TemplatePatternArr[i] + "}"; } foreach (var _template in TemplatePatternArr) { var template = _template; if (!template.StartsWith("{")) //Looks like there is text between the next template token, lets just grab the token { template = "{" + template.Pluck("{", "}") + "}"; } var strResolved = ""; if (template.Contains(Configuration.SCHEMA_NAME.Substring(0, Configuration.SCHEMA_NAME.Length - 1))) { strResolved = ReplaceEx(template, schemaObjectName.SchemaName); returnString = returnString.Replace(template, strResolved); } else if (template.Contains(Configuration.OBJECT_NAME.Substring(0, Configuration.OBJECT_NAME.Length - 1))) { strResolved = ReplaceEx(template, schemaObjectName.TableName); returnString = returnString.Replace(template, strResolved); } } return(returnString); }
public static SchemaObjectName FullTableName(Identifier databaseName, Identifier schemaName, Identifier tableName) { var fragment = new SchemaObjectName(); fragment.Identifiers.AddRange(databaseName, schemaName, tableName); return(fragment); }
private InsertStatement CreateStatement(string tableName, string identifier = "INSERTED") { var statement = new InsertStatement() { InsertSpecification = new InsertSpecification() }; var schemaObjectName = new SchemaObjectName(); schemaObjectName.Identifiers.Add(new Identifier() { Value = tableName }); statement.InsertSpecification.Target = new NamedTableReference() { SchemaObject = schemaObjectName }; statement.InsertSpecification.OutputClause = new OutputClause(); statement.InsertSpecification.OutputClause.SelectColumns.Add(new SelectStarExpression() { Qualifier = new MultiPartIdentifier() }); ((SelectStarExpression)statement.InsertSpecification.OutputClause.SelectColumns.Single()).Qualifier.Identifiers.Add(new Identifier() { Value = identifier }); return(statement); }
private void PrintTableReferenceRecurse(TableReference aTableReference) { if (aTableReference.GetType() == typeof(NamedTableReference)) { NamedTableReference aNamedTableReference = (NamedTableReference)aTableReference; Identifier aAliasIdentifier = aNamedTableReference.Alias; SchemaObjectName aSchemaObjectName = aNamedTableReference.SchemaObject; AddLogText(string.Format("Table Reference Schema.Base={0}.{1}", (aSchemaObjectName.SchemaIdentifier != null) ? aSchemaObjectName.SchemaIdentifier.Value : "", (aSchemaObjectName.BaseIdentifier != null) ? aSchemaObjectName.BaseIdentifier.Value : "") ); //foreach (Identifier aSchemaObjectNameIdentifier in aSchemaObjectName.Identifiers) //{ // AddText(string.Format("Table Reference Identifier={0}", aSchemaObjectNameIdentifier.Value)); //} if (aAliasIdentifier != null) { AddLogText(string.Format("Table Reference Alias:{0}", aAliasIdentifier.Value)); } aColumnInfoList.AddTableReference(aSchemaObjectName, aAliasIdentifier); } if (aTableReference.GetType() == typeof(QualifiedJoin)) { QualifiedJoin aQualifiedJoin = (QualifiedJoin)aTableReference; AddLogText(string.Format("Table Reference QualifiedJoinType ={0}", aQualifiedJoin.QualifiedJoinType)); PrintTableReferenceRecurse(aQualifiedJoin.FirstTableReference); PrintTableReferenceRecurse(aQualifiedJoin.SecondTableReference); } if (aTableReference.GetType() == typeof(JoinTableReference)) { JoinTableReference aJoinTableReference = (JoinTableReference)aTableReference; PrintTableReferenceRecurse(aJoinTableReference.FirstTableReference); PrintTableReferenceRecurse(aJoinTableReference.SecondTableReference); } }
public static bool EqualsName(this ObjectIdentifier source, SchemaObjectName target) { if (target.SchemaIdentifier == null) return Quote.Name(source.GetName()) == Quote.Name(target.BaseIdentifier.Value); return Quote.Name(source.GetSchema()) == Quote.Name(target.SchemaIdentifier.Value) && Quote.Name(source.GetName()) == Quote.Name(target.BaseIdentifier.Value); }
private void TestViewReference(SchemaObjectName ObjectName) { if (ObjectName.SchemaIdentifier == null) { _smells.SendFeedBack(24, ObjectName); } }
/// <summary> /// /// </summary> /// <param name="entityNameToCheck">Needs to be the full object name for the entity</param> /// <param name="objectNametoCheck"></param> /// <returns></returns> public bool IsObjectNameFiltered(string entityNameToCheck, string objectNametoCheck) { var schemaObjectName = new SchemaObjectName(entityNameToCheck); var filterObject = false; var configEntityFound = Entities.Find(e => e.Name == entityNameToCheck); if (configEntityFound == null) { configEntityFound = Entities.Find(e => e.Name == schemaObjectName.AsFullName()); } if (configEntityFound != null) { filterObject = configEntityFound.ObjectFilters.IsInWildcardList(objectNametoCheck); } if (!filterObject) { foreach (var entity in this.Entities) { if (entity.Name.Contains(@"*")) //contains wildcard? { var isMatched = Regex.IsMatch(schemaObjectName.AsFullName(), "^" + Regex.Escape(entity.Name).Replace("\\?", ".").Replace("\\*", ".*") + "$"); if (isMatched) { filterObject = entity.ObjectFilters.IsInWildcardList(objectNametoCheck); } if (filterObject) { break; } } } } return(filterObject); }
private TSqlStatement GetTableScript(Entity entity, string namespaceName) { var tableName = new SchemaObjectName(); tableName.Identifiers.Add(new Identifier { Value = namespaceName, QuoteType = QuoteType.SquareBracket }); tableName.Identifiers.Add(new Identifier { Value = entity.Name, QuoteType = QuoteType.SquareBracket }); var definition = new TableDefinition(); foreach (var prop in entity.Properties ?? Enumerable.Empty <EntityProperty>()) { definition.ColumnDefinitions.Add(GetColumn(prop)); } return(new CreateTableStatement() { SchemaObjectName = tableName, Definition = definition }); }
public SchemaObjectName GenerateSchemaObjectName(Column column, QuoteType quoteType = QuoteType.NotQuoted) { var retVal = new SchemaObjectName(); retVal.Identifiers.Add(ScriptFactory.Identifier(column.Parent.Name, quoteType)); retVal.Identifiers.Add(ScriptFactory.Identifier(column.Name, quoteType)); return(retVal); }
public static CreateTableStatement CreateTable(SchemaObjectName tableName, TableDefinition definition) { var fragment = new CreateTableStatement(); fragment.SchemaObjectName = tableName; fragment.Definition = definition; return(fragment); }
public static SchemaObjectName ToSchemaObjectName(this ObjectIdentifier source) { var target = new SchemaObjectName(); target.Identifiers.Add(source.GetSchema().ToScriptDomIdentifier().Quote()); target.Identifiers.Add(source.GetName().ToScriptDomIdentifier().Quote()); return target; }
public static ObjectIdentifier GetObjectIdentifier(this SchemaObjectName name, string assumedSchema = "dbo") { if (name.Identifiers.Count == 1 && !string.IsNullOrWhiteSpace(assumedSchema)) { return(new ObjectIdentifier(new[] { assumedSchema, name.Identifiers.First().Value })); } return(new ObjectIdentifier(name.Identifiers.Select(x => x.Value))); }
/// <summary> /// The CreateCompletedBatchesName method creates the name that will be inserted /// into the temporary table for a batch. /// </summary> private static SchemaObjectName CreateCompletedBatchesName() { SchemaObjectName name = new SchemaObjectName(); name.Identifiers.Add(CreateIdentifier("tempdb", QuoteType.SquareBracket)); name.Identifiers.Add(CreateIdentifier("dbo", QuoteType.SquareBracket)); name.Identifiers.Add(CreateIdentifier(CompletedBatchesVariable, QuoteType.SquareBracket)); return(name); }
public override void Visit(SchemaObjectName node) { String s = ""; if (node.DatabaseIdentifier != null) s += node.DatabaseIdentifier.Value + "."; if (node.BaseIdentifier != null) s += node.BaseIdentifier.Value; sw.WriteLine("<SchemaObject>" + s + "</SchemaObject>"); }
public static string ToUnquotedString(this SchemaObjectName source) { if (source.SchemaIdentifier != null) { return(string.Format("{0}.{1}", source.SchemaIdentifier.Value.UnQuote(), source.BaseIdentifier.Value.UnQuote())); } return(source.BaseIdentifier.Value.UnQuote()); }
public static SchemaObjectName ToSchemaObjectName(this ObjectIdentifier source) { var target = new SchemaObjectName(); target.Identifiers.Add(source.GetSchema().ToScriptDomIdentifier().Quote()); target.Identifiers.Add(source.GetName().ToScriptDomIdentifier().Quote()); return(target); }
public override void Visit(SchemaObjectName node) { var schemaIdentifier = node.SchemaIdentifier?.Value != null; if (schemaIdentifier && node.SchemaIdentifier.Value.Equals("INFORMATION_SCHEMA", StringComparison.InvariantCultureIgnoreCase)) { errorCallback(RULE_NAME, RULE_TEXT, node.StartLine, node.StartColumn); } }
public static bool EqualsName(this ObjectIdentifier source, SchemaObjectName target) { if (target.SchemaIdentifier == null) { return(Quote.Name(source.GetName()) == Quote.Name(target.BaseIdentifier.Value)); } return(Quote.Name(source.GetSchema()) == Quote.Name(target.SchemaIdentifier.Value) && Quote.Name(source.GetName()) == Quote.Name(target.BaseIdentifier.Value)); }
public static string GetName(this SchemaObjectName name) { var builder = new StringBuilder(); AddPart(builder, name.DatabaseIdentifier, false); AddPart(builder, name.SchemaIdentifier, false); AddPart(builder, name.BaseIdentifier, true); return(builder.ToString()); }
public override void ExplicitVisit(SchemaObjectName node) { for (int index = 0; index < node.Identifiers.Count; ++index) { node.Identifiers[index].Accept(this); if (index < node.Identifiers.Count - 1) { _buffer.Append("."); } } }
public static string FullyQualifiedName(this SchemaObjectName schemaObjectName) { string serverIdentifier = schemaObjectName.ServerIdentifier?.Value; string databaseIdentifier = schemaObjectName.DatabaseIdentifier?.Value; string schemaIdentifier = schemaObjectName.SchemaIdentifier?.Value; string baseIdentifier = schemaObjectName.BaseIdentifier?.Value; string result = $"{serverIdentifier.FormatPart()}{databaseIdentifier.FormatPart()}{schemaIdentifier.FormatPart()}{baseIdentifier.FormatPart(true)}"; return(result); }
internal static string GetTableName(SchemaObjectName tableName) { string schemaName; if (tableName.SchemaIdentifier != null) { schemaName = tableName.SchemaIdentifier.Value; return(schemaName + "." + tableName.BaseIdentifier.Value); } return("dbo." + tableName.BaseIdentifier.Value); }
private bool isCteName(SchemaObjectName ObjectName, WithCtesAndXmlNamespaces cte) { if (cte == null) return false; foreach (CommonTableExpression Expression in cte.CommonTableExpressions) { if (Expression.ExpressionName.Value == ObjectName.BaseIdentifier.Value) { return true; } } return false; }
public EqualityJoin(string qualifiedObjectNameLeft, string qualifiedObjectNameRight) { var parser = new TSql120Parser(initialQuotedIdentifiers: false); IList<ParseError> errors; using (var reader = new StringReader(qualifiedObjectNameLeft)) { _left = parser.ParseSchemaObjectName(reader, out errors); } using (var reader = new StringReader(qualifiedObjectNameRight)) { _right = parser.ParseSchemaObjectName(reader, out errors); } }
public void Generates_Declare_Variables_Statements() { var name = new SchemaObjectName(); name.Identifiers.Add("dbo".ToIdentifier()); name.Identifiers.Add("procedureName".ToIdentifier()); var builder = new ProcedureBuilder("[procedureName]", "[test blah bloo blah]", name); builder.AddParameter("@p1", SqlDataType.Date); builder.AddTable(name); var script = builder.GetScript(); const string expectedP1 = "DECLARE @p1 AS DATE"; Assert.True(script.IndexOf(expectedP1) > -1, "Did not find declare statement for p1: \"{0}\"", script); }
private TSqlStatement BuildDelete(InsertSpecification originalInsert) { var delete = new DeleteStatement(); delete.WithCtesAndXmlNamespaces = new WithCtesAndXmlNamespaces(); var cte = new CommonTableExpression(); cte.ExpressionName = new Identifier() { Value = "to_delete" }; cte.QueryExpression = BuildNewRowSource(originalInsert); delete.WithCtesAndXmlNamespaces.CommonTableExpressions.Add(cte); delete.DeleteSpecification = new DeleteSpecification(); var tableName = new SchemaObjectName(); tableName.Identifiers.Add( new Identifier() { Value = "to_delete" }); delete.DeleteSpecification.Target = new NamedTableReference() {SchemaObject = tableName }; var outputInto = delete.DeleteSpecification.OutputIntoClause = new OutputIntoClause(); var deletedTable = new MultiPartIdentifier(); deletedTable.Identifiers.Add(new Identifier() { Value = "deleted" }); outputInto.SelectColumns.Add(new SelectStarExpression() { Qualifier = deletedTable }); outputInto.IntoTable = originalInsert.Target; foreach (var col in originalInsert.Columns) { outputInto.IntoTableColumns.Add(col); } return delete; }
public void Generates_FakeTake_Statements() { var name = new SchemaObjectName(); name.Identifiers.Add("dbo".ToIdentifier()); name.Identifiers.Add("procedureName".ToIdentifier()); var builder = new ProcedureBuilder("[procedureName]", "[test blah bloo blah]", name); var tableName = new SchemaObjectName(); tableName.Identifiers.Add("someschema".ToIdentifier()); tableName.Identifiers.Add("table_thingy".ToIdentifier()); builder.AddTable(tableName); var script = builder.GetScript(); const string expected = "EXECUTE tSQLt.FakeTable 'table_thingy', 'someschema'"; Assert.True(script.IndexOf(expected) > -1, "Did not find faketable statement: \"{0}\"", script); }
private string GetPlanForFunc(SchemaObjectName procName, IList<ProcedureParameter> parameters) { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); var c = connection.CreateCommand(); c.CommandText = "SET SHOWPLAN_XML ON"; c.ExecuteNonQuery(); c.CommandText = "SET NOEXEC ON"; c.ExecuteNonQuery(); using (var cmd = connection.CreateCommand()) { cmd.CommandText = string.Format("select * from {0}.{1}({2})", procName.SchemaIdentifier.Value.Quote(), procName.BaseIdentifier.Value.Quote(),(GetArgsString(parameters))); return cmd.ExecuteScalar() as string; } } throw new NotImplementedException(); }
public TestClassEnumerator(string path) { var tSQLtName = new SchemaObjectName(); tSQLtName.Identifiers.Add(new Identifier() {Value = "tSQLt"}); tSQLtName.Identifiers.Add(new Identifier() { Value = "TestClass" }); var model = Model.Get(path); var extendedProperties = model.GetObjects<TSqlExtendedProperty>(DacQueryScopes.UserDefined).Where(s => s.Name.EqualsName(tSQLtName)); foreach (var prop in extendedProperties) { Console.WriteLine(prop.Name); } //_tables = dacTables.Select(t => new TableDescriptor(t)).ToList(); Model.Close(path); }
public void Test_Proc_Body_Is_Indented() { var name = new SchemaObjectName(); name.Identifiers.Add("dbo".ToIdentifier()); name.Identifiers.Add("procedureName".ToIdentifier()); var builder = new ProcedureBuilder("[procedureName]", "[test blah bloo blah]", name); builder.AddParameter("@p1", SqlDataType.Date); builder.AddParameter("@p2222222222222222222]", SqlDataType.VarChar); builder.AddTable(name); var script = builder.GetScript(); var lines = script.Replace("\r\n", "~").Split('~'); for (var i = 2; i < lines.Length; i++) { if (!(Char.IsWhiteSpace(lines[i][0]))) { Assert.Fail("Line does not start with whitespace: \"{0}\"", lines[i]); } } }
public void AddTable(SchemaObjectName name) { _tables.Add(name); }
public static bool EqualsName(this SchemaObjectName source, SchemaObjectName target) { return source.BaseIdentifier.Quote() == target.BaseIdentifier.Quote() && source.SchemaIdentifier.Quote() == target.SchemaIdentifier.Quote(); }
public static bool AreSame(ObjectIdentifier id, SchemaObjectName son) { switch (id.Parts.Count) { case 4: return id.Parts[0] == son.ServerIdentifier.Value && id.Parts[1] == son.DatabaseIdentifier.Value && id.Parts[2] == son.SchemaIdentifier.Value && id.Parts[3] == son.BaseIdentifier.Value; case 3: return id.Parts[0] == son.DatabaseIdentifier.Value && id.Parts[1] == son.SchemaIdentifier.Value && id.Parts[2] == son.BaseIdentifier.Value; case 2: return id.Parts[0] == son.SchemaIdentifier.Value && id.Parts[1] == son.BaseIdentifier.Value; case 1: return id.Parts[0] == son.BaseIdentifier.Value; } return false; }
public static SchemaObjectName ToSchemaObjectName(this string src) { var name = new SchemaObjectName(); name.Identifiers.Add(src.ToIdentifier()); return name; }
public override void Visit(SchemaObjectName node) { this.action(node); }
private void CreateExecForProcUnderTest(SchemaObjectName procUnderTest) { var calleeProcedure = new ProcedureReference(); calleeProcedure.Name = new SchemaObjectName(); _execProc.ExecuteSpecification = new ExecuteSpecification(); var entity = new ExecutableProcedureReference(); entity.ProcedureReference = new ProcedureReferenceName(); entity.ProcedureReference.ProcedureReference = calleeProcedure; entity.ProcedureReference.ProcedureReference.Name = procUnderTest; _execProc.ExecuteSpecification.ExecutableEntity = entity; }
private void CreateFakeTableDefinition(SchemaObjectName table) { var fakeTable = new ExecuteStatement(); fakeTable.ExecuteSpecification = new ExecuteSpecification(); var procedureReference = new ProcedureReference(); procedureReference.Name = new SchemaObjectName(); procedureReference.Name.Identifiers.Add("tSQLt".ToIdentifier()); procedureReference.Name.Identifiers.Add("FakeTable".ToIdentifier()); var entity = new ExecutableProcedureReference(); entity.ProcedureReference = new ProcedureReferenceName(); entity.ProcedureReference.ProcedureReference = procedureReference; entity.Parameters.Add( ParametersHelper.CreateStoredProcedureParameter(string.Format("{0}", table.BaseIdentifier.Value))); entity.Parameters.Add( ParametersHelper.CreateStoredProcedureParameter(string.Format("{0}", table.SchemaIdentifier.Value))); fakeTable.ExecuteSpecification.ExecutableEntity = entity; _testProcedure.StatementList.Statements.Add(fakeTable); }
public static bool EqualsObjectIdentifier(this ObjectIdentifier destination, SchemaObjectName source) { if (destination.Parts.Count == 1) { return destination.Parts[0].UnQuote() == source.BaseIdentifier.Value.UnQuote(); } if (destination.Parts.Count == 2) { return destination.Parts[0].UnQuote() == source.SchemaIdentifier.Value.UnQuote() && destination.Parts[1].UnQuote() == source.BaseIdentifier.Value.UnQuote(); } if (destination.Parts.Count == 3) { return destination.Parts[0].UnQuote() == source.DatabaseIdentifier.Value.UnQuote() && destination.Parts[1].UnQuote() == source.SchemaIdentifier.Value.UnQuote() && destination.Parts[2].UnQuote() == source.BaseIdentifier.Value.UnQuote(); } if (destination.Parts.Count == 4) { return destination.Parts[0].UnQuote() == source.ServerIdentifier.Value.UnQuote() && destination.Parts[1].UnQuote() == source.DatabaseIdentifier.Value.UnQuote() && destination.Parts[2].UnQuote() == source.SchemaIdentifier.Value.UnQuote() && destination.Parts[3].UnQuote() == source.BaseIdentifier.Value.UnQuote(); } return false; }
private string GetPlanForProc(SchemaObjectName procName) { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); var c = connection.CreateCommand(); c.CommandText = "SET SHOWPLAN_XML ON"; c.ExecuteNonQuery(); c.CommandText = "SET NOEXEC ON"; c.ExecuteNonQuery(); using (var cmd = connection.CreateCommand()) { cmd.CommandText = string.Format("exec {0}.{1}", procName.SchemaIdentifier.Value.Quote(), procName.BaseIdentifier.Value.Quote()); return cmd.ExecuteScalar() as string; } } return null; }
private WSchemaObjectName ParseSchemaObjectName(SchemaObjectName name) { if (name == null) return null; var wSchemaObjectName = new WSchemaObjectName { FirstTokenIndex = name.FirstTokenIndex, LastTokenIndex = name.LastTokenIndex, }; if (name.Identifiers != null) { wSchemaObjectName.Identifiers = new List<Identifier>(); foreach (var identifier in name.Identifiers) { if (GraphViewKeywords._keywords.Contains(identifier.Value)) { var token = _tokens[identifier.FirstTokenIndex]; throw new SyntaxErrorException(token.Line, identifier.Value, "System restricted Name cannot be used"); } wSchemaObjectName.Identifiers.Add(identifier); } } return wSchemaObjectName; }
public override void ExplicitVisit(SchemaObjectName fragment) { _fragments.Add(fragment); }