Skip to content

Commit

Permalink
Rectifies a confusing issue with the field ordering in RetrievableWri…
Browse files Browse the repository at this point in the history
…ter and RetrievableInitializer.
  • Loading branch information
ppanopticon committed Feb 7, 2025
1 parent 7f9e3b5 commit 9ae4336
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ internal class RetrievableInitializer(private val connection: PgVectorConnection
}

/* Create 'relationship' entity. */
this.connection.jdbc.prepareStatement(/* sql = postgres */ "CREATE TABLE IF NOT EXISTS ${RELATIONSHIP_ENTITY_NAME} ($OBJECT_ID_COLUMN_NAME uuid NOT NULL, $PREDICATE_COLUMN_NAME VARCHAR(100) NOT NULL, $SUBJECT_ID_COLUMN_NAME uuid NOT NULL, PRIMARY KEY ($OBJECT_ID_COLUMN_NAME, $PREDICATE_COLUMN_NAME, $SUBJECT_ID_COLUMN_NAME), FOREIGN KEY($OBJECT_ID_COLUMN_NAME) REFERENCES $RETRIEVABLE_ENTITY_NAME($RETRIEVABLE_ID_COLUMN_NAME) ON DELETE CASCADE, FOREIGN KEY($SUBJECT_ID_COLUMN_NAME) REFERENCES $RETRIEVABLE_ENTITY_NAME($RETRIEVABLE_ID_COLUMN_NAME) ON DELETE CASCADE);")
.use {
it.execute()
}
this.connection.jdbc.prepareStatement(/* sql = postgres */ "CREATE TABLE IF NOT EXISTS ${RELATIONSHIP_ENTITY_NAME} ($SUBJECT_ID_COLUMN_NAME uuid NOT NULL, $PREDICATE_COLUMN_NAME VARCHAR(100) NOT NULL, $OBJECT_ID_COLUMN_NAME uuid NOT NULL), PRIMARY KEY ($SUBJECT_ID_COLUMN_NAME, $PREDICATE_COLUMN_NAME, $OBJECT_ID_COLUMN_NAME), FOREIGN KEY($OBJECT_ID_COLUMN_NAME) REFERENCES $RETRIEVABLE_ENTITY_NAME($RETRIEVABLE_ID_COLUMN_NAME) ON DELETE CASCADE, FOREIGN KEY($SUBJECT_ID_COLUMN_NAME) REFERENCES $RETRIEVABLE_ENTITY_NAME($RETRIEVABLE_ID_COLUMN_NAME) ON DELETE CASCADE);").use {
it.execute()
}
} catch (e: SQLException) {
LOGGER.error(e) { "Failed to initialize entity due to exception." }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ internal class RetrievableWriter(override val connection: PgVectorConnection, pr
*/
override fun connect(relationship: Relationship): Boolean {
try {
this.connection.jdbc.prepareStatement("INSERT INTO $RELATIONSHIP_ENTITY_NAME ($OBJECT_ID_COLUMN_NAME,$PREDICATE_COLUMN_NAME,$SUBJECT_ID_COLUMN_NAME) VALUES (?,?,?)").use { stmt ->
stmt.setObject(1, relationship.objectId)
this.connection.jdbc.prepareStatement("INSERT INTO $RELATIONSHIP_ENTITY_NAME ($SUBJECT_ID_COLUMN_NAME,$PREDICATE_COLUMN_NAME,$OBJECT_ID_COLUMN_NAME) VALUES (?,?,?)").use { stmt ->
stmt.setObject(1, relationship.subjectId)
stmt.setString(2, relationship.predicate)
stmt.setObject(3, relationship.subjectId)
stmt.setObject(3, relationship.objectId)
return stmt.execute()
}
} catch (e: SQLException) {
LOGGER.error(e) { "Failed to insert relationship ${relationship.objectId} -(${relationship.predicate}-> ${relationship.subjectId} due to SQL error." }
LOGGER.error(e) { "Failed to insert relationship ${relationship.subjectId} -(${relationship.predicate}-> ${relationship.objectId} due to SQL error." }
return false
}
}
Expand All @@ -167,11 +167,11 @@ internal class RetrievableWriter(override val connection: PgVectorConnection, pr
this.connection.jdbc.autoCommit = false
var success = true
var batched = 0
this.connection.jdbc.prepareStatement("INSERT INTO $RELATIONSHIP_ENTITY_NAME ($OBJECT_ID_COLUMN_NAME,$PREDICATE_COLUMN_NAME,$SUBJECT_ID_COLUMN_NAME) VALUES (?,?,?)").use { stmt ->
this.connection.jdbc.prepareStatement("INSERT INTO $RELATIONSHIP_ENTITY_NAME ($SUBJECT_ID_COLUMN_NAME,$PREDICATE_COLUMN_NAME,$OBJECT_ID_COLUMN_NAME) VALUES (?,?,?)").use { stmt ->
for (relationship in relationships) {
stmt.setObject(1, relationship.objectId)
stmt.setObject(1, relationship.subjectId)
stmt.setString(2, relationship.predicate)
stmt.setObject(3, relationship.subjectId)
stmt.setObject(3, relationship.objectId)
stmt.addBatch()
batched += 1

Expand Down Expand Up @@ -222,7 +222,7 @@ internal class RetrievableWriter(override val connection: PgVectorConnection, pr
return stmt.execute()
}
} catch (e: SQLException) {
LOGGER.error(e) { "Failed to delete relationship ${relationship.objectId} -(${relationship.predicate}-> ${relationship.subjectId} due to SQL error." }
LOGGER.error(e) { "Failed to delete relationship ${relationship.subjectId} -(${relationship.predicate}-> ${relationship.objectId} due to SQL error." }
return false
}
}
Expand Down

0 comments on commit 9ae4336

Please sign in to comment.