コード例 #1
0
        public static String GetSameTypeIds(Type throughType, Type t, int id)
        {
            // 1029
            ObjectInfo         state = new ObjectInfo(throughType);
            String             relationPropertyName = state.EntityInfo.GetRelationPropertyName(t);
            EntityPropertyInfo info     = state.EntityInfo.FindRelationProperty(t);
            String             ids      = ObjectDB.Find(state, relationPropertyName + ".Id=" + id).get(info.Name + ".Id");
            EntityPropertyInfo property = state.EntityInfo.GetProperty(relationPropertyName);


            String sql = String.Format("select distinct {0} from {1} where {2} in ({3}) and {0}<>{4}", property.ColumnName, state.EntityInfo.TableName, info.ColumnName, ids, id);

            IDbCommand    command = DataFactory.GetCommand(sql, DbContext.getConnection(state.EntityInfo));
            IDataReader   rd      = null;
            StringBuilder builder = new StringBuilder();

            try {
                rd = command.ExecuteReader();
                while (rd.Read())
                {
                    builder.Append(rd[0]);
                    builder.Append(",");
                }
            }
            catch (Exception exception) {
                logger.Error(exception.Message);
                throw new OrmException(exception.Message, exception);
            }
            finally {
                OrmHelper.CloseDataReader(rd);
            }
            return(builder.ToString().TrimEnd(','));
        }
コード例 #2
0
        internal static IList Find(ConditionInfo condition)
        {
            IList results = ObjectPool.FindByQuery(condition);

            if (results != null)
            {
                return(results);
            }

            if ("*".Equals(condition.SelectedItem))
            {
                condition.State.includeAll();
            }
            else
            {
                condition.State.include(SqlBuilder.GetIncludeProperty(condition.SelectedItem));
            }

            IList      includeEntityPropertyList = condition.State.Includer.EntityPropertyList;
            IDbCommand cmd = DataFactory.GetCommand(condition.Sql, DbContext.getConnection(condition.State.EntityInfo));

            foreach (String key in condition.Parameters.Keys)
            {
                DataFactory.SetParameter(cmd, key, condition.Parameters[key]);
            }

            Hashtable   hashtable = new Hashtable();
            IDataReader record    = null;

            results = new ArrayList();
            try {
                record = cmd.ExecuteReader();
                while (record.Read())
                {
                    EntityPropertyUtil.Fill_EntityProperty_Ids(record, includeEntityPropertyList, ref hashtable);
                    results.Add(FillUtil.Populate(record, condition.State));
                }
            }
            catch (Exception ex) {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                throw new OrmException(ex.Message, ex);
            }
            finally {
                OrmHelper.CloseDataReader(record);
            }

            if (results.Count == 0)
            {
                return(results);
            }

            return(EntityPropertyUtil.setEntityProperty(condition.State, results, hashtable));
        }
コード例 #3
0
        private static IEntity findById_Private(int id, ObjectInfo state)
        {
            if (id < 0)
            {
                return(null);
            }

            IEntity result = null;

            SqlBuilder sh = new SqlBuilder(state.EntityInfo);

            processIncluder(state.Includer);
            String      sql  = sh.GetFindById(id, state.Includer.SelectedProperty);
            IDbCommand  cmd  = DataFactory.GetCommand(sql, DbContext.getConnection(state.EntityInfo));
            IList       list = new ArrayList();
            IDataReader rd   = null;

            try {
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    list.Add(FillUtil.Populate(rd, state));
                }
            }
            catch (Exception ex) {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                throw ex;
            }
            finally {
                OrmHelper.CloseDataReader(rd);
            }

            if (list.Count > 0)
            {
                result = list[0] as IEntity;
            }

            result = setEntityProperty(result, id, state);

            return(result);
        }
コード例 #4
0
        public static String GetSameTypeIds(Type throughType, Type t, int id)
        {
            // 1029
            ObjectInfo         state = new ObjectInfo(throughType);
            String             relationPropertyName = state.EntityInfo.GetRelationPropertyName(t);
            EntityPropertyInfo info     = state.EntityInfo.FindRelationProperty(t);
            String             ids      = ObjectDb.Find(state, relationPropertyName + ".Id=" + id).get(info.Name + ".Id");
            EntityPropertyInfo property = state.EntityInfo.GetProperty(relationPropertyName);


            String sql = String.Format("select distinct {0} from {1} where {2} in ({3}) and {0}<>{4}", property.ColumnName, state.EntityInfo.TableName, info.ColumnName, ids, id);

            IDbCommand    command = DataFactory.GetCommand(sql, DbContext.getConnection(state.EntityInfo));
            IDataReader   rd      = null;
            StringBuilder builder = new StringBuilder();

            try {
                rd = command.ExecuteReader();
                while (rd.Read())
                {
                    builder.Append(rd[0]);
                    builder.Append(",");
                }
            }
            catch (Exception exception) {
                logger.Error(exception.Message);
                throw exception;
            }
            finally
            {
                if (command.Connection.State != ConnectionState.Closed)
                {
                    command.Connection.Close();
                    command.Connection.Dispose();
                    OrmHelper.clostCount++;
                    LogManager.GetLogger("Class:System.ORM.Operation.FindRelationOperation Method:GetSameTypeIds").Info("数据库连接已关闭【" + OrmHelper.clostCount + "】");
                }
                OrmHelper.CloseDataReader(rd);
            }
            return(builder.ToString().TrimEnd(','));
        }
コード例 #5
0
        private static IEntity findById_Private(int id, ObjectInfo state)
        {
            if (id < 0)
            {
                return(null);
            }

            IEntity result = null;

            SqlBuilder sh = new SqlBuilder(state.EntityInfo);

            processIncluder(state.Includer);
            String sql  = sh.GetFindById(id, state.Includer.SelectedProperty);
            var    conn = DbContext.getConnection(state.EntityInfo);

            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
                OrmHelper.initCount++;
                LogManager.GetLogger("Class:System.ORM.Operation.FindByIdOperation Method:findById_Private").Info("数据库连接已开启【" + OrmHelper.initCount + "】");
            }
            IDbCommand  cmd  = DataFactory.GetCommand(sql, conn);
            IList       list = new ArrayList();
            IDataReader rd   = null;

            try {
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    list.Add(FillUtil.Populate(rd, state));
                }
            }
            catch (Exception ex) {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                throw ex;
            }
            finally
            {
                if (!DbContext.shouldTransaction())
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                        OrmHelper.clostCount++;
                        LogManager.GetLogger("Class:System.ORM.Operation.FindByIdOperation Method:findById_Private").Info("数据库连接已关闭【" + OrmHelper.clostCount + "】");
                    }
                }
                OrmHelper.CloseDataReader(rd);
            }

            if (list.Count > 0)
            {
                result = list[0] as IEntity;
            }

            result = setEntityProperty(result, id, state);

            return(result);
        }
コード例 #6
0
        internal static IList Find(ConditionInfo condition)
        {
            IList results = ObjectPool.FindByQuery(condition);

            if (results != null)
            {
                return(results);
            }

            if ("*".Equals(condition.SelectedItem))
            {
                condition.State.includeAll();
            }
            else
            {
                condition.State.include(SqlBuilder.GetIncludeProperty(condition.SelectedItem));
            }

            IList      includeEntityPropertyList = condition.State.Includer.EntityPropertyList;
            IDbCommand cmd = DataFactory.GetCommand(condition.Sql, DbContext.getConnection(condition.State.EntityInfo));

            foreach (String key in condition.Parameters.Keys)
            {
                DataFactory.SetParameter(cmd, key, condition.Parameters[key]);
            }

            Hashtable   hashtable = new Hashtable();
            IDataReader record    = null;

            results = new ArrayList();
            try
            {
                record = cmd.ExecuteReader();
                while (record.Read())
                {
                    EntityPropertyUtil.Fill_EntityProperty_Ids(record, includeEntityPropertyList, ref hashtable);
                    results.Add(FillUtil.Populate(record, condition.State));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                throw ex;
            }
            finally
            {
                if (!DbContext.shouldTransaction())
                {
                    if (cmd.Connection.State != ConnectionState.Closed)
                    {
                        cmd.Connection.Close();
                        cmd.Connection.Dispose();
                        OrmHelper.clostCount++;
                        LogManager.GetLogger("Class:System.ORM.Operation.FindByOperation Method:Find").Info("数据库连接已关闭【" + OrmHelper.clostCount + "】");
                    }
                }
                OrmHelper.CloseDataReader(record);
            }

            if (results.Count == 0)
            {
                return(results);
            }

            return(EntityPropertyUtil.setEntityProperty(condition.State, results, hashtable));
        }