static void Main(string[] args) { QueryExpression exp1 = new StringQueryExpression("identity_code", Operator.Equals, "C"); QueryExpression exp2 = new StringQueryExpression("identity_code", Operator.NotEqual, "CE"); QueryExpression exp3 = new StringQueryExpression("last_name", Operator.BeginsWith, "Sick"); QueryExpression exp4 = new NumericQueryExpression("number_of_logins", Operator.GreaterThan, 5); NullQueryExpression exp5 = new NullQueryExpression("identity_code"); QueryObject q = new QueryObject("identities"); q.Conditions = (!exp1 | !exp2 & exp3 | exp4) & (!exp3 & exp4) | !exp5; q.Sorts.Add(new SortObject("identity_code", SortDirection.Descending)); q.Sorts.Add(new SortObject("last_name")); q.SetPaging(50, 0); Console.WriteLine(MySqlQueryObjectParser.ToCommand(new MySqlConnection(), "identities", q).CommandText); Console.ReadLine(); }
public static MySqlCommand ToCommand(MySqlConnection Connection, string TableName, QueryObject QueryObject) { Dictionary<string, object> parameters = new Dictionary<string,object>(); StringBuilder sb = new StringBuilder(); sb.Append("SELECT * FROM "); sb.Append(TableName); if (QueryObject.Conditions != null) { sb.Append(" WHERE "); sb.Append(ToCommand(QueryObject.Conditions, parameters)); } if (QueryObject.Sorts.Count > 0) { sb.Append(" ORDER BY "); foreach (SortObject s in QueryObject.Sorts) { sb.Append(string.Format("{0} {1}", s.FieldName, s.Direction)); sb.Append(", "); } sb.Remove(sb.Length - 2, 2); } if (QueryObject.Offset >= 0 && QueryObject.PageSize > 0) { sb.Append(" LIMIT "); sb.Append(QueryObject.Offset); sb.Append(", "); sb.Append(QueryObject.PageSize); } sb.Append(";"); MySqlCommand com = new MySqlCommand(sb.ToString(), Connection); foreach (string field in parameters.Keys) { com.Parameters.AddWithValue(field, parameters[field]); } return com; }