Пример #1
0
        public override void Translate()
        {
            if (this.sourceDbType == this.targetDbType)
            {
                return;
            }

            this.LoadMappings();

            this.sourceFuncSpecs = FunctionManager.GetFunctionSpecifications(this.sourceDbType);
            this.targetFuncSpecs = FunctionManager.GetFunctionSpecifications(this.targetDbType);

            foreach (TokenInfo token in this.functions)
            {
                List <FunctionFomular> fomulars = this.GetFunctionFomulars(token.Symbol);

                foreach (FunctionFomular fomular in fomulars)
                {
                    string name = fomular.Name;

                    bool useBrackets = false;

                    MappingFunctionInfo targetFunctionInfo = this.GetMappingFunctionInfo(name, out useBrackets);

                    if (!string.IsNullOrEmpty(targetFunctionInfo.Name))
                    {
                        if (targetFunctionInfo.Name.ToUpper().Trim() != name.ToUpper().Trim())
                        {
                            string oldExp = fomular.Expression;
                            string newExp = this.ReplaceValue(fomular.Expression, name, targetFunctionInfo.Name);

                            fomular.Expression = newExp;

                            token.Symbol = this.ReplaceValue(token.Symbol, oldExp, newExp);
                        }
                    }

                    Dictionary <string, string> dictDataType = null;
                    string newExpression = this.ParseFomular(this.sourceFuncSpecs, this.targetFuncSpecs, fomular, targetFunctionInfo, out dictDataType);

                    if (newExpression != fomular.Expression)
                    {
                        token.Symbol = this.ReplaceValue(token.Symbol, fomular.Expression, newExpression);
                    }
                }
            }
        }
Пример #2
0
        public string ParseFomular(List <FunctionSpecification> sourceFuncSpecs, List <FunctionSpecification> targetFuncSpecs,
                                   FunctionFomular fomular, MappingFunctionInfo targetFunctionInfo, out Dictionary <string, string> dictDataType)
        {
            dictDataType = new Dictionary <string, string>();

            string name = fomular.Name;

            FunctionSpecification sourceFuncSpec = sourceFuncSpecs.FirstOrDefault(item => item.Name.ToUpper() == name.ToUpper());
            FunctionSpecification targetFuncSpec = targetFuncSpecs.FirstOrDefault(item => item.Name.ToUpper() == targetFunctionInfo.Name.ToUpper());

            string newExpression = fomular.Expression;

            if (sourceFuncSpec != null && targetFuncSpec != null)
            {
                string delimiter = sourceFuncSpec.Delimiter == "," ? "," : $" {sourceFuncSpec.Delimiter} ";
                fomular.Delimiter = delimiter;

                List <string> fomularArgs = fomular.Args;

                int fetchCount = string.IsNullOrEmpty(targetFunctionInfo.Args) ? fomularArgs.Count : -1;

                Dictionary <int, string> sourceTokens = this.GetFunctionArgumentTokens(sourceFuncSpec, null, fetchCount);
                Dictionary <int, string> targetTokens = this.GetFunctionArgumentTokens(targetFuncSpec, targetFunctionInfo.Args, fetchCount);

                bool ignore = false;

                if (fomularArgs.Count > 0 && (targetTokens.Count == 0 || sourceTokens.Count == 0))
                {
                    ignore = true;
                }

                if (!ignore)
                {
                    List <string> args = new List <string>();

                    foreach (var kp in targetTokens)
                    {
                        int    targetIndex = kp.Key;
                        string token       = kp.Value;

                        if (sourceTokens.ContainsValue(token))
                        {
                            int sourceIndex = sourceTokens.FirstOrDefault(item => item.Value == token).Key;

                            if (fomularArgs.Count > sourceIndex)
                            {
                                string oldArg = fomular.Args[sourceIndex];
                                string newArg = oldArg;

                                switch (token.ToUpper())
                                {
                                case "TYPE":

                                    if (!dictDataType.ContainsKey(oldArg))
                                    {
                                        newArg = this.GetNewDataType(this.dataTypeMappings, oldArg);

                                        dictDataType.Add(oldArg, newArg.Trim());
                                    }
                                    else
                                    {
                                        newArg = dictDataType[oldArg];
                                    }
                                    break;
                                }

                                args.Add(newArg);
                            }
                        }
                        else if (!string.IsNullOrEmpty(targetFunctionInfo.Args))
                        {
                            args.Add(token);
                        }
                    }

                    string targetDelimiter = targetFuncSpec.Delimiter == "," ? "," : $" {targetFuncSpec.Delimiter} ";

                    string strArgs = string.Join(targetDelimiter, args);

                    newExpression = $"{targetFunctionInfo.Name}{ (targetFuncSpec.NoParenthesess ? "" : $"({strArgs})") }";
                }
            }
        protected string HandleDefinition(string definition, List <TSQLToken> tokens, out bool changed)
        {
            this.sourceFuncSpecs = FunctionManager.GetFunctionSpecifications(this.sourceDbInterpreter.DatabaseType);
            this.targetFuncSpecs = FunctionManager.GetFunctionSpecifications(this.targetDbInterpreter.DatabaseType);

            changed = false;

            string newDefinition = definition;

            foreach (TSQLToken token in tokens)
            {
                string text = token.Text;
                string functionExpression = null;

                switch (token.Type)
                {
                case TSQLTokenType.SystemIdentifier:

                    functionExpression = this.GetFunctionExpression(token, definition);

                    break;

                case TSQLTokenType.Identifier:

                    switch (text.ToUpper())
                    {
                    case "CAST":
                    case "TO_NUMBER":
                        functionExpression = this.GetFunctionExpression(token, definition);

                        break;
                    }
                    break;

                case TSQLTokenType.Keyword:

                    break;
                }

                if (!string.IsNullOrEmpty(functionExpression))
                {
                    bool useBrackets = false;
                    MappingFunctionInfo targetFunctionInfo = this.GetMappingFunctionInfo(text, out useBrackets);

                    FunctionFomular fomular = new FunctionFomular(functionExpression);

                    Dictionary <string, string> dictDataType = null;

                    string newExpression = this.ParseFomular(this.sourceFuncSpecs, this.targetFuncSpecs, fomular, targetFunctionInfo, out dictDataType);

                    if (newExpression != fomular.Expression)
                    {
                        newDefinition = this.ReplaceValue(newDefinition, fomular.Expression, newExpression);

                        changed = true;
                    }

                    if (dictDataType != null)
                    {
                        this.convertedDataTypes.AddRange(dictDataType.Values);
                    }

                    if (!string.IsNullOrEmpty(targetFunctionInfo.Args) && changed)
                    {
                        if (!this.convertedFunctions.Contains(targetFunctionInfo.Name))
                        {
                            this.convertedFunctions.Add(targetFunctionInfo.Name);
                        }
                    }
                }
            }

            return(newDefinition);
        }
        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());
        }