Exemplo n.º 1
0
        /// <summary>
        ///  Modifying data according to modifying parameters, filtering conditions and filtering condition parameters
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="updateObjParams"></param>
        /// <param name="whereSql"></param>
        /// <param name="whereObjParams"></param>
        /// <returns></returns>
        public virtual DbEntity UpdateByParam <T>(object updateObjParams, string whereSql, object whereObjParams)
        {
            DbEntity dbEntity = null;
            Type     type     = typeof(T);

            if (updateObjParams == null)
            {
                return(dbEntity);
            }
            var attributeBuilder = new AttributeBuilder();
            var tableEntity      = attributeBuilder.GetTableInfo(type);
            List <TableColumnAttribute> updateColumns = attributeBuilder.GetColumnInfos(updateObjParams);

            if (updateColumns == null)
            {
                return(dbEntity);
            }
            dbEntity = new DbEntity()
            {
                DbType      = DbFactory.DbType,
                TableEntity = tableEntity
            };
            string        dbOperator = DbFactory.GetDbOperator();
            var           dbParams   = new List <IDbDataParameter>();
            StringBuilder sqlBuild   = new StringBuilder("update {tableName} set {updateCriteria} {whereCriteria}");

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            HandleUpdateParam(updateColumns, ref sqlBuild, ref dbParams);
            List <TableColumnAttribute> whereColumns = attributeBuilder.GetColumnInfos(whereObjParams);

            HandleWhereParam(whereSql, whereColumns, ref sqlBuild, ref dbParams);
            dbEntity.CommandText = sqlBuild.ToString();
            dbEntity.DbParams    = dbParams;
            return(dbEntity);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Querying the corresponding data according to the primary key
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryColumns"></param>
        /// <param name="whereSql"></param>
        /// <param name="whereObjParams"></param>
        /// <returns></returns>
        public virtual DbEntity QueryByParam <T>(string queryColumns, string whereSql, object whereObjParams)
        {
            DbEntity dbEntity = null;

            if (string.IsNullOrEmpty(queryColumns))
            {
                return(dbEntity);
            }
            Type type             = typeof(T);
            var  attributeBuilder = new AttributeBuilder();
            var  tableEntity      = attributeBuilder.GetTableInfo(type);

            dbEntity = new DbEntity()
            {
                DbType      = DbFactory.DbType,
                TableEntity = tableEntity
            };
            List <TableColumnAttribute> whereColumns = attributeBuilder.GetColumnInfos(whereObjParams);
            string        dbOperator = DbFactory.GetDbOperator();
            var           dbParams   = new List <IDbDataParameter>();
            StringBuilder sqlBuild   = new StringBuilder("select {queryColumns} from {tableName} {whereCriteria}");

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            HandleQuerColumns(queryColumns, "", ref sqlBuild, ref dbParams);
            HandleWhereParam(whereSql, whereColumns, ref sqlBuild, ref dbParams);
            dbEntity.CommandText = sqlBuild.ToString();
            dbEntity.DbParams    = dbParams;
            return(dbEntity);
        }
Exemplo n.º 3
0
        /// <summary>
        ///  Modifying the corresponding data based on the modified parameters and primary key values
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="updateObjParams"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual DbEntity UpdateById <T>(object updateObjParams, object id)
        {
            DbEntity dbEntity = null;
            Type     type     = typeof(T);

            if (updateObjParams == null)
            {
                return(dbEntity);
            }
            if (id == null)
            {
                return(dbEntity);
            }
            var attributeBuilder = new AttributeBuilder();
            var tableEntity      = attributeBuilder.GetTableInfo(type);

            if (tableEntity == null)
            {
                return(dbEntity);
            }
            List <TableColumnAttribute> updateColumns = attributeBuilder.GetColumnInfos(updateObjParams);

            if (updateColumns == null)
            {
                return(dbEntity);
            }
            var pkColumn = attributeBuilder.GetPkColumnInfo(type);

            if (pkColumn == null)
            {
                return(dbEntity);
            }
            pkColumn.ColumnValue = id;
            dbEntity             = new DbEntity()
            {
                DbType      = DbFactory.DbType,
                TableEntity = tableEntity
            };
            string        dbOperator = DbFactory.GetDbOperator();
            var           dbParams   = new List <IDbDataParameter>();
            StringBuilder sqlBuild   = new StringBuilder("update {tableName} set {updateCriteria}  where {columnName}={dbOperator}{columnName}");

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            sqlBuild.Replace("{columnName}", pkColumn.ColumnName);
            sqlBuild.Replace("{dbOperator}", dbOperator);
            HandleUpdateParam(updateColumns, ref sqlBuild, ref dbParams);
            var dbParam = DbFactory.GetDbParam(pkColumn);

            dbParams.Add(dbParam);
            dbEntity.CommandText = sqlBuild.ToString();
            dbEntity.DbParams    = dbParams;
            return(dbEntity);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add single data
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual DbEntity Insert <T>(T entity)
        {
            DbEntity dbEntity = null;
            Type     type     = typeof(T);

            if (entity == null)
            {
                return(dbEntity);
            }
            var attributeBuilder = new AttributeBuilder();
            var tableEntity      = attributeBuilder.GetTableInfo(type);

            if (tableEntity == null)
            {
                return(dbEntity);
            }
            List <TableColumnAttribute> columns = attributeBuilder.GetColumnInfos(type, entity);

            if (columns == null)
            {
                return(dbEntity);
            }
            if (columns.Count == 0)
            {
                return(dbEntity);
            }
            columns = columns.Where(a => !a.IsAutoIncrement).ToList();
            if (columns == null)
            {
                return(dbEntity);
            }
            if (columns.Count == 0)
            {
                return(dbEntity);
            }
            dbEntity = new DbEntity()
            {
                DbType      = DbFactory.DbType,
                TableEntity = tableEntity
            };
            List <IDbDataParameter> dbParams = new List <IDbDataParameter>();
            string        dbOperatore        = DbFactory.GetDbOperator();
            StringBuilder sqlBuild           = new StringBuilder("insert into {tableName}({columnNames}) values({columnValues})");

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            StringBuilder columnNameSql  = new StringBuilder();
            StringBuilder columnValueSql = new StringBuilder();
            int           i = 0;

            foreach (var column in columns)
            {
                columnNameSql.Append(column.ColumnName);
                columnValueSql.Append(dbOperatore + column.ColumnName);
                if (i != columns.Count - 1)
                {
                    columnNameSql.Append(",");
                    columnValueSql.Append(",");
                }
                var dbParam = DbFactory.GetDbParam(column);
                dbParams.Add(dbParam);
                i++;
            }
            sqlBuild.Replace("{columnNames}", columnNameSql.ToString());
            sqlBuild.Replace("{columnValues}", columnValueSql.ToString());
            var autoIncrementSql = DbFactory.GetIncrementSql(tableEntity.IsGetIncrementValue);

            sqlBuild.Append(autoIncrementSql);
            dbEntity.CommandText = sqlBuild.ToString();
            dbEntity.DbParams    = dbParams;
            return(dbEntity);
        }