Hyperledger Composer中的查询构建器

时间:2018-07-20 10:48:25

标签: hyperledger-composer

我正在开发事务中的动态查询。 这是我的代码

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; 

我在日志中打印了此查询,它显示为

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))

但是它抛出错误

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

任何人都可以帮助我找到错误。无法破解。

2 个答案:

答案 0 :(得分:1)

可以正确构建查询的代码示例:

模型文件

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

交易功能

/**
* 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

答案 1 :(得分:0)

您将需要在WHERE子句中的字符串周围使用<section id="anchor"></section>

选择org.zcon.healthcare。患者在哪里((firstName == “ Tony” )AND(lastName == “ Stark” )等