where method

Generator where(
  1. String columnName,
  2. dynamic value, {
  3. Operator operator = Operator.equal,
  4. String? conjuncation,
})
inherited

Specify 'where' conditions in query.

var userEloquent = UserEloquent();
//get users where name is john
userEloquent.where('name','john').get();

//get users where name is john and createdAt greater than   2022-05-03
userEloquent.where('name','john').where('createdAt','2022-05-03', operator:Operator.greaterThan).get();

//get users where name is not john
userEloquent.where('name','john',operator:Operator.notEqual).get();

//get users where name has 'j'
userEloquent.where('name','%j%',operator:Operator.like).get();

Implementation

Generator where(String columnName, value,
    {Operator operator = Operator.equal, String? conjuncation}) {
  String? _operator;
  switch (operator) {
    case Operator.equal:
      _operator = '=';
      break;

    case Operator.greaterThan:
      _operator = '>';
      break;

    case Operator.lessThan:
      _operator = '<';
      break;

    case Operator.notEqual:
      _operator = '!=';
      break;

    case Operator.like:
      _operator = 'LIKE';
      break;

    case Operator.notLike:
      _operator = 'NOT LIKE';
      break;

    case Operator.inArray:
      if (value is! List) {
        throw Exception('Value must be List type.');
      }
      String temp = '';
      List values = value;
      values.asMap().entries.forEach((element) {
        temp += '"${element.value.toString()}"';
        if (element.key != values.length - 1) {
          temp += ',';
        }
      });
      value = temp;
      _operator = 'IN';
      break;
    case Operator.notInArray:
      if (value is! List) {
        throw Exception('Value must be List type.');
      }
      String temp = '';
      List values = value;
      values.asMap().entries.forEach((element) {
        temp += '"${element.value.toString()}"';
        if (element.key != values.length - 1) {
          temp += ',';
        }
      });
      value = temp;
      _operator = 'NOT IN';
      break;
  }
  _wheres.add(_Where(
      columnName: columnName,
      value: value.toString(),
      operator: _operator,
      conjunction: conjuncation ?? 'and'));
  return this;
}