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."); }
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)); }
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 }
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)); }
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); } }
public SqlBoolean IsNotLike(ISqlString value, ISqlString pattern) { var likeResult = IsLike(value, pattern); if (likeResult.IsNull) { return(likeResult); } return(likeResult.Not()); }
/// <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)); }
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; }
/// <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)); }
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, '\\')); }
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())); }
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 }
public static SqlBoolean PatternMatch(this ISqlString pattern, string expression, char escapeChar) { throw new NotImplementedException(); }
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); } }
public SqlBoolean IsNotLike(ISqlString value, ISqlString pattern) { var likeResult = IsLike(value, pattern); if (likeResult.IsNull) return likeResult; return likeResult.Not(); }
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, '\\'); }
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(); }
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); }
int IComparable <ISqlString> .CompareTo(ISqlString other) { throw new NotSupportedException(); }
public static SqlString Substring(this ISqlString source, int offset) { return(Substring(source, offset, (int)source.Length - offset)); }