summaryrefslogtreecommitdiffstats
path: root/kexi/migration/pqxx/pqxxmigrate.h
blob: c09c8a7a9c2cdb37e55c0136e006c8d7a5d08c67 (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2004 Adam Pigg <adam@piggz.co.uk>
   Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>

   This program 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 program 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 program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef PQXXIMPORT_H
#define PQXXIMPORT_H

#include <migration/keximigrate.h>

//Kexi Includes
#include <kexidb/field.h>
#include <kexidb/connection.h>

#include <pqxx/pqxx>

namespace KexiMigration
{
	class PqxxMigrate : public KexiMigrate
	{
		Q_OBJECT
		KEXIMIGRATION_DRIVER

		public:
			PqxxMigrate(QObject *parent, const char *name, const QStringList &args = QStringList());
			virtual ~PqxxMigrate();
			
		protected:
			//Driver specific function to return table names
			virtual bool drv_tableNames(QStringList& tablenames);
			
			//Driver specific implementation to read a table schema
			virtual bool drv_readTableSchema(
				const QString& originalName, KexiDB::TableSchema& tableSchema);
			
			//Driver specific connection implementation
			virtual bool drv_connect();
			virtual bool drv_disconnect();

			virtual tristate drv_queryStringListFromSQL(
				const QString& sqlStatement, uint columnNumber, QStringList& stringList, 
				int numRecords = -1);

			/*! Fetches single record from result obtained 
			 by running \a sqlStatement.
			 \a firstRecord should be first initialized to true, so the method can run
			 the query at first call and then set it will set \a firstRecord to false,
			 so subsequent calls will only fetch records.
			 On success the result is stored in \a data and true is returned,
			 \a data is resized to appropriate size. cancelled is returned on EOF. */
//! @todo SQL-dependent!
			virtual tristate drv_fetchRecordFromSQL(const QString& sqlStatement, 
				KexiDB::RowData& data, bool &firstRecord);

			virtual bool drv_copyTable(const QString& srcTable, 
				KexiDB::Connection *destConn, KexiDB::TableSchema* dstTable);
		
		private:
			//lowlevel functions/objects
			//database connection
			pqxx::connection* m_conn;

			//transaction
			pqxx::nontransaction* m_trans;

			//lowlevel result
			pqxx::result* m_res;

			//! Used in drv_fetchRecordFromSQL
			pqxx::result::const_iterator m_fetchRecordFromSQL_iter;

			//perform a query on the database
			bool query(const QString& statement);

			//Clear the result info
			void clearResultInfo ();
			
			pqxx::oid tableOid(const QString& tablename);
			
			//Convert the pqxx type to a kexi type
			KexiDB::Field::Type type(int t, const QString& fname);
			
			//Find out the field constraints
			//Return whether or not the field is a pkey
			bool primaryKey(pqxx::oid table, int col) const;
			
			//Return whether or not the field is unique
			bool uniqueKey(pqxx::oid table, int col) const;
			
			//Return whether or not the field is a foreign key
			bool foreignKey(pqxx::oid table, int col) const;
			
			//Return whether or not the field is not null
			bool notNull(pqxx::oid table, int col) const;
			
			//Return whether or not the field is not empty
			bool notEmpty(pqxx::oid table, int col) const;
			
			//Return whether or not the field is auto incrementing
			bool autoInc(pqxx::oid table, int col) const;
		
	};
}

#endif