How to write a SQLDatabase Driver: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(convert {{doclinkanchor}} to the improved {{doclink}})
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Learning::Demos_and_Examples]]
[[Category:HowTo]]
== Introduction ==
This article explains how to write a new {{DocLink|QSQLDriver}} that can be used in concert with the {{DocLink|QSQLDatabase}} class. In this example we create a driver to read and write CSV files.


= QSQLDatabase CSV Driver =
A rudimentary selection language will be used to perform queries and return lists of {{DocLink|QVariant}}.


This article explains how to write a new QSQLDriver that can be used in concert with the QSQLDatabase class. In this example we create a driver to read and write CSV files.
For the driver we actually need two substantial classes:
 
* The actual driver class inheriting <tt>QSqlDriver</tt> that can be registered with <tt>QSqlDatabase</tt>
* A class holding the result data inheriting {{DocLink|QSqlResult}}
 
So let us start with the driver. Actually we do nothing special but opening a CSV file, reading the contents into a {{DocLink|QStringList}} and interpret some commands like <tt>SELECT</tt>, <tt>INSERT</tt>, <tt>DELETE</tt>, <tt>COMMIT</tt>. The insert and delete commands both manipulate the <tt>QStringList</tt> in memory, the commit command writes the list back to the file on disk.
 
The header for the driver sqlcsvdriver.hpp:
 
<code>
class CSVDriver : public QSqlDriver {
public:
  bool hasFeature( DriverFeature feature ) const;
  bool open(
  const QString& db,
  const QString& user,
  const QString& password,
  const QString& host,
  int port,
  const QString& options );
  void close();
  QSqlResult* createResult() const;
  QFile* getFile();
  QStringList* getContents();
  QString getCsvFileName();
 
private:
  QString csvFilename;
  QFile *f;
  QStringList *contents;
};
</code>
 
Now we head over to the implementation. The first member <tt>hasFeature</tt> is important for the <tt>QSqlDatabase</tt> abstraction layer to get to know what database features the driver supports. There is a list of [http://doc.qt.io/qt-5/qsqldriver.html#DriverFeature-enum driver features] in the {{DocLink|QSqlDriver}} documentation available.
 
In our driver we decide to support the following features:
 
<code>
bool CSVDriver::hasFeature( DriverFeature feature ) const {
switch ( feature ) {
  case QSqlDriver::BLOB:
  case QSqlDriver::PositionalPlaceholders:
  return false;
 
  case QSqlDriver::Unicode:
  case QSqlDriver::Transactions:
  case QSqlDriver::QuerySize:
  return true;
 
  default:
  return false;
}
}
</code>
 
We wanted to stay minimalistic and do not want the features BLOBs (Binary Large OBject) and positional placeholders. Furthermore since the entire CSV file is stored in memory, supporting BLOBs does not seem very practical.
 
Next is the member <tt>open</tt> that takes quite a number of arguments because we need to cover different cases for different database engines. In some cases you need to supply username and password, in our case for a simple CSV file not. So the first parameter we pass as newDB is just a string containing the path to the CSV file that will be opened and accessed by QFile.
 
<code>
bool CSVDriver::open(
const QString& newDb,
const QString& newUser = "nobody",
const QString& newPassword = "anonymous",
const QString& newHost = "localhost",
int newPort = -1,
const QString& newOptions = "nop" ) {
  csvFilename = newDb;
  user = newUser;
  password = newPassword;
  host = newHost;
  options = newOptions;
  port = newPort;


A rudimentary selection language will be used to perform queries and return QVariant lists.
  f = new QFile( csvFilename );
  contents = new QStringList();


For the driver we actually need two substantial classes:
  if ( !f->open( QIODevice::ReadWrite | QIODevice::Text ) ) {
  setLastError( QSqlError( "Unable to open Backingstore: " + csvFilename, "[CSVDRIVER]",
    QSqlError::ConnectionError, 42 ) );
  return false;
  }


* The actual driver class inheriting &quot;QSqlDriver&amp;quot;:http://doc.qt.io/qt-5/qsqldriver.html that can be registered with &quot;QSqlDatabase&amp;quot;:http://doc.qt.io/qt-5/qsqldatabase.html
  while ( !f->atEnd() ) {
* A class holding the result data inheriting &quot;QSqlResult&amp;quot;:http://doc.qt.io/qt-5/qsqlresult.html
  contents->append( QString( f->readLine() ).trimmed() );
  }
  f->close();
  setOpen( true );
  setOpenError( false );
  return true;
}
</code>


So let us start with the driver. Actually we do nothing special but opening a CSV file, reading the contents into a QStringList and interpret some commands like &quot;select&amp;quot;, &quot;insert&amp;quot;, &quot;delete&amp;quot;, &quot;commit&amp;quot;. The insert and delete commands both manipulate the QStringList in memory, the commit command writes the changed list back to the file on disk.
After we read all the contents of the file line by line into a QStringList we return true to signal success. In case the file could not be opened we raise a <tt>{{DocLink|QSqlError}}::ConnectionError</tt> by a call to {{DocLink|QsqlError|setLastError}}.


The header for the driver sqlcsvdriver.hpp:
Important here is to set the flags <tt>setOpen</tt> and <tt>setOpenError</tt> accordingly at the end of this member.


<code><br />class CSVDriver : public QSqlDriver {<br /> public:<br /> bool hasFeature( DriverFeature feature ) const;<br /> bool open(<br /> const QString&amp;amp; db,<br /> const QString&amp;amp; user,<br /> const QString&amp;amp; password,<br /> const QString&amp;amp; host,<br /> int port,<br /> const QString&amp;amp; options<br /> );<br /> void close();<br /> QSqlResult* createResult() const;<br /> QFile* getFile&amp;amp;#40;&amp;#41;;<br /> QStringList* getContents();<br /> QString getCsvFileName();
<code>
void CSVDriver::close() {
if ( isOpen() ) {
  delete contents;
  delete f;
  setOpen( false );
  setOpenError( false );
}
}


private:<br /> QString csvFilename;<br /> QFile *f;<br /> QStringList '''contents;<br />};<br /></code>
QSqlResult* CSVDriver::createResult() const {
<br />Now we head over to the implementation. The first member &quot;hasFeature&amp;quot; is important for the QSqlDatabase abstraction to get to know what database features the driver supports. There is a list of driver features ( http://doc.qt.io/qt-5/qsqldriver.html#DriverFeature-enum ) in the QSqlDriver documentation available.
return new CSVResult( this );
<br />In our driver we decide to support the following features:
}
<br /><code><br />bool CSVDriver::hasFeature( DriverFeature feature ) const {<br /> switch ( feature ) {<br /> case QSqlDriver::BLOB:<br /> case QSqlDriver::PositionalPlaceholders:<br /> return false;<br /> case QSqlDriver::Unicode:<br /> case QSqlDriver::Transactions:<br /> case QSqlDriver::QuerySize:<br /> return true;<br /> default:<br /> return false;<br /> }<br />}<br /></code>
</code>
<br />We wanted to stay minimalistic and did not want the features BLOBs (Binary Large OBject) and positional placeholders. Furthermore since the entire CSV file is stored in memory, supporting BLOBs does not seem very practical.
<br />Next is the member &quot;open&amp;quot; that takes quite a number of arguments because we need to cover different cases for different database engines. In some cases you need to supply username and password, in our case for a simple CSV file not. So the first parameter we pass as newDB is just a string containing the path to the CSV file that will be opened and accessed by QFile.
<br /><code><br />bool CSVDriver::open(<br /> const QString&amp;amp; newDb,<br /> const QString&amp;amp; newUser = &quot;nobody&amp;quot;,<br /> const QString&amp;amp; newPassword = &quot;anonymous&amp;quot;,<br /> const QString&amp;amp; newHost = &quot;localhost&amp;quot;,<br /> int newPort = <s>1,<br /> const QString&amp;amp; newOptions = &quot;nop&amp;quot;<br /> ) {<br /> csvFilename = newDb;<br /> user = newUser;<br /> password = newPassword;<br /> host = newHost;<br /> options = newOptions;<br /> port = newPort;
<br /> f = new QFile&amp;amp;#40; csvFilename &amp;#41;;<br /> contents = new QStringList();
<br /> if ( !f</s>&gt;open( QIODevice::ReadWrite | QIODevice::Text ) ) {<br /> setLastError( QSqlError( &quot;Unable to open Backingstore: &quot; + csvFilename, &quot;[CSVDRIVER]&quot;,<br /> QSqlError::ConnectionError, 42 ) );<br /> return false;<br /> }
<br /> while ( !f-&gt;atEnd() ) {<br /> contents-&gt;append( QString( f-&gt;readLine() ).trimmed() );<br /> }
<br /> f-&gt;close();<br /> setOpen( true );<br /> setOpenError( false );
<br /> return true;<br />}<br /></code>
<br />After we read all the contents of the file line by line into a QStringList we return true to signal success. In case the file could not be opened we raise a QSqlError::ConnectionError by a call to &quot;setLastError&amp;quot;.
<br />Important here is to set the flags setOpen and setOpenError accordingly at the end of this member.
<br /><code><br />void CSVDriver::close() {<br /> if ( isOpen() ) {<br /> delete contents;<br /> delete f;
<br /> setOpen( false );<br /> setOpenError( false );<br /> }<br />}
<br />QSqlResult''' CSVDriver::createResult() const {<br /> return new CSVResult( this );<br />}<br /></code>


The rest of the members are almost self-explanatory. Close does the opposite of open in opposite order: Delete the file object and the string list and set the flags.
The rest of the members are almost self-explanatory. Close does the opposite of open in opposite order: Delete the file object and the string list and set the flags.
Line 40: Line 121:
CreateResults returns a new CSVResult object what takes us to the header file csvresult.hpp we need:
CreateResults returns a new CSVResult object what takes us to the header file csvresult.hpp we need:


<code><br />#include &lt;QtCore&amp;gt;<br />#include &lt;QtSql&amp;gt;
<code>
#include <QtCore>
#include <QtSql>


#include &quot;sqlcsvdriver.hpp&amp;quot;
#include "sqlcsvdriver.hpp"


class CSVResult : public QSqlResult {
class CSVResult : public QSqlResult {


public:<br /> CSVResult( const QSqlDriver* driver );
public:
  CSVResult( const QSqlDriver* driver );


protected:<br /> QVariant data( int index );<br /> bool isNull( int );<br /> bool reset( const QString&amp;amp; query );<br /> bool fetch( int );<br /> bool fetchFirst();<br /> bool fetchNext();<br /> bool fetchLast();<br /> int size();<br /> int numRowsAffected();
protected:
  QVariant data( int index );
  bool isNull( int );
  bool reset( const QString& query );
  bool fetch( int );
  bool fetchFirst();
  bool fetchNext();
  bool fetchLast();
  int size();
  int numRowsAffected();


private:<br /> QString csvFilename;<br /> QStringList *contents;<br />};<br /></code>
private:
  QString csvFilename;
  QStringList *contents;
};
</code>


The CSVResult inherits QSqlResult and we need to re-implement the following methods: data returns a QVariant of the data of the current record. The fetch~ method move the internal pointer to the first/next/previous/last record. The members size and numRowsAffected are self-explanatory.
The <tt>CSVResult</tt> inherits <tt>QSqlResult</tt> and we need to re-implement the following methods:


In reset() the actual work is done why I would like to step through it:
* <tt>data()</tt> returns a QVariant of the data of the current record
* The <tt>fetch~()</tt> methods move the internal pointer to the first/next/previous/last record.
* The members size and numRowsAffected are self-explanatory.
* In <tt>reset()</tt> the actual work is done why I would like to step through it:


The query string normally consists of two tokens: The command followed by a space followed by a parameter (e.g. a record number)
The query string normally consists of two tokens: The command followed by a space followed by a parameter (e.g. a record number)


<code><br />bool CSVResult::reset( const QString&amp;amp; query ) {<br /> QString data;<br /> int space = query.indexOf( &quot; &quot; );
<code>
bool CSVResult::reset( const QString& query ) {
QString data;
int space = query.indexOf( " " );


if ( space &gt; –1 ) {<br /> data = query.mid( space+1 );<br /> }<br /></code>
if ( space > –1 ) {
  data = query.mid( space+1 );
}
</code>


If the command has a parametr and we encountered a space, store the parameter in &quot;data&amp;quot;. Sure, we could have used a QRegExp here and used the capturing groups but this was developed when I was young and you know…
If the command has a parameter and we encountered a space, store the parameter in "data". Sure, we could have used a QRegExp here and used the capturing groups but this was developed when I was young and you know...


Now we decide what to do if the query string starts with a select command:<br />Select moves the cursor to index –1 so that a call to fetchNext will return the first record at index 0. SetAt sets the internal record pointer to an integer index.
Now we decide what to do if the query string starts with a <tt>SELECT</tt> command:
Select moves the cursor to index -1 so that a call to <tt>fetchNext</tt> will return the first record at index 0. <tt>SetAt</tt> sets the internal record pointer to an integer index.


Don't forget to set this query active (has records to be retrieved) with setActive to inform the QSqlDatabase instance. Furthermore we set the SELECT flag to true.
Don't forget to set this query active (has records to be retrieved) with <tt>setActive</tt> to inform the QSqlDatabase instance. Furthermore we set the SELECT flag to true.


<code><br /> if ( query.startsWith( &quot;select&amp;quot; ) ) {<br /> if ( at() &gt; –1 ) {<br /> setAt( <s>1 );<br /> }<br /> setSelect( true );<br /> setActive( true );
<code>
<br /> return true;<br /> } else<br /></code>
if ( query.startsWith( "select" ) ) {
<br />Data is being appended to the contents string list
  if ( at() > –1 ) {
<br /><code><br /> if ( query.startsWith( &quot;insert&amp;quot; ) ) {<br /> contents</s>&gt;append( data );<br /> setActive( true );<br /> return true;<br /> } else<br /></code>
  setAt( -1 );
  }
  setSelect( true );
  setActive( true );
  return true;
} else
</code>


The commit command writes the in-memory stringlist to the<br />underlying file.<br />If payload to commit is not empty take that as the output<br />filename to write the data back to
Data is being appended to the contents string list


<code><br /> if ( query.startsWith( &quot;commit&amp;quot; ) ) {<br /> QFile *f = new QFile&amp;amp;#40; !data.isEmpty(&amp;#41; ? data : csvFilename );<br /> if ( !f-&gt;open( QIODevice::WriteOnly | QIODevice::Text ) ) {<br /> setLastError(<br /> QSqlError(<br /> &quot;Unable to commit: The CSV file could not be openend for write.&quot;,<br /> &quot;[CSVDRIVER]&quot;,<br /> QSqlError::ConnectionError,<br /> 102<br /> )<br /> );<br /> return false;<br /> }
<code>
if ( query.startsWith( "insert" ) ) {
  contents->append( data );
  setActive( true );
  return true;
} else
</code>


foreach( QString buffer, *contents ) {<br /> f-&gt;write( buffer.toAscii() + &quot;&quot; );<br /> }
The commit command writes the in-memory stringlist to the underlying file. If payload to commit is not empty take that as the output filename to write the data back to


f-&gt;close();<br /> delete f;
<code>
if ( query.startsWith( "commit" ) ) {
  QFile *f = new QFile( !data.isEmpty() ? data : csvFilename );
  if ( !f->open( QIODevice::WriteOnly | QIODevice::Text ) ) {
  setLastError( QSqlError(
    "Unable to commit: The CSV file could not be openend for write.",
    "[CSVDRIVER]",
    QSqlError::ConnectionError,
    102
  );
  );
  return false;
}


return true;<br /> } else<br /></code>
foreach( QString buffer, *contents ) {
  f->write( buffer.toAscii() + "" );
}
 
f->close();
delete f;
 
return true;
} else
</code>


Delete the item specified at the index
Delete the item specified at the index


<code><br /> if ( query.startsWith( &quot;delete&amp;quot; ) ) {<br /> // if no argument given, return false<br /> if ( data.isEmpty() ) {<br /> setLastError(<br /> QSqlError(<br /> &quot;No delete index supplied&amp;quot;,<br /> &quot;[CSVDRIVER]&quot;,<br /> QSqlError::StatementError,<br /> 101<br /> )<br /> );<br /> return false;<br /> }
<code>
 
if ( query.startsWith( "delete" ) ) {
// try cast to real int from string<br /> int index = data.toInt();
  // if no argument given, return false
  if ( data.isEmpty() ) {
  setLastError( QSqlError( "No delete index supplied", "[CSVDRIVER]",
    QSqlError::StatementError, 101 );
  );
  return false;
}


// if cast fails it is 0 but doesn't harm due to<br /> // following constraint check<br /> if ( index &gt; <s>1 &amp;&amp; index &lt; contents</s>&gt;size() ) {
// try cast to real int from string
int index = data.toInt();


// if within range do remove<br /> contents-&gt;removeAt( index );<br /> return true;<br /> } else {<br /> setLastError(<br /> QSqlError(<br /> &quot;Delete index out of valid range: &quot; + QString::number( index ),<br /> &quot;[CSVDRIVER]&quot;,<br /> QSqlError::StatementError,<br /> 100<br /> )<br /> );<br /> return false;<br /> }<br /> } else {<br /> // return false on non-implemented commands<br /> // set last SQL error to QSqlError::StatementError<br /> setLastError(<br /> QSqlError(<br /> &quot;Command not implemented: &quot; + query,<br /> &quot;[CSVDRIVER]&quot;,<br /> QSqlError::StatementError,<br /> 43<br /> )<br /> );<br /> return false;<br /> }<br />}<br /></code>
// if cast fails it is 0 but doesn't harm due to
// following constraint check
if ( index > -1 && index < contents->size() ) {
  // if within range do remove
  contents->removeAt( index );
  return true;
} else {
  setLastError( QSqlError( "Delete index out of valid range: " + QString::number( index ),
  "[CSVDRIVER]", QSqlError::StatementError, 100 )
  );
  return false;
}
} else {
// return false on non-implemented commands
// set last SQL error to QSqlError::StatementError
setLastError(
  QSqlError( "Command not implemented: " + query, "[CSVDRIVER]",
  QSqlError::StatementError, 43 ) );
  return false;
}
}
</code>


== The end… ==
== The end... ==


If you do not agree with all this you might want to join<br />&quot;this project on gitorious&amp;quot;:https://www.gitorious.org/sxw-qt-projects/qsqlplaindriver/ and send me some merge requests.
If you do not agree with all this you might want to join
[https://www.gitorious.org/sxw-qt-projects/qsqlplaindriver/ this project on gitorious] and send me some merge requests.

Latest revision as of 09:31, 23 October 2015

Introduction

This article explains how to write a new QSQLDriver that can be used in concert with the QSQLDatabase class. In this example we create a driver to read and write CSV files.

A rudimentary selection language will be used to perform queries and return lists of QVariant.

For the driver we actually need two substantial classes:

  • The actual driver class inheriting QSqlDriver that can be registered with QSqlDatabase
  • A class holding the result data inheriting QSqlResult

So let us start with the driver. Actually we do nothing special but opening a CSV file, reading the contents into a QStringList and interpret some commands like SELECT, INSERT, DELETE, COMMIT. The insert and delete commands both manipulate the QStringList in memory, the commit command writes the list back to the file on disk.

The header for the driver sqlcsvdriver.hpp:

class CSVDriver : public QSqlDriver {
 public:
  bool hasFeature( DriverFeature feature ) const;
  bool open(
   const QString& db,
   const QString& user,
   const QString& password,
   const QString& host,
   int port,
   const QString& options );
  void close();
  QSqlResult* createResult() const;
  QFile* getFile();
  QStringList* getContents();
  QString getCsvFileName();

 private:
  QString csvFilename;
  QFile *f;
  QStringList *contents;
};

Now we head over to the implementation. The first member hasFeature is important for the QSqlDatabase abstraction layer to get to know what database features the driver supports. There is a list of driver features in the QSqlDriver documentation available.

In our driver we decide to support the following features:

bool CSVDriver::hasFeature( DriverFeature feature ) const {
 switch ( feature ) {
  case QSqlDriver::BLOB:
  case QSqlDriver::PositionalPlaceholders:
   return false;

  case QSqlDriver::Unicode:
  case QSqlDriver::Transactions:
  case QSqlDriver::QuerySize:
   return true;

  default:
   return false;
 }
}

We wanted to stay minimalistic and do not want the features BLOBs (Binary Large OBject) and positional placeholders. Furthermore since the entire CSV file is stored in memory, supporting BLOBs does not seem very practical.

Next is the member open that takes quite a number of arguments because we need to cover different cases for different database engines. In some cases you need to supply username and password, in our case for a simple CSV file not. So the first parameter we pass as newDB is just a string containing the path to the CSV file that will be opened and accessed by QFile.

bool CSVDriver::open(
 const QString& newDb,
 const QString& newUser = "nobody",
 const QString& newPassword = "anonymous",
 const QString& newHost = "localhost",
 int newPort = -1,
 const QString& newOptions = "nop" ) {
  csvFilename = newDb;
  user = newUser;
  password = newPassword;
  host = newHost;
  options = newOptions;
  port = newPort;

  f = new QFile( csvFilename );
  contents = new QStringList();

  if ( !f->open( QIODevice::ReadWrite | QIODevice::Text ) ) {
   setLastError( QSqlError( "Unable to open Backingstore: " + csvFilename, "[CSVDRIVER]",
    QSqlError::ConnectionError, 42 ) );
   return false;
  }

  while ( !f->atEnd() ) {
   contents->append( QString( f->readLine() ).trimmed() );
  }
  f->close();
  setOpen( true );
  setOpenError( false );
  return true;
}

After we read all the contents of the file line by line into a QStringList we return true to signal success. In case the file could not be opened we raise a QSqlError::ConnectionError by a call to setLastError.

Important here is to set the flags setOpen and setOpenError accordingly at the end of this member.

void CSVDriver::close() {
 if ( isOpen() ) {
  delete contents;
  delete f;
  setOpen( false );
  setOpenError( false );
 }
}

QSqlResult* CSVDriver::createResult() const {
 return new CSVResult( this );
}

The rest of the members are almost self-explanatory. Close does the opposite of open in opposite order: Delete the file object and the string list and set the flags.

CreateResults returns a new CSVResult object what takes us to the header file csvresult.hpp we need:

#include <QtCore>
#include <QtSql>

#include "sqlcsvdriver.hpp"

class CSVResult : public QSqlResult {

 public:
  CSVResult( const QSqlDriver* driver );

 protected:
  QVariant data( int index );
  bool isNull( int );
  bool reset( const QString& query );
  bool fetch( int );
  bool fetchFirst();
  bool fetchNext();
  bool fetchLast();
  int size();
  int numRowsAffected();

 private:
  QString csvFilename;
  QStringList *contents;
};

The CSVResult inherits QSqlResult and we need to re-implement the following methods:

  • data() returns a QVariant of the data of the current record
  • The fetch~() methods move the internal pointer to the first/next/previous/last record.
  • The members size and numRowsAffected are self-explanatory.
  • In reset() the actual work is done why I would like to step through it:

The query string normally consists of two tokens: The command followed by a space followed by a parameter (e.g. a record number)

bool CSVResult::reset( const QString& query ) {
 QString data;
 int space = query.indexOf( " " );

 if ( space > 1 ) {
  data = query.mid( space+1 );
 }

If the command has a parameter and we encountered a space, store the parameter in "data". Sure, we could have used a QRegExp here and used the capturing groups but this was developed when I was young and you know...

Now we decide what to do if the query string starts with a SELECT command: Select moves the cursor to index -1 so that a call to fetchNext will return the first record at index 0. SetAt sets the internal record pointer to an integer index.

Don't forget to set this query active (has records to be retrieved) with setActive to inform the QSqlDatabase instance. Furthermore we set the SELECT flag to true.

 if ( query.startsWith( "select" ) ) {
  if ( at() > 1 ) {
   setAt( -1 );
  }
  setSelect( true );
  setActive( true );
  return true;
 } else

Data is being appended to the contents string list

 if ( query.startsWith( "insert" ) ) {
  contents->append( data );
  setActive( true );
  return true;
 } else

The commit command writes the in-memory stringlist to the underlying file. If payload to commit is not empty take that as the output filename to write the data back to

 if ( query.startsWith( "commit" ) ) {
  QFile *f = new QFile( !data.isEmpty() ? data : csvFilename );
  if ( !f->open( QIODevice::WriteOnly | QIODevice::Text ) ) {
   setLastError( QSqlError(
    "Unable to commit: The CSV file could not be openend for write.",
    "[CSVDRIVER]",
    QSqlError::ConnectionError,
    102
   );
  );
  return false;
 }

 foreach( QString buffer, *contents ) {
  f->write( buffer.toAscii() + "" );
 }

 f->close();
 delete f;

 return true;
} else

Delete the item specified at the index

 if ( query.startsWith( "delete" ) ) {
  // if no argument given, return false
  if ( data.isEmpty() ) {
   setLastError( QSqlError( "No delete index supplied", "[CSVDRIVER]",
    QSqlError::StatementError, 101 );
  );
  return false;
 }

 // try cast to real int from string
 int index = data.toInt();

 // if cast fails it is 0 but doesn't harm due to
 // following constraint check
 if ( index > -1 && index < contents->size() ) {
  // if within range do remove
  contents->removeAt( index );
  return true;
 } else {
  setLastError( QSqlError( "Delete index out of valid range: " + QString::number( index ),
   "[CSVDRIVER]", QSqlError::StatementError, 100 )
  );
  return false;
 }
} else {
 // return false on non-implemented commands
 // set last SQL error to QSqlError::StatementError
 setLastError(
  QSqlError( "Command not implemented: " + query, "[CSVDRIVER]",
   QSqlError::StatementError, 43 ) );
  return false;
 }
}

The end...

If you do not agree with all this you might want to join this project on gitorious and send me some merge requests.