public IJoinedQueryBuilder <TEntity, T> InnerJoin <T>(string alias) { string typeName = typeof(T).Name; QueryStringBuilder.Append($" INNER JOIN {StringToolkit.PascalToUnderscore(typeName)} {alias} "); return(new JoinedQueryBuilder <TEntity, T>(this)); }
public IInsertQueryBuilder <TEntity> InsertInto <TEntity>() where TEntity : class { QueryStringBuilder.Append($"INSERT INTO " + $"{StringToolkit.PascalToUnderscore(typeof(TEntity).Name)} " /* + $"{PropertyToolkit.BuildInsertString(PropertyToolkit.GetInsertProperties<TEntity>())}"*/); return(new InsertQueryBuilder <TEntity>(this)); }
public ISelectQueryBuilder <TEntity> Select <TEntity>(params Expression <Func <TEntity, object> >[] columns) where TEntity : class { string columnStr; if (columns.Length == 0) { columnStr = "*"; } else { string[] clmCpy = new string[columns.Length]; for (int i = 0; i < columns.Length; i++) { clmCpy[i] = $"{ExpressionEvaluator.BuildOrderByQueryString(columns[i], false).Replace("ORDER BY", "")}" .Trim(); } columnStr = string.Join(", ", clmCpy) .Replace("[", "(") .Replace("]", ")"); } QueryStringBuilder.Append($"SELECT {columnStr} " + $"FROM {StringToolkit.PascalToUnderscore(typeof(TEntity).Name)}"); return(new SelectQueryBuilder <TEntity>(this)); }
protected void RefactorAlias(LambdaExpression predicate) { ParameterExpression[] parameters = ExpressionEvaluator.GetAlias(predicate).ToArray(); IDictionary <Type, string> aliasMap = ExpressionEvaluator.GetAliasArray(parameters); foreach (KeyValuePair <Type, string> alias in aliasMap) { Regex rg = new Regex($" {StringToolkit.PascalToUnderscore(alias.Key.Name)} [a-z_]*"); // already has alias string currentQueryValue = this.ToString(); MatchCollection matches = rg.Matches(currentQueryValue); if (matches.Count != 0) { foreach (Match m in matches) { QueryStringBuilder.Replace(m.Value, $" {StringToolkit.PascalToUnderscore(alias.Key.Name)} {alias.Value} "); } } else { rg = new Regex($" {StringToolkit.PascalToUnderscore(alias.Key.Name)}$"); matches = rg.Matches(currentQueryValue); foreach (Match m in matches) { QueryStringBuilder.Replace(m.Value, $" {StringToolkit.PascalToUnderscore(alias.Key.Name)} {alias.Value} "); } } } QueryStringBuilder.Replace(" ", " "); }
public Task Remove <T>(T item) where T : class { string queryStr = $"DELETE FROM {StringToolkit.PascalToUnderscore(typeof(T).Name)} " + $"WHERE {ObjectEvaluator.ToWhereString<T>(item)}"; return(QueryBuilder.ExecuteQueryAsync <T>(queryStr)); }
public void RemoveRange <T>(IEnumerable <T> items) where T : class { string queryStr = $"DELETE FROM {StringToolkit.PascalToUnderscore(typeof(T).Name)} " + $"WHERE {ObjectEvaluator.ToWhereString<T>(items)}"; QueryBuilder.ExecuteQuery <T>(queryStr); }
public async Task <bool> Contains <T>(T item) where T : class { string whereStr = $"WHERE {ObjectEvaluator.ToWhereString<T>(item)}"; string queryStr = $"SELECT * " + $"FROM {StringToolkit.PascalToUnderscore(typeof(T).Name)} " + $"{whereStr}"; return((await QueryBuilder.ExecuteQueryAsync <T>(queryStr)).Any()); }
public Task Set <T>(T oldValue, T newValue) where T : class { string setStr = $"SET {ObjectEvaluator.ToWhereString<T>(newValue).Replace(" AND ", ", \n")}"; string whereStr = $"WHERE {ObjectEvaluator.ToWhereString<T>(oldValue)}"; string queryStr = $"UPDATE {StringToolkit.PascalToUnderscore(typeof(T).Name)} " + $"{setStr} {whereStr}"; return(QueryBuilder.ExecuteNonQueryAsync(queryStr)); }
public ISelectQueryBuilder <TEntity> SelectCount <TEntity>(Expression <Func <TEntity, object> > columns = null) where TEntity : class { QueryStringBuilder.Append("SELECT COUNT(") .Append((columns == null ? "*" : $"{StringToolkit.PascalToUnderscore(((columns.Body as BinaryExpression).Right as MemberExpression).Member.Name)}")) .Append(")") .Append($" FROM {StringToolkit.PascalToUnderscore(typeof(TEntity).Name)}"); return(new SelectQueryBuilder <TEntity>(this)); }
public virtual async Task <T> FindByKey <T>(Expression <Func <T, object> > key, object value) where T : class { string leftHandSide = ExpressionEvaluator.BuildOrderByQueryString(key, false) .Replace("ORDER BY ", ""); string conditionStr = $"WHERE {leftHandSide} = {value}"; string queryStr = $"SELECT * " + $"FROM {StringToolkit.PascalToUnderscore(typeof(T).Name)} " + $"WHERE {conditionStr} " + $"LIMIT 1"; return((await QueryBuilder.ExecuteQueryAsync <T>(queryStr)).FirstOrDefault()); }
public IUpdateQueryBuilder <TEntity> Update <TEntity>() where TEntity : class { QueryStringBuilder.Append($"UPDATE {StringToolkit.PascalToUnderscore(typeof(TEntity).Name)}"); return(new UpdateQueryBuilder <TEntity>(this)); }
public IDeleteQueryBuilder <TEntity> DeleteFrom <TEntity>() where TEntity : class { QueryStringBuilder.Append($"DELETE FROM " + $"{StringToolkit.PascalToUnderscore(typeof(TEntity).Name)}"); return(new DeleteQueryBuilder <TEntity>(this)); }