bool TeSQLitePortal::query(const string &qry, TeCursorLocation l, TeCursorType t, TeCursorEditType e, TeCursorDataType dt)
{
	errorMessage_ = "";
	freeResult();

	_skipFirstFetch = false;
	_firstFetchResult = false;
	_mapColumnNames.clear();

	int retValue = sqlite3_prepare_v2(_conn, qry.c_str(), -1, &_recordSet, 0);
	if(retValue != SQLITE_OK)
	{
		errorMessage_ = errorMessage();
		freeResult();
		return false;
	}

	_query = qry;	

	_firstFetchResult = fetchRow();
	_skipFirstFetch = true;
	_currentRow = -1;	

	numFields_ = sqlite3_column_count(_recordSet);
	for(int i = 0; i < numFields_; ++i)
	{
		int type = sqlite3_column_type(_recordSet, i);
		std::string cName = sqlite3_column_name(_recordSet, i);		

		_mapColumnNames[cName] = i;
		
		TeAttribute attr;
		attr.rep_.name_ = cName;

		if(type == SQLITE3_TEXT)
		{
			attr.rep_.type_ = TeSTRING;
		}
		else if(type == SQLITE_INTEGER)
		{
			attr.rep_.type_ = TeINT;
		}
		else if(type == SQLITE_FLOAT)
		{
			attr.rep_.type_ = TeREAL;
		}
		else if(type == SQLITE_BLOB)
		{
			attr.rep_.type_ = TeBLOB;
		}
		else
		{
			attr.rep_.type_ = TeSTRING;
		}

		attList_.push_back(attr);
	}

	return true;
}
Example #2
0
bool Database::connect()
{
	// connection handle initialization
	m_handle = mysql_init(nullptr);
	if (!m_handle) {
		std::cout << std::endl << "Failed to initialize MySQL connection handle." << std::endl;
		return false;
	}

	// automatic reconnect
	my_bool reconnect = true;
	mysql_options(m_handle, MYSQL_OPT_RECONNECT, &reconnect);

	// connects to database
	if (!mysql_real_connect(m_handle, g_config.getString(ConfigManager::MYSQL_HOST).c_str(), g_config.getString(ConfigManager::MYSQL_USER).c_str(), g_config.getString(ConfigManager::MYSQL_PASS).c_str(), g_config.getString(ConfigManager::MYSQL_DB).c_str(), g_config.getNumber(ConfigManager::SQL_PORT), nullptr, 0)) {
		std::cout << std::endl << "MySQL Error Message: " << mysql_error(m_handle) << std::endl;
		return false;
	}

	m_connected = true;

	DBResult* result = storeQuery("SHOW variables LIKE 'max_allowed_packet'");
	if (result) {
		int32_t max_query = result->getDataInt("Value");
		freeResult(result);

		if (max_query < 16777216) {
			std::cout << std::endl << "[Warning] max_allowed_packet might too low for house item storage" << std::endl;
			std::cout << "Use the following query to raise max_allow_packet: ";
			std::cout << "SET GLOBAL max_allowed_packet = 16777216";
		}
	}
	return true;
}
Example #3
0
int32 CSql::execute( const char *format, ... )
{
	freeResult();

	char *mysql_string = NULL;

	va_list args;
	va_start(args, format);
	int32 mysql_string_length = vasprintf( &mysql_string, format, args );
	va_end(args);

	if ( mysql_string == NULL )
		return 0;

    if ( (int32)m_lastString.size() < mysql_string_length + 1 )
        m_lastString.resize( mysql_string_length + 1 );
    memcpy( &m_lastString[0], mysql_string, mysql_string_length );
    m_lastString[ mysql_string_length ] = '\0';
	//printf( "Query[%s]\r\n", mysql_string );

	if ( mysql_real_query( m_hSql, mysql_string, mysql_string_length ) != 0 )
	{
		free( mysql_string );
		return 0;
	}

	free( mysql_string );
	return (int32)mysql_affected_rows( m_hSql );
}
Example #4
0
CMySQLQuery::~CMySQLQuery()
{
#ifdef DEBUG
  qDebug("CMySQLQuery::~CMySQLQuery()");
#endif
  
  freeResult();
}
Example #5
0
DBResult* Database::verifyResult(DBResult* result)
{
	if (!result->next()) {
		freeResult(result);
		return nullptr;
	}
	return result;
}
Example #6
0
void MySQLHandle::closeConnection()
{
	if (connection != NULL)
	{
		mysql_close(connection);
		if (storedResult != NULL) freeResult();
	}
}
Example #7
0
void CSql::disconnect(void)
{
	freeResult();

	if ( m_hSql != NULL)
	{
		mysql_close( m_hSql );
		m_hSql = NULL;
	}
}
Example #8
0
DatabaseMySQL::DatabaseMySQL()
{
	m_connected = false;

	// connection handle initialization
	if(!mysql_init(&m_handle))
	{
		std::cout << std::endl << "Failed to initialize MySQL connection handle." << std::endl;
		return;
	}

	// automatic reconnect
	my_bool reconnect = true;
	mysql_options(&m_handle, MYSQL_OPT_RECONNECT, &reconnect);

	// connects to database
	if(!mysql_real_connect(&m_handle, g_config.getString(ConfigManager::MYSQL_HOST).c_str(), g_config.getString(ConfigManager::MYSQL_USER).c_str(), g_config.getString(ConfigManager::MYSQL_PASS).c_str(), g_config.getString(ConfigManager::MYSQL_DB).c_str(), g_config.getNumber(ConfigManager::SQL_PORT), NULL, 0))
	{
		std::cout << "Failed to connect to database. MYSQL ERROR: " << mysql_error(&m_handle) << std::endl;
		return;
	}

	if(MYSQL_VERSION_ID < 50019)
	{
		//mySQL servers < 5.0.19 has a bug where MYSQL_OPT_RECONNECT is (incorrectly) reset by mysql_real_connect calls
		//See http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html for more information.
		mysql_options(&m_handle, MYSQL_OPT_RECONNECT, &reconnect);
		std::cout << std::endl << "[Warning] Outdated mySQL server detected. Consider upgrading to a newer version." << std::endl;
	}

	m_connected = true;

	if(g_config.getString(ConfigManager::MAP_STORAGE_TYPE) == "binary")
	{
		DBQuery query;
		query << "SHOW variables LIKE 'max_allowed_packet';";

		DBResult* result;
		if((result = storeQuery(query.str())))
		{
			int32_t max_query = result->getDataInt("Value");
			freeResult(result);

			if(max_query < 16777216)
			{
				std::cout << std::endl << "[Warning] max_allowed_packet might be set too low for binary map storage." << std::endl;
				std::cout << "Use the following query to raise max_allow_packet: ";
				std::cout << "SET GLOBAL max_allowed_packet = 16777216;";
			}
		}
	}
}
Example #9
0
bool CMySQLQuery::next(bool blocking)
{
#ifdef DEBUG
  qDebug("CMySQLQuery::next()");
#endif

  bool r = ((mysql_row=mysql_fetch_row(mysql_res)) != NULL);
  if (check_pending_event++ >= PROCESS_EVENT)
  {
    if (!blocking && !blocking_queries)
      if (qApp->hasPendingEvents())
        qApp->processEvents();

    if (cancel_execution)
      r = false;
    check_pending_event = 0;
  }

  if (!r && autodelete )
    freeResult();
  return r;
}
Example #10
0
bool CMySQLQuery::execRealQuery(const char * qry, ulong len)
{
#ifdef DEBUG
  qDebug("CMySQLQuery::execRealQuery()");
#endif
  
  check_pending_event = 0;
  cancel_execution = false;
  freeResult();
  returned_results = false;
  QString c = QString::null;
  if (!m_mysql->connectionName().isEmpty())
    c = "[" + m_mysql->connectionName() + "] ";
  emit m_mysql->sqldebug(m_mysql->codec()->fromUnicode(c + qry));
  if (!m_mysql->isConnected())
  {
    if (emiterror)
      m_mysql->emitError();
    return false;
  }  
  
  QTime tm;
  tm.start();

#ifdef QT_THREAD_SUPPORT
  QueryThread queryThread(m_mysql->mysql, qry, len);
  queryThread.start();
  queryThread.wait();
  if (!queryThread.getResult())
  {    
    if (emiterror)
      m_mysql->emitError();
    return false;
  }
#else
  if (mysql_real_query(m_mysql->mysql, qry, len) != 0)
  {
    if (emiterror)
      m_mysql->emitError();
    return false;
  }
#endif

  if (!(mysql_res=mysql_store_result(m_mysql->mysql)))
  {
    mysql_res = NULL;
    if (mysql_error(m_mysql->mysql)[0])
    {
      if (emiterror)
        m_mysql->emitError();
      return false;
    }
  }
  last_query = qry;
  if (mysql_res)
  {
    mysql_fields = mysql_fetch_fields(mysql_res);
    num_fields = mysql_num_fields(mysql_res);
    returned_results = true;
  }
  else
  {
    mysql_fields = 0;
    num_fields = 0;
  }
 
  if (emitmessages)
  {
    QString time_buff = " (" + QString::number((double)tm.elapsed() / 1000L, 'f', 2) + ") " + tr("sec");
    QString buff;
    uint flag;
    if (mysql_res)
    {
      flag = INFORMATION;
      if (!mysql_num_rows(mysql_res))
        buff = tr("Empty set");
      else
      {
        buff = QString::number(numRows()) + " ";
        buff += numRows() == 1 ? tr("row") : tr("rows");
        buff += " " + tr("in set");
      }
    }
    else
    {
      flag = WARNING;
      if (mysql_affected_rows(m_mysql->mysql) == ~(ulong) 0)
        buff = tr("Query OK");
      else
      {
        buff = tr("Query OK,") + " ";
        buff += QString::number((ulong) mysql_affected_rows(m_mysql->mysql)) + " ";
        buff += mysql_affected_rows(m_mysql->mysql) == 1 ? tr("row") : tr("rows");
        buff += " " + tr("affected");
      }
    }
    buff += time_buff;
    m_mysql->emitMessage(flag, buff);
    if (mysql_info(m_mysql->mysql))
      m_mysql->emitMessage(INFORMATION, mysql_info(m_mysql->mysql));
  }

  return true;
}
Example #11
0
File: db.C Project: AresTao/xframe
CDB::~CDB()
{
    freeResult();
    delete m_pSelectResult;
    m_pSelectResult = NULL;
}
TeSQLitePortal::~TeSQLitePortal()
{
	freeResult();
}