Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: EXPOSED-626 fix nullTransform example code #2292

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/data-types.html
Original file line number Diff line number Diff line change
Expand Up @@ -538,22 +538,23 @@
val mealTime: Column<Meal> = time("meal_time").transform(MealTimeTransformer())
}
</div><section class="chapter"><h3 id="null-transform" data-toc="null-transform">Null transform</h3><p id="-yo0vxf_313">Special case is <code class="code" id="-yo0vxf_314">nullTransform()</code> method. That method applies a special transformation that allows a non-nullable database column to accept and/or return values as `null` on the client side.</p><p id="-yo0vxf_315">This transformation does not alter the column's definition in the database, which will still be <code class="code" id="-yo0vxf_316">NON NULL</code>. It enables reflecting non-null values from the database as <code class="code" id="-yo0vxf_317">null</code> in Kotlin (e.g., converting an empty string from a non-nullable text column, empty lists, negative IDs, etc., to <code class="code" id="-yo0vxf_318">null</code>).</p><div class="code-block" data-lang="kotlin">
class MealTimeNullTransformer : ColumnTransformer&lt;LocalTime, Meal?&gt; {
override fun wrap(value: LocalTime): Meal? = when {
class MealTimeNullTransformer : ColumnTransformer&lt;LocalTime?, Meal?&gt; {
override fun wrap(value: LocalTime?): Meal? = when {
value == null -&gt; null
value.hour &lt; 10 -&gt; Meal.BREAKFAST
value.hour &lt; 15 -&gt; Meal.LUNCH
else -&gt; Meal.DINNER
}

override fun unwrap(value: Meal?): LocalTime = when (value) {
override fun unwrap(value: Meal?): LocalTime? = when (value) {
Meal.BREAKFAST -&gt; LocalTime(8, 0)
Meal.LUNCH -&gt; LocalTime(12, 0)
Meal.DINNER -&gt; LocalTime(18, 0)
else -&gt; LocalTime(0, 0)
else -&gt; null
}
}

object Meals : Table() {
val mealTime: Column&lt;Meal?&gt; = time(&quot;meal_time&quot;).transform(MealTimeNullTransformer())
val mealTime: Column&lt;Meal?&gt; = time(&quot;meal_time&quot;).nullable().transform(MealTimeNullTransformer())
}
</div></section></section><div class="last-modified">Last modified: 30 October 2024</div><div data-feedback-placeholder="true"></div><div class="navigation-links _bottom"><a href="working-with-tables.html" class="navigation-links__prev">Working with tables</a><a href="sql-functions.html" class="navigation-links__next">SQL Functions</a></div></article><div id="disqus_thread"></div></div></section></main></div><script src="https://resources.jetbrains.com/writerside/apidoc/6.10.0-b267/app.js"></script></body></html>
</div></section></section><div class="last-modified">Last modified: 30 October 2024</div><div data-feedback-placeholder="true"></div><div class="navigation-links _bottom"><a href="working-with-tables.html" class="navigation-links__prev">Working with tables</a><a href="sql-functions.html" class="navigation-links__next">SQL Functions</a></div></article><div id="disqus_thread"></div></div></section></main></div><script src="https://resources.jetbrains.com/writerside/apidoc/6.10.0-b267/app.js"></script></body></html>