DBObject.Where

Ok it works. I also created a checker for the opertators so we don’t get invalid/malicious code.

 ///
        /// Returns a DBList of objects that match the Where
        ///
        ///
Property to Match        ///
"=", "<", ">", "LIKE"        ///
Value to Match        ///
Type of the object to get (Must inherent from DBOBject)        ///
Connection String to use        ///
        static protected DBList Where(string property, string evaluator, object value, Type who_type, string connection)
        {
            DBList list = new DBList();

            //Find the Column Name and check that we have everything, if fail return the blank list
            string column_name = DBObject.GetColumn(property, who_type);
            if (column_name == string.Empty || evaluator == string.Empty || value == null) { return list; }
            //Check the evaluator, If fail, Return a blank list
            evaluator = DBObject.CheckEvaluator(evaluator);
            if (evaluator == string.Empty) { return list; }

            //Create the WHERE
            string query = DBObject.SelectQuery(who_type) + " WHERE " + column_name + " " + evaluator + "?value";

            using (MySqlConnection conn = new MySqlConnection(connection))
            {
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("?value", value);

                try
                {
                    conn.Open();
                    MySqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        DBObject obj = (DBObject)DBObject.FromReader(reader, who_type);
                        list.Add( obj );
                    }

                    conn.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("nnERROR:n" + ex.Message);
                }
            }

            //No matter what happens, always return a valid list
            return list;
        }

///
        /// Accepted Evaluators: '<', '<=', '>', '>=', '=', '!=', 'LIKE', 'REGEXP', 'IS NOT'
        ///
        ///
        ///
        static protected string CheckEvaluator(string evaluator)
        {
            //Lower and trim
            evaluator = evaluator.ToLower().Trim();

            switch (evaluator)
            {
                case "<":
                    return evaluator;
                    break;
                case "<=":
                    return evaluator;
                    break;
                case ">":
                    return evaluator;
                    break;
                case ">=":
                    return evaluator;
                    break;
                case "=":
                    return evaluator;
                    break;
                case "!=":
                    return evaluator;
                    break;
                case "like":
                    return evaluator;
                    break;
                case "regexp":
                    return evaluator;
                    break;
                case "is not":
                    return evaluator;
                    break;
                default:
                    return string.Empty;
            }
        }

And an example:

//Now lets try to get a list of users
            Console.WriteLine("nnTrying the Where Methiod");
            DBList users1 = DBObject.Where("id", "<=", 1313, typeof(DBUser), Utility.connMy);
            foreach (DBUser u4 in users1)
            {
                Console.WriteLine(u4.ID + " : " + u4.FullName);
            }
Share

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled