<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: subrata71</title>
    <description>The latest articles on DEV Community by subrata71 (@subrata71).</description>
    <link>https://dev.to/subrata71</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F707410%2Fe174ce05-f11b-4a75-b3fd-252198572c5b.png</url>
      <title>DEV Community: subrata71</title>
      <link>https://dev.to/subrata71</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/subrata71"/>
    <language>en</language>
    <item>
      <title>Capitalizing Words in PostgreSQL: Handling Hyphenated Words and More</title>
      <dc:creator>subrata71</dc:creator>
      <pubDate>Tue, 21 Jan 2025 17:56:35 +0000</pubDate>
      <link>https://dev.to/subrata71/capitalizing-words-in-postgresql-handling-hyphenated-words-and-more-1l63</link>
      <guid>https://dev.to/subrata71/capitalizing-words-in-postgresql-handling-hyphenated-words-and-more-1l63</guid>
      <description>&lt;p&gt;Source: &lt;a href="https://leetcode.com/problems/first-letter-capitalization-ii/description/" rel="noopener noreferrer"&gt;Leetcode&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog post, we’ll explore how to capitalize words in a text column using PostgreSQL while considering special cases for hyphenated words. This process involves transforming a sentence such that the first letter of each word is capitalized, and hyphenated words are properly handled by capitalizing both parts of the word.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Overview
&lt;/h2&gt;

&lt;p&gt;Imagine you have a table &lt;code&gt;user_content&lt;/code&gt; with two columns: &lt;code&gt;content_id&lt;/code&gt; and &lt;code&gt;content_text&lt;/code&gt;. The &lt;code&gt;content_text&lt;/code&gt; column contains sentences or phrases, and you want to:&lt;/p&gt;

&lt;p&gt;Capitalize the first letter of each word in content_text.&lt;br&gt;
For words containing hyphens (like "top-rated" or "well-known"), both parts of the word should be capitalized (e.g., "Top-Rated" and "Well-Known").&lt;br&gt;
Ensure that the rest of the letters in each word are in lowercase.&lt;br&gt;
This transformation is often required for proper formatting in text processing, especially when dealing with user-generated content, titles, or descriptions.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Input:&lt;/p&gt;

&lt;p&gt;user_content table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+------------+---------------------------------+
| content_id | content_text                    |
+------------+---------------------------------+
| 1          | hello world of SQL              |
| 2          | the QUICK-brown fox             |
| 3          | modern-day DATA science         |
| 4          | web-based FRONT-end development |
+------------+---------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+------------+---------------------------------+---------------------------------+
| content_id | original_text                   | converted_text                  |
+------------+---------------------------------+---------------------------------+
| 1          | hello world of SQL              | Hello World Of Sql              |
| 2          | the QUICK-brown fox             | The Quick-Brown Fox             |
| 3          | modern-day DATA science         | Modern-Day Data Science         |
| 4          | web-based FRONT-end development | Web-Based Front-End Development |
+------------+---------------------------------+---------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Solution Approach
&lt;/h2&gt;

&lt;p&gt;We can solve this problem using a combination of string manipulation functions in PostgreSQL, including unnest, string_to_array, substring, and string_agg. Let’s break down the query:&lt;/p&gt;

&lt;h3&gt;
  
  
  Step-by-Step Explanation
&lt;/h3&gt;

&lt;p&gt;Breaking Down content_text into Words:&lt;/p&gt;

&lt;p&gt;First, we need to split the content_text into individual words. We use the string_to_array function to split the text by spaces.&lt;br&gt;
We then apply the unnest function to convert the array of words into individual rows. This allows us to process each word separately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
SELECT
    content_id,
    content_text,
    unnest(string_to_array(content_text, ' ')) AS word
FROM user_content
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;unnest(string_to_array(content_text, ' '))&lt;/code&gt; part splits the content_text into individual words, and each word is returned in a separate row.&lt;/p&gt;

&lt;p&gt;Handling Hyphenated Words:&lt;/p&gt;

&lt;p&gt;Now, we need to address words that contain hyphens (e.g., "top-rated").&lt;br&gt;
We use a CASE statement to check if a word contains a hyphen (LIKE '%-%').&lt;br&gt;
For hyphenated words, we split the word by the hyphen, capitalize each part, and then join the parts back with a hyphen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WHEN word LIKE '%-%' THEN
    (
        SELECT string_agg(
            UPPER(substring(part, 1, 1)) || LOWER(substring(part, 2)), '-'
        )
        FROM unnest(string_to_array(word, '-')) AS part
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;string_to_array(word, '-')&lt;/code&gt; splits the hyphenated word into parts (e.g., "top-rated" becomes ['top', 'rated']).&lt;br&gt;
unnest then converts the array into individual rows, and the &lt;code&gt;UPPER(substring(part, 1, 1)) || LOWER(substring(part, 2))&lt;/code&gt; part capitalizes each part by making the first letter uppercase and the rest lowercase.&lt;br&gt;
Finally, &lt;code&gt;string_agg(..., '-')&lt;/code&gt; joins the capitalized parts back together with a hyphen.&lt;br&gt;
Regular Word Capitalization:&lt;/p&gt;

&lt;p&gt;For words that do not contain hyphens, we simply capitalize the first letter and make the rest of the letters lowercase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ELSE
    UPPER(substring(word, 1, 1)) || LOWER(substring(word, 2))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that for words like "hello" or "world", the first letter is capitalized, and the rest are in lowercase, turning "hello" into "Hello" and "world" into "World".&lt;/p&gt;

&lt;p&gt;Aggregating Words Back Together:&lt;/p&gt;

&lt;p&gt;After processing each word, we need to combine them back into a sentence. We use string_agg to aggregate the processed words into a single string, separated by spaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string_agg(
    CASE
        ...
    END, ' ') AS converted_text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step ensures that all the processed words are joined together to form the final sentence.&lt;/p&gt;

&lt;p&gt;Final Output:&lt;/p&gt;

&lt;p&gt;Finally, we select the content_id, the original content_text, and the newly converted converted_text for each row. We also order the results by content_id to maintain the original order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT content_id, original_text, converted_text
FROM SplitWords
ORDER BY content_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Full Query&lt;br&gt;
Putting everything together, the full query looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH SplitWords AS (
    SELECT
        content_id,
        content_text AS original_text,
        string_agg(
            CASE
                -- Check if the word contains a hyphen
                WHEN word LIKE '%-%' THEN
                    -- Use a lateral join to split by hyphen and capitalize each part
                    (
                        SELECT string_agg(UPPER(substring(part, 1, 1)) || LOWER(substring(part, 2)), '-')
                        FROM unnest(string_to_array(word, '-')) AS part
                    )
                ELSE
                    -- For regular words, just capitalize the first letter
                    UPPER(substring(word, 1, 1)) || LOWER(substring(word, 2))
            END, ' ') AS converted_text
    FROM (
        SELECT
            content_id,
            content_text,
            unnest(string_to_array(content_text, ' ')) AS word
        FROM user_content
    ) AS Words
    GROUP BY content_id, content_text
)
SELECT content_id, original_text, converted_text
FROM SplitWords
ORDER BY content_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;This query uses PostgreSQL’s powerful string manipulation functions (substring, string_agg, unnest) to handle text transformations.&lt;br&gt;
Hyphenated words are split, processed, and rejoined with proper capitalization.&lt;br&gt;
The string_agg function is essential for rejoining words after processing.&lt;br&gt;
This approach ensures that all words are correctly capitalized according to the specified rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By using PostgreSQL’s text manipulation capabilities, we can efficiently capitalize words in a sentence and handle special cases like hyphenated words. This query serves as a practical solution for text formatting in PostgreSQL, ensuring consistency and correct capitalization across various text inputs.&lt;/p&gt;

</description>
      <category>postgressql</category>
      <category>sql</category>
      <category>leetcode</category>
      <category>database</category>
    </item>
    <item>
      <title>How to Run an Asynchronous Task in Spring WebFlux Without Blocking the Main Response?</title>
      <dc:creator>subrata71</dc:creator>
      <pubDate>Sat, 27 Jul 2024 17:04:39 +0000</pubDate>
      <link>https://dev.to/subrata71/how-to-run-an-asynchronous-task-in-spring-webflux-without-blocking-the-main-response-57d7</link>
      <guid>https://dev.to/subrata71/how-to-run-an-asynchronous-task-in-spring-webflux-without-blocking-the-main-response-57d7</guid>
      <description>&lt;p&gt;I'm working with Spring WebFlux and I need to perform an asynchronous task as part of a method that should not block the main response to the user. Specifically, I want to call an asynchronous method after completing the main task, but without delaying the response.&lt;/p&gt;

&lt;p&gt;Here's a simplified version of what I'm trying to achieve:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Mono&amp;lt;ResponseDTO&amp;gt; publishPackage(RequestDTO requestDTO) {
    return publishPackageService.doSomething(requestDTO)
        .flatMap(responseDTO -&amp;gt; 
            doSomethingInAsync(requestDTO, responseDTO)
                .thenReturn(responseDTO)
        );
}

// Method that simulates an asynchronous task with a 5-second delay
public Mono&amp;lt;Void&amp;gt; doSomethingInAsync(RequestDTO requestDTO, ResponseDTO responseDTO) {
    return Mono.delay(Duration.ofSeconds(5))
        .then(); // Converts the delayed Mono&amp;lt;Long&amp;gt; to Mono&amp;lt;Void&amp;gt;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this call completes, I want to execute &lt;code&gt;doSomethingInAsync(requestDTO, responseDTO)&lt;/code&gt; asynchronously.&lt;br&gt;
The doSomethingInAsync method should be non-blocking and not delay the main response.&lt;br&gt;
Problem:&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;doSomethingInAsync&lt;/code&gt; method is being executed, but it seems like it might be blocking the response or not running asynchronously as intended. How can I ensure that &lt;code&gt;doSomethingInAsync&lt;/code&gt; runs asynchronously and does not block the response to the user?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Details:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;publishPackageService.doSomething(requestDTO): Returns a Mono.&lt;br&gt;
doSomethingInAsync(requestDTO, responseDTO): Is an asynchronous method that I want to run without blocking the response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Questions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How can I ensure doSomethingInAsync runs in the background without blocking the response?&lt;/p&gt;

</description>
      <category>java</category>
      <category>webflux</category>
      <category>springboot</category>
      <category>thread</category>
    </item>
    <item>
      <title>How to Run a Method Asynchronously in a Reactive Chain in Spring WebFlux?</title>
      <dc:creator>subrata71</dc:creator>
      <pubDate>Fri, 26 Jul 2024 10:17:30 +0000</pubDate>
      <link>https://dev.to/subrata71/how-to-run-a-method-asynchronously-in-a-reactive-chain-in-spring-webflux-hpf</link>
      <guid>https://dev.to/subrata71/how-to-run-a-method-asynchronously-in-a-reactive-chain-in-spring-webflux-hpf</guid>
      <description>&lt;p&gt;I'm trying to execute a method asynchronously within an existing reactive chain in my Project Reactor-based application. The method doUpdateLayoutInAsync is intended to perform a heavy background task, but it seems like my approach isn't working as expected. Here's my current implementation:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Mono&amp;lt;Boolean&amp;gt; publishPackage(String branchedPackageId) {
    PackagePublishingMetaDTO publishingMetaDTO = new PackagePublishingMetaDTO();
    publishingMetaDTO.setPublishEvent(true);

    return packageRepository
            .findById(branchedPackageId, packagePermission.getPublishPermission())
            .switchIfEmpty(Mono.error(new AppsmithException(
                    AppsmithError.ACL_NO_RESOURCE_FOUND, FieldName.PACKAGE_ID, branchedPackageId)))
            .flatMap(originalPackage -&amp;gt; {
                String nextVersion = PackageUtils.getNextVersion(originalPackage.getVersion());

                Package packageToBePublished = constructPackageToBePublished(originalPackage);

                originalPackage.setVersion(nextVersion);
                originalPackage.setLastPublishedAt(packageToBePublished.getLastPublishedAt());
                publishingMetaDTO.setOriginPackageId(branchedPackageId);
                publishingMetaDTO.setWorkspaceId(originalPackage.getWorkspaceId());

                Mono&amp;lt;Void&amp;gt; unsetCurrentLatestMono = packageRepository.unsetLatestPackageByOriginId(originalPackage.getId(), null);
                Mono&amp;lt;Package&amp;gt; saveOriginalPackage = packageRepository.save(originalPackage);
                Mono&amp;lt;Package&amp;gt; savePackageToBePublished = packageRepository.save(packageToBePublished);

                return unsetCurrentLatestMono
                        .then(Mono.zip(saveOriginalPackage, savePackageToBePublished))
                        .flatMap(tuple2 -&amp;gt; {
                            Package publishedPackage = tuple2.getT2();
                            publishingMetaDTO.setPublishedPackage(publishedPackage);

                            return modulePackagePublishableService
                                    .publishEntities(publishingMetaDTO)
                                    .flatMap(publishedModules -&amp;gt; {
                                        if (publishedModules.isEmpty()) {
                                            return Mono.error(new AppsmithException(
                                                    AppsmithError.PACKAGE_CANNOT_BE_PUBLISHED,
                                                    originalPackage.getUnpublishedPackage().getName()));
                                        }
                                        return moduleInstancePackagePublishableService
                                                .publishEntities(publishingMetaDTO)
                                                .then(Mono.defer(() -&amp;gt;
                                                        newActionPackagePublishableService.publishEntities(publishingMetaDTO))
                                                        .then(Mono.defer(() -&amp;gt;
                                                                actionCollectionPackagePublishableService
                                                                        .publishEntities(publishingMetaDTO))));
                                    })
                                    .then(Mono.defer(() -&amp;gt; autoUpgradeService.handleAutoUpgrade(publishingMetaDTO)));
                        })
                        .as(transactionalOperator::transactional)
                        .then(Mono.defer(() -&amp;gt; doUpdateLayoutInAsync(publishingMetaDTO)));
            });
}

private Mono&amp;lt;Boolean&amp;gt; doUpdateLayoutInAsync(PackagePublishingMetaDTO publishingMetaDTO) {
    Mono&amp;lt;List&amp;lt;String&amp;gt;&amp;gt; updateLayoutsMono = Flux.fromIterable(publishingMetaDTO.getAutoUpgradedPageIds())
            .flatMap(pageId -&amp;gt; updateLayoutService
                    .updatePageLayoutsByPageId(pageId)
                    .onErrorResume(throwable -&amp;gt; {
                        log.warn("Update layout failed for pageId: {} with error: {}", pageId, throwable.getMessage());
                        return Mono.just(pageId);
                    }))
            .collectList();

    // Running the updateLayoutsMono task asynchronously
    updateLayoutsMono.subscribeOn(Schedulers.boundedElastic()).subscribe();

    return Mono.just(Boolean.TRUE);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Issue:&lt;/strong&gt; I want &lt;code&gt;doUpdateLayoutInAsync&lt;/code&gt; to run in the background while the rest of the reactive chain completes. However, the method seems to execute synchronously, and the reactive chain does not continue as expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; How can I ensure that &lt;code&gt;doUpdateLayoutInAsync&lt;/code&gt; runs asynchronously and does not block the continuation of the reactive chain?&lt;/p&gt;

</description>
      <category>java</category>
      <category>webflux</category>
      <category>springboot</category>
      <category>asynchronous</category>
    </item>
  </channel>
</rss>
