public void JumpTo(Control sender, string text, SqlAnalyzerResults aResults) { try { if (sender != null) { sender.Cursor = Cursors.WaitCursor; } _lastJump = text; _selector.JumpTo(text, aResults); } finally { if (sender != null) { sender.Cursor = Cursors.Default; } } }
public void InsertParsedObjects(SqlAnalyzerResults aResults) { if (aResults == null) { return; } DataRow row = null; //1- Insert temp tables if (aResults.TableAsTemp != null && aResults.TableAsTemp.Values != null) { foreach (SqlTable tbl in aResults.TableAsTemp.Values) { row = _tblObjects.NewRow(); row["Type"] = "Tbl"; row["Order"] = 0; row["Name"] = tbl.TableName; row["DisplayName"] = tbl.TableName; row["QualifiedDataType"] = "Temp Tbl"; row["IsOffline"] = true; row["Catalog"] = ""; row["Schema"] = ""; _tblObjects.Rows.Add(row); for (int i = 0; i < tbl.ColumnNames.Count; i++) { row = _tblObjects.NewRow(); row["Order"] = 5; row["Type"] = "Col"; row["Name"] = tbl.ColumnNames[i]; row["DisplayName"] = tbl.FullyQualifiedColumns[i]; row["QualifiedDataType"] = tbl.ColumnDataTypes[i]; row["ParentName"] = tbl.TableName; row["IsOffline"] = true; row["Catalog"] = ""; row["Schema"] = ""; _tblObjects.Rows.Add(row); } } } //2- Insert local variable tables if (aResults.TableAsVariable != null && aResults.TableAsVariable.Values != null) { foreach (SqlTable tbl in aResults.TableAsVariable.Values) { row = _tblObjects.NewRow(); row["Type"] = "Tbl"; row["Order"] = 0; row["Name"] = tbl.TableName; row["DisplayName"] = tbl.TableName; row["QualifiedDataType"] = "Local Var Tbl"; row["IsOffline"] = true; row["Catalog"] = ""; row["Schema"] = ""; _tblObjects.Rows.Add(row); for (int i = 0; i < tbl.ColumnNames.Count; i++) { row = _tblObjects.NewRow(); row["Type"] = "Col"; row["Order"] = 5; row["Name"] = tbl.ColumnNames[i]; row["DisplayName"] = tbl.FullyQualifiedColumns[i]; row["QualifiedDataType"] = tbl.ColumnDataTypes[i]; row["ParentName"] = tbl.TableName; row["IsOffline"] = true; row["Catalog"] = ""; row["Schema"] = ""; _tblObjects.Rows.Add(row); } } } //3- Insert variable names if (aResults.VariableNamesWithDataTypes != null && aResults.VariableNamesWithDataTypes.Values != null) { foreach (SqlVariable v in aResults.VariableNamesWithDataTypes.Values) { row = _tblObjects.NewRow(); row["Order"] = -1; row["Type"] = "localVar"; row["Name"] = v.Name; row["DisplayName"] = v.FullyQualifiedName; row["QualifiedDataType"] = v.DataType; row["IsOffline"] = true; row["Catalog"] = ""; row["Schema"] = ""; _tblObjects.Rows.Add(row); } } //4- Insert table aliases (From) if (aResults.TableAliasFrom != null && aResults.TableAliasFrom.Values != null) { foreach (SqlTableAlias tblAlias in aResults.TableAliasFrom.Values) { if (tblAlias.TableAlias.ToLowerInvariant() == tblAlias.TableName.ToLowerInvariant()) { continue; } if (_tableAliases.ContainsKey(tblAlias.TableAlias.ToLowerInvariant())) { continue; } _tableAliases.Add(tblAlias.TableAlias.ToLowerInvariant(), tblAlias.TableName.ToLowerInvariant()); row = _tblObjects.NewRow(); row["Type"] = "Tbl"; row["Order"] = 0; row["Name"] = tblAlias.TableAlias; row["DisplayName"] = tblAlias.TableAlias; row["QualifiedDataType"] = "Table Alias"; row["IsOffline"] = true; row["Catalog"] = ""; row["Schema"] = ""; _tblObjects.Rows.Add(row); } } //5- Insert table aliases (Join) if (aResults.TableAliasJoin != null && aResults.TableAliasJoin.Values != null) { foreach (SqlTableAlias tblAlias in aResults.TableAliasJoin.Values) { if (tblAlias.TableAlias.ToLowerInvariant() == tblAlias.TableName.ToLowerInvariant()) { continue; } if (_tableAliases.ContainsKey(tblAlias.TableAlias.ToLowerInvariant())) { continue; } _tableAliases.Add(tblAlias.TableAlias.ToLowerInvariant(), tblAlias.TableName.ToLowerInvariant()); row = _tblObjects.NewRow(); row["Type"] = "Tbl"; row["Order"] = 0; row["Name"] = tblAlias.TableAlias; row["DisplayName"] = tblAlias.TableAlias; row["QualifiedDataType"] = "Table Alias"; row["IsOffline"] = true; row["Catalog"] = ""; row["Schema"] = ""; _tblObjects.Rows.Add(row); } } //_bsObjects.Filter = " ( Type <> 'Param' AND Type <> 'Col' AND Type <> 'Usr')"; //_bsObjects.Sort = "Order ASC, Name ASC"; _parsedObjectsAvailable = true; }
public void JumpTo(string jumpString, SqlAnalyzerResults aResults) { _aResults = aResults; string parentNamePlaceHolder = "$\x26\x26\x26$"; string parentName = String.Empty; string filterStr = String.Empty; string dbName = _defaultDatabase; string schemaName = "???"; bool isInvalidFilter = false; label1.Text = jumpString; CustomStringTokenizer tok = new CustomStringTokenizer(jumpString); IList <CustomToken> tokens = new List <CustomToken>(); CustomToken token = null; do { token = tok.Next(); if (token.Kind == CustomTokenKind.EOL || token.Kind == CustomTokenKind.EOF || token.Kind == CustomTokenKind.Unknown || (token.Kind == CustomTokenKind.Symbol && token.Value != ".") ) { continue; } tokens.Add(token); } while (token.Kind != CustomTokenKind.EOF); if (tokens.Count == 0) { filterStr = " ( Type <> 'Param' AND Type <> 'Col' AND Type <> 'Usr')"; schemaName = "dbo"; } // 1- Object1 else if (tokens.Count == 1) { filterStr = " ( Type <> 'Param' AND Type <> 'Col' ) " + " AND ( Name Like '" + tokens[0].Value + "%') "; schemaName = "dbo"; } // 2- Object1. else if (tokens.Count == 2) { CustomToken lastToken = tokens[1]; if (lastToken.Value == ".") { CustomToken parentToken = tokens[0]; if (IsValidDatabase(parentToken.Value)) { dbName = parentToken.Value; filterStr = "( Type = 'Usr' )"; } else if (IsValidUser(parentToken.Value)) { dbName = _defaultDatabase; filterStr = "( Catalog = '" + _defaultDatabase + "' OR Catalog = '') " + " AND ( Schema = '" + parentToken.Value + "' OR Schema = '') " + " AND ( Type <> 'Param' AND Type <> 'Col' AND Type <> 'Usr' AND Type <> 'Db') "; schemaName = parentToken.Value; } else { dbName = _defaultDatabase; parentName = parentToken.Value; filterStr = "ParentName = '" + parentNamePlaceHolder + "' " + " AND ( " + " ( ( Catalog = '" + _defaultDatabase + "' OR Catalog = '') " + " AND ( Schema = 'dbo' ) OR ( Schema = '')" + " AND ( Type = 'Param' OR Type = 'Col' ) ) " + " OR (IsOffline = 1) " + " )"; schemaName = "dbo"; } } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } // 3- Object1.Object2, Object1.. else if (tokens.Count == 3) { CustomToken lastToken = tokens[2]; // 3.1 - Object1.. if (lastToken.Value == ".") { CustomToken parentToken = tokens[0]; if (IsValidDatabase(parentToken.Value)) { dbName = parentToken.Value; filterStr = "( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = 'dbo' OR Schema = '') " + " AND ( Type <> 'Param' AND Type <> 'Col' AND Type <> 'Db' AND Type <> 'Usr') "; schemaName = "dbo"; } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } // 3.2 - Object1.Object2 else { CustomToken parentToken = tokens[0]; // Object1 if (IsValidDatabase(parentToken.Value)) { dbName = parentToken.Value; filterStr = "Name Like '" + tokens[2].Value + "%' " + " AND ( Type = 'Usr')"; schemaName = "dbo"; } else if (IsValidUser(parentToken.Value)) { dbName = _defaultDatabase; filterStr = "( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = '" + parentToken.Value + "' OR Schema = '') " + " AND ( Type <> 'Param' AND Type <> 'Col' AND Type <> 'Db' AND Type <> 'Usr') " + " AND ( Name Like '" + tokens[2].Value + "%') "; schemaName = parentToken.Value; } else { dbName = _defaultDatabase; parentName = parentToken.Value; filterStr = "ParentName = '" + parentNamePlaceHolder + "' " + " AND ( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = 'dbo' OR Schema = '') " + " AND ( Type = 'Param' OR Type = 'Col') " + " AND ( Name Like '" + tokens[2].Value + "%') "; schemaName = "dbo"; } } } // 4- Object1.Object2. else if (tokens.Count == 4) { if (tokens[3].Value == ".") //. { if (IsValidDatabase(tokens[0].Value) && IsValidUser(tokens[2].Value)) // Object1 and Object2 { dbName = tokens[0].Value; filterStr = "( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = '" + tokens[2].Value + "' OR Schema = '')" + " AND ( Type <> 'Param' AND Type <> 'Col' AND Type <> 'Usr' AND Type <> 'Db' ) "; schemaName = tokens[2].Value; } else if (IsValidUser(tokens[0].Value)) { dbName = _defaultDatabase; parentName = tokens[2].Value; filterStr = "( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = '" + tokens[0].Value + "' OR Schema = '')" + " AND ( Type = 'Param' OR Type = 'Col')" + " AND ( ParentName = '" + parentNamePlaceHolder + "' ) "; schemaName = tokens[2].Value; } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } // 5- Object1.Object2.Object3 else if (tokens.Count == 5) { if (IsValidDatabase(tokens[0].Value) && IsValidUser(tokens[2].Value)) // Object1 and Object2 { dbName = tokens[0].Value; filterStr = "( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = '" + tokens[2].Value + "' OR Schema = '')" + " AND ( Type <> 'Param' AND Type <> 'Col' AND Type <> 'Usr' AND Type <> 'Db' ) " + " AND ( Name Like '" + tokens[4].Value + "%') "; schemaName = tokens[2].Value; } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } // 6- Object1.Object2.Object3. else if (tokens.Count == 6) { if (tokens[5].Value == ".") { if (IsValidDatabase(tokens[0].Value) && IsValidUser(tokens[2].Value)) // Object1 and Object2 { dbName = tokens[0].Value; parentName = tokens[4].Value; filterStr = "( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = '" + tokens[2].Value + "' OR Schema = '')" + " AND ( Type = 'Param' OR Type = 'Col')" + " AND ( ParentName = '" + parentNamePlaceHolder + "') "; schemaName = tokens[2].Value; } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } // 7- Object1.Object2.Object3.Object4 else if (tokens.Count == 7) { if (IsValidDatabase(tokens[0].Value) && IsValidUser(tokens[2].Value)) // Object1 and Object2 { dbName = tokens[0].Value; parentName = tokens[4].Value; filterStr = "( Catalog = '" + dbName + "' OR Catalog = '') " + " AND ( Schema = '" + tokens[2].Value + "' OR Schema = '')" + " AND ( Type = 'Param' OR Type = 'Col')" + " AND ( ParentName = '" + parentNamePlaceHolder + "') " + " AND ( Name Like '" + tokens[6].Value + "%') "; } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } } else { dbName = _defaultDatabase; // Invalid filter filterStr = " ( Type = '@@@')"; isInvalidFilter = true; } if (_currentDatabase.ToLowerInvariant() != dbName.ToLowerInvariant()) { _currentDatabase = dbName; PopulateCodeCompletionList(false); } if (_bsObjects.DataSource == null || _tblObjects == null) { this.Hide(); return; } if (!_parsedObjectsAvailable) { ClearParsedObjects(); InsertParsedObjects(aResults); } if (!String.IsNullOrEmpty(parentName)) { parentName = TableNameFromAlias(parentName); filterStr = filterStr.Replace(parentNamePlaceHolder, parentName); } _bsObjects.Filter = filterStr; _bsObjects.Sort = "Order ASC, Name ASC"; if (_bsObjects.Count > 0) { _bsObjects.Position = 0; } if (!isInvalidFilter) { label2.Text = "Catalog = '" + dbName + "', Schema = '" + schemaName + "'"; } else { label2.Text = "Can not parse code completion proposal!"; } }