示例#1
0
        // Delete by Attributes
        public int RemoveByKey(Dictionary <string, object> key)
        {
            int result = 0;

            Stopwatch tw = new Stopwatch();

            tw.Start();

            log.Info(string.Format("Starting Deleting procedure ..."));

            string table = this.DBTabName;

            try
            {
                string connectionString = this.DBConnectionString;

                // DELETE
                if (key != null)
                {
                    int  i       = 0;
                    bool hasNext = true;
                    Dictionary <string, DBSQL.QueryCondition> conditions = new Dictionary <string, DBSQL.QueryCondition>();
                    foreach (KeyValuePair <string, object> kvPair in key)
                    {
                        if (i == key.Count - 1)
                        {
                            hasNext = false;
                        }
                        string condName = "cond" + i;
                        DBSQL.QueryCondition condValue = new DBSQL.QueryCondition()
                        {
                            Key   = kvPair.Key,
                            Value = kvPair.Value,
                            Op    = DBSQL.Op.Equal,
                            Conj  = hasNext ? DBSQL.Conj.And : DBSQL.Conj.None,
                        };
                        conditions.Add(condName, condValue);
                        i++;
                    }
                    result = DBSQL.DeleteOperation(connectionString, table, conditions);
                }

                log.Info(string.Format("Deleted {0} records!", result));
            }
            catch (Exception ex)
            {
                string msg = "An Error occured! Exception detected!";
                log.Info(msg);
                throw ex;
            }
            finally
            {
                tw.Stop();
                log.Info(string.Format("Completed! Elapsed time {0}", LibString.TimeSpanToTimeHmsms(tw.Elapsed)));
            }

            return(result);
        }
示例#2
0
        // Delete by Entities
        public int Remove(List <T> items)
        {
            int result = 0;

            Stopwatch tw = new Stopwatch();

            tw.Start();

            log.Info(string.Format("Starting Deleting procedure ..."));

            if (items == null || items.Count == 0)
            {
                throw new ArgumentNullException("Unable to delete NULL entities!");
            }

            string table = this.DBTabName;

            try
            {
                string connectionString = this.DBConnectionString;
                if (primaryKey == null)
                {
                    throw new Exception(string.Format("No Primary Key has been defined for the entity '{0}'. Use a removing procedure based on conditions!", typeof(T).Name));
                }

                List <Dictionary <string, object> > keys = new List <Dictionary <string, object> >();
                foreach (T item in items)
                {
                    Dictionary <string, object> key = new Dictionary <string, object>();
                    foreach (string k in this.primaryKey)
                    {
                        object v = item.GetType().GetProperty(k).GetValue(item, null);
                        key.Add(k, v);
                    }
                    keys.Add(key);
                }

                // DELETE
                if (keys != null)
                {
                    int i = 0;
                    Dictionary <string, DBSQL.QueryCondition> conditions = new Dictionary <string, DBSQL.QueryCondition>();
                    foreach (Dictionary <string, object> key in keys)
                    {
                        foreach (KeyValuePair <string, object> kvPair in key)
                        {
                            int  j       = 0;
                            bool hasNext = true;
                            if (i == keys.Count - 1)
                            {
                                if (j == key.Count - 1)
                                {
                                    hasNext = false;
                                }
                            }

                            string condName = "cond" + i;
                            DBSQL.QueryCondition condValue = new DBSQL.QueryCondition()
                            {
                                Key   = kvPair.Key,
                                Value = kvPair.Value,
                                Op    = DBSQL.Op.Equal,
                                Conj  = hasNext ? DBSQL.Conj.And : DBSQL.Conj.None,
                            };
                            conditions.Add(condName, condValue);
                            i++;
                            j++;
                        }
                    }
                    result = DBSQL.DeleteOperation(connectionString, table, conditions);
                }

                log.Info(string.Format("Deleted {0} records!", result));
            }
            catch (Exception ex)
            {
                string msg = "An Error occured! Exception detected!";
                log.Info(msg);
                throw ex;
            }
            finally
            {
                tw.Stop();
                log.Info(string.Format("Completed! Elapsed time {0}", LibString.TimeSpanToTimeHmsms(tw.Elapsed)));
            }

            return(result);
        }
示例#3
0
        // Update
        public T Update(T item)
        {
            T written = default(T);

            Stopwatch tw = new Stopwatch();

            tw.Start();

            log.Info(string.Format("Starting Updating procedure ..."));

            if (item == null)
            {
                throw new ArgumentNullException("Unable to update NULL entity!");
            }

            string table = this.DBTabName;

            try
            {
                string connectionString = this.DBConnectionString;
                if (primaryKey == null)
                {
                    throw new Exception(string.Format("No Primary Key has been defined for the entity '{0}'. Use an updating procedure with declared conditions!", typeof(T).Name));
                }
                int  i       = 0;
                bool hasNext = true;
                Dictionary <string, DBSQL.QueryCondition> conditions = new Dictionary <string, DBSQL.QueryCondition>();
                foreach (string k in primaryKey)
                {
                    object v = item.GetType().GetProperty(k).GetValue(item, null);

                    if (i == primaryKey.Count - 1)
                    {
                        hasNext = false;
                    }
                    string condName = "cond" + i;
                    DBSQL.QueryCondition condValue = new DBSQL.QueryCondition()
                    {
                        Key   = k,
                        Value = v,
                        Op    = DBSQL.Op.Equal,
                        Conj  = hasNext ? DBSQL.Conj.And : DBSQL.Conj.None,
                    };
                    conditions.Add(condName, condValue);
                    i++;
                }
                int result = DBSQL.UpdateOperation(connectionString, table, item, conditions, this.autoincrement);
                log.Info(string.Format("Updated {0} records!", result));

                //Retrieve Item By Condition to give it back as result!
            }
            catch (Exception ex)
            {
                string msg = "An Error occured! Exception detected!";
                log.Info(msg);
                throw ex;
            }
            finally
            {
                tw.Stop();
                log.Info(string.Format("Completed! Elapsed time {0}", LibString.TimeSpanToTimeHmsms(tw.Elapsed)));
            }

            return(written);
        }