예제 #1
0
파일: DbFactory.cs 프로젝트: aooshi/adf
        /// <summary>
        /// 根据对象创建参数列表,使用<see cref="ParameterChar"/>所指定的参数前缀
        /// </summary>
        /// <param name="entity">对象</param>
        protected virtual IDbDataParameter[] CreateParameter(DbEntity entity)
        {
            int i    = 0;
            int size = entity.GetInitializePropertyCount();

            Dictionary <string, object> .Enumerator etor = entity.GetEnumerator();
            IDbDataParameter[] parameters = new IDbDataParameter[size];
            while (etor.MoveNext())
            {
                parameters[i] = this.CreateParameter(string.Concat(ParameterChar, etor.Current.Key), etor.Current.Value);
                i++;
            }
            return(parameters);

            //Dictionary<string, object>.Enumerator etor = entity.GetEnumerator();
            //List<IDbDataParameter> list = new List<IDbDataParameter>();

            //if (entity.IsSetFieldLength())
            //{
            //    while (etor.MoveNext())
            //        list.Add(this.CreateParameter(string.Concat(ParameterChar, etor.Current.Key), etor.Current.Value,entity.GetFieldLength(etor.Current.Key)));
            //}
            //else
            //{
            //while (etor.MoveNext())
            //    list.Add(this.CreateParameter(string.Concat(ParameterChar, etor.Current.Key), etor.Current.Value));
            //}

            //return list;
        }
예제 #2
0
        /// <summary>
        /// get insert sql
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="parameters"></param>
        public virtual String GetInsert(DbEntity entity, out IDbDataParameter[] parameters)
        {
            parameters = null;
            var parameterList = new List <IDbDataParameter>(entity.GetInitializePropertyCount());

            var filed  = new StringBuilder();
            var values = new StringBuilder();

            //object property
            Dictionary <string, object> .Enumerator etor = entity.GetEnumerator();
            //Type type;
            while (etor.MoveNext())
            {
                filed.Append("," + etor.Current.Key);
                values.AppendFormat(",{0}{1}", Factory.ParameterChar, etor.Current.Key);
                parameterList.Add(Factory.CreateParameter(etor.Current.Key, etor.Current.Value));
            }

            parameters = parameterList.ToArray();

            //remove comma
            if (filed.Length > 0)
            {
                filed  = filed.Remove(0, 1);
                values = values.Remove(0, 1);
            }
            else
            {
                return(null);
            }

            return(string.Format("INSERT INTO {0} ({1}) VALUES ({2})", entity.GetTableName(), filed.ToString(), values.ToString()));
        }
예제 #3
0
        /// <summary>
        /// get update sql
        /// </summary>
        /// <param name="where">更新条件</param>
        /// <param name="update"></param>
        /// <param name="parameters"></param>
        public virtual String GetUpdate(DbEntity update, DbEntity where, out IDbDataParameter[] parameters)
        {
            parameters = null;
            var parameterList = new List <IDbDataParameter>(update.GetInitializePropertyCount() + where.GetInitializePropertyCount());

            StringBuilder tmp = new StringBuilder();

            //object property
            Dictionary <string, object> .Enumerator etor = update.GetEnumerator();
            //Type type;
            while (etor.MoveNext())
            {
                tmp.AppendFormat(",{0}={1}{0}", etor.Current.Key, Factory.ParameterChar);
                parameterList.Add(Factory.CreateParameter(etor.Current.Key, etor.Current.Value));
            }
            string result = null;

            if (tmp.Length > 0)
            {
                IDbDataParameter[] whereParameters = null;
                result = string.Format("UPDATE {0} SET {1}{2}", update.GetTableName(), tmp.Remove(0, 1).ToString(), this.GetWhere(where, out whereParameters));
                if (whereParameters != null)
                {
                    parameterList.AddRange(whereParameters);
                }
            }
            parameters = parameterList.ToArray();
            return(result);
        }
예제 #4
0
        /// <summary>
        /// get object where
        /// </summary>
        /// <param name="where">指定的条件生成对象</param>
        /// <param name="parameters"></param>
        public virtual String GetWhere(DbEntity where, out IDbDataParameter[] parameters)
        {
            var i = 0;

            parameters = new IDbDataParameter[where.GetInitializePropertyCount()];
            string selectRelation = where.GetWhereRelation() == WhereRelation.AND ? " AND " : " OR ";

            string[] selectRelations = new string[parameters.Length];
            var      etor            = where.GetEnumerator();

            //
            while (etor.MoveNext())
            {
                selectRelations[i] = string.Format("{0}={1}{0}", etor.Current.Key, Factory.ParameterChar);
                parameters[i]      = Factory.CreateParameter(etor.Current.Key, etor.Current.Value);
                i++;
            }
            //
            if (i > 0)
            {
                return(string.Concat(" WHERE ", string.Join(selectRelation, selectRelations)));
            }

            return(string.Empty);
        }
예제 #5
0
        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <param name="where">指定的条件生成对象</param>
        /// <param name="parameters"></param>
        public override String GetWhere(DbEntity where, out IDbDataParameter[] parameters)
        {
            var    parameterList  = new List <IDbDataParameter>(where.GetInitializePropertyCount());
            string selectRelation = where.GetWhereRelation() == WhereRelation.AND ? " AND" : " OR";
            var    etor           = where.GetEnumerator();

            var build = new StringBuilder();

            //Type type;
            while (etor.MoveNext())
            {
                build.Append(selectRelation);

                if (etor.Current.Value != null && etor.Current.Value is DateTime)
                {
                    build.AppendFormat("{0}=#{1}#", etor.Current.Key, etor.Current.Value);
                }
                else
                {
                    build.AppendFormat("{0}={1}{0}", etor.Current.Key, Factory.ParameterChar);
                    parameterList.Add(Factory.CreateParameter(etor.Current.Key, etor.Current.Value));
                }
            }

            parameters = parameterList.ToArray();

            if (build.Length > 0)
            {
                return(string.Concat(" WHERE ", build.Remove(0, selectRelation.Length).ToString()));
            }

            return(string.Empty);
        }