Skip to content

Commit

Permalink
Migrate QuestionSyncAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
antweb committed Jul 25, 2024
1 parent 3980be5 commit 04b5936
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 114 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package eu.pretix.libpretixsync.sync

import app.cash.sqldelight.TransactionWithoutReturn
import app.cash.sqldelight.db.QueryResult
import eu.pretix.libpretixsync.api.PretixApi
import eu.pretix.libpretixsync.db.Migrations
import eu.pretix.libpretixsync.sqldelight.Question
import eu.pretix.libpretixsync.sqldelight.SyncDatabase
import eu.pretix.libpretixsync.sync.SyncManager.ProgressFeedback
import eu.pretix.libpretixsync.utils.JSONUtils
import org.json.JSONException
import org.json.JSONObject

class QuestionSyncAdapter(
db: SyncDatabase,
fileStorage: FileStorage,
eventSlug: String,
api: PretixApi,
syncCycleId: String,
feedback: ProgressFeedback?,
) : SqBaseConditionalSyncAdapter<Question, Long>(
db = db,
fileStorage = fileStorage,
eventSlug = eventSlug,
api = api,
syncCycleId = syncCycleId,
feedback = feedback,
) {

override fun getResourceName(): String = "questions"

override fun getId(obj: Question): Long = obj.server_id!!

override fun getId(obj: JSONObject): Long = obj.getLong("id")

override fun getJSON(obj: Question): JSONObject = JSONObject(obj.json_data!!)

override fun queryKnownIDs(): MutableSet<Long>? {
val res = mutableSetOf<Long>()
db.questionQueries.selectServerIdsByEventSlug(event_slug = eventSlug).execute { cursor ->
while (cursor.next().value) {
val id = cursor.getLong(0) ?: throw RuntimeException("id column not available")
res.add(id)
}

QueryResult.Unit
}

return res
}

override fun insert(jsonobj: JSONObject) {
val questionId = db.questionQueries.transactionWithResult {
db.questionQueries.insert(
event_slug = eventSlug,
json_data = jsonobj.toString(),
position = jsonobj.getLong("position"),
required = jsonobj.optBoolean("required", false),
server_id = jsonobj.getLong("id"),
)
db.compatQueries.getLastInsertedQuestionId().executeAsOne()
}

upsertItemRelations(questionId, emptySet(), jsonobj)
}

override fun update(obj: Question, jsonobj: JSONObject) {
val existingRelations = db.questionQueries.selectRelationsForQuestion(obj.id)
.executeAsList()
.map { it.ItemId }
.toSet()

db.questionQueries.updateFromJson(
event_slug = eventSlug,
json_data = jsonobj.toString(),
position = jsonobj.getLong("position"),
required = jsonobj.optBoolean("required", false),
id = obj.id,
)

upsertItemRelations(obj.id, existingRelations, jsonobj)
}

private fun upsertItemRelations(questionId: Long, existingIds: Set<Long>, jsonobj: JSONObject) {
val itemsarr = jsonobj.getJSONArray("items")
val itemids = ArrayList<Long>(itemsarr.length())
for (i in 0 until itemsarr.length()) {
itemids.add(itemsarr.getLong(i))
}
val newIds = db.itemQueries.selectByServerIdListAndEventSlug(
server_id = itemids,
event_slug = eventSlug,
).executeAsList().map { it.id }.toSet()

for (newId in newIds - existingIds) {
db.questionQueries.insertItemRelation(
item_id = newId,
question_id = questionId,
)
}
for (oldId in existingIds - newIds) {
db.questionQueries.deleteItemRelation(
item_id = oldId,
question_id = questionId,
)
}
}

override fun delete(key: Long) {
db.questionQueries.deleteItemRelationsForQuestion(key)
db.questionQueries.deleteByServerId(key)
}

override fun runInTransaction(body: TransactionWithoutReturn.() -> Unit) {
db.questionQueries.transaction(false, body)
}

override fun runBatch(parameterBatch: List<Long>): List<Question> =
db.questionQueries.selectByServerIdListAndEventSlug(
server_id = parameterBatch,
event_slug = eventSlug,
).executeAsList()

@Throws(JSONException::class)
fun standaloneRefreshFromJSON(data: JSONObject) {
val known = db.questionQueries.selectByServerId(data.getLong("id")).executeAsOneOrNull()

// Store object
data.put("__libpretixsync_dbversion", Migrations.CURRENT_VERSION)
data.put("__libpretixsync_syncCycleId", syncCycleId)
if (known == null) {
insert(data)
} else {
val old = JSONObject(known.json_data!!)
if (!JSONUtils.similar(data, old)) {
update(known, data)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ protected void downloadData(ProgressFeedback feedback, Boolean skip_orders, Stri
}
download(new ItemCategorySyncAdapter(db, fileStorage, eventSlug, api, configStore.getSyncCycleId(), feedback));
download(new ItemSyncAdapter(db, fileStorage, eventSlug, api, configStore.getSyncCycleId(), feedback));
download(new QuestionSyncAdapter(dataStore, fileStorage, eventSlug, api, configStore.getSyncCycleId(), feedback));
download(new QuestionSyncAdapter(db, fileStorage, eventSlug, api, configStore.getSyncCycleId(), feedback));
if (profile == Profile.PRETIXPOS) {
download(new QuotaSyncAdapter(dataStore, fileStorage, eventSlug, api, configStore.getSyncCycleId(), feedback, subEvent));
download(new TaxRuleSyncAdapter(db, fileStorage, eventSlug, api, configStore.getSyncCycleId(), feedback));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
selectByServerId:
SELECT *
FROM Question
WHERE server_id = ?;

selectForItem:
SELECT Question.*
FROM Question
Expand All @@ -6,3 +11,63 @@ WHERE Question.id IN (
FROM Question_Item
WHERE Question_Item.ItemId = :item_id
);

selectByServerIdListAndEventSlug:
SELECT *
FROM Question
WHERE server_id IN ? AND event_slug = ?;

selectServerIdsByEventSlug:
SELECT server_id
FROM Question
WHERE event_slug = ?;

deleteByServerId:
DELETE FROM Question
WHERE server_id = ?;

insert:
INSERT INTO Question(
event_slug,
json_data,
"position",
required,
server_id
) VALUES(
?,
?,
?,
?,
?
);

selectRelationsForQuestion:
SELECT *
FROM Question_Item
WHERE QuestionId = :question_id;

insertItemRelation:
INSERT INTO Question_Item(
ItemId,
QuestionId
) VALUES(
:item_id,
:question_id
);

deleteItemRelation:
DELETE FROM Question_Item
WHERE ItemId = :item_id AND QuestionId = :question_id;

deleteItemRelationsForQuestion:
DELETE FROM Question_Item
WHERE QuestionId = :question_id;

updateFromJson:
UPDATE Question
SET
event_slug = ?,
json_data = ?,
"position" = ?,
required = ?
WHERE id = ?;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ SELECT currval('checkin_id_seq') AS Long;
getLastInsertedOrderPositionId:
SELECT currval('orderposition_id_seq') AS Long;

-- Switch to RETURNING once it is also supported by SQLite
getLastInsertedQuestionId:
SELECT currval('question_id_seq') AS Long;

truncateAllTables:
TRUNCATE
BadgeLayout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ SELECT last_insert_rowid();
getLastInsertedOrderPositionId:
SELECT last_insert_rowid();

-- Switch to RETURNING once it is also supported by SQLite
getLastInsertedQuestionId:
SELECT last_insert_rowid();

truncateAllTables {
DELETE FROM BadgeLayout;
DELETE FROM BadgeLayoutItem;
Expand Down
Loading

0 comments on commit 04b5936

Please sign in to comment.