~ubuntu-branches/debian/jessie/ugene/jessie

« back to all changes in this revision

Viewing changes to src/include/U2Core/U2DbiUtils.h

  • Committer: Package Import Robot
  • Author(s): Steffen Moeller
  • Date: 2011-11-02 13:29:07 UTC
  • mfrom: (1.2.1) (3.1.11 natty)
  • Revision ID: package-import@ubuntu.com-20111102132907-o34gwnt0uj5g6hen
Tags: 1.9.8+repack-1
* First release to Debian
  - added README.Debian
  - increased policy version to 3.9.2
  - added URLs for version control system
* Added debug package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * UGENE - Integrated Bioinformatics Tools.
 
3
 * Copyright (C) 2008-2011 UniPro <ugene@unipro.ru>
 
4
 * http://ugene.unipro.ru
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
19
 * MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
#ifndef _U2_DBI_UTILS_H_
 
23
#define _U2_DBI_UTILS_H_
 
24
 
 
25
#include <U2Core/U2Dbi.h>
 
26
#include <U2Core/Task.h>
 
27
 
 
28
namespace U2 {
 
29
 
 
30
class U2OpStatus;
 
31
 
 
32
/** 
 
33
    Helper class that allocates connection in constructor and automatically releases it in the destructor 
 
34
    It uses app-global connection pool.
 
35
 
 
36
    Note: DbiHandle caches U2OpStatus and reuses it in destructor on DBI release. Ensure that 
 
37
    U2OpStatus live range contains DbiHandle live range
 
38
*/
 
39
class U2CORE_EXPORT DbiHandle {
 
40
public:
 
41
    /** Opens connection to existing DBI */
 
42
    DbiHandle(U2DbiFactoryId id, const QString& url,  U2OpStatus& os);
 
43
    
 
44
    /** Opens connection to existing DBI or create news DBI*/
 
45
    DbiHandle(U2DbiFactoryId id, const QString& url,  bool create, U2OpStatus& os);
 
46
 
 
47
    DbiHandle(const DbiHandle & dbiHandle_);
 
48
 
 
49
    ~DbiHandle();
 
50
 
 
51
    U2Dbi* dbi;
 
52
    U2OpStatus& os;
 
53
private: //TODO
 
54
    DbiHandle & operator=(const DbiHandle & DbiHandle_);
 
55
};
 
56
 
 
57
 
 
58
/**
 
59
    Iterator over buffered data set
 
60
*/
 
61
template<class T> class BufferedDbiIterator : public U2DbiIterator<T> {
 
62
public:
 
63
    BufferedDbiIterator(const QList<T>& _buffer, const T& _errValue = T()) : buffer(_buffer), pos(0), errValue(_errValue) {}
 
64
 
 
65
    /** returns true if there are more reads to iterate*/
 
66
    virtual bool hasNext() {
 
67
        return pos < buffer.size();
 
68
    }
 
69
 
 
70
    /** returns next read and shifts one element*/
 
71
    virtual T next() {
 
72
        if (!hasNext()) {
 
73
            return errValue;
 
74
        }
 
75
        const T& res = buffer.at(pos);
 
76
        pos++;
 
77
        return res;
 
78
    }
 
79
 
 
80
    /** returns next read without shifting*/
 
81
    virtual T peek() {
 
82
        if (!hasNext()) {
 
83
            return errValue;
 
84
        }
 
85
        return buffer.at(pos);
 
86
 
 
87
    }
 
88
 
 
89
private:
 
90
    QList<T>    buffer;
 
91
    int         pos;
 
92
    T           errValue;
 
93
};
 
94
 
 
95
 
 
96
class U2CORE_EXPORT U2DbiUtils : public QObject{
 
97
    Q_OBJECT
 
98
public:
 
99
    /** 
 
100
        Logs that operation called is not supported by DBI 
 
101
        If U2OpStatus has no error set, sets the error message
 
102
    */
 
103
    static void logNotSupported(U2DbiFeature f, U2Dbi* dbi, U2OpStatus& os);
 
104
 
 
105
    template<class T> static QList<T> toList(U2DbiIterator<T>* it);
 
106
};
 
107
 
 
108
 
 
109
template<class T> QList<T> U2DbiUtils::toList(U2DbiIterator<T>* it) {
 
110
    QList<T> result;
 
111
    while (it->hasNext()) {
 
112
        result << it->next();
 
113
    }
 
114
    return result;
 
115
}
 
116
 
 
117
}// namespace
 
118
 
 
119
#endif