|
1 | 1 | package com.powersync.db.crud |
2 | 2 |
|
| 3 | +import com.powersync.PowerSyncDatabase |
3 | 4 | import com.powersync.db.schema.Table |
4 | 5 | import com.powersync.utils.JsonUtil |
5 | | -import kotlinx.serialization.json.JsonElement |
6 | | -import kotlinx.serialization.json.JsonNull |
7 | 6 | import kotlinx.serialization.json.jsonObject |
8 | 7 | import kotlinx.serialization.json.jsonPrimitive |
9 | 8 |
|
@@ -58,75 +57,29 @@ public data class CrudEntry internal constructor( |
58 | 57 | * |
59 | 58 | * For DELETE, this is null. |
60 | 59 | */ |
61 | | - @Deprecated("Use data instead", replaceWith = ReplaceWith("data")) |
62 | | - val opData: Map<String, String?>?, |
63 | | - /** |
64 | | - * Data associated with the change. |
65 | | - * |
66 | | - * For PUT, this is contains all non-null columns of the row. |
67 | | - * |
68 | | - * For PATCH, this is contains the columns that changed. |
69 | | - * |
70 | | - * For DELETE, this is null. |
71 | | - */ |
72 | | - val data: Map<String, Any?>?, |
| 60 | + val opData: SqliteRow?, |
73 | 61 | /** |
74 | 62 | * Previous values before this change. |
75 | 63 | * |
76 | 64 | * These values can be tracked for `UPDATE` statements when [Table.trackPreviousValues] is |
77 | 65 | * enabled. |
78 | 66 | */ |
79 | | - @Deprecated("Use typedPreviousValues instead", replaceWith = ReplaceWith("typedPreviousValues")) |
80 | | - val previousValues: Map<String, String?>? = null, |
81 | | - /** |
82 | | - * Previous values before this change. |
83 | | - * |
84 | | - * These values can be tracked for `UPDATE` statements when [Table.trackPreviousValues] is |
85 | | - * enabled. |
86 | | - */ |
87 | | - val typedPreviousValues: Map<String, Any?>? = null, |
| 67 | + val previousValues: SqliteRow? = null, |
88 | 68 | ) { |
89 | 69 | public companion object { |
90 | 70 | public fun fromRow(row: CrudRow): CrudEntry { |
91 | 71 | val data = JsonUtil.json.parseToJsonElement(row.data).jsonObject |
92 | | - val opData = data["data"]?.asData() |
93 | | - val previousValues = data["old"]?.asData() |
94 | | - |
95 | 72 | return CrudEntry( |
96 | 73 | id = data["id"]!!.jsonPrimitive.content, |
97 | 74 | clientId = row.id.toInt(), |
98 | 75 | op = UpdateType.fromJsonChecked(data["op"]!!.jsonPrimitive.content), |
99 | | - opData = opData?.toStringMap(), |
100 | | - data = opData, |
| 76 | + opData = data["data"]?.let { SerializedRow(it.jsonObject) }, |
101 | 77 | table = data["type"]!!.jsonPrimitive.content, |
102 | 78 | transactionId = row.txId, |
103 | 79 | metadata = data["metadata"]?.jsonPrimitive?.content, |
104 | | - typedPreviousValues = previousValues, |
105 | | - previousValues = previousValues?.toStringMap(), |
| 80 | + previousValues = data["old"]?.let { SerializedRow(it.jsonObject) }, |
106 | 81 | ) |
107 | 82 | } |
108 | | - |
109 | | - private fun JsonElement.asData(): Map<String, Any?> = |
110 | | - jsonObject.mapValues { (_, value) -> |
111 | | - val primitive = value.jsonPrimitive |
112 | | - if (primitive === JsonNull) { |
113 | | - null |
114 | | - } else if (primitive.isString) { |
115 | | - primitive.content |
116 | | - } else { |
117 | | - primitive.content.jsonNumberOrBoolean() |
118 | | - } |
119 | | - } |
120 | | - |
121 | | - private fun String.jsonNumberOrBoolean(): Any = |
122 | | - when { |
123 | | - this == "true" -> true |
124 | | - this == "false" -> false |
125 | | - this.any { char -> char == '.' || char == 'e' || char == 'E' } -> this.toDouble() |
126 | | - else -> this.toInt() |
127 | | - } |
128 | | - |
129 | | - private fun Map<String, Any?>.toStringMap(): Map<String, String> = mapValues { (_, v) -> v.toString() } |
130 | 83 | } |
131 | 84 |
|
132 | 85 | override fun toString(): String = "CrudEntry<$transactionId/$clientId ${op.toJson()} $table/$id $opData>" |
|
0 commit comments