Exemplo n.º 1
0
        public string BuildSelect()
        {
            StringBuilder sbSql         = new StringBuilder();
            var           selectColumns = _tableMapping.Columns;
            var           keyColumns    = _tableMapping.GetKeyColumns();
            var           lsSelect      = new List <string>();
            var           lsWhere       = new List <string>();

            if (keyColumns.Count < 1)
            {
                throw new EntityException("实体没有设置主键!");
            }

            foreach (var col in selectColumns)
            {
                lsSelect.Add(string.Format("{0} as {1}", col.ColumnName, col.PropertyName));
            }

            foreach (var keyCol in keyColumns)
            {
                lsWhere.Add(string.Format("{0}=#{1}#", keyCol.ColumnName, keyCol.PropertyName));
            }

            sbSql.AppendFormat("Select {0} \n From {1} Where {2}",
                               string.Join(" , ", lsSelect.ToArray()),
                               _tableMapping.TableName,
                               string.Join(" And ", lsWhere.ToArray())
                               );

            return(sbSql.ToString());
        }
Exemplo n.º 2
0
        public static int Delete <T>(this DbAccessor db, object objIdParameters)
        {
            var         tableMapping = new TableMapping(typeof(T));
            ISqlBuilder sqlBuilder   = new DefaultSqlBuilder(tableMapping);

            #region 处理参数为基础类型,即直接为主键的值
            if (objIdParameters.GetType() == typeof(string) || objIdParameters.GetType().GetTypeInfo().IsPrimitive)
            {
                var dicParam = new Dictionary <string, object>();

                var keyColumns = tableMapping.GetKeyColumns();

                if (keyColumns.Count == 0)
                {
                    throw new Exceptions.EntityException("实体没有设置主键!");
                }

                string keyColumnName = keyColumns[0].ColumnName;

                dicParam.Add(keyColumnName, objIdParameters);
                objIdParameters = dicParam;
            }
            #endregion

            return(db.ExecuteNonQuery(sqlBuilder.BuildDelete(), objIdParameters));
        }
Exemplo n.º 3
0
        public static int UpdateFields(this DbAccessor db, object objEntity, params string[] inclusiveFields)
        {
            List <string> lsInclusiveFields = new List <string>();

            lsInclusiveFields.AddRange(inclusiveFields);
            var mapping = new TableMapping(objEntity.GetType());

            foreach (var item in mapping.GetKeyColumns())
            {
                lsInclusiveFields.Add(item.ColumnName);
            }
            mapping.FilterColumn(lsInclusiveFields.ToArray());
            ISqlBuilder sqlBuilder = new DefaultSqlBuilder(mapping);

            return(db.ExecuteNonQuery(sqlBuilder.BuildUpdate(), objEntity));
        }