コード例 #1
0
/*! Copy PostgreSQL table to KexiDB database */
bool PqxxMigrate::drv_copyTable(const QString& srcTable, KexiDB::Connection *destConn,
                                KexiDB::TableSchema* dstTable)
{
    std::vector<std::string> R;

    pqxx::work T(*m_conn, "PqxxMigrate::drv_copyTable");

    pqxx::tablereader stream(T, (srcTable.toLatin1().constData()));

    //Loop round each row, reading into a vector of strings
    const KexiDB::QueryColumnInfo::Vector fieldsExpanded(dstTable->query()->fieldsExpanded());
    for (int n = 0; (stream >> R); ++n) {
        QList<QVariant> vals;
        std::vector<std::string>::const_iterator i, end(R.end());
        int index = 0;
        for (i = R.begin(); i != end; ++i, index++) {
            if (fieldsExpanded.at(index)->field->type() == KexiDB::Field::BLOB
                    || fieldsExpanded.at(index)->field->type() == KexiDB::Field::LongText) {
                vals.append(KexiDB::pgsqlByteaToByteArray((*i).c_str(), (*i).size()));
            } else if (fieldsExpanded.at(index)->field->type() == KexiDB::Field::Boolean) {
                vals.append(QString((*i).c_str()).toLower() == "t" ? QVariant(true) : QVariant(false));
            } else
                vals.append(KexiDB::cstringToVariant((*i).c_str(),
                                                     fieldsExpanded.at(index)->field, (*i).size()));
        }
        if (!destConn->insertRecord(*dstTable, vals))
            return false;
        updateProgress();
        R.clear();
    }

    //This does not work in <libpqxx 2.2
    //stream.complete();

    return true;
}
コード例 #2
0
/*! Copy MySQL table to KexiDB database */
bool MySQLMigrate::drv_copyTable(const QString& srcTable, KexiDB::Connection *destConn, 
	KexiDB::TableSchema* dstTable)
{
	if(d->executeSQL("SELECT * FROM `" + drv_escapeIdentifier(srcTable)) + "`") {
		MYSQL_RES *res = mysql_use_result(d->mysql);
		if (res != NULL) {
			MYSQL_ROW row;
			const KexiDB::QueryColumnInfo::Vector fieldsExpanded( dstTable->query()->fieldsExpanded() );
			while ((row = mysql_fetch_row(res)) != NULL) {
				const int numFields = QMIN((int)fieldsExpanded.count(), (int)mysql_num_fields(res));
				QValueList<QVariant> vals;
				unsigned long *lengths = mysql_fetch_lengths(res);
				if (!lengths) {
					mysql_free_result(res);
					return false;
				}
				for(int i = 0; i < numFields; i++)
					vals.append( KexiDB::cstringToVariant(row[i], fieldsExpanded.at(i)->field, (int)lengths[i]) );
				if (!destConn->insertRecord(*dstTable, vals)) {
					mysql_free_result(res);
					return false;
				}
				updateProgress();
			}
			if (!row && mysql_errno(d->mysql)) {
				mysql_free_result(res);
				return false;
			}
			/*! @todo Check that wasn't an error, rather than end of result set */
			mysql_free_result(res);
		} else {
			kdDebug() << "MySQLMigrate::drv_copyTable: null result" << endl;
		}
		return true;
	} else {
		return false;
	}
}