示例#1
0
    def n1ql_query(self, query, *args, **kwargs):
        """
        Issue a "cluster-level" query. This requires that at least one
        connection to a bucket is active.
        :param query: The query string or object
        :param args: Additional arguments to :cb_bmeth:`n1ql_query`

        .. seealso:: :cb_bmeth:`n1ql_query`
        """
        from couchbase_core.n1ql import N1QLQuery
        if not isinstance(query, N1QLQuery):
            query = N1QLQuery(query)

        query.cross_bucket = True

        to_purge = []
        for k, v in self._buckets.items():
            bucket = v()
            if bucket:
                return bucket.n1ql_query(query, *args, **kwargs)
            else:
                to_purge.append(k)

        for k in to_purge:
            del self._buckets[k]

        raise NoBucketError('Must have at least one active bucket for query')
def N1QLFetchAirports(search, field='airportname'):
    query = "SELECT airportname, city FROM `travel-sample` WHERE LOWER(airportname) LIKE $1"
    simple_query = "SELECT meta().id FROM `travel-sample` WHERE LOWER(airportname) LIKE $1"
    param = "%" + search.lower() + "%"
    res = False
    try:
        q = N1QLQuery(query, param)
        res = cluster.n1ql_query(q)

    except Exception as e:
        print(e)

    # except N1QLError as e:
    #     q = N1QLQuery(simple_query, param)
    #     docMetas = bucket.n1ql_query(q)
    #     ids = [meta['id'] for meta in docMetas]
    #     try:
    #         res = bucket.get_multi(ids)
    #     except CBErr.CouchbaseNetworkError as e:
    #         res = e.all_results
    #         failedKeys = [k for k,v in res.items() if v.value == None]
    #         failedReplicas = bucket.get_multi(failedKeys, replica=True)
    #         # TODO: Check for failed gets here too
    #         res.update(failedReplicas)
    return res
    def test_n1ql(self):

        default_bucket = self.cb
        yield from (default_bucket.connect() or asyncio.sleep(0.01))

        q = N1QLQuery("SELECT mockrow")
        it = default_bucket.query(q)
        yield from it.future

        data = list(it)
        self.assertEqual('value', data[0]['row'])
示例#4
0
    def query(self, query, *args, **kwargs):
        """
        Execute a N1QL query.

        This method is mainly a wrapper around the :class:`~.N1QLQuery`
        and :class:`~.N1QLRequest` objects, which contain the inputs
        and outputs of the query.

        Using an explicit :class:`~.N1QLQuery`::

            query = N1QLQuery(
                'SELECT airportname FROM `travel-sample` WHERE city=$1', "Reno")
            # Use this option for often-repeated queries
            query.adhoc = False
            for row in cb.n1ql_query(query):
                print 'Name: {0}'.format(row['airportname'])

        Using an implicit :class:`~.N1QLQuery`::

            for row in cb.n1ql_query(
                'SELECT airportname, FROM `travel-sample` WHERE city="Reno"'):
                print 'Name: {0}'.format(row['airportname'])

        With the latter form, *args and **kwargs are forwarded to the
        N1QL Request constructor, optionally selected in kwargs['iterclass'],
        otherwise defaulting to :class:`~.N1QLRequest`.

        :param query: The query to execute. This may either be a
            :class:`.N1QLQuery` object, or a string (which will be
            implicitly converted to one).
        :param kwargs: Arguments for :class:`.N1QLRequest`.
        :return: An iterator which yields rows. Each row is a dictionary
            representing a single result
        """
        if not isinstance(query, N1QLQuery):
            query = N1QLQuery(query)

        itercls = kwargs.pop('itercls', N1QLRequest)
        return itercls(query, self, *args, **kwargs)
示例#5
0
 def n1ql_query(self, statement, prepared=False):
     try:
         return N1QLQuery(statement, prepared)
     except CouchbaseError as e:
         raise