Execute the appropriate method for a query

@param \Muffin\Webservice\Query $query The query to execute
@param array $options The options to use
@return bool|int|\Muffin\Webservice\ResultSet
512483
protected function _executeQuery(Query $query, array $options = [])
    {
        switch ($query->action()) {
            case Query::ACTION_CREATE:
                return $this->_executeCreateQuery($query, $options);
            case Query::ACTION_READ:
                return $this->_executeReadQuery($query, $options);
            case Query::ACTION_UPDATE:
                return $this->_executeUpdateQuery($query, $options);
            case Query::ACTION_DELETE:
                return $this->_executeDeleteQuery($query, $options);
        }

        return false;
    }
1086768
protected function performUpdate($query, array $options = [])
    {
        $this->compileWheres();
        $result = $this->query->update($query)->run();

        if (0 == (int) $result['errors']) {
            return $result['replaced'];
        }

        return 0;
    }
979078
public function delete($query, array $options = []) {
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$query = $params['query'];
			$params = $query->export($this);
			list($_path, $conditions) = $params['conditions'];
			$data = $query->data();

			if (!empty($data['rev'])) {
				$conditions['rev'] = $data['rev'];
			}
			$result = $this->connection->delete(
				"{$this->_config['database']}/{$_path}",
				$conditions
			);
			$result = json_decode($result);
			$result = (isset($result->ok) && $result->ok === true);

			if ($query->entity()) {
				$query->entity()->sync(null, [], ['dematerialize' => true]);
			}
			return $result;
		});
	}
1065058
protected function performUpdate($query, array $options = array(), array $response = array())
	{
		$wheres = $this->compileWheres();

		//Update one.
		if (isset($query['_id']))
		{
			$response = $this->collection->update($this->formatQuery($query));
		}
		//Update many.
		else
		{
			foreach ($this->get() as $entity)
			{
				$response[] = $this->collection->update(array_merge($entity, $this->formatQuery($query)));
			}
		}

		return count($response);
	}
705836
public function create($query, array $options = []) {
		$defaults = ['model' => $query->model()];
		$options += $defaults;
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$request = ['type' => 'json'];
			$query = $params['query'];
			$options = $params['options'];
			$data = $query->data();
			$data += ['type' => $options['model']::meta('source')];

			if (isset($data['id'])) {
				return $this->update($query, $options);
			}

			$retry = false;
			do {
				$result = $this->connection->post($this->_config['database'], $data, $request);
				$result = is_string($result) ? json_decode($result, true) : $result;

				$retry = $retry ? !$retry : $this->_autoBuild($result);
			} while ($retry);

			if (isset($result['_id']) || (isset($result['ok']) && $result['ok'] === true)) {
				$result = $this->_format($result, $options);
				$query->entity()->sync($result['id'], $result);
				return true;
			}
			return false;
		});
	}
159240
public static function get($url, $query = [], $options = [])
    {
        $options['query'] = $query;
        return self::doRequest('get', $url, $options);
    }
48885
protected function assignOptions($query, Options $options)
    {
        if ($options->hasLimit()) {
            /*
             * For mysql queries, to allow counting rows we must prepend
             * SQL_CALC_FOUND_ROWS to the select quantifiers
             */
            if ($options->getLimit() > 0) {
                $calc_found_rows = 'SQL_CALC_FOUND_ROWS';
                if (!preg_match("/$calc_found_rows/", $query)) {
                    $q = trim($query);
                    $query = preg_replace('/^select\b/i', "SELECT $calc_found_rows", $q);
                }
            }

            // mysql only, @todo make it rule everything (use traits)
            $replace_regexp = "LIMIT[\s]+[\d]+((\s*,\s*\d+)|(\s+OFFSET\s+\d+)){0,1}";

            $search_regexp = "$replace_regexp";
            if (!preg_match("/$search_regexp$/i", $query)) {
                // Limit is not already present
                $query .= ' LIMIT ' . $options->getLimit();
            } else {
                $query = preg_replace("/($replace_regexp)/i", 'LIMIT ' . $options->getLimit(), $query);
            }

            if ($options->hasOffset()) {
                $query .= ' OFFSET ' . $options->getOffset();
            }
        }

        return $query;
    }
1142631
private function queryArray($query, $options, $fetcher)
    {
        $statement = $this->_runQuery($query, $options);
        $result = call_user_func($fetcher, $statement);
        return is_array($result) ? $result : [];
    }
736211
public function read($query, array $options = []) {
		$query = !is_object($query) ? new Query() : $query;
		$query->method() ?: $query->method("get");
		$query->path() ?: $query->path("/{:source}");

		$params = [$query, $options];

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			list($query, $options) = $params;
			return $this->send($query, $options);
		});
	}
675346
public function queryAssoc($query, $options = array())
    {
        $statement = $this->_runQuery($query, $options);
        return $statement->fetch(\PDO::FETCH_ASSOC);
    }
381366
public function select(Query $query)
    {
        $options = $query->getOptions();

        $where = $this->parseWhere($query, $options['where']);
        $query = new MongoQuery($where, $options);

        $this->log('find', $where, $options);

        return $query;
    }
1275785
public function findByTag(Query $query, array $options) {
		if (!isset($options[$this->getConfig('finderField')])) {
			throw new RuntimeException('Key not present');
		}
		$slug = $options[$this->getConfig('finderField')];
		if (empty($slug)) {
			return $query;
		}
		$query->matching($this->getConfig('tagsAlias'), function (QueryInterface $q) use ($slug) {
			return $q->where([
				$this->getConfig('tagsAlias') . '.slug' => $slug,
			]);
		});

		return $query;
	}
84915
public function getNativeQuery(Query $query, array $options = array())
    {
        // Clone the original SimpleExpression
        $statement = clone $query->getStatement();

        // Apply CustomVisitors to modify SimpleExpression
        $customVisitors = $this->getCustomVisitors();
        if($customVisitors) {
            $statement->dispatch($customVisitors);
        }

        // Apply OuterVisitor to output the nativeQuery from SimpleExpression
        $outerVisitor = $this->getOuterVisitor();
        $statement->dispatch($outerVisitor);
        
        // Get NativeQuery from the Visitor
        return $outerVisitor->getNativeQuery($options);
    }
145272
public function queryTable($query, $options = array())
    {
        return $this->queryArray($query, $options,
            function(\PDOStatement $statement) { return $statement->fetchAll(\PDO::FETCH_ASSOC); }
        );
    }
54460
public function findFilter(Query $query, array $options)
    {
        foreach ($this->filters()->collection() as $name => $filter) {
            $filter->apply($query, $options);
        }

        return $query;
    }
1345508
public function findCurrent(Query $query, array $options)
    {
        $query
            ->where(['Current.language_id' => Wasabi::contentLanguage()->id])
            ->order(['Current.modified' => 'DESC'])->limit(1);
        return $query;
    }
1368512
public function update($query, array $options = []) {
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$query = $params['query'];
			$exportedQuery = $query->export($this);

			if ($exportedQuery['fields'] === null) {
				return true;
			}

			$sql = $this->renderCommand('update', $exportedQuery, $query);
			$result = (boolean) $this->_execute($sql);

			if ($result && is_object($query) && $query->entity()) {
				$query->entity()->sync();
			}
			return $result;
		});
	}