예제 #1
0
        /// <summary>
        /// Trying to set object data to database.
        /// </summary>
        /// <param name="tableType">Type with defined Table attribute.
        /// Contains columns\properties with defined column attributes. Using as map for collecting data.</param>
        /// <param name="obj">Instance that contains data tha with the same column sttributes as in tableType.</param>
        /// <param name="error">Occurred error. Null if operation passed success.</param>
        /// <param name="select">Array that contains columns' names that would be requested in select block.
        /// If empty then would auto select all columns.</param>
        /// <param name="where">Array that contains columns' names taht wouyld be added to Where block.</param>
        /// <returns>Result of operation.</returns>
        public bool SetToObject(
            Type tableType,
            object obj,
            out string error,
            string[] select,
            params string[] where)
        {
            #region Detecting target object
            // Detect object that contains querie's data.
            object internalObj;
            if (obj is IList objList)
            {
                // Set first element of list as target;
                internalObj = objList[0];
            }
            else
            {
                // Set input object as target.
                internalObj = obj;
            }
            #endregion

            #region Building command
            // Get coommon list of available members.
            List <MemberInfo> members = MembersAllowedToSet(tableType, internalObj, out error);

            // Generate command.
            DbCommand command = GenerateSetToObjectCommand(
                tableType,
                internalObj,
                TableAttribute.FindMembersByColumns(members, where),
                TableAttribute.FindMembersByColumns(members, select));

            // Drop if error has been occured.
            if (!string.IsNullOrEmpty(error))
            {
                error = "Commnad generation failed. Details:\n" + error;
                return(false);
            }
            #endregion

            #region Execute command
            // Opening connection to DB srver.
            if (!Active.OpenConnection(out error))
            {
                error = "Connection failed. Details:\n" + error;
                return(false);
            }
            command.Connection = SqlOperatorHandler.Active.Connection;

            // Await for reader.
            DbDataReader reader = command.ExecuteReader();

            // Drop if DbDataReader is invalid.
            if (reader == null || reader.IsClosed)
            {
                error = "DbDataReader is null or closed. Operation declined.";
                return(false);
            }

            #region Reading data
            bool result = true;
            // If collection.
            if (obj is IList)
            {
                bool readed;
                ((IList)obj).Clear();

                // Read stream till possible.
                do
                {
                    // Instiniate obect for receving.
                    object instance = Activator.CreateInstance(tableType);
                    readed = SqlOperatorHandler.DatabaseDataToObject(reader, members, instance, out _);
                    // If readed then add to output.
                    if (readed)
                    {
                        ((IList)obj).Add(instance);
                    }
                }while (readed);
            }
            else
            {
                // Try to apply data from reader to object.
                result = SqlOperatorHandler.DatabaseDataToObject(reader, members, obj, out error);
            }
            #endregion

            // Closing connection.
            Active.CloseConnection();
            #endregion

            return(result);
        }