Exemplo n.º 1
0
        /// <summary>
        /// Delete the corresponding data from the primary key
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual DbEntity DeleteById <T>(object id)
        {
            DbEntity dbEntity         = null;
            Type     type             = typeof(T);
            var      attributeBuilder = new AttributeBuilder();
            var      tableEntity      = attributeBuilder.GetTableInfo(type);

            if (tableEntity == null)
            {
                return(dbEntity);
            }
            var column = attributeBuilder.GetPkColumnInfo(type);

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

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            sqlBuild.Replace("{columnName}", column.ColumnName);
            sqlBuild.Replace("{dbOperator}", dbOperator);
            var dbParam = DbFactory.GetDbParam(column);

            dbParams.Add(dbParam);
            dbEntity.CommandText = sqlBuild.ToString();
            dbEntity.DbParams    = dbParams;
            return(dbEntity);
        }
Exemplo n.º 2
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);
        }