Example #1
0
 public static void UpdateObject(
     DbConnection conn,
     object obj,
     string tableName,
     string[] idFieldNames,
     string[] includeFields,
     string[] excludeFields,
     ObjectsChangeTracker changeTracker,
     DbSettings dbSettings
     )
 {
     using (DbCommand cmd = conn.CreateCommand())
     {
         if (
             cmd.BuildUpdateCommand(
                 obj,
                 tableName,
                 idFieldNames,
                 includeFields,
                 excludeFields,
                 changeTracker,
                 dbSettings
                 )
             )
         {
             cmd.ExecuteNonQuery();
         }
     }
 }
        public T ReadSingle(DbDataReader reader, ObjectsChangeTracker changeTracker)
        {
            T result = reader.Read() ? MapUsingState(reader, reader) : default(T);

            if (changeTracker != null)
            {
                changeTracker.RegisterObject(result);
            }
            return(result);
        }
Example #3
0
 public static void UpdateObject(
     DbConnection conn,
     object obj,
     string tableName,
     string[] idFieldNames,
     ObjectsChangeTracker changeTracker,
     DbSettings dbSettings
     )
 {
     UpdateObject(conn, obj, tableName, idFieldNames, null, null, changeTracker, dbSettings);
 }
Example #4
0
        public static T ToObject <T>(this DbDataReader reader, string readerName, string[] excludeFields,
                                     ObjectsChangeTracker changeTracker)
        {
            T result = new DataReaderToObjectMapper <T>(readerName, null, excludeFields).MapUsingState(reader, reader);

            if (changeTracker != null)
            {
                changeTracker.RegisterObject(result);
            }
            return(result);
        }
 public IEnumerable <T> ReadCollection(DbDataReader reader, ObjectsChangeTracker changeTracker)
 {
     while (reader.Read())
     {
         T result = MapUsingState(reader, reader);
         if (changeTracker != null)
         {
             changeTracker.RegisterObject(result);
         }
         yield return(result);
     }
     reader.Close();
 }
Example #6
0
 public static IEnumerable <T> ToObjects <T>(this DbDataReader reader, string readerName, string[] excludeFields,
                                             ObjectsChangeTracker changeTracker)
 {
     if (string.IsNullOrEmpty(readerName))
     {
         var mappingKeyBuilder = new StringBuilder();
         for (int i = 0; i < reader.FieldCount; ++i)
         {
             mappingKeyBuilder.Append(reader.GetName(i));
             mappingKeyBuilder.Append(' ');
         }
         readerName = mappingKeyBuilder.ToString();
     }
     return(new DataReaderToObjectMapper <T>(readerName, null, excludeFields).ReadCollection(reader, changeTracker));
 }
Example #7
0
 public static IEnumerable <T> ReadCollection <T>(
     DbConnection conn,
     string commandText,
     CmdParams cmdParams,
     string[] excludeFields,
     ObjectsChangeTracker changeTracker,
     Func <DbDataReader, T> func = null)
 {
     using (DbCommand cmd = CreateCommand(conn, commandText, cmdParams))
         using (DbDataReader reader = cmd.ExecuteReader())
         {
             if (func == null)
             {
                 func = (r => r.ToObject <T>(null, excludeFields, changeTracker));
             }
             while (reader.Read())
             {
                 yield return(func(reader));
             }
         }
 }
Example #8
0
        public static bool BuildUpdateCommand(
            this DbCommand cmd,
            object obj,
            string tableName,
            IEnumerable <string> idFieldNames,
            IEnumerable <string> includeFields,
            IEnumerable <string> excludeFields,
            ObjectsChangeTracker changeTracker,
            DbSettings dbSettings
            )
        {
            if (idFieldNames == null)
            {
                idFieldNames = new string[0];
            }

            //idFieldNames = idFieldNames.Select(n => n.ToUpper()).ToArray(); //不Upper会怎样

            if (changeTracker != null)
            {
                ObjectsChangeTracker.TrackingMember[] changedFields = changeTracker.GetChanges(obj);
                if (changedFields != null)
                {
                    includeFields = includeFields == null?changedFields.Select(c => c.name).Except(idFieldNames).ToArray()
                                        : includeFields.Intersect(changedFields.Select(c => c.name)).Except(idFieldNames).ToArray();
                }
            }
            if (includeFields != null)
            {
                includeFields = includeFields.Concat(idFieldNames);
            }
            IMappingConfigurator config = new AddDbCommandsMappingConfig(
                dbSettings,
                includeFields,
                excludeFields,
                "updateop_inc_" + includeFields.ToCSV("_") + "_exc_" + excludeFields.ToCSV("_")
                );

            ObjectsMapperBaseImpl mapper = ObjectMapperManager.DefaultInstance.GetMapperImpl(
                obj.GetType(),
                typeof(DbCommand),
                config
                );

            string[] fields = mapper
                              .StroredObjects
                              .OfType <SrcReadOperation>()
                              .Select(m => m.Source.MemberInfo.Name)
                              .Where(f => !idFieldNames.Contains(f))
                              .Except(idFieldNames)
                              .ToArray(); //2014-4-29 cannot change id.

            if (fields.Length == 0)
            {
                return(false);
            }

            var enableCol = new string[] { "TableName", "IdName" };

            fields = fields.Where(c => !enableCol.Contains(c)).ToArray();

            string cmdStr =
                "UPDATE " +
                tableName +
                " SET " +
                fields.Select(
                    f => dbSettings.GetEscapedName(f) + "=" + dbSettings.GetParamName(f)
                    )
                .ToCSV(",") +
                " WHERE " +
                idFieldNames.Select(fn => dbSettings.GetEscapedName(fn) + "=" + dbSettings.GetParamName(fn))
                .ToCSV(" AND ")
            ;

            cmd.CommandText = cmdStr;
            cmd.CommandType = CommandType.Text;

            mapper.Map(obj, cmd, null);
            return(true);
        }