public void GenerateInsertExpression() { StringBuilder columnStr = new StringBuilder(); StringBuilder valueStr = new StringBuilder(); int i = 0; foreach (ColumnMapping column in Columns) { if (SessionFactory.Provider == DataProvider.MySql && column.IsAutoGenerated) { continue; } columnStr.Append(String.Format("{0},", DataMapper.EscapeName(column.ColumnName))); valueStr.Append(String.Format("{0},", DataMapper.ParamName(i++))); } this.InsertExpression = String.Format("INSERT INTO {0} ({1}) VALUES ({2});", DataMapper.EscapeName(this.TableName), columnStr.ToString().TrimEnd(new char[] { ',' }), valueStr.ToString().TrimEnd(new char[] { ',' })); }
public void GenerateGetExpression() { StringBuilder whereStr = new StringBuilder(); int i = 0; foreach (ColumnMapping column in Columns) { if (column.IsPk || column.IsPartitionKey) { if (whereStr.Length > 0) { whereStr.Append(" AND "); } whereStr.Append(String.Format("{0} = {1}", DataMapper.EscapeName(column.ColumnName), DataMapper.ParamName(column.IsPk ? String.Format("_id{0}", i++) : "ts"))); } } this.GetExpression = String.Format("{0} WHERE {1}", this.SelectExpression, whereStr.ToString()); }
public void GenerateUpdateExpression() { StringBuilder columnStr = new StringBuilder(); StringBuilder whereStr = new StringBuilder(); int i = 0; foreach (ColumnMapping column in Columns) { if (column.IsPk || column.IsPartitionKey) { if (whereStr.Length > 0) { whereStr.Append(" AND "); } whereStr.Append(String.Format("{0} = {1}", DataMapper.EscapeName(column.ColumnName), DataMapper.ParamName(i++))); } else { columnStr.Append(String.Format("{0} = {1},", DataMapper.EscapeName(column.ColumnName), DataMapper.ParamName(i++))); } } this.UpdateExpression = String.Format("UPDATE {0} SET {1} WHERE {2};", DataMapper.EscapeName(this.TableName), columnStr.ToString().TrimEnd(new char[] { ',' }), whereStr.ToString()); }
public void GenerateDeleteExpression() { StringBuilder whereStr = new StringBuilder(); int i = 0; foreach (ColumnMapping column in Columns.Where(c => c.IsPk)) { if (whereStr.Length > 0) { whereStr.Append(" AND "); } whereStr.Append(String.Format("{0} = {1}", DataMapper.EscapeName(column.ColumnName), DataMapper.ParamName(i++))); } this.DeleteExpression = String.Format("DELETE FROM {0} WHERE {1};", DataMapper.EscapeName(this.TableName), whereStr.ToString()); }
protected virtual void AddParameter(StringBuilder sb, string name, object value, Type type, QueryParameter data = null) { // Is this an enumerable type that needs to be split into separate variables? bool isEnumerable = typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(String); if (isEnumerable) { sb.Append("("); } if (isEnumerable && value != null) { // If the value is enumerable int i = 0; foreach (object val in (IEnumerable)value) { string paramName = String.IsNullOrEmpty(name) ? DataMapper.ParamName(Parameters.Count, true) : DataMapper.ParamName(String.Format("{0}{1}", name, i)); if (++i > 1) { sb.Append(","); } sb.Append(paramName); Parameters.Add(paramName, new QueryParameter { Value = val }); } } else { if (data != null && value != null && !String.IsNullOrEmpty(data.AppendStart) && value.GetType() == typeof(String)) { value = String.Format("{0}{1}", data.AppendStart, value); } if (data != null && value != null && !String.IsNullOrEmpty(data.AppendEnd) && value.GetType() == typeof(String)) { value = String.Format("{0}{1}", value, data.AppendEnd); } string paramName = String.IsNullOrEmpty(name) ? DataMapper.ParamName(Parameters.Count, true) : DataMapper.ParamName(name); sb.Append(paramName); if (data == null) { data = new QueryParameter { Value = value } } ; else { data.Value = value; } Parameters.Add(paramName, data); } if (isEnumerable) { sb.Append(")"); } }
/// <summary> /// Replace a parameter when a compiled query is executed. /// </summary> /// <param name="number"></param> /// <param name="value"></param> /// <returns></returns> protected QueryBuilder <T> ReplaceParameter(string name, object value) { if (value is IEnumerable && value.GetType() != typeof(String)) { StringBuilder sb = new StringBuilder(); int i = 0; foreach (object val in (IEnumerable)value) { string paramName = DataMapper.ParamName(String.Format("{0}{1}", name, i++)); if (i > 1) { sb.Append(","); } sb.Append(paramName); // Add or replace the new parameter. if (Parameters.ContainsKey(paramName)) { Parameters[paramName].Value = val; } else { Parameters.Add(paramName, new QueryParameter { Value = val }); } } string baseParamName = DataMapper.ParamName(name); if (Parameters.ContainsKey(baseParamName)) { // Replace the single parameter in the WHERE and UPDATE clauses with the parameter list. this.WhereStr.Replace(baseParamName, sb.ToString()); this.UpdateStr.Replace(baseParamName, sb.ToString()); // Remove the single placeholder parameter. Parameters.Remove(baseParamName); } } else { string paramName = DataMapper.ParamName(name); if (Parameters.ContainsKey(paramName)) { QueryParameter parameter = Parameters[paramName]; if (value != null && !String.IsNullOrEmpty(parameter.AppendStart) && value.GetType() == typeof(String)) { value = String.Format("{0}{1}", parameter.AppendStart, value); } if (value != null && !String.IsNullOrEmpty(parameter.AppendEnd) && value.GetType() == typeof(String)) { value = String.Format("{0}{1}", value, parameter.AppendEnd); } parameter.Value = value; } } return(this); }