Example #1
0
//==================================================================================
//Return the value for a given column for the current record - Private const version
QVariant pqxxSqlCursor::pValue(uint pos)const
{	
	if (m_res->size() <= 0)
	{
		KexiDBDrvWarn << "pqxxSqlCursor::value - ERROR: result size not greater than 0" << endl;
		return QVariant();
	}

	if (pos>=(m_fieldCount+(m_containsROWIDInfo ? 1 : 0)))
	{
//		KexiDBDrvWarn << "pqxxSqlCursor::value - ERROR: requested position is greater than the number of fields" << endl;
		return QVariant();
	}

	KexiDB::Field *f = (m_fieldsExpanded && pos<QMIN(m_fieldsExpanded->count(), m_fieldCount)) 
		? m_fieldsExpanded->at(pos)->field : 0;

//	KexiDBDrvDbg << "pqxxSqlCursor::value(" << pos << ")" << endl;

	//from most to least frequently used types:
	if (f) //We probably have a schema type query so can use kexi to determin the row type
	{
		if ((f->isIntegerType()) || (/*ROWID*/!f && m_containsROWIDInfo && pos==m_fieldCount))
		{
			return (*m_res)[at()][pos].as(int());
		}
		else if (f->isTextType())
		{
			return QString::fromUtf8((*m_res)[at()][pos].c_str()); //utf8?
		}
		else if (f->isFPNumericType())
		{
			return (*m_res)[at()][pos].as(double());
		}
		else if (f->typeGroup() == Field::BLOBGroup)
		{
//			pqxx::result::field r = (*m_res)[at()][pos];
//			kdDebug() << r.name() << ", " << r.c_str() << ", " << r.type() << ", " << r.size() << endl;
			return ::pgsqlByteaToByteArray((*m_res)[at()][pos]);
		}
	}
	else // We probably have a raw type query so use pqxx to determin the column type
	{
		return pgsqlCStrToVariant((*m_res)[at()][pos]);
	}

	return QString::fromUtf8((*m_res)[at()][pos].c_str(), (*m_res)[at()][pos].size()); //utf8?
}
Example #2
0
bool SQLitePreparedStatement::execute()
{
#ifdef SQLITE2
//! @todo
#else
	if (!prepared_st_handle)
		return false;
	if (m_resetRequired) {
		res = sqlite3_reset(prepared_st_handle);
		if (SQLITE_OK != res) {
			//! @todo msg?
			return false;
		}
		m_resetRequired = false;
	}

	int arg=1; //arg index counted from 1
	KexiDB::Field *field;

	Field::List _dummy;
	Field::ListIterator itFields(_dummy);
	//for INSERT, we're iterating over inserting values
	//for SELECT, we're iterating over WHERE conditions
	if (m_type == SelectStatement)
		itFields = *m_whereFields;
	else if (m_type == InsertStatement)
		itFields = m_fields->fieldsIterator();
	else
		assert(0); //impl. error

	for (QValueListConstIterator<QVariant> it = m_args.constBegin(); 
		(field = itFields.current()); ++it, ++itFields, arg++)
	{
		if (it==m_args.constEnd() || (*it).isNull()) {//no value to bind or the value is null: bind NULL
			res = sqlite3_bind_null(prepared_st_handle, arg);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
			continue;
		}
		if (field->isTextType()) {
			//! @todo optimize: make a static copy so SQLITE_STATIC can be used
			QCString utf8String((*it).toString().utf8());
			res = sqlite3_bind_text(prepared_st_handle, arg, 
				(const char*)utf8String, utf8String.length(), SQLITE_TRANSIENT /*??*/);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
		}
		else switch (field->type()) {
		case KexiDB::Field::Byte:
		case KexiDB::Field::ShortInteger:
		case KexiDB::Field::Integer:
		{
//! @todo what about unsigned > INT_MAX ?
			bool ok;
			const int value = (*it).toInt(&ok);
			if (ok) {
				res = sqlite3_bind_int(prepared_st_handle, arg, value);
				if (SQLITE_OK != res) {
					//! @todo msg?
					return false;
				}
			}
			else {
				res = sqlite3_bind_null(prepared_st_handle, arg);
				if (SQLITE_OK != res) {
					//! @todo msg?
					return false;
				}
			}
			break;
		}
		case KexiDB::Field::Float:
		case KexiDB::Field::Double:
			res = sqlite3_bind_double(prepared_st_handle, arg, (*it).toDouble());
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
			break;
		case KexiDB::Field::BigInteger:
		{
//! @todo what about unsigned > LLONG_MAX ?
			bool ok;
			Q_LLONG value = (*it).toLongLong(&ok);
			if (ok) {
				res = sqlite3_bind_int64(prepared_st_handle, arg, value);
				if (SQLITE_OK != res) {
					//! @todo msg?
					return false;
				}
			}
			else {
				res = sqlite3_bind_null(prepared_st_handle, arg);
				if (SQLITE_OK != res) {
					//! @todo msg?
					return false;
				}
			}
			break;
		}
		case KexiDB::Field::Boolean:
			res = sqlite3_bind_text(prepared_st_handle, arg, 
				QString::number((*it).toBool() ? 1 : 0).latin1(), 
				1, SQLITE_TRANSIENT /*??*/);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
			break;
		case KexiDB::Field::Time:
			res = sqlite3_bind_text(prepared_st_handle, arg, 
				(*it).toTime().toString(Qt::ISODate).latin1(), 
				sizeof("HH:MM:SS"), SQLITE_TRANSIENT /*??*/);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
			break;
		case KexiDB::Field::Date:
			res = sqlite3_bind_text(prepared_st_handle, arg, 
				(*it).toDate().toString(Qt::ISODate).latin1(), 
				sizeof("YYYY-MM-DD"), SQLITE_TRANSIENT /*??*/);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
			break;
		case KexiDB::Field::DateTime:
			res = sqlite3_bind_text(prepared_st_handle, arg, 
				(*it).toDateTime().toString(Qt::ISODate).latin1(), 
				sizeof("YYYY-MM-DDTHH:MM:SS"), SQLITE_TRANSIENT /*??*/);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
			break;
		case KexiDB::Field::BLOB:
		{
			const QByteArray byteArray((*it).toByteArray());
			res = sqlite3_bind_blob(prepared_st_handle, arg, 
				(const char*)byteArray, byteArray.size(), SQLITE_TRANSIENT /*??*/);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
			break;
		}
		default:
			KexiDBWarn << "PreparedStatement::execute(): unsupported field type: " 
				<< field->type() << " - NULL value bound to column #" << arg << endl;
			res = sqlite3_bind_null(prepared_st_handle, arg);
			if (SQLITE_OK != res) {
				//! @todo msg?
				return false;
			}
		} //switch
	}

	//real execution
	res = sqlite3_step(prepared_st_handle);
	m_resetRequired = true;
	if (m_type == InsertStatement && res == SQLITE_DONE) {
		return true;
	}
	if (m_type == SelectStatement) {
		//fetch result

		//todo
	}
#endif
	return false;
}