Example #1
0
        /// <summary>
        /// Adds a table to the FROM statement with "T0" alias and sets it as target for future field selections.
        /// </summary>
        /// <param name="row">Row object.</param>
        /// <returns>The query itself.</returns>
        public static SqlQuery From(this SqlQuery query, IEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("row");
            }

            var row = entity as Row;

            if (row != null)
            {
                var fields = row.GetFields();
                query.From(fields);
                if (!query.IsDialectOverridden && !string.IsNullOrEmpty(fields.connectionKey))
                {
                    var cs = SqlConnections.TryGetConnectionString(fields.connectionKey);
                    if (cs != null)
                    {
                        query.Dialect(cs.Dialect);
                    }
                }
            }
            else
            {
                var alias = entity as IAlias;
                if (alias != null && (alias.Name == "t0" || alias.Name == "T0") && alias.Table == entity.Table)
                {
                    query.From(alias);
                }
                else
                {
                    query.From(entity.Table, string.Empty, Alias.T0);
                }
            }

            return(query.Into(entity));
        }
        public TAttribute GetBestMatch <TAttribute>(IEnumerable <TAttribute> expressions, Func <TAttribute, string> getDialect)
        {
            if (!expressions.Any(x => !string.IsNullOrEmpty(getDialect(x))))
            {
                return(expressions.FirstOrDefault());
            }

            if (dialectTypeName == null)
            {
                ISqlDialect dialect = null;

                if (!string.IsNullOrEmpty(connectionKey))
                {
                    var csi = SqlConnections.TryGetConnectionString(connectionKey);
                    if (csi != null)
                    {
                        dialect = csi.Dialect;
                    }
                }

                dialect           = dialect ?? SqlSettings.DefaultDialect;
                dialectServerType = dialect.ServerType;
                dialectTypeName   = dialect.GetType().Name;
            }

            var st = dialectServerType;
            var tn = dialectTypeName;

            Dictionary <TAttribute, int> weight = null;

            var bestMatch = expressions.Where(x =>
            {
                var d = getDialect(x);

                if (string.IsNullOrEmpty(getDialect(x)))
                {
                    return(true);
                }

                if (d.IndexOf(',') < 0)
                {
                    return(IsMatch(d));
                }

                var best = d.Split(comma, StringSplitOptions.RemoveEmptyEntries)
                           .Select(z => z.Trim())
                           .Where(z => IsMatch(z))
                           .OrderByDescending(z => z.Length)
                           .FirstOrDefault();

                if (best != null)
                {
                    if (weight == null)
                    {
                        weight = new Dictionary <TAttribute, int>();
                    }

                    weight[x] = best.Length;
                    return(true);
                }

                return(false);
            })
                            .OrderByDescending(x =>
            {
                var d = getDialect(x);
                if (string.IsNullOrEmpty(d))
                {
                    return(0);
                }

                int w;
                if (weight != null && weight.TryGetValue(x, out w))
                {
                    return(w);
                }

                return(d.Length);
            })
                            .FirstOrDefault();

            return(bestMatch);
        }
Example #3
0
        private ExpressionAttribute GetBestMatchingExpression(IEnumerable <ExpressionAttribute> expressions,
                                                              ref string dialectServerType, ref string dialectTypeName)
        {
            if (!expressions.Any(x => !string.IsNullOrEmpty(x.Dialect)))
            {
                return(expressions.FirstOrDefault());
            }

            if (dialectTypeName == null)
            {
                ISqlDialect dialect = null;

                if (!string.IsNullOrEmpty(connectionKey))
                {
                    var csi = SqlConnections.TryGetConnectionString(connectionKey);
                    if (csi != null)
                    {
                        dialect = csi.Dialect;
                    }
                }

                dialect           = dialect ?? SqlSettings.DefaultDialect;
                dialectServerType = dialect.ServerType;
                dialectTypeName   = dialect.GetType().Name;
            }

            var st = dialectServerType;
            var tn = dialectTypeName;

            Func <string, bool> isMatch = s =>
            {
                return(st.StartsWith(s, StringComparison.OrdinalIgnoreCase) ||
                       tn.StartsWith(s, StringComparison.OrdinalIgnoreCase));
            };

            Dictionary <ExpressionAttribute, int> weight = null;

            var bestMatch = expressions.Where(x =>
            {
                if (string.IsNullOrEmpty(x.Dialect))
                {
                    return(true);
                }

                if (x.Dialect.IndexOf(',') < 0)
                {
                    return(isMatch(x.Dialect));
                }

                var best = x.Dialect.Split(comma, StringSplitOptions.RemoveEmptyEntries)
                           .Select(z => z.Trim())
                           .Where(z => isMatch(z))
                           .OrderByDescending(z => z.Length)
                           .FirstOrDefault();

                if (best != null)
                {
                    if (weight == null)
                    {
                        weight = new Dictionary <ExpressionAttribute, int>();
                    }

                    weight[x] = best.Length;
                    return(true);
                }

                return(false);
            })
                            .OrderByDescending(x =>
            {
                if (string.IsNullOrEmpty(x.Dialect))
                {
                    return(0);
                }

                int w;
                if (weight != null && weight.TryGetValue(x, out w))
                {
                    return(w);
                }

                return(x.Dialect.Length);
            })
                            .FirstOrDefault();

            return(bestMatch);
        }