Exemple #1
0
DataColumnBase* DataSet::getCellValue( const unsigned int row, const string& columnName )
{
	DataRow* myRow = NULL;

	if ( row >= this->size() )
		throw invalid_argument( "Row index out of bounds" );

	myRow = ( *this )[ row ];
	if ( myRow == NULL )
		throw runtime_error( "Unable to access row" );

	DataColumnBase* result = NULL;
	try
	{
		result = ( *myRow )[ columnName ];

		if ( result == NULL )
		{
			DEBUG( "Warning : cell empty for request : " << row << ", [" << columnName << "]" );
			myRow->DumpHeader();
		}
	}
	catch( ... )
	{
		TRACE( "Invalid name for column : [" << columnName << "]" );
		throw invalid_argument( "columnName" );
	}

	return result;
}
void DataColumn::addRow()
{
    // Creates a pointer to a new DataRow and adds it to the end of the DataColumn
	DataRow* newRow = new DataRow;

    // If firstRow is NULL, list is empty so we need to make the firstRow
    // Else append the new DataRow to the end of the DataColumn
	if ( m_firstRow == NULL )
    {
		newRow->setNext(NULL);
		newRow->setPrev(NULL);

		m_firstRow = newRow;
		m_lastRow = newRow;
	}
	else
    {
		newRow->setNext(NULL);
		newRow->setPrev(m_lastRow);

		m_lastRow->setNext(newRow);

		m_lastRow = newRow;
	}

    // Sets the currentRow pointer to the newly created DataRow for editing purposes later on
	m_currentRow = m_lastRow;

}
Exemple #3
0
void DisjointCoreData<Num,ChunkSize>::set( DataRow& chunk, int current_point, const data_point& point )
{
    chunk.inputs(0,0) = point.y();
    chunk.output[ current_point ] = point.value();
    chunk.logoutput[ current_point ] = point.logoutput();
    chunk.residues[ current_point ] = point.residue();
    xs[ current_point ] = point.x();
}
void DataColumn::removeAll()
{
    // Removes all the rows within the DataColumn
	if ( m_firstRow == NULL )
    {
		return;
	}

	DataRow* rowToDelete;
	DataRow* currentRow = m_firstRow;

	while ( currentRow->getNext() != NULL )
    {

		rowToDelete = currentRow;

		currentRow = currentRow->getNext();

		rowToDelete->setPrev(NULL);
		rowToDelete->setNext(NULL);
		rowToDelete->setText("");

		delete rowToDelete;

	}

	m_firstRow = NULL;
	m_lastRow = NULL;
	m_currentRow = NULL;

}
void DataColumn::recalculateRowLength()
{
    // Goes through the DataRow list in the DataColumn to determine
    // what the longest string in the DataColumn is
    m_rowLength = m_name.length();

    DataRow* currentRow = m_firstRow;

    while ( currentRow != NULL )
    {
        if ( currentRow->getText().length() > m_rowLength )
        {
            m_rowLength = currentRow->getText().length();
        }
        currentRow = currentRow->getNext();
    }

}
Exemple #6
0
DataPoint<Num>
DisjointCoreData<Num,ChunkSize>::get( const DataRow& chunk, int in_chunk ) const
{
    return data_point( 
        xs[in_chunk],
        chunk.inputs(0,0),
        chunk.output[in_chunk],
        chunk.logoutput[in_chunk],
        chunk.residues[in_chunk]);
}
Exemple #7
0
DataColumnBase* DataSet::getCellValue( const unsigned int row, const unsigned int columnIndex )
{
	DataRow* myRow = NULL;

	if ( row >= this->size() )
		throw invalid_argument( "Row index out of bounds" );

	myRow = ( *this )[ row ];
	if ( myRow == NULL )
		throw runtime_error( "Unable to access row" );

	if ( columnIndex > myRow->size() )
		throw invalid_argument( "Column index out of bounds" );

	string columnName = "";
	map< std::string, DataColumnBase* >::iterator myIterator = myRow->begin();
	for ( unsigned int j = 0; j<=columnIndex; myIterator++, j++ )
	{
		columnName = myIterator->first;
	}

	DataColumnBase* result = NULL;
	try
	{
		result = ( *myRow )[ columnName ];

		if ( result == NULL )
		{
			DEBUG( "Warning : cell empty for request : " <<
			       row << ", " << columnIndex << " (" << columnName << ")" );
			myRow->DumpHeader();
		}
	}
	catch( ... )
	{
		throw invalid_argument( "columnIndex" );
	}

	return result;
}
void initDb()
{
	std::string sql;
	
	SqliteHandler dbSqlite(DB_FILE_PATH);
	
	dbSqlite.executeUpdate("CREATE TABLE IF NOT EXISTS settings(id INTEGER PRIMARY KEY AUTOINCREMENT, music_on INTEGER)");
	drSetting = dbSqlite.getFirstRow("SELECT * FROM settings");
	if (drSetting.size() == 0)
	{
		sql = "INSERT INTO settings(music_on) VALUES(1)";
		dbSqlite.executeUpdate(sql);
	}
	
	dbSqlite.executeUpdate("CREATE TABLE IF NOT EXISTS scores(id INTEGER PRIMARY KEY AUTOINCREMENT, best_score INTEGER)");
	drScore = dbSqlite.getFirstRow("SELECT * FROM scores");
	if (drScore.size() == 0)
	{
		sql = "INSERT INTO scores(best_score) VALUES(0)";
		dbSqlite.executeUpdate(sql);
	}
}
void DataColumn::removeRow()
{
    // Removes the current DataRow from the DataColumn and the
    // currentRow pointer will be set to the DataRow after the DataRow you are deleting
    if ( m_firstRow == NULL )
    {
        return;
    }

    DataRow* row;

    // deals with the case if you only have one row in your DataColumn list
    if ( m_firstRow == m_lastRow )
    {

        row = m_firstRow;
        row->setNext(NULL);
        row->setPrev(NULL);
        row->setText("");

        delete row;

        m_firstRow = NULL;
        m_lastRow = NULL;
        m_currentRow = NULL;

        return;

    }

    // deals with if the row up for deletion is the first row or the last row in the DataColumn list
    if ( m_currentRow == m_firstRow )
    {
        row = m_firstRow;

        m_firstRow = m_firstRow->getNext();

        row->setNext(NULL);
        row->setPrev(NULL);
        row->setText("");

        m_firstRow->setPrev(NULL);

        m_currentRow = m_firstRow;

        return;
    }
    else if ( m_currentRow == m_lastRow )
    {

        row = m_lastRow;

        m_lastRow = m_lastRow->getPrev();

        m_lastRow->setNext(NULL);

        row->setNext(NULL);
        row->setPrev(NULL);
        row->setText("");

        m_currentRow = m_lastRow;

        delete row;

        return;
    }

    // deals with a random DataRow in the DataColumn up for deletion
    row = m_currentRow;

    m_currentRow->getPrev()->setNext(m_currentRow->getNext());
    m_currentRow->getNext()->setPrev(m_currentRow->getPrev());

    row->setPrev(NULL);
    row->setNext(NULL);
    row->setText("");

    m_currentRow = m_currentRow->getNext();

    delete row;

}
Exemple #10
0
	int SqliteClient::executeSqlQuery(const string& sql, DataTable& table)
	{
		Locker locker(&_sqliteDbMutex);

#if DEBUG
		Stopwatch sw("executeSqlQuery", 100);
#endif
		sqlite3_stmt *stmt;
		int result = sqlite3_prepare_v2(_sqliteDb, sql.c_str(),  sql.length(), &stmt, 0);  
		if(result != SQLITE_OK)
		{
			printErrorInfo("sqlite3_prepare_v2", sql);
			return result;
		}

		//const char* name = sqlite3_column_table_name(stmt, 0);
		//table.setName(name != NULL ? name : "temp");
        // todo: linke error for sqlite3_column_table_name.
        table.setName("temp");

#if DEBUG
		sw.setInfo(Convert::convertStr("executeSqlQuery, the table name is '%s'", table.getName().c_str()));
#endif

		int columnCount = sqlite3_column_count(stmt);
		for (int i = 0; i < columnCount; i++)
		{
			char* nameStr = (char*)sqlite3_column_name(stmt, i);
			string name;
			if(nameStr != NULL)
			{
				name = nameStr;
			}
			else
			{
				char temp[32];
				sprintf(temp, "tempCol%d", i);
				name = temp;
			}
			const char* typeStr = sqlite3_column_decltype(stmt, i);
			string type = typeStr != NULL ? typeStr : "int";
			DataColumn* column = new DataColumn(name, type);
			table.addColumn(column);
		}

		while(sqlite3_step(stmt) == SQLITE_ROW)
		{
			DataRow* row = new DataRow();
			for (int i = 0; i < columnCount; i++)
			{
				DataColumn* column = table.getColumns()->at(i);
				DataCell* cell = NULL;
				Value value;
				memset(&value, 0, sizeof(value));
				ValueTypes type = column->getType();
				switch (type)
				{
				case Null:
					break;
				case Integer:
					value.nValue = sqlite3_column_int(stmt, i);
					break;
				case String:
				case DateTime:
					{
						char* str = (char*)sqlite3_column_text(stmt, i);
						DataCell::setStringValue(value, str);
					}
					break;
				case Float:
					value.dValue = sqlite3_column_double(stmt, i);
					break;
				default:
					assert(false);
					break;
				}

				cell = new DataCell(column, value);
				row->addCell(cell);
			}
			table.addRow(row);
		}
		result = sqlite3_finalize(stmt);
		if(result != SQLITE_OK)
		{
			printErrorInfo("sqlite3_finalize", sql);
			return result;
		}

		return SQLITE_OK;
	}
int QueryThread::run()
{
  // Note: The m_abort flag does not actually abort, after the mysql_query function:
  // MySql documentation states:
  //    "When using mysql_use_result(), you must execute mysql_fetch_row() until
  //     a NULL value is returned, otherwise, the unfetched rows are returned as 
  //     part of the result set for your next query. The C API gives the error Commands 
  //     out of sync; you can't run this command now if you forget to do this!"
  // In otherwords: once we've started, we can't stop. So what we actually do is run the 
  // query in full, and just not post any events if this flag is set.

  MYSQL* sql = 0;

  {
    MutexLocker lock(m_resultInfo);
    m_columns.clear();
    m_columnType.clear();
  }

  if (checkAbort())
  {
    MutexLocker lock(m_resultInfo);
    m_abort = false;
    postEvent(QUERY_ABORTED);
    return 0;
  }

  sql = m_database->lockHandle();
  if (mysql_query(sql, m_query.c_str()) != 0)
	{
    MutexLocker lock(m_resultInfo);
    m_error = true;
    m_errorText = mysql_error(sql);
    postEvent( QUERY_ERROR );

    m_database->unlockHandle();
		return 0;
	}

  MYSQL_RES* pResult = mysql_use_result(sql);
  if (!pResult)
  {
    if (checkAbort())
    {
      m_database->unlockHandle();
      return 0;
    }

		if(mysql_errno(sql))
		{
      MutexLocker lock(m_resultInfo);
      m_error = true;
      m_errorText = mysql_error(sql);
      postEvent( QUERY_ERROR );
		}
		else
		{
      MutexLocker lock(m_resultInfo);
      m_error = false;
      m_lastInsert = mysql_insert_id(sql);
      m_affectedRows = mysql_affected_rows(sql);
      postEvent( QUERY_SUCCESS_NO_DATA );
		}
    m_database->unlockHandle();
		return 0;
  }

  int numColumns = mysql_num_fields(pResult);
  
  {
    {
      MutexLocker lock(m_resultInfo);
	    for(int i = 0; i < numColumns; i++)
      {
        MYSQL_FIELD* field = mysql_fetch_field_direct(pResult, i);
        if (field && field->name && strlen(field->name))
        {
          m_columns.push_back( std::string(field->name) );

          if (field->type == MYSQL_TYPE_TINY || field->type == MYSQL_TYPE_SHORT || field->type == MYSQL_TYPE_LONG || field->type == MYSQL_TYPE_LONG)
            m_columnType.push_back( INTEGER );
          else if (field->type == MYSQL_TYPE_FLOAT || field->type == MYSQL_TYPE_DOUBLE)
            m_columnType.push_back( FLOATING_POINT );
          else
            m_columnType.push_back( STRING );
        }
        else
        {
          std::stringstream col;
          col << (i+1);

          m_columns.push_back( col.str() );
          m_columnType.push_back( STRING );
        }
      }
    }

    if (!checkAbort())
      postEvent( QUERY_COLUMNS );
  }

	MYSQL_ROW CurrentRow = mysql_fetch_row(pResult);
	while (CurrentRow)
	{
    if (checkAbort())
    {
      CurrentRow = mysql_fetch_row(pResult);
      continue;
    }

    DataRow* row = new DataRow;

	  for(int i = 0; i < numColumns; i++)
	  {
		  if (CurrentRow[i])
		  {
        row->push_back( new std::string( CurrentRow[i] ) );
		  }
		  else
		  {
			  row->push_back( 0 );
		  }
	  }    

    postEvent( QUERY_DATA, row );

    CurrentRow = mysql_fetch_row(pResult);
  }

	if(mysql_errno(sql))
	{
    {
      MutexLocker lock(m_resultInfo);
      m_error = true;
      m_errorText = mysql_error(sql);
    }
    if (!checkAbort())
      postEvent( QUERY_ERROR );
    else
    {
      postEvent( QUERY_ABORTED );

      MutexLocker lock(m_resultInfo);
      m_abort = false;
    }
	}
	else
	{
    {
      MutexLocker lock(m_resultInfo);
      m_error = false;
      m_lastInsert = mysql_insert_id(sql);
      m_affectedRows = mysql_affected_rows(sql);
    }
    if (!checkAbort())
      postEvent( QUERY_SUCCESS );
    else
    {
      postEvent( QUERY_ABORTED );

      MutexLocker lock(m_resultInfo);
      m_abort = false;
    }
	}

  m_database->unlockHandle();

	mysql_free_result(pResult);

  return 0;
}