Exemplo n.º 1
0
        private void SaveOrUpdate <T>(ActionType actionType, string[] columns, object[] values)
        {
            Type     entity    = typeof(T);
            List <T> list      = new List <T>();
            string   tableName = AttributeInfo.GetTableName(entity);

            try
            {
                using (var em = EntityManagerFactory.CreateInstance(dataSource))
                {
                    Query q = null;

                    if (actionType == ActionType.Save)
                    {
                        q = new Query().Select(columns).From(tableName).Insert(values);
                    }
                    else
                    {
                        string id = values[0].ToString();

                        q = new Query().Select(columns).From(tableName).Update(values)
                            .Where(GetQueryClause(em.DataSource, entity, id));
                    }

                    string sql = q.ToSql();

                    em.ExecuteNonQuery(sql);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
Exemplo n.º 2
0
 public object ExecuteScalar(string sql)
 {
     using (var em = EntityManagerFactory.CreateInstance(dataSource))
     {
         return(em.ExecuteScalar(sql));
     }
 }
Exemplo n.º 3
0
 public void ExecuteNonQuery(string sql)
 {
     using (var em = EntityManagerFactory.CreateInstance(dataSource))
     {
         em.ExecuteNonQuery(sql);
     }
 }
Exemplo n.º 4
0
        public IDataReader ExecuteReader(string sql)
        {
            IDataReader rdr = null;

            var em = EntityManagerFactory.CreateInstance(dataSource);

            rdr = em.ExecuteReader(sql);

            return(rdr);
        }
Exemplo n.º 5
0
        public List <T> GetByQuery <T>(string sql)
        {
            List <T> list = new List <T>();

            try
            {
                using (var em = EntityManagerFactory.CreateInstance(dataSource))
                {
                    list = em.ExecuteList <T>(sql);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(list);
        }
Exemplo n.º 6
0
        public void Delete <T, P>(P id)
        {
            Type   entity    = typeof(T);
            string tableName = AttributeInfo.GetTableName(entity);

            try
            {
                using (var em = EntityManagerFactory.CreateInstance(dataSource))
                {
                    var    q   = new Query().From(tableName).Delete().Where(GetQueryClause(em.DataSource, entity, id));
                    string sql = q.ToSql().Substring(6);

                    em.ExecuteNonQuery(sql);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
Exemplo n.º 7
0
        public List <T> GetAll <T>(string orderClause)
        {
            Type     entity    = typeof(T);
            List <T> list      = new List <T>();
            string   tableName = AttributeInfo.GetTableName(entity);

            try
            {
                using (var em = EntityManagerFactory.CreateInstance(dataSource))
                {
                    string sql = "SELECT * FROM " + tableName + " " + orderClause;
                    list = em.ExecuteList <T>(sql);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(list);
        }
Exemplo n.º 8
0
        public T GetById <T, P>(P id)
        {
            Type entity = typeof(T);
            T    obj    = default(T);

            string tableName = AttributeInfo.GetTableName(entity);

            try
            {
                using (var em = EntityManagerFactory.CreateInstance(dataSource))
                {
                    string sql = "SELECT * FROM " + tableName + " WHERE " + GetQueryClause(em.DataSource, entity, id);
                    obj = em.ExecuteObject <T>(sql);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(obj);
        }