PKB
PKB

Reputation: 51

query builder in hyperledger composer

Im developing dynamic query in transaction. Here is my code

let queryString = `SELECT org.zcon.healthcare.Patient WHERE(`;
let conditions = [];
if (tx.hasOwnProperty('firstName')) {
    conditions.push(`(firstName == tx.firstName)`)
};
if (tx.hasOwnProperty('lastName')) {
    conditions.push(`(lastName == tx.lastName)`)
};
if (tx.hasOwnProperty('gender')) {
    conditions.push(`(gender == tx.gender)`)
};
if (tx.hasOwnProperty('birthDate')) {
    conditions.push(`(birthDate == tx.birthDate)`)
};
if (tx.hasOwnProperty('ssn')) {
    conditions.push(`(ssn == tx.ssn)`)
};
if (tx.hasOwnProperty('medicalRecordNumber')) {
    conditions.push(`(medicalRecordNumber == tx.medicalRecordNumber)`)
};
queryString += conditions.join(' AND ') + ')';

let query =  buildQuery(queryString);
const searchPatient = await query('query');
if(searchPatient.length ==0){
    throw "No Patient Records found!!"
}else

return searchPatient; 

I printed this query in the log and it shows like

SELECT org.zcon.healthcare.Patient WHERE ((firstName == Tony) AND (lastName == Stark) AND (gender == M) AND (birthDate == 03/16/1991) AND (ssn == 452896325) AND (medicalRecordNumber == 00001))

But it's throwing error

Expected "!", "(", "[", "false", "function", "new", "null", "this", "true", "{", comment, end of line, identifier, number, regular expression, string or whitespace but "0" found. Line 1 column 123 Line 1 column 123

Can anybody help me out to find the error. Not able to crack it.

Upvotes: 0

Views: 145

Answers (2)

Paul O'Mahony
Paul O'Mahony

Reputation: 6740

EXAMPLE OF CODE THAT BUILDS QUERY CORRECTLY:

Model file

transaction qry1 {
  o Patient patient    // which has all the fields outlined above
}

transaction function

/**
* Sample transaction calling dynamic build query
* @param {org.zcon.healthcare.qry1} qtx1 
* @transaction
*/
async function queryHistory(qtx1) 

{

  const serializer = getSerializer();
  const tx = serializer.toJSON(qtx1); // now an object
  // console.log(' firstname is ' + tx.patient.firstName);  // eg. 'joe'



let queryString = 'SELECT org.zcon.healthcare.Patient WHERE(';
let conditions = [];
  if (tx.patient.hasOwnProperty('firstName')) {
    conditions.push( '(firstName == "' + tx.patient.firstName + '")')
};
if (tx.patient.hasOwnProperty('lastName')) {
    conditions.push( '(lastName == "' + tx.patient.lastName + '")' )
};
if (tx.patient.hasOwnProperty('gender')) {
    conditions.push( '(gender  == "' +  tx.patient.gender + '")' )
};
if (tx.hasOwnProperty('birthDate')) {
    conditions.push(`(birthDate == tx.birthDate)`)
};
if (tx.patient.hasOwnProperty('ssn')) {
    conditions.push( '(ssn == "' + tx.patient.ssn + '")' )
};
if (tx.patient.hasOwnProperty('medicalRecordNumber')) {
    conditions.push( '(medicalRecordNumber == "' + tx.patient.medicalRecordNumber + '")')
} ;

queryString += conditions.join(' AND ') + ')';

let q1 =  buildQuery(queryString);
const searchPatient = await query(q1);


// EXAMPLE OF PRINTING ELEMENTS OF THE RESULT SET (USING CONSOLE.LOG):

if(searchPatient.length ==0){
    throw "No Patient Records found!!"
} else {

   console.log(searchPatient);  // results, array of objects
   console.log('element is ' + searchPatient[0]);   // first object
   console.log('element field is ' + searchPatient[0].medicalRecordNumber); // field
 }


// return searchPatient;  // you do not need this 

} // end function

Upvotes: 1

R Thatcher
R Thatcher

Reputation: 5570

You will need "" around the strings in the WHERE clause:

SELECT org.zcon.healthcare.Patient WHERE ((firstName == "Tony") AND (lastName == "Stark") etc

Upvotes: 0

Related Questions