Esempio n. 1
0
        /// <summary>
        /// Update the specified table, using anonymous objects as parameters
        /// </summary>
        /// <param name="table">Table.</param>
        /// <param name="data">Data.</param>
        /// <param name="wheres">Wheres.</param>
        public int Update(string table, object data, object wheres)
        {
            string query    = "UPDATE " + table + " SET ";
            var    dataType = data.GetType();

            if (dataType == typeof(Hashtable))
            {
                foreach (string col in (data as Hashtable).Keys)
                {
                    query += string.Format("{0} = {1},", col, this.FormatValue((data as Hashtable)[col]));
                }
            }
            else if (dataType.BaseType == typeof(Entity))
            {
                var fields = data.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
                foreach (var field in fields)
                {
                    object[] attributes = field.GetCustomAttributes(true);
                    if (attributes.Length <= 0)
                    {
                        continue;
                    }
                    if (attributes[0].GetType() != typeof(EntityAttribute))
                    {
                        continue;
                    }
                    EntityAttribute attr = (EntityAttribute)attributes[0];

                    object the_value = field.GetValue(data);
                    query += string.Format("{0} = {1},", field.Name, this.FormatValue(the_value));
                }
            }
            else
            {
                var properties = data.GetType().GetProperties();
                if (properties.Length > 0)
                {
                    foreach (PropertyInfo col in properties)
                    {
                        object the_value = col.GetValue(data, null);
                        query += string.Format("{0} = {1},", col.Name, this.FormatValue(the_value));
                    }
                }
            }
            query  = query.Substring(0, query.Length - 1);
            query += " WHERE ";

            //build where
            if (wheres.GetType() == typeof(Hashtable))
            {
                foreach (string col in (wheres as Hashtable).Keys)
                {
                    query += string.Format("{0} = {1} AND ", col, this.FormatValue((wheres as Hashtable)[col]));
                }
            }
            else
            {
                foreach (PropertyInfo col in wheres.GetType().GetProperties())
                {
                    object the_value = col.GetValue(wheres, null);
                    query += string.Format("{0} = {1} AND ", col.Name, this.FormatValue(the_value));
                }
            }

            query          = query.Substring(0, query.LastIndexOf("AND"));
            this.LastQuery = query;

            return(this.Execute(query));
        }
Esempio n. 2
0
        public virtual int Insert(string table, object data)
        {
            string columns  = "";
            string values   = "";
            var    dataType = data.GetType();

            if (dataType == typeof(Hashtable))
            {
                foreach (string col in (data as Hashtable).Keys)
                {
                    //build columns
                    if (this.db_type == "mysql")
                    {
                        columns += String.Format("`{0}`,", col);
                    }
                    else if (this.db_type == "sqlite" || this.db_type == "sqlite3")
                    {
                        columns += String.Format("[{0}],", col);
                    }
                    else
                    {
                        columns += String.Format("{0},", col);
                    }
                    //build values
                    values += this.FormatValue((data as Hashtable)[col]) + ",";
                }
            }
            else if (dataType.BaseType == typeof(Entity))
            {
                FieldInfo[] fields = data.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
                foreach (var col in fields)
                {
                    object[] attributes = col.GetCustomAttributes(true);
                    if (attributes.Length <= 0)
                    {
                        continue;
                    }
                    if (attributes[0].GetType() != typeof(EntityAttribute))
                    {
                        continue;
                    }
                    EntityAttribute attr = (EntityAttribute)attributes[0];
                    //build columns
                    if (this.db_type == "mysql")
                    {
                        columns += String.Format("`{0}`,", col.Name);
                    }
                    else if (this.db_type == "sqlite" || this.db_type == "sqlite3")
                    {
                        columns += String.Format("[{0}],", col.Name);
                    }
                    else
                    {
                        columns += String.Format("{0},", col.Name);
                    }
                    //build values
                    object the_value = col.GetValue(data);
                    //	(col as FieldInfo).GetValue(data);
                    values += attr.PrimaryKey ? "NULL," : this.FormatValue(the_value) + ",";
                }
            }
            else
            {
                PropertyInfo[] properties = data.GetType().GetProperties();
                if (properties.Length > 0)
                {
                    foreach (var col in properties)
                    {
                        //build columns
                        if (this.db_type == "mysql")
                        {
                            columns += String.Format("`{0}`,", col.Name);
                        }
                        else if (this.db_type == "sqlite" || this.db_type == "sqlite3")
                        {
                            columns += String.Format("[{0}],", col.Name);
                        }
                        else
                        {
                            columns += String.Format("{0},", col.Name);
                        }
                        //build values
                        object the_value = col.GetValue(data, null);
                        //	(col as FieldInfo).GetValue(data);
                        values += this.FormatValue(the_value) + ",";
                    }
                }
            }
            columns = columns.Substring(0, columns.Length - 1);
            values  = values.Substring(0, values.Length - 1);

            string query = String.Format("INSERT INTO {0}({1}) VALUES({2})", table, columns, values);
            int    res   = 0;

            try
            {
                this.LastQuery = query;
                res            = this.Execute(query);
            }
            catch (DataException dbex)
            {
                throw new Exception("DATABASE ERROR: " + dbex.Message + ", QUERY WAS: " + query);
            }
            catch (Exception ex)
            {
                string error = "ERROR: " + ex.Message + ", QUERY WAS: " + query;
                Console.WriteLine(ex.StackTrace);
                throw new Exception(error);
            }
            return(res);
        }