src/Koldy/Db/Where.php
protected function getWhereSql(array $whereArray = null, $cnt = 0): string
    {
    	if ($this->bindings === null) {
    		$this->bindings = new Bindings();
	    }

        $query = '';

        if ($whereArray === null) {
            $whereArray = $this->where;
        }

        foreach ($whereArray as $index => $where) {
            if ($index > 0) {
                $query .= "\t{$where['link']}";
            }

            $field = $where['field'];
            $value = $where['value'];

            if (gettype($field) == 'object' && $value === null) {
                // function or instance of self is passed, do something

                $q = ($field instanceof self) ? clone $field : $field(new self());
                if ($q === null) {
                    throw new DbException('Can not build query, statement\'s where function didn\'t return anything');
                }

                $whereSql = trim($q->getWhereSql(null, $cnt++));
                $whereSql = str_replace("\n", ' ', $whereSql);
                $whereSql = str_replace("\t", '', $whereSql);

                $query .= " ({$whereSql})\n";

                /*
                foreach ($q->getBindings() as $k => $v) {
                    $this->bindings[$k] = $v;
                }
                */

                $this->bindings->addBindingsFromInstance($q->getBindings());

            } else if ($value instanceof Expr) {
                $query .= " ({$field} {$where['operator']} {$value})\n";

            } else if (is_array($value)) {

                switch ($where['operator']) {
                    case 'BETWEEN':
                    case 'NOT BETWEEN':
                        $query .= " ({$field} {$where['operator']} ";

                        if ($value[0] instanceof Expr) {
                            $query .= $value[0];
                        } else {
                            //$key = $this->bind($field, $value[0]);
	                        $key = $this->bindings->makeAndSet($field, $value[0]);
                            $query .= ":{$key}";
                        }

                        $query .= ' AND ';

                        if ($value[1] instanceof Expr) {
                            $query .= $value[1];
                        } else {
                            //$key = $this->bind($field, $value[1]);
	                        $key = $this->bindings->makeAndSet($field, $value[1]);
                            $query .= ":{$key}";
                        }

                        $query .= ")\n";
                        break;

                    case 'IN':
                    case 'NOT IN':
                        $query .= " ({$field} {$where['operator']} (";

                        foreach ($value as $val) {
                            //$key = $this->bind($field, $val);
	                        $key = $this->bindings->makeAndSet($field, $val);
                            $query .= ":{$key},";
                        }

                        $query = substr($query, 0, -1);
                        $query .= "))\n";
                        break;

                    // default: nothing by default
                }

            } else {
                //$key = $this->bind($field, $where['value']);
	            $key = $this->bindings->makeAndSet($field, $where['value']);
                $query .= " ({$field} {$where['operator']} :{$key})\n";

            }

        }

        return $query;
    }
protected function getWhereSql(array $whereArray = null, $cnt = 0): string
    {
    	if ($this->bindings === null) {
    		$this->bindings = new Bindings();
	    }

        $query = '';

        if ($whereArray === null) {
            $whereArray = $this->where;
        }

        foreach ($whereArray as $index => $where) {
            if ($index > 0) {
                $query .= "\t{$where['link']}";
            }

            $field = $where['field'];
            $value = $where['value'];

            if (gettype($field) == 'object' && $value === null) {
                // function or instance of self is passed, do something

                $q = ($field instanceof self) ? clone $field : $field(new self());
                if ($q === null) {
                    throw new DbException('Can not build query, statement\'s where function didn\'t return anything');
                }

                $whereSql = trim($q->getWhereSql(null, $cnt++));
                $whereSql = str_replace("\n", ' ', $whereSql);
                $whereSql = str_replace("\t", '', $whereSql);

                $query .= " ({$whereSql})\n";

                /*
                foreach ($q->getBindings() as $k => $v) {
                    $this->bindings[$k] = $v;
                }
                */

                $this->bindings->addBindingsFromInstance($q->getBindings());

            } else if ($value instanceof Expr) {
                $query .= " ({$field} {$where['operator']} {$value})\n";

            } else if (is_array($value)) {

                switch ($where['operator']) {
                    case 'BETWEEN':
                    case 'NOT BETWEEN':
                        $query .= " ({$field} {$where['operator']} ";

                        if ($value[0] instanceof Expr) {
                            $query .= $value[0];
                        } else {
                            //$key = $this->bind($field, $value[0]);
	                        $key = $this->bindings->makeAndSet($field, $value[0]);
                            $query .= ":{$key}";
                        }

                        $query .= ' AND ';

                        if ($value[1] instanceof Expr) {
                            $query .= $value[1];
                        } else {
                            //$key = $this->bind($field, $value[1]);
	                        $key = $this->bindings->makeAndSet($field, $value[1]);
                            $query .= ":{$key}";
                        }

                        $query .= ")\n";
                        break;

                    case 'IN':
                    case 'NOT IN':
                        $query .= " ({$field} {$where['operator']} (";

                        foreach ($value as $val) {
                            //$key = $this->bind($field, $val);
	                        $key = $this->bindings->makeAndSet($field, $val);
                            $query .= ":{$key},";
                        }

                        $query = substr($query, 0, -1);
                        $query .= "))\n";
                        break;

                    // default: nothing by default
                }

            } else {
                //$key = $this->bind($field, $where['value']);
	            $key = $this->bindings->makeAndSet($field, $where['value']);
                $query .= " ({$field} {$where['operator']} :{$key})\n";

            }

        }

        return $query;
    }
protected function getWhereSql ( array $ whereArray = null , $ cnt = 0 ) : string { if ( $ this -> bindings === null ) { $ this -> bindings = new Bindings ( ) ; } $ query = '' ; if ( $ whereArray === null ) { $ whereArray = $ this -> where ; } foreach ( $ whereArray as $ index => $ where ) { if ( $ index > 0 ) { $ query .= "\t{$where['link']}" ; } $ field = $ where [ 'field' ] ; $ value = $ where [ 'value' ] ; if ( gettype ( $ field ) == 'object' && $ value === null ) { // function or instance of self is passed, do something $ q = ( $ field instanceof self ) ? clone $ field : $ field ( new self ( ) ) ; if ( $ q === null ) { throw new DbException ( 'Can not build query, statement\'s where function didn\'t return anything' ) ; } $ whereSql = trim ( $ q -> getWhereSql ( null , $ cnt ++ ) ) ; $ whereSql = str_replace ( "\n" , ' ' , $ whereSql ) ; $ whereSql = str_replace ( "\t" , '' , $ whereSql ) ; $ query .= " ({$whereSql})\n" ; /*
                foreach ($q->getBindings() as $k => $v) {
                    $this->bindings[$k] = $v;
                }
                */ $ this -> bindings -> addBindingsFromInstance ( $ q -> getBindings ( ) ) ; } else if ( $ value instanceof Expr ) { $ query .= " ({$field} {$where['operator']} {$value})\n" ; } else if ( is_array ( $ value ) ) { switch ( $ where [ 'operator' ] ) { case 'BETWEEN' : case 'NOT BETWEEN' : $ query .= " ({$field} {$where['operator']} " ; if ( $ value [ 0 ] instanceof Expr ) { $ query .= $ value [ 0 ] ; } else { //$key = $this->bind($field, $value[0]); $ key = $ this -> bindings -> makeAndSet ( $ field , $ value [ 0 ] ) ; $ query .= ":{$key}" ; } $ query .= ' AND ' ; if ( $ value [ 1 ] instanceof Expr ) { $ query .= $ value [ 1 ] ; } else { //$key = $this->bind($field, $value[1]); $ key = $ this -> bindings -> makeAndSet ( $ field , $ value [ 1 ] ) ; $ query .= ":{$key}" ; } $ query .= ")\n" ; break ; case 'IN' : case 'NOT IN' : $ query .= " ({$field} {$where['operator']} (" ; foreach ( $ value as $ val ) { //$key = $this->bind($field, $val); $ key = $ this -> bindings -> makeAndSet ( $ field , $ val ) ; $ query .= ":{$key}," ; } $ query = substr ( $ query , 0 , - 1 ) ; $ query .= "))\n" ; break ; // default: nothing by default } } else { //$key = $this->bind($field, $where['value']); $ key = $ this -> bindings -> makeAndSet ( $ field , $ where [ 'value' ] ) ; $ query .= " ({$field} {$where['operator']} :{$key})\n" ; } } return $ query ; }
Get where statement appended to query

@param array $whereArray
@param int $cnt

@throws Exception
@return string
Get where statement appended to query