Beispiel #1
0
        private int GetDbUpdateNumber()
        {
            System.Data.DataTable dt = null;
            try
            {
                DbBase.DbConnection.Open();
                dt = DbBase.DbConnection.GetSchema("Tables");
            }
            finally
            {
                DbBase.DbConnection.Close();
            }

            bool PossuitdUpdate = false;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["TABLE_NAME"].ToString().ToUpper() == "TB_UPDATE")
                {
                    PossuitdUpdate = true;
                    break;
                }
            }

            if (!PossuitdUpdate)
            {
                DbBase.DbExecute("CREATE TABLE TB_UPDATE (UPDATE_NUMBER INTEGER);");
                DbBase.DbExecute("INSERT INTO TB_UPDATE (UPDATE_NUMBER) VALUES (0);");
            }

            return(cnv.ToInt(DbBase.DbGetValue("SELECT UPDATE_NUMBER FROM TB_UPDATE")));
        }
Beispiel #2
0
        public T[] List <T>(DbDataReader dr, List <string> columns = null)
        {
            List <T> list = new List <T>();

            while (dr.Read())
            {
                T md = Activator.CreateInstance <T>();
                lib.Class.Reflection rfc  = new lib.Class.Reflection(md);
                string[]             cols = rfc.GetProperties();

                for (int i = 0; i < cols.Length; i++)
                {
                    int ordinal = -1;
                    if (columns == null)
                    {
                        ordinal = dr.GetOrdinal(cols[i]);
                    }
                    else
                    {
                        ordinal = columns.IndexOf(cols[i].ToUpper());
                    }

                    if (ordinal >= 0)
                    {
                        string columnName  = cols[i];
                        object columnValue = dr.GetValue(ordinal);
                        rfc.SetAttibute(columnName, columnValue);
                    }
                }
                list.Add(md);
                DbBase.OnLineCounter();
            }
            return(list.ToArray());
        }
Beispiel #3
0
 public int Count <T>(DbTransaction Transaction = null)
 {
     try
     {
         return(Convert.ToInt32(DbBase.DbGetValue(string.Format("SELECT COUNT(*) FROM {0} {1}", typeof(T).Name, DbBase.DbWithNolock), Transaction)));
     }
     catch { return(0); }
 }
Beispiel #4
0
        public void Insert <T>(T entity, DbTransaction Transaction = null)
        {
            lib.Class.Reflection r = new lib.Class.Reflection(entity);

            if (r.Properties.Length == 0)
            {
                return;
            }

            string db_fields = "";

            for (int i = 0; i < r.Properties.Length; i++)
            {
                KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]);
                bool IsDefaultValue         = r.IsDefaultValue(r.Properties[i]);

                if (state.IsQueryField || (IsDefaultValue && state.IsOmitIfIsNull))
                {
                    continue;
                }

                if (!state.IsPrimaryKey && !(state.IsForeignKey && IsDefaultValue))
                {
                    db_fields += (string.IsNullOrEmpty(db_fields) ? "" : ",") + r.Properties[i].Name;
                }
            }

            string db_values = "";

            for (int i = 0; i < r.Properties.Length; i++)
            {
                KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]);
                bool IsDefaultValue         = r.IsDefaultValue(r.Properties[i]);

                if (state.IsQueryField || (IsDefaultValue && state.IsOmitIfIsNull))
                {
                    continue;
                }

                if (!state.IsPrimaryKey && !(state.IsForeignKey && IsDefaultValue))
                {
                    db_values += (string.IsNullOrEmpty(db_values) ? "" : ",") + GetDbValue(r, r.Properties[i], false);
                }
            }

            string sql = string.Format("{0} INSERT INTO {1} ({2}) VALUES ({3});", DbBase.DbSetDateFormat, typeof(T).Name, db_fields, db_values);

            DbBase.DbExecute(sql, Transaction);
        }
Beispiel #5
0
        public void Update <T>(T entity, T Conditions, DbTransaction Transaction = null)
        {
            lib.Class.Reflection r = new lib.Class.Reflection(entity);

            if (r.Properties.Length == 0)
            {
                return;
            }

            System.Reflection.PropertyInfo[] PropsCondition = null;

            if (Conditions != null)
            {
                PropsCondition = GetValidProperties(new lib.Class.Reflection(Conditions));
            }

            string db_fields_values = "";

            for (int i = 0; i < r.Properties.Length; i++)
            {
                KeyTypeAttributeState state = GetKeyTypeAttributeState(r.Properties[i]);
                bool IsDefaultValue         = r.IsDefaultValue(r.Properties[i]);

                if (state.IsQueryField || state.IsPrimaryKey || (IsDefaultValue && state.IsOmitIfIsNull))
                {
                    continue;
                }

                if (state.IsForeignKey && IsDefaultValue)
                {
                    db_fields_values += (string.IsNullOrEmpty(db_fields_values) ? "" : ",") + string.Format("{0}=NULL", r.Properties[i].Name);
                }
                else
                {
                    db_fields_values += (string.IsNullOrEmpty(db_fields_values) ? "" : ",") + string.Format("{0}={1}", r.Properties[i].Name, GetDbValue(r, r.Properties[i], false));
                }
            }

            DbBase.DbExecute(
                string.Format("{0} UPDATE {1} SET {2} {3}", DbBase.DbSetDateFormat, typeof(T).Name, db_fields_values, Condition(Conditions)),
                Transaction);
        }
Beispiel #6
0
 public DefaultDataSource(DbBase DbBase)
     : base(DbBase)
 {
 }
Beispiel #7
0
 public UpdateScript(string FileName, DbBase DbBase)
 {
     this.cnv      = new lib.Class.Conversion();
     this.FileName = FileName;
     this.DbBase   = DbBase;
 }
Beispiel #8
0
        public void Execute()
        {
            lib.Class.TextFile tf = new lib.Class.TextFile();
            tf.Open(lib.Class.enmOpenMode.Reading, FileName);
            string[] lines = tf.GetLines();
            tf.Close();

            #region Gera a lista de comandos
            List <ItemUpdate> itms = new List <ItemUpdate>();
            for (int i = 0; i < lines.Length; i++)
            {
                try
                {
                    if (string.IsNullOrEmpty(lines[i]))
                    {
                        continue;
                    }

                    string line = lines[i];

                    if (DbBase is lib.Entity.MySQL)
                    {
                        line = lines[i].ToUpper();
                    }

                    if (line.IndexOf("/*[UPDATE]") != -1)
                    {
                        int idx_ini = line.IndexOf("/*[UPDATE]") + 10;
                        int idx_end = line.IndexOf("*/");
                        itms.Add(new ItemUpdate(cnv.ToInt(line.Substring(idx_ini, idx_end - idx_ini))));
                    }
                    else
                    {
                        itms[itms.Count - 1].Script += line;
                    }
                }
                catch { continue; }
            }
            #endregion

            #region Executa atualização de Scripts
            int UpdateNumber = GetDbUpdateNumber();
            for (int i = 0; i < itms.Count; i++)
            {
                if (itms[i].Number > UpdateNumber)
                {
                    try
                    {
                        string[] cmds = itms[i].GetCommands();
                        for (int c = 0; c < cmds.Length; c++)
                        {
                            if (string.IsNullOrEmpty(cmds[c]))
                            {
                                continue;
                            }
                            DbBase.DbExecute(cmds[c]);
                        }
                    }
                    catch (Exception ex)
                    {
                        string mensagem = ex.Message;
                        mensagem = mensagem + "";
                    }

                    DbBase.DbExecute("UPDATE TB_UPDATE SET UPDATE_NUMBER = " + itms[i].Number);
                }
            }
            #endregion
        }
Beispiel #9
0
 public void Remove <T>(T Conditions, DbTransaction Transaction = null)
 {
     DbBase.DbExecute(string.Format("{0} DELETE FROM {1} {2}", DbBase.DbSetDateFormat, typeof(T).Name, Condition(Conditions)), Transaction);
 }
Beispiel #10
0
 public void RemoveAllData <T>(DbTransaction Transaction = null)
 {
     DbBase.DbExecute(string.Format("DELETE FROM {0}", typeof(T).Name), Transaction);
 }
Beispiel #11
0
 protected string DbQuoted(string s)
 {
     return(DbBase.DbQuoted(s));
 }
Beispiel #12
0
 public lib.Class.Conversion ReturnLastID(System.Data.Common.DbTransaction transaction)
 {
     return(DbBase.ReturnLastID(transaction));
 }
Beispiel #13
0
 public SimpleDataSource(DbBase DbBase)
 {
     this.cnv    = new Class.Conversion();
     this.DbBase = DbBase;
 }