1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
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 #include "MysqlCrossDatabaseReferenceDbi.h"
23 
24 #include <U2Core/U2SafePoints.h>
25 
26 #include "MysqlObjectDbi.h"
27 #include "util/MysqlHelpers.h"
28 
29 namespace U2 {
30 
MysqlCrossDatabaseReferenceDbi(MysqlDbi * dbi)31 MysqlCrossDatabaseReferenceDbi::MysqlCrossDatabaseReferenceDbi(MysqlDbi *dbi)
32     : U2CrossDatabaseReferenceDbi(dbi),
33       MysqlChildDbiCommon(dbi) {
34 }
35 
initSqlSchema(U2OpStatus & os)36 void MysqlCrossDatabaseReferenceDbi::initSqlSchema(U2OpStatus &os) {
37     MysqlTransaction t(db, os);
38 
39     // cross database reference object
40     // factory - remote dbi factory
41     // dbi - remote dbi id (url)
42     // rid  - remote object id
43     // version - remove object version
44     U2SqlQuery("CREATE TABLE CrossDatabaseReference (object BIGINT, factory LONGTEXT NOT NULL, dbi TEXT NOT NULL, "
45                "rid BLOB NOT NULL, version INTEGER NOT NULL, "
46                " FOREIGN KEY(object) REFERENCES Object(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8",
47                db,
48                os)
49         .execute();
50 }
51 
createCrossReference(U2CrossDatabaseReference & reference,const QString & folder,U2OpStatus & os)52 void MysqlCrossDatabaseReferenceDbi::createCrossReference(U2CrossDatabaseReference &reference, const QString &folder, U2OpStatus &os) {
53     MysqlTransaction t(db, os);
54 
55     dbi->getMysqlObjectDbi()->createObject(reference, folder, U2DbiObjectRank_TopLevel, os);
56     CHECK_OP(os, );
57 
58     static const QString queryString = "INSERT INTO CrossDatabaseReference(object, factory, dbi, rid, version) VALUES(:object, :factory, :dbi, :rid, :version)";
59     U2SqlQuery q(queryString, db, os);
60     q.bindDataId(":object", reference.id);
61     q.bindString(":factory", reference.dataRef.dbiRef.dbiFactoryId);
62     q.bindString(":dbi", reference.dataRef.dbiRef.dbiId);
63     q.bindBlob(":rid", reference.dataRef.entityId);
64     q.bindInt64(":version", reference.dataRef.version);
65     q.execute();
66 }
67 
removeCrossReferenceData(const U2DataId & referenceId,U2OpStatus & os)68 void MysqlCrossDatabaseReferenceDbi::removeCrossReferenceData(const U2DataId &referenceId, U2OpStatus &os) {
69     MysqlTransaction t(db, os);
70 
71     static const QString queryString = "DELETE FROM CrossDatabaseReference WHERE object = :object";
72     U2SqlQuery q(queryString, db, os);
73     q.bindDataId(":object", referenceId);
74     q.execute();
75 }
76 
getCrossReference(const U2DataId & objectId,U2OpStatus & os)77 U2CrossDatabaseReference MysqlCrossDatabaseReferenceDbi::getCrossReference(const U2DataId &objectId, U2OpStatus &os) {
78     U2CrossDatabaseReference res(objectId, dbi->getDbiId(), 0);
79 
80     static const QString queryString = "SELECT r.factory, r.dbi, r.rid, r.version, o.name, o.version FROM CrossDatabaseReference AS r, Object AS o WHERE o.id = :id AND r.object = o.id";
81     U2SqlQuery q(queryString, db, os);
82     q.bindDataId(":id", objectId);
83     if (q.step()) {
84         res.dataRef.dbiRef.dbiFactoryId = q.getString(0);
85         res.dataRef.dbiRef.dbiId = q.getString(1);
86         res.dataRef.entityId = q.getBlob(2);
87         res.dataRef.version = q.getInt64(3);
88         res.visualName = q.getString(4);
89         res.version = q.getInt64(5);
90         q.ensureDone();
91     }
92 
93     return res;
94 }
95 
updateCrossReference(const U2CrossDatabaseReference & reference,U2OpStatus & os)96 void MysqlCrossDatabaseReferenceDbi::updateCrossReference(const U2CrossDatabaseReference &reference, U2OpStatus &os) {
97     MysqlTransaction t(db, os);
98 
99     static const QString queryString = "UPDATE CrossDatabaseReference SET factory = :factory, dbi = :dbi, rid = :rid, version = :version WHERE object = :object";
100     U2SqlQuery q(queryString, db, os);
101     q.bindString(":factory", reference.dataRef.dbiRef.dbiFactoryId);
102     q.bindString(":dbi", reference.dataRef.dbiRef.dbiId);
103     q.bindBlob(":rid", reference.dataRef.entityId);
104     q.bindInt64(":version", reference.dataRef.version);
105     q.bindDataId(":object", reference.id);
106     q.execute();
107 }
108 
109 }  // namespace U2
110