public function createQuery($statement, $language, $options = 0)
    {
        // TODO: refactor this to use the odm query builder
        $qb = $this->dm->createPhpcrQueryBuilder()->setFromQuery($statement, $language);
        if ($options & self::QUERY_REPLACE_WITH_FIELDNAMES) {
            $columns = $qb->getColumns();
            if (1 === count($columns)) {
                $column = reset($columns);
                if ('*' === $column->getColumnName() && null == $column->getPropertyName()) {
                    $qb->setColumns([]);
                    foreach ($this->class->getFieldNames() as $name) {
                        $qb->addSelect('a', $name);
                    }
                }
            }
        }

        $factory = $qb->getQOMFactory();

        $comparison = $factory->comparison(
            $factory->propertyValue('a', 'phpcr:class'), Constants::JCR_OPERATOR_EQUAL_TO, $factory->literal($this->className)
        );

        $qb->andWhere($comparison);

        return new Query($qb->getQuery(), $this->getDocumentManager());
    }