예제 #1
0
        /// <summary>
        /// 根据查询参数获取其中的某些值
        /// </summary>
        /// <param name="value_columns">获取值的列</param>
        /// <param name="query_param">查询参数</param>
        /// <returns></returns>
        public static T getValues <T>(List <DBColumn> value_columns, DBQueryParamList query_param)
        {
            var table = tableInfo <T>();

            if (query_param == null || query_param.count == 0)
            {
                return(default(T));
            }
            var query = new StringBuilder();

            query.Append("select ");
            foreach (DBColumn col in value_columns)
            {
                query.Append($"{DBUtil.connection.left_quote}{col.name}{DBUtil.connection.right_quote},");
            }
            if (query[query.Length - 1] == ',')
            {
                query.Remove(query.Length - 1, 1);
            }
            query.Append($" from {DBUtil.connection.left_quote}{table.table_name}{DBUtil.connection.right_quote} ");
            var query_string = new StringBuilder();
            var pm           = new List <DBParameter>();

            foreach (DBQueryParam param in query_param.pm_list)
            {
                //如果有参数传递为空值, 则代表本次查询不能查询到想要的结果(会把结果集扩大化), 此时返回为查询失败
                if (!param.isAvailableWithValue())
                {
                    return(default);
예제 #2
0
        /// <summary>
        /// 根据更新Model获取当前实例
        ///  --如果记录不存在, 返回带有更新Model内容的新实例
        ///  --如果记录存在, 返回原有实例并根据情况更新传入model中的内容
        ///  --如果返回为空值, 则表示传入的model与现有实例的某些值冲突, 无法赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="update_model"></param>
        /// <returns></returns>
        public static T instanceByUpdate <T>(UpdateModelBase update_model)
        {
            var pm       = DBQueryParamList.create();
            var table    = tableInfo <T>();
            T   instance = default(T);

            try
            {
                foreach (var col in table.primary_columns)
                {
                    pm.add(new DBQueryParam(col, update_model.getValue(col.name)));
                }
                instance = get <T>(pm);
            }
            catch { }
            if (instance == null)
            {
                instance = Activator.CreateInstance <T>();
            }
            if (update_model != null)
            {
                update_model.setValuesTo(ref instance);
            }
            try
            {
                var method = typeof(T).GetMethod("processRefFeilds");
                if (method != null)
                {
                    method.Invoke(instance, null);
                }
            }
            catch { }
            return(instance);
        }
예제 #3
0
        /// <summary>
        /// 使用当前参数创建一个查询参数列表
        /// 注: 如果查询条件不可用, 创建列表中则不会包含本参数
        /// </summary>
        /// <returns></returns>
        public DBQueryParamList createList()
        {
            DBQueryParamList list = new DBQueryParamList();

            if (isAvailable())
            {
                list.add(this);
            }
            return(list);
        }
예제 #4
0
        /// <summary>
        /// 通过查询参数获取实例
        /// </summary>
        /// <param name="query_param">查询参数</param>
        /// <returns></returns>
        public static T get <T>(DBQueryParamList query_param)
        {
            var table = tableInfo <T>();

            if (query_param == null || query_param.count == 0)
            {
                return(default(T));
            }
            var query = new StringBuilder();

            query.Append($"select * from {DBUtil.connection.left_quote}{table.table_name}{DBUtil.connection.right_quote}");
            var query_string      = new StringBuilder();
            List <DBParameter> pm = new List <DBParameter>();

            foreach (DBQueryParam param in query_param.pm_list)
            {
                //如果有参数传递为空值, 则代表本次查询不能查询到想要的结果(会把结果集扩大化), 此时返回为查询失败
                if (!param.isAvailableWithValue())
                {
                    return(default(T));
                }
                if (query_string.Length > 0)
                {
                    query_string.Append(" and ");
                }
                query_string.Append(param.queryString());
                param.setQueryParameter(ref pm);
            }
            if (query_string.Length > 0)
            {
                query.Append($" where {query_string.ToString()}");
            }
            DataTable data = DBQuery.query(query.ToString(), pm);

            try
            {
                var result = Activator.CreateInstance <T>();
                foreach (DBColumn column in table.columns)
                {
                    result.setValue(column.field_info, data.Rows[0][column.name]);
                }
                try
                {
                    FieldInfo info = result.GetType().GetField("is_new");
                    info.SetValue(result, false);
                }
                catch { }
                return(result);
            }
            catch { }
            return(default(T));
        }