diff --git a/src/helpers/DatabaseHelper.ts b/src/helpers/DatabaseHelper.ts index d9c6581..1336402 100644 --- a/src/helpers/DatabaseHelper.ts +++ b/src/helpers/DatabaseHelper.ts @@ -7,6 +7,11 @@ import { conf } from "./ConfigHelper"; * @author Pierre HUBERT */ +export enum JoinType { + NORMAL, + LEFT +} + export interface JoinTableInfo { table: string, tableAlias ?: string, @@ -16,6 +21,7 @@ export interface JoinTableInfo { export interface QueryInformation { table: string, tableAlias?: string, + joinType ?: JoinType, joins ?: Array, fields ?: Array, where ?: Object, @@ -98,6 +104,10 @@ export class DatabaseHelper { // Joins condition if(info.joins) { + + if(info.joinType == JoinType.LEFT) + request += " LEFT "; + info.joins.forEach(join => { request += " JOIN " + join.table + (join.tableAlias ? " " + join.tableAlias : "") + " ON " + join.condition }); diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index 966783f..38b7886 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -1,5 +1,5 @@ import { Survey, SurveyChoice } from "../entities/Survey"; -import { DatabaseHelper } from "./DatabaseHelper"; +import { DatabaseHelper, JoinType } from "./DatabaseHelper"; import { NewSurvey } from "../entities/NewSurvey"; import { mysql_date } from "../utils/DateUtils"; @@ -79,6 +79,7 @@ export class SurveyHelper { const choices = await DatabaseHelper.Query({ table: SURVEY_CHOICES_TABLE, tableAlias: "c", + joinType: JoinType.LEFT, joins: [ { table: SURVEY_RESPONSE_TABLE, @@ -90,7 +91,7 @@ export class SurveyHelper { "c.ID_sondage": survey.id }, groupBy: "c.ID", - fields: ["c.*", "COUNT(*) AS count_choice"] + fields: ["c.*", "COUNT(r.ID) AS count_choice"] }); choices.forEach((row) => survey.choices.push(this.DBToSurveyChoice(row)));