public override void Translate() { if (sourceDbInterpreter.DatabaseType == targetDbInterpreter.DatabaseType) { return; } if (this.hasError) { return; } this.LoadMappings(); if (string.IsNullOrEmpty(targetOwnerName)) { if (targetDbInterpreter is SqlServerInterpreter) { targetOwnerName = "dbo"; } else { targetOwnerName = DbInterpreterHelper.GetOwnerName(targetDbInterpreter); } } foreach (View view in views) { try { string viewNameWithQuotation = $"{targetDbInterpreter.QuotationLeftChar}{view.Name}{targetDbInterpreter.QuotationRightChar}"; string definition = view.Definition; definition = definition .Replace(sourceDbInterpreter.QuotationLeftChar, '"') .Replace(sourceDbInterpreter.QuotationRightChar, '"') .Replace("<>", "!=") .Replace(">", " > ") .Replace("<", " < ") .Replace("!=", "<>"); StringBuilder sb = new StringBuilder(); string[] lines = definition.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { if (line.StartsWith(this.sourceDbInterpreter.CommentString)) { continue; } sb.AppendLine(line); } definition = this.ParseDefinition(sb.ToString()); string createClause = this.targetDbInterpreter.DatabaseType == DatabaseType.Oracle ? "CREATE OR REPLACE" : "CREATE"; string createAsClause = $"{createClause} VIEW {(string.IsNullOrEmpty(targetOwnerName)? "": targetOwnerName + "." )}{viewNameWithQuotation} AS "; if (!definition.Trim().ToLower().StartsWith("create")) { definition = createAsClause + Environment.NewLine + definition; } else { int asIndex = definition.ToLower().IndexOf("as"); definition = createAsClause + definition.Substring(asIndex + 2); } view.Definition = definition; if (this.OnTranslated != null) { this.OnTranslated(this.targetDbInterpreter.DatabaseType, view, new TranslateResult() { Data = view.Definition }); } } catch (Exception ex) { var vce = new ViewConvertException(ex) { SourceServer = sourceDbInterpreter.ConnectionInfo.Server, SourceDatabase = sourceDbInterpreter.ConnectionInfo.Database, SourceObject = view.Name, TargetServer = targetDbInterpreter.ConnectionInfo.Server, TargetDatabase = targetDbInterpreter.ConnectionInfo.Database, TargetObject = view.Name }; if (!this.SkipError) { throw vce; } else { this.FeedbackError(ExceptionHelper.GetExceptionDetails(ex), this.SkipError); } } } }
public string BuildDefinition(List <TSQL.Tokens.TSQLToken> tokens) { StringBuilder sb = new StringBuilder(); this.sourceOwnerName = DbInterpreterHelper.GetOwnerName(sourceDbInterpreter); int ignoreCount = 0; TSQLTokenType previousType = TSQLTokenType.Whitespace; string previousText = ""; for (int i = 0; i < tokens.Count; i++) { if (ignoreCount > 0) { ignoreCount--; continue; } var token = tokens[i]; var tokenType = token.Type; string text = token.Text; switch (tokenType) { case TSQLTokenType.Identifier: var nextToken = i + 1 < tokens.Count ? tokens[i + 1] : null; if (this.sourceDbInterpreter.DatabaseType == DatabaseType.SqlServer) { if ((text == "dbo" || text == "[dbo]") && this.TargetDbOwner?.ToLower() != "dbo") { if (nextToken != null && nextToken.Text == ".") { ignoreCount++; } continue; } } if (convertedDataTypes.Contains(text)) { sb.Append(text); continue; } //Remove owner name if (nextToken != null && nextToken.Text.Trim() != "(" && text.Trim('"') == sourceOwnerName && i + 1 < tokens.Count && tokens[i + 1].Text == "." ) { ignoreCount++; continue; } else if (nextToken != null && nextToken.Text.Trim() == "(") //function handle { if (this.convertedFunctions.Contains(text)) { sb.Append(text); continue; } string textWithBrackets = text.ToLower() + "()"; bool useBrackets = false; MappingFunctionInfo targetFunctionInfo = this.GetMappingFunctionInfo(text, out useBrackets); if (targetFunctionInfo.Name.ToLower() != text.ToLower()) { string targetFunction = targetFunctionInfo.Name; if (!string.IsNullOrEmpty(targetFunction)) { sb.Append(targetFunction); } if (useBrackets) { ignoreCount += 2; } } else { if (text.StartsWith(this.sourceDbInterpreter.QuotationLeftChar.ToString()) && text.EndsWith(this.sourceDbInterpreter.QuotationRightChar.ToString())) { sb.Append(this.GetQuotedString(text.Trim(this.sourceDbInterpreter.QuotationLeftChar, this.sourceDbInterpreter.QuotationRightChar))); } else { sb.Append(text); } } } else { sb.Append(this.GetQuotedString(text)); } break; case TSQLTokenType.StringLiteral: if (previousType != TSQLTokenType.Whitespace && previousText.ToLower() == "as") { sb.Append(this.GetQuotedString(text)); } else { sb.Append(text); } break; case TSQLTokenType.SingleLineComment: case TSQLTokenType.MultilineComment: continue; case TSQLTokenType.Keyword: switch (text.ToUpper()) { case "AS": if (targetDbInterpreter is OracleInterpreter) { var previousKeyword = (from t in tokens where t.Type == TSQLTokenType.Keyword && t.EndPosition < token.BeginPosition select t).LastOrDefault(); if (previousKeyword != null && previousKeyword.Text.ToUpper() == "FROM") { continue; } } break; } sb.Append(text); break; default: sb.Append(text); break; } if (!string.IsNullOrWhiteSpace(text)) { previousText = text; previousType = tokenType; } } return(sb.ToString()); }