File: sesame2queryresultiteratorbackend.cpp

package info (click to toggle)
soprano 2.9.4%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,008 kB
  • ctags: 5,755
  • sloc: cpp: 38,602; xml: 155; ansic: 143; makefile: 25; java: 14
file content (202 lines) | stat: -rw-r--r-- 5,403 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * This file is part of Soprano Project.
 *
 * Copyright (C) 2007-2010 Sebastian Trueg <trueg@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "sesame2queryresultiteratorbackend.h"
#include "sesame2iterator.h"
#include "sesame2utils.h"
#include "sesame2types.h"
#include "sesame2bindingset.h"
#include "sesame2model.h"
#include "jniwrapper.h"

#include "statement.h"
#include "node.h"

#include <QtCore/QDebug>
#include <QtCore/QStringList>


class Soprano::Sesame2::QueryResultIteratorBackend::Private
{
public:
    Private( bool result )
        : result( 0 ),
          booleanResult( result ),
          isTupleResult( false ),
          isBooleanResult( true ) {
    }

    Private( const JObjectRef& result_ )
        : result( new Iterator( result_ ) ),
          booleanResult( false ),
          isBooleanResult( false ) {
        isTupleResult = JNIWrapper::instance()->env()->IsInstanceOf( result_,
                                                                     JNIWrapper::instance()->env()->FindClass( ORG_OPENRDF_QUERY_TUPLEQUERYRESULT ) );

        // cache the binding names, it is just simpler
        if ( isTupleResult ) {
            JObjectRef bindingList = result->callObjectMethod( result->getMethodID( "getBindingNames", "()L"JAVA_UTIL_LIST";" ) );
            JNIObjectWrapper listWrapper( bindingList );
            Iterator it( listWrapper.callObjectMethod( listWrapper.getMethodID( "iterator", "()L"JAVA_UTIL_ITERATOR";" ) ) );
            while ( it.hasNext() ) {
                bindingNames.append( JStringRef( it.next() ).toQString() );
            }
        }
    }

    Iterator* result;
    bool booleanResult;

    bool isTupleResult;
    bool isBooleanResult;

    Statement currentStatement;
    BindingSet currentBindings;

    QStringList bindingNames;

    const Model* model;
};


Soprano::Sesame2::QueryResultIteratorBackend::QueryResultIteratorBackend( const JObjectRef& result, const Model* model )
    : d( new Private( result ) )
{
    d->model = model;
}


Soprano::Sesame2::QueryResultIteratorBackend::QueryResultIteratorBackend( bool result, const Model* model )
    : d( new Private( result ) )
{
    d->model = model;
}


Soprano::Sesame2::QueryResultIteratorBackend::~QueryResultIteratorBackend()
{
    close();
    delete d->result;
    delete d;
}


bool Soprano::Sesame2::QueryResultIteratorBackend::next()
{
    if ( d->isBooleanResult ) {
        return false;
    }

    if ( d->result->hasNext() ) {
        JObjectRef next = d->result->next();
        if ( next ) {
            if ( d->isTupleResult ) {
                d->currentBindings.setObject( next );
            }
            else {
                d->currentStatement = convertStatement( next );
            }

            return true;
        }
    }

    // if there is an exception d->result returns false and it
    // is sufficient to catch it once here
    setError( JNIWrapper::instance()->convertAndClearException() );
    return false;
}


Soprano::Statement Soprano::Sesame2::QueryResultIteratorBackend::currentStatement() const
{
    return d->currentStatement;
}


Soprano::Node Soprano::Sesame2::QueryResultIteratorBackend::binding( const QString &name ) const
{
    // make sure we do not crash
    if ( d->currentBindings.object() ) {
        JObjectRef node = d->currentBindings.getValue( JStringRef( name ) );
        setError( JNIWrapper::instance()->convertAndClearException() );
        return convertNode( node );
    }
    else {
        setError( "Invalid iterator" );
        return Node();
    }
}


Soprano::Node Soprano::Sesame2::QueryResultIteratorBackend::binding( int offset ) const
{
    return binding( d->bindingNames[offset] );
}


int Soprano::Sesame2::QueryResultIteratorBackend::bindingCount() const
{
    return d->bindingNames.count();
}


QStringList Soprano::Sesame2::QueryResultIteratorBackend::bindingNames() const
{
    return d->bindingNames;
}


bool Soprano::Sesame2::QueryResultIteratorBackend::isGraph() const
{
    return !d->isTupleResult && !d->isBooleanResult;
}


bool Soprano::Sesame2::QueryResultIteratorBackend::isBinding() const
{
    return d->isTupleResult;
}


bool Soprano::Sesame2::QueryResultIteratorBackend::isBool() const
{
    return d->isBooleanResult;
}


bool Soprano::Sesame2::QueryResultIteratorBackend::boolValue() const
{
    return d->booleanResult;
}


void Soprano::Sesame2::QueryResultIteratorBackend::close()
{
    if ( d->model ) {
        if ( d->result ) {
            d->result->close();
        }
        setError( JNIWrapper::instance()->convertAndClearException() );
        d->model->removeIterator( this );
        d->model = 0;
    }
}