<commit_before>/*
 * Copyright (c) 2007-2008 Digital Bazaar, Inc.  All rights reserved.
 */
#include "db/sql/mysql/MySqlConnection.h"
#include "db/sql/mysql/MySqlStatement.h"

#include <mysql/errmsg.h>

using namespace std;
using namespace db::sql;
using namespace db::sql::mysql;
using namespace db::net;
using namespace db::rt;

MySqlConnection::MySqlConnection()
{
   // no handle yet
   mHandle = NULL;
}

MySqlConnection::~MySqlConnection()
{
   // ensure connection is closed
   MySqlConnection::close();
}

Statement* MySqlConnection::createStatement(const char* sql)
{
   // create statement
   Exception::clearLast();
   Statement* rval = new MySqlStatement(this, sql);
   if(Exception::hasLast())
   {
      // delete statement if exception was thrown while creating statement
      delete rval;
      rval = NULL;
   }
   
   return rval;
}

bool MySqlConnection::connect(Url* url)
{
   bool rval = false;
   
   if(strcmp(url->getScheme().c_str(), "mysql") != 0)
   {
      string urlStr = url->toString();
      int length = 120 + urlStr.length();
      char msg[length];
      snprintf(msg, length,
         "Could not connect to mysql database, "
         "url scheme doesn't start with 'mysql', url='%s'", urlStr.c_str());
      ExceptionRef e = new SqlException(msg, "db.sql.BadUrlScheme");
      Exception::setLast(e, false);
   }
   else
   {
      // initialize handle
      mHandle = mysql_init(NULL);
      
      // default character set to UTF-8
      mysql_options(mHandle, MYSQL_SET_CHARSET_NAME, "utf8");
      
      // determine default database (if any)
      const char* db = (url->getPath().length() <= 1 ?
         NULL : url->getPath().c_str() + 1);
      
      // FIXME: we want to add read/write/create params to the URL
      // so connections can be read-only/write/etc (use query in URL)
      if(mysql_real_connect(
         mHandle,
         url->getHost().c_str(),
         url->getUser().c_str(), url->getPassword().c_str(),
         db,
         url->getPort(), NULL, 0) == NULL)
      {
         // create exception, close connection<commit_msg>Added client flag to mysql connections.<commit_after>/*
 * Copyright (c) 2007-2008 Digital Bazaar, Inc.  All rights reserved.
 */
#include "db/sql/mysql/MySqlConnection.h"
#include "db/sql/mysql/MySqlStatement.h"

#include <mysql/errmsg.h>

using namespace std;
using namespace db::sql;
using namespace db::sql::mysql;
using namespace db::net;
using namespace db::rt;

MySqlConnection::MySqlConnection()
{
   // no handle yet
   mHandle = NULL;
}

MySqlConnection::~MySqlConnection()
{
   // ensure connection is closed
   MySqlConnection::close();
}

Statement* MySqlConnection::createStatement(const char* sql)
{
   // create statement
   Exception::clearLast();
   Statement* rval = new MySqlStatement(this, sql);
   if(Exception::hasLast())
   {
      // delete statement if exception was thrown while creating statement
      delete rval;
      rval = NULL;
   }
   
   return rval;
}

bool MySqlConnection::connect(Url* url)
{
   bool rval = false;
   
   if(strcmp(url->getScheme().c_str(), "mysql") != 0)
   {
      string urlStr = url->toString();
      int length = 120 + urlStr.length();
      char msg[length];
      snprintf(msg, length,
         "Could not connect to mysql database, "
         "url scheme doesn't start with 'mysql', url='%s'", urlStr.c_str());
      ExceptionRef e = new SqlException(msg, "db.sql.BadUrlScheme");
      Exception::setLast(e, false);
   }
   else
   {
      // initialize handle
      mHandle = mysql_init(NULL);
      
      // default character set to UTF-8
      mysql_options(mHandle, MYSQL_SET_CHARSET_NAME, "utf8");
      
      // setup client flag
      int clientFlag = CLIENT_FOUND_ROWS | CLIENT_COMPRESS;
      
      // determine default database (if any)
      const char* db = (url->getPath().length() <= 1 ?
         NULL : url->getPath().c_str() + 1);
      
      // FIXME: we want to add read/write/create params to the URL
      // so connections can be read-only/write/etc (use query in URL)
      if(mysql_real_connect(
         mHandle,
         url->getHost().c_str(),
         url->getUser().c_str(), url->getPassword().c_str(),
         db,
         url->getPort(), NULL, clientFlag) == NULL)
      {
         // create exception, close connection
{'fraction_non_alphanumeric': 0.13314776154024588, 'fraction_numerical': 0.007654836464857342, 'mean_word_length': 2.7560975609756095, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6258255', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1323, 'n_words': 353}