Пример #1
0
        public int CompareTo(ISqlString other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            if (IsNull && other.IsNull)
            {
                return(0);
            }
            if (IsNull)
            {
                return(1);
            }
            if (other.IsNull)
            {
                return(-1);
            }

            if (other is SqlString)
            {
                var otherString = (SqlString)other;
                return(String.Compare(Value, otherString.Value, StringComparison.Ordinal));
            }

            throw new NotImplementedException("Comparison with long strong not implemented yet.");
        }
Пример #2
0
        public static SqlXmlNode XmlType(ISqlString s)
        {
            var len     = s.Length;
            var content = new char[len];
            var offset  = 0;

            const int bufferSize = 1024 * 10;

            using (var reader = s.GetInput(s.Encoding)) {
                while (true)
                {
                    var buffer    = new char[bufferSize];
                    var readCount = reader.Read(buffer, 0, bufferSize);

                    if (readCount == 0)
                    {
                        break;
                    }

                    Array.Copy(buffer, 0, content, offset, readCount);

                    offset += readCount;
                }
            }

            var bytes = s.Encoding.GetBytes(content);

            if (!s.Encoding.Equals(Encoding.UTF8))
            {
                bytes = Encoding.Convert(s.Encoding, Encoding.UTF8, bytes);
            }

            return(new SqlXmlNode(bytes));
        }
Пример #3
0
        public SqlString Concat(ISqlString other)
        {
            if (other == null || other.IsNull)
            {
                return(this);
            }

            if (other is SqlString)
            {
                var otheString = (SqlString)other;
                var length     = (int)(Length + otheString.Length);
                if (length >= MaxLength)
                {
                    throw new ArgumentException("The final string will be over the maximum length");
                }

                var sourceChars = ToCharArray();
                var otherChars  = otheString.ToCharArray();
                var destChars   = new char[length];

                Array.Copy(sourceChars, 0, destChars, 0, (int)Length);
                Array.Copy(otherChars, 0, destChars, (int)Length, (int)otheString.Length);
                return(new SqlString(destChars, length));
            }

            var sb = new StringBuilder(Int16.MaxValue);

            using (var output = new StringWriter(sb)) {
                // First read the current stream
                using (var reader = GetInput(Encoding.Unicode)) {
                    var buffer = new char[2048];
                    int count;
                    while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        output.Write(buffer, 0, count);
                    }
                }

                // Then read the second stream
                using (var reader = other.GetInput(Encoding.Unicode)) {
                    var buffer = new char[2048];
                    int count;
                    while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        output.Write(buffer, 0, count);
                    }
                }

                output.Flush();
            }

#if PCL
            var s = sb.ToString();
            return(new SqlString(s));
#else
            var outChars = new char[sb.Length];
            sb.CopyTo(0, outChars, 0, sb.Length);
            return(new SqlString(outChars, outChars.Length));
#endif
        }
Пример #4
0
        public bool Matches(ISqlString source, string pattern, char escapeChar)
        {
            if (!(source is SqlString))
            {
                throw new ArgumentException("This implementation of the string search does not support long strings");
            }

            var s = ((SqlString)source).Value;

            return(PatternSearch.PatternMatch(pattern, s, escapeChar));
        }
Пример #5
0
        private static int LexicographicalOrder(ISqlString str1, ISqlString str2)
        {
            // If both strings are small use the 'toString' method to compare the
            // strings.  This saves the overhead of having to store very large string
            // objects in memory for all comparisons.
            long str1Size = str1.Length;
            long str2Size = str2.Length;

            if (str1Size < 32 * 1024 &&
                str2Size < 32 * 1024)
            {
                return(String.Compare(str1.ToString(), str2.ToString(), StringComparison.Ordinal));
            }

            // TODO: pick one of the two encodings?

            // The minimum size
            long       size = System.Math.Min(str1Size, str2Size);
            TextReader r1   = str1.GetInput();
            TextReader r2   = str2.GetInput();

            try {
                try {
                    while (size > 0)
                    {
                        int c1 = r1.Read();
                        int c2 = r2.Read();
                        if (c1 != c2)
                        {
                            return(c1 - c2);
                        }
                        --size;
                    }
                    // They compare equally up to the limit, so now compare sizes,
                    if (str1Size > str2Size)
                    {
                        // If str1 is larger
                        return(1);
                    }
                    else if (str1Size < str2Size)
                    {
                        // If str1 is smaller
                        return(-1);
                    }
                    // Must be equal
                    return(0);
                } finally {
                    r1.Dispose();
                    r2.Dispose();
                }
            } catch (IOException e) {
                throw new Exception("IO Error: " + e.Message);
            }
        }
Пример #6
0
        public SqlBoolean IsNotLike(ISqlString value, ISqlString pattern)
        {
            var likeResult = IsLike(value, pattern);

            if (likeResult.IsNull)
            {
                return(likeResult);
            }

            return(likeResult.Not());
        }
Пример #7
0
        /// <summary>
        /// Get a sql statement and corresponding sql paramaters from the builder
        /// </summary>
        /// <param name="builder">The sql builder to use in order to render sql</param>
        public static QueryParts ToSql(this ISqlString builder)
        {
            var builderResult    = builder.ToSqlString();
            var sql              = builderResult.Sql;
            var queryTeardownSql = builderResult.QueryTeardownSql;

            if (builderResult.TeardownSqlCanBeInlined)
            {
                sql += $";\n\n{queryTeardownSql}";
                queryTeardownSql = null;
            }

            return(new QueryParts(
                       $"{builderResult.QuerySetupSql}\n\n{sql}",
                       queryTeardownSql));
        }
Пример #8
0
        public MappedSqlStatementBuilder(
            BuildMapState state,
            IEnumerable <QueryElementBasedMappedProperty> selectProperties,
            ISqlSelectStatement statement,
            ISqlString innerSqlString,
            ISqlSyntax sqlSyntax)
        {
            State            = state ?? throw new ArgumentNullException(nameof(state));
            SelectProperties = selectProperties?.ToDictionary(x => x.To) ?? throw new ArgumentNullException(nameof(selectProperties));
            Statement        = statement ?? throw new ArgumentNullException(nameof(statement));
            InnerSqlString   = innerSqlString ?? throw new ArgumentNullException(nameof(innerSqlString));
            SqlSyntax        = sqlSyntax ?? throw new ArgumentNullException(nameof(innerSqlString));

            // this paradigm can be extended if there are more than 1
            // alias needed per query
            InnerQueryAlias = SqlStatementConstants.InnerQueryAlias;
        }
Пример #9
0
        /// <summary>
        /// Compile a sqlBuilder into a query which can be executed multiple times
        /// </summary>
        /// <param name="sqlBuilder">The builder with all properties populated</param>
        /// <param name="parameters">Any constant parameters in the statement</param>
        /// <param name="queryParseType">Define the way results are to be parsed</param>
        public static CompiledQuery <TArgs, TResult> Compile <TArgs, TResult> (
            this ISqlString sqlBuilder,
            ISqlSelectStatement statement,
            IEnumerable <object> parameters,
            ISqlSyntax sqlSyntax,
            QueryParseType queryParseType,
            bool requiresSimpleValueUnwrap)
        {
            var sql = ToSql(sqlBuilder);

            var selectColumns = statement.SelectColumns.Select(Alias).ToArray();
            var resultType    = requiresSimpleValueUnwrap
                ? ReflectionUtils.CreatePropMapValue(typeof(TResult))
                : typeof(TResult);
            var propertyGraph = statement.BuildObjetPropertyGraph(resultType, queryParseType);

            return(new CompiledQuery <TArgs, TResult>(sql, parameters.ToArray(), selectColumns, propertyGraph, sqlSyntax, requiresSimpleValueUnwrap));
        }
Пример #10
0
        public SqlBoolean IsLike(ISqlString value, ISqlString pattern)
        {
            if (value == null && IsNull)
            {
                return(true);
            }
            if (!IsNull && value == null)
            {
                return(false);
            }
            if (value == null)
            {
                return(false);
            }

            var s1 = value.ToString();
            var s2 = pattern.ToString();

            return(PatternSearch.FullPatternMatch(s1, s2, '\\'));
        }
Пример #11
0
        public static int Compare(CultureInfo locale, ISqlString x, ISqlString y)
        {
            if (x == null && y == null)
            {
                return(0);
            }
            if (x == null)
            {
                return(1);
            }
            if (y == null)
            {
                return(-1);
            }

            // If lexicographical ordering,
            if (locale == null)
            {
                return(LexicographicalOrder(x, y));
            }

            return(locale.CompareInfo.Compare(x.ToString(), y.ToString()));
        }
Пример #12
0
        public static SqlString Substring(this ISqlString source, int offset, int count)
        {
            if (source == null || source.IsNull)
            {
                return(SqlString.Null);
            }

            var en = source.GetEnumerator();
            var sb = new StringBuilder(count);

            int index = -1;

            while (en.MoveNext())
            {
                if (++index < offset)
                {
                    continue;
                }

                sb.Append(en.Current);

                if (index == count - 1)
                {
                    break;
                }
            }

#if PCL
            var s = sb.ToString();
            return(new SqlString(s));
#else
            var chars = new char[count];
            sb.CopyTo(0, chars, 0, count);
            return(new SqlString(chars));
#endif
        }
Пример #13
0
 public static SqlBoolean PatternMatch(this ISqlString pattern, string expression, char escapeChar)
 {
     throw new NotImplementedException();
 }
Пример #14
0
        private static int LexicographicalOrder(ISqlString str1, ISqlString str2)
        {
            // If both strings are small use the 'toString' method to compare the
            // strings.  This saves the overhead of having to store very large string
            // objects in memory for all comparisons.
            long str1Size = str1.Length;
            long str2Size = str2.Length;
            if (str1Size < 32 * 1024 &&
                str2Size < 32 * 1024) {
                return String.Compare(str1.ToString(), str2.ToString(), StringComparison.Ordinal);
            }

            // TODO: pick one of the two encodings?

            // The minimum size
            long size = System.Math.Min(str1Size, str2Size);
            TextReader r1 = str1.GetInput(str1.Encoding);
            TextReader r2 = str2.GetInput(str2.Encoding);
            try {
                try {
                    while (size > 0) {
                        int c1 = r1.Read();
                        int c2 = r2.Read();
                        if (c1 != c2) {
                            return c1 - c2;
                        }
                        --size;
                    }
                    // They compare equally up to the limit, so now compare sizes,
                    if (str1Size > str2Size) {
                        // If str1 is larger
                        return 1;
                    } else if (str1Size < str2Size) {
                        // If str1 is smaller
                        return -1;
                    }
                    // Must be equal
                    return 0;
                } finally {
                    r1.Dispose();
                    r2.Dispose();
                }
            } catch (IOException e) {
                throw new Exception("IO Error: " + e.Message);
            }
        }
Пример #15
0
        public SqlBoolean IsNotLike(ISqlString value, ISqlString pattern)
        {
            var likeResult = IsLike(value, pattern);
            if (likeResult.IsNull)
                return likeResult;

            return likeResult.Not();
        }
Пример #16
0
        public SqlBoolean IsLike(ISqlString value, ISqlString pattern)
        {
            if (value == null && IsNull)
                return true;
            if (!IsNull && value == null)
                return false;
            if (value == null)
                return false;

            var s1 = value.ToString();
            var s2 = pattern.ToString();
            return PatternSearch.FullPatternMatch(s1, s2, '\\');
        }
Пример #17
0
 public static SqlNumber IndexOf(this ISqlString pattern, SqlString expression)
 {
     // TODO: Implement a version of the Boyer-Moore algorithm over a SQL String
     throw new NotImplementedException();
 }
Пример #18
0
        public static SqlXmlNode XmlType(ISqlString s)
        {
            var len = s.Length;
            var content = new char[len];
            var offset = 0;

            const int bufferSize = 1024*10;

            using (var reader = s.GetInput(s.Encoding)) {
                while (true) {
                    var buffer = new char[bufferSize];
                    var readCount = reader.Read(buffer, 0, bufferSize);

                    if (readCount == 0)
                        break;

                    Array.Copy(buffer, 0, content, offset, readCount);

                    offset += readCount;
                }
            }

            var bytes = s.Encoding.GetBytes(content);
            if (!s.Encoding.Equals(Encoding.UTF8))
                bytes = Encoding.Convert(s.Encoding, Encoding.UTF8, bytes);

            return new SqlXmlNode(bytes);
        }
Пример #19
0
 int IComparable <ISqlString> .CompareTo(ISqlString other)
 {
     throw new NotSupportedException();
 }
Пример #20
0
 public static SqlString Substring(this ISqlString source, int offset)
 {
     return(Substring(source, offset, (int)source.Length - offset));
 }