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

Endpoint composition xml update and loader enhancements for Conformance and Constraints #1406

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4,902 changes: 2,451 additions & 2,451 deletions docs/zap-schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 54 additions & 28 deletions src-electron/db/query-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,54 +938,80 @@ async function insertAtomics(db, packageId, data) {
* @param {*} context - The context containing the mandatory device type to check against.
* @returns A promise resolved with the result of the database insert operation.
*/
function insertEndpointComposition(db, composition, context) {
if (parseInt(context.mandatoryDeviceTypes, 16) === composition.code) {
return dbApi.dbInsert(
db,
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
[dbEnum.composition.mandatoryEndpoint, composition.code],
)
} else {
return dbApi.dbInsert(
db,
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
[composition.compositionType, composition.code],
)
async function insertEndpointComposition(db, composition, context) {
try {
if (parseInt(context.mandatoryDeviceTypes, 16) === composition.code) {
return await dbApi.dbInsert(
db,
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
[dbEnum.composition.mandatoryEndpoint, composition.code],
)
} else {
return await dbApi.dbInsert(
db,
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
[composition.compositionType, composition.code],
)
}
} catch (error) {
console.error('Error inserting endpoint composition:', error)
throw error // Re-throw the error after logging it
}
}

/**
* Asynchronously retrieves the ID of an endpoint composition based on its code.
* Retrieves the endpoint composition ID by device code.
*
* This function executes a SQL query to fetch the endpoint composition ID
* associated with a given device code. If the query fails, an error is logged.
*
* @param {Object} db - The database connection object.
* @param {Object} deviceType - An object representing the device type, which contains the 'code' property.
* @returns {Promise<number|null>} A promise that resolves with the ID of the endpoint composition if found, or null otherwise.
* @param {Object} deviceType - The device type object containing the device code.
* @returns {Promise<number|null>} The endpoint composition ID or null if not found.
*/
async function getEndpointCompositionIdByCode(db, deviceType) {
const query =
'SELECT ENDPOINT_COMPOSITION_ID FROM ENDPOINT_COMPOSITION WHERE CODE = ?'
const result = await dbApi.dbGet(db, query, [deviceType.code])
return result ? result.ENDPOINT_COMPOSITION_ID : null
try {
const result = await dbApi.dbGet(db, query, [deviceType.code])
return result ? result.ENDPOINT_COMPOSITION_ID : null
} catch (error) {
console.error('Error retrieving endpoint composition ID:', error)
return null
}
}

/**
* Inserts a new device composition record into the database.
* Inserts a device composition record into the DEVICE_COMPOSITION table.
*
* This function constructs an SQL INSERT query to add a new record to the
* DEVICE_COMPOSITION table. It handles the insertion of the device code,
* endpoint composition reference, conformance, and constraint values.
* Note that the "CONSTRAINT" column name is escaped with double quotes
* to avoid conflicts with the SQL reserved keyword.
*
* @param {Object} db - The database connection object.
* @param {Object} deviceType - An object representing the device type, which contains the 'childDeviceId' property.
* @param {number} endpointCompositionId - The ID of the endpoint composition associated with this device composition.
* @returns {Promise} A promise that resolves with the result of the database insertion operation.
* @param {Object} deviceType - The device type object containing the data to be inserted.
* @param {number} endpointCompositionId - The ID of the endpoint composition.
* @returns {Promise} A promise that resolves when the insertion is complete.
*/

function insertDeviceComposition(db, deviceType, endpointCompositionId) {
const insertQuery = `
INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF)
VALUES (?, ?)
INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF, CONFORMANCE, DEVICE_CONSTRAINT)
VALUES (?, ?, ?, ?)
`
return dbApi.dbInsert(db, insertQuery, [
parseInt(deviceType.childDeviceId, 16),
endpointCompositionId,
])
try {
return dbApi.dbInsert(db, insertQuery, [
parseInt(deviceType.childDeviceId, 16),
endpointCompositionId,
deviceType.conformance,
deviceType.constraint,
])
} catch (error) {
console.error('Error inserting device composition:', error)
throw error // Re-throw the error after logging it
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src-electron/db/zap-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,11 @@ CREATE TABLE IF NOT EXISTS "ENDPOINT_COMPOSITION" (

Columns:
DEVICE_COMPOSITION_ID: The primary key of the table, auto-incremented for each new record.
CODE: An integer representing the device code.
DEVICE_TYPE_REF: An integer that acts as a foreign key to reference a specific device type.
ENDPOINT_COMPOSITION_REF: A foreign key linking to the ENDPOINT_COMPOSITION table to specify the endpoint composition associated with this device.
CONFORMANCE: A text field describing the conformance level of the device composition.
CONSTRAINT: An integer representing any constraints applied to the device composition.
DEVICE_CONSTRAINT: An integer representing any constraints applied to the device composition.

Foreign Key Constraints:
The DEVICE_TYPE_REF column references the DEVICE_TYPE_ID column of the DEVICE_TYPE table. On deletion of a device type, corresponding records in this table are deleted (CASCADE).
Expand All @@ -409,7 +410,7 @@ CREATE TABLE IF NOT EXISTS "DEVICE_COMPOSITION" (
"DEVICE_TYPE_REF" integer,
"ENDPOINT_COMPOSITION_REF" integer,
"CONFORMANCE" text,
"CONSTRAINT" integer,
"DEVICE_CONSTRAINT" integer,
FOREIGN KEY ("ENDPOINT_COMPOSITION_REF") REFERENCES "ENDPOINT_COMPOSITION"("ENDPOINT_COMPOSITION_ID") ON DELETE CASCADE
FOREIGN KEY ("DEVICE_TYPE_REF") REFERENCES "DEVICE_TYPE"("DEVICE_TYPE_ID") ON DELETE CASCADE
);
Expand Down
Loading
Loading