예제 #1
0
        private async Task Insert(User user)
        {
            using (IPostgresCommand command = Connection.CreateCommand())
            {
                command.CommandText = @"
                    INSERT INTO users.users (
                        id, 
                        username, 
                        firstname, 
                        lastname, 
                        email,
                        created_date
                    ) values (
                        :p_id, 
                        :p_username, 
                        :p_firstname, 
                        :p_lastname, 
                        :p_email,
                        :p_created_date
                    )
                ";

                AddParameter(command, DbType.Guid, ParameterDirection.Input, "p_id", user.Id);
                AddParameter(command, DbType.String, ParameterDirection.Input, "p_username", user.UserName);
                AddParameter(command, DbType.String, ParameterDirection.Input, "p_firstname", user.FirstName);
                AddParameter(command, DbType.String, ParameterDirection.Input, "p_lastname", user.LastName);
                AddParameter(command, DbType.String, ParameterDirection.Input, "p_email", user.Email);
                AddParameter(command, DbType.DateTime, ParameterDirection.Input, "p_created_date", user.CreatedDate);

                await command.PrepareAsync();

                await command.ExecuteNonQueryAsync();
            }
        }
예제 #2
0
        private async Task Update(User modifiedUser, User existingUser)
        {
            using (IPostgresCommand command = Connection.CreateCommand())
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("UPDATE users.users ");

                if (modifiedUser.UserName != existingUser.UserName)
                {
                    AddSetClause(builder, command, DbType.String, "username", modifiedUser.UserName);
                }

                if (modifiedUser.FirstName != existingUser.FirstName)
                {
                    AddSetClause(builder, command, DbType.String, "firstname", modifiedUser.FirstName);
                }

                if (modifiedUser.LastName != existingUser.LastName)
                {
                    AddSetClause(builder, command, DbType.String, "lastname", modifiedUser.LastName);
                }

                if (modifiedUser.Email != existingUser.Email)
                {
                    AddSetClause(builder, command, DbType.String, "email", modifiedUser.Email);
                }

                if (modifiedUser.CreatedDate != existingUser.CreatedDate)
                {
                    AddSetClause(builder, command, DbType.DateTime, "created_date", modifiedUser.CreatedDate);
                }

                builder.Append(" WHERE id = :p_id");
                AddParameter(command, DbType.Guid, ParameterDirection.Input, "p_id", modifiedUser.Id);

                command.CommandText = builder.ToString();
                await command.PrepareAsync();

                await command.ExecuteNonQueryAsync();
            }
        }