Exemplo n.º 1
0
        /// <summary>
        /// 从主键获取数据对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="primaryKeys">主键数组</param>
        /// <returns>数据对象</returns>
        public T SelectSingleFromKey <T>(params object[] primaryKeys)
            where T : class, new()
        {
            DataTableEntityMapping dtmapping = DataMapping.GetTableMapping(typeof(T));

            if (primaryKeys.Length != dtmapping.PrimaryKeyFields.Length)
            {
                throw new LightDataException(RE.TheNumberOfPrimaryKeysIsNotMatch);
            }
            DataFieldInfo[] primaryKeyInfos = new DataFieldInfo[primaryKeys.Length];
            QueryExpression query           = null;

            for (int i = 0; i < primaryKeys.Length; i++)
            {
                primaryKeyInfos[i] = new DataFieldInfo(dtmapping.PrimaryKeyFields[i]);
                if (i == 0)
                {
                    query = primaryKeyInfos[i] == primaryKeys[i];
                }
                else
                {
                    query = query & primaryKeyInfos[i] == primaryKeys[i];
                }
            }
            return(SelectSingle(dtmapping, query, null, 0, SafeLevel.None) as T);
        }
Exemplo n.º 2
0
        public override int Delete()
        {
            DataTableEntityMapping mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            QueryCommand           queryCommand = _context.Database.QueryDelete(_context, mapping, _query);

            return(_context.ExecuteNonQuery(queryCommand.Command, _level));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 生成数据删除命令
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>删除命令对象</returns>
        public virtual IDbCommand CreateDeleteCommand(object entity)
        {
            DataTableEntityMapping mapping = DataMapping.GetTableMapping(entity.GetType());

            if (mapping.PrimaryKeyFields == null || mapping.PrimaryKeyFields.Length == 0)
            {
                throw new LightDataException(RE.PrimaryKeyIsNotExist);
            }
            List <DataParameter> primaryList = GetDataParameters(mapping.PrimaryKeyFields, entity);

            StringBuilder where = new StringBuilder();
            IDataParameter[] dataParameters = new IDataParameter[primaryList.Count];
            int count = 0;

            foreach (DataParameter dataParameter in primaryList)
            {
                IDataParameter param = _database.CreateParameter("P" + count, dataParameter.Value, dataParameter.DbType, dataParameter.Direction);
                where.AppendFormat("{0}={1} and ", CreateDataFieldSql(dataParameter.ParameterName), param.ParameterName);
                dataParameters [count] = param;
                count++;
            }
            where.Remove(where.Length - 5, 5);
            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("delete from {0} where {1}", CreateDataTableSql(mapping.TableName), where);
            IDbCommand command = _database.CreateCommand(sql.ToString());

            command.CommandType = CommandType.Text;
            foreach (IDataParameter param in dataParameters)
            {
                command.Parameters.Add(param);
            }
            return(command);
        }
Exemplo n.º 4
0
        public async override Task <int> InsertAsync <K>(CancellationToken cancellationToken = default(CancellationToken))
        {
            DataTableEntityMapping insertMapping = DataEntityMapping.GetTableMapping(typeof(K));
            QueryCommand           queryCommand  = _context.Database.SelectInsert(_context, insertMapping, _mapping, _query, _order);

            return(await _context.ExecuteNonQueryAsync(queryCommand.Command, _level, cancellationToken));
        }
Exemplo n.º 5
0
        public override int Insert <K>()
        {
            DataTableEntityMapping insertMapping = DataEntityMapping.GetTableMapping(typeof(K));
            QueryCommand           queryCommand  = _context.Database.SelectInsert(_context, insertMapping, _mapping, _query, _order);

            return(_context.ExecuteNonQuery(queryCommand.Command, _level));
        }
Exemplo n.º 6
0
        public virtual IDbCommand CreateUpdateMassCommand(DataTableEntityMapping mapping, UpdateSetValue[] updateSetValues, QueryExpression query)
        {
            List <DataParameter> parameterlist = new List <DataParameter> ();

            StringBuilder sql = new StringBuilder();

            DataParameter[] parameters;
            string          queryString = GetQueryString(mapping, query, out parameters);

            int length = updateSetValues.Length;

            DataParameter[] setparameters = new DataParameter[length];

            sql.AppendFormat("update {0} set ", CreateDataTableSql(mapping.TableName));

            for (int i = 0; i < length; i++)
            {
                if (!mapping.Equals(updateSetValues [i].DataField.DataField.EntityMapping))
                {
                    throw new LightDataException(RE.UpdateFieldTypeIsError);
                }
                setparameters [i] = updateSetValues [i].CreateDataParameter(this);
                sql.AppendFormat("{0}={1}{2}", updateSetValues [i].DataField.CreateDataFieldSql(this), setparameters [i].ParameterName, i < length - 1 ? "," : " ");
            }

            if (!string.IsNullOrEmpty(queryString))
            {
                sql.AppendFormat(" {0}", queryString);
            }
            parameterlist.AddRange(parameters);
            parameterlist.AddRange(setparameters);
            IDbCommand command = BuildCommand(sql.ToString(), parameterlist.ToArray());

            return(command);
        }
Exemplo n.º 7
0
        public async override Task <int> DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            DataTableEntityMapping mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            QueryCommand           queryCommand = _context.Database.QueryDelete(_context, mapping, _query);

            return(await _context.ExecuteNonQueryAsync(queryCommand.Command, _level, cancellationToken));
        }
Exemplo n.º 8
0
        public async override Task <int> UpdateAsync(Expression <Func <T, T> > expression, CancellationToken cancellationToken = default(CancellationToken))
        {
            DataTableEntityMapping mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            MassUpdator            updator      = LambdaExpressionExtend.CreateMassUpdator(expression);
            QueryCommand           queryCommand = _context.Database.QueryUpdate(_context, mapping, updator, _query);

            return(await _context.ExecuteNonQueryAsync(queryCommand.Command, _level, cancellationToken));
        }
Exemplo n.º 9
0
        public override int Update(Expression <Func <T, T> > expression)
        {
            DataTableEntityMapping mapping      = DataEntityMapping.GetTableMapping(typeof(T));
            MassUpdator            updator      = LambdaExpressionExtend.CreateMassUpdator(expression);
            QueryCommand           queryCommand = _context.Database.QueryUpdate(_context, mapping, updator, _query);

            return(_context.ExecuteNonQuery(queryCommand.Command, _level));
        }
Exemplo n.º 10
0
        public static string CreateKey(DataTableEntityMapping mapping, CreateSqlState state)
        {
            state.TryGetAliasTableName(mapping, out var aliasName);
            var type = mapping.ObjectType;
            var name = aliasName == null ? type.FullName : string.Concat(type.FullName, "_", aliasName);

            return(name);
        }
Exemplo n.º 11
0
 public MassUpdator(DataTableEntityMapping mapping)
 {
     if (mapping == null)
     {
         throw new ArgumentNullException(nameof(mapping));
     }
     this.mapping = mapping;
 }
Exemplo n.º 12
0
        private int CheckDbSave(bool refresh, SafeLevel level)
        {
            DataContext            context = GetContext();
            int                    ret;
            DataTableEntityMapping mapping = DataEntityMapping.GetTableMapping(this.GetType());

            ret = context.InsertOrUpdate(mapping, this, level, refresh);
            return(ret);
        }
Exemplo n.º 13
0
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns>受影响数据</returns>
        public int DeleteMass()
        {
            DataTableEntityMapping dtMapping = _mapping as DataTableEntityMapping;

            if (dtMapping == null)
            {
                throw new LightDataException(RE.TheDataMappingNotAllowDeleteMass);
            }
            return(_context.DeleteMass(dtMapping, _query));
        }
Exemplo n.º 14
0
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="updates">更新字段值数组,类型必须和更新对象一致</param>
        /// <returns>受影响数据</returns>
        public int UpdateMass(UpdateSetValue[] updates)
        {
            DataTableEntityMapping dtMapping = _mapping as DataTableEntityMapping;

            if (dtMapping == null)
            {
                throw new LightDataException(RE.TheDataMappingNotAllowDeleteMass);
            }
            return(_context.UpdateMass(dtMapping, updates, _query));
        }
Exemplo n.º 15
0
        /// <summary>
        /// 批量更新数据
        /// </summary>
        /// <param name="mapping">表映射</param>
        /// <param name="updates">更新对象类型</param>
        /// <param name="expression">查询表达式</param>
        /// <returns>受影响行数</returns>
        internal int UpdateMass(DataTableEntityMapping mapping, UpdateSetValue[] updates, QueryExpression expression)
        {
            int rInt = 0;

            using (IDbCommand command = _dataBase.Factory.CreateUpdateMassCommand(mapping, updates, expression))
            {
                rInt = ExecuteNonQuery(command, SafeLevel.Default);
            }
            return(rInt);
        }
Exemplo n.º 16
0
        private async Task <int> CheckDbSaveAsync(bool refresh, SafeLevel level, CancellationToken cancellationToken = default(CancellationToken))
        {
            DataContext            context = GetContext();
            int                    ret;
            DataTableEntityMapping mapping = DataEntityMapping.GetTableMapping(this.GetType());

            ret = await context.InsertOrUpdateAsync(mapping, this, level, refresh, cancellationToken);

            return(ret);
        }
Exemplo n.º 17
0
 public virtual void UpdateDataIdentity(DataTableEntityMapping mapping, object data, object id)
 {
     if (!Equals(id, null))
     {
         if (id.GetType() != mapping.IdentityField.ObjectType)
         {
             id = Convert.ChangeType(id, mapping.IdentityField.ObjectType);
         }
         mapping.IdentityField.Handler.Set(data, id);
     }
 }
Exemplo n.º 18
0
 protected override string CreateIdentitySql(DataTableEntityMapping mapping)
 {
     if (mapping.IdentityField != null)
     {
         return("select last_insert_id();");
     }
     else
     {
         return(string.Empty);
     }
 }
Exemplo n.º 19
0
        public virtual IDbCommand CreateIdentityCommand(DataTableEntityMapping mapping)
        {
            IDbCommand command = null;
            string     sql     = CreateIdentitySql(mapping);

            if (!string.IsNullOrEmpty(sql))
            {
                command = BuildCommand(sql.ToString(), null);
            }
            return(command);
        }
Exemplo n.º 20
0
 protected virtual string CreateIdentitySql(DataTableEntityMapping mapping)
 {
     if (mapping.IdentityField != null)
     {
         return("select @@Identity;");
     }
     else
     {
         return(string.Empty);
     }
 }
Exemplo n.º 21
0
        public virtual QueryCommand QueryUpdate(DataContext context, DataTableEntityMapping mapping, MassUpdator updator, QueryExpression query)
        {
            CreateSqlState state        = new CreateSqlState(context);
            CommandData    commandData  = _factory.CreateMassUpdateCommand(mapping, updator, query, state);
            DbCommand      command      = commandData.CreateCommand(this, state);
            QueryCommand   queryCommand = new QueryCommand()
            {
                Command = command
            };

            return(queryCommand);
        }
Exemplo n.º 22
0
 protected override string CreateIdentitySql(DataTableEntityMapping mapping)
 {
     if (mapping.IdentityField != null)
     {
         string seq = GetIndentitySeq(mapping);
         return(string.Format("select {0}.currval from dual", seq));
     }
     else
     {
         return(string.Empty);
     }
 }
Exemplo n.º 23
0
        public virtual QueryCommand SelectInsert(DataContext context, DataTableEntityMapping insertMapping, DataEntityMapping selectMapping, QueryExpression query, OrderExpression order)
        {
            CreateSqlState state        = new CreateSqlState(context);
            CommandData    commandData  = _factory.CreateSelectInsertCommand(insertMapping, selectMapping, query, order, state);
            DbCommand      command      = commandData.CreateCommand(this, state);
            QueryCommand   queryCommand = new QueryCommand()
            {
                Command = command
            };

            return(queryCommand);
        }
Exemplo n.º 24
0
        public virtual QueryCommand DeleteByKey(DataContext context, DataTableEntityMapping mapping, object[] keys)
        {
            CreateSqlState state        = new CreateSqlState(context, false);
            CommandData    commandData  = _factory.CreateDeleteKeyCommand(mapping, keys, state);
            DbCommand      command      = commandData.CreateCommand(this, state);
            QueryCommand   queryCommand = new QueryCommand()
            {
                Command = command
            };

            return(queryCommand);
        }
Exemplo n.º 25
0
        public virtual QueryCommand SelectById(DataContext context, DataTableEntityMapping mapping, object id)
        {
            CreateSqlState state        = new CreateSqlState(context, false);
            CommandData    commandData  = _factory.CreateSelectByIdCommand(mapping, id, state);
            DbCommand      command      = commandData.CreateCommand(this, state);
            QueryCommand   queryCommand = new QueryCommand()
            {
                Command = command
            };

            return(queryCommand);
        }
Exemplo n.º 26
0
        public virtual QueryCommand BatchDelete(DataContext context, DataTableEntityMapping mapping, IList datas)
        {
            CreateSqlState state        = new CreateSqlState(context, false);
            CommandData    commandData  = _factory.CreateBatchDeleteCommand(mapping, datas, state);
            DbCommand      command      = commandData.CreateCommand(this, state);
            QueryCommand   queryCommand = new QueryCommand()
            {
                Command = command
            };

            return(queryCommand);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Gets the table mapping.
        /// </summary>
        /// <returns>The table mapping.</returns>
        /// <param name="type">Type.</param>
        public static DataTableEntityMapping GetTableMapping(Type type)
        {
            DataTableEntityMapping dataMapping = GetEntityMapping(type) as DataTableEntityMapping;

            if (dataMapping == null)
            {
                throw new LightDataException(SR.IsNotDataTableMapping);
            }
            else
            {
                return(dataMapping);
            }
        }
Exemplo n.º 28
0
        /// <summary>
        /// 创建新对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <returns>数据对象</returns>
        public T CreateNew <T>()
            where T : class, new()
        {
            DataTableEntityMapping rawmapping = DataMapping.GetTableMapping(typeof(T));
            T obj = rawmapping.InitialData() as T;

            if (rawmapping.IsDataEntity)
            {
                DataEntity data = obj as DataEntity;
                data.SetContext(this);
            }
            return(obj);
        }
Exemplo n.º 29
0
        public virtual QueryCommand TruncateTable(DataContext context, DataTableEntityMapping mapping)
        {
            CreateSqlState state        = new CreateSqlState(context);
            CommandData    commandData  = _factory.CreateTruncateTableCommand(mapping, state);
            DbCommand      command      = commandData.CreateCommand(this, state);
            QueryCommand   queryCommand = new QueryCommand()
            {
                Command   = command,
                InnerPage = commandData.InnerPage
            };

            return(queryCommand);
        }
Exemplo n.º 30
0
        public virtual QueryCommand Insert(DataContext context, DataTableEntityMapping mapping, object data, bool refresh, bool updateIdentity)
        {
            CreateSqlState state        = new CreateSqlState(context, false);
            CommandData    commandData  = _factory.CreateBaseInsertCommand(mapping, data, refresh, updateIdentity, state);
            DbCommand      command      = commandData.CreateCommand(this, state);
            QueryCommand   queryCommand = new QueryCommand()
            {
                Command     = command,
                IdentitySql = commandData.IdentitySql
            };

            return(queryCommand);
        }