Пример #1
0
static void assert_workdir_matches_tree(
    git_repository *repo, const git_oid *id, const char *root, bool recurse)
{
    git_object *obj;
    git_tree *tree;
    size_t i, max_i;
    git_buf path = GIT_BUF_INIT;

    if (!root)
        root = git_repository_workdir(repo);
    cl_assert(root);

    cl_git_pass(git_object_lookup(&obj, repo, id, GIT_OBJ_ANY));
    cl_git_pass(git_object_peel((git_object **)&tree, obj, GIT_OBJ_TREE));
    git_object_free(obj);

    max_i = git_tree_entrycount(tree);

    for (i = 0; i < max_i; ++i) {
        const git_tree_entry *te = git_tree_entry_byindex(tree, i);
        cl_assert(te);

        cl_git_pass(git_buf_joinpath(&path, root, git_tree_entry_name(te)));

        switch (git_tree_entry_type(te)) {
        case GIT_OBJ_COMMIT:
            assert_dir_exists(path.ptr);
            break;
        case GIT_OBJ_TREE:
            assert_dir_exists(path.ptr);
            if (recurse)
                assert_workdir_matches_tree(
                    repo, git_tree_entry_id(te), path.ptr, true);
            break;
        case GIT_OBJ_BLOB:
            switch (git_tree_entry_filemode(te)) {
            case GIT_FILEMODE_BLOB:
            case GIT_FILEMODE_BLOB_EXECUTABLE:
                assert_file_exists(path.ptr);
                /* because of cross-platform, don't confirm exec bit yet */
                break;
            case GIT_FILEMODE_LINK:
                cl_assert_(git_path_exists(path.ptr), path.ptr);
                /* because of cross-platform, don't confirm link yet */
                break;
            default:
                cl_assert(false); /* really?! */
            }
            break;
        default:
            cl_assert(false); /* really?!! */
        }
    }

    git_tree_free(tree);
    git_buf_free(&path);
}
Пример #2
0
void RBEIMEvaluation::legacy_read_offline_data_from_files(const std::string & directory_name,
                                                          bool read_error_bound_data,
                                                          const bool read_binary_data)
{
  LOG_SCOPE("legacy_read_offline_data_from_files()", "RBEIMEvaluation");

  Parent::legacy_read_offline_data_from_files(directory_name, read_error_bound_data);

  // First, find out how many basis functions we had when Greedy terminated
  // This was set in RBSystem::read_offline_data_from_files
  unsigned int n_bfs = this->get_n_basis_functions();

  // The writing mode: DECODE for binary, READ for ASCII
  XdrMODE mode = read_binary_data ? DECODE : READ;

  // The suffix to use for all the files that are written out
  const std::string suffix = read_binary_data ? ".xdr" : ".dat";

  // Stream for creating file names
  std::ostringstream file_name;

  // Read in the interpolation matrix
  file_name.str("");
  file_name << directory_name << "/interpolation_matrix" << suffix;
  assert_file_exists(file_name.str());

  Xdr interpolation_matrix_in(file_name.str(), mode);

  for(unsigned int i=0; i<n_bfs; i++)
    {
      for(unsigned int j=0; j<=i; j++)
        {
          Number value;
          interpolation_matrix_in >> value;
          interpolation_matrix(i,j) = value;
        }
    }
  interpolation_matrix_in.close();

  // Next read in interpolation_points
  file_name.str("");
  file_name << directory_name << "/interpolation_points" << suffix;
  assert_file_exists(file_name.str());

  Xdr interpolation_points_in(file_name.str(), mode);

  for(unsigned int i=0; i<n_bfs; i++)
    {
      Real x_val, y_val, z_val = 0.;
      interpolation_points_in >> x_val;

      if(LIBMESH_DIM >= 2)
        interpolation_points_in >> y_val;

      if(LIBMESH_DIM >= 3)
        interpolation_points_in >> z_val;

      Point p(x_val, y_val, z_val);
      interpolation_points.push_back(p);
    }
  interpolation_points_in.close();

  // Next read in interpolation_points_var
  file_name.str("");
  file_name << directory_name << "/interpolation_points_var" << suffix;
  assert_file_exists(file_name.str());

  Xdr interpolation_points_var_in(file_name.str(), mode);

  for(unsigned int i=0; i<n_bfs; i++)
    {
      unsigned int var;
      interpolation_points_var_in >> var;
      interpolation_points_var.push_back(var);
    }
  interpolation_points_var_in.close();

  // Read in the elements corresponding to the interpolation points
  legacy_read_in_interpolation_points_elem(directory_name);
}
Пример #3
0
Файл: do_mysql.c Проект: NZX/do
static void full_connect(VALUE self, MYSQL* db) {
  // Check to see if we're on the db machine.  If so, try to use the socket
  VALUE r_host, r_user, r_password, r_path, r_query, r_port;

  const char *host = "localhost", *user = "******"; 
  char *database = NULL, *socket = NULL, *password = NULL, *path = NULL;
  VALUE encoding = Qnil;

  MYSQL *result;

  int port = 3306;
  unsigned long client_flags = 0;
  int encoding_error;

  if((r_host = rb_iv_get(self, "@host")) != Qnil) {
    host     = StringValuePtr(r_host);
  }

  if((r_user = rb_iv_get(self, "@user")) != Qnil) {
    user     = StringValuePtr(r_user);
  }

  if((r_password = rb_iv_get(self, "@password")) != Qnil) {
    password = StringValuePtr(r_password);
  }

  if((r_port = rb_iv_get(self, "@port")) != Qnil) {
    port = NUM2INT(r_port);
  }

  if((r_path = rb_iv_get(self, "@path")) != Qnil) {
    path = StringValuePtr(r_path);
    database = strtok(path, "/");
  }

  if (NULL == database || 0 == strlen(database)) {
    rb_raise(eConnectionError, "Database must be specified");
  }

  r_query        = rb_iv_get(self, "@query");

  if (0 == strcasecmp(host, "localhost")) {
    socket = get_uri_option(r_query, "socket");
    if (NULL != socket) {
      rb_iv_set(self, "@using_socket", Qtrue);
    }
  }

#ifdef HAVE_MYSQL_SSL_SET
  char *ssl_client_key, *ssl_client_cert, *ssl_ca_cert, *ssl_ca_path, *ssl_cipher;
  VALUE r_ssl;

  if(rb_obj_is_kind_of(r_query, rb_cHash)) {
    r_ssl = rb_hash_aref(r_query, rb_str_new2("ssl"));

    if(rb_obj_is_kind_of(r_ssl, rb_cHash)) {
      ssl_client_key  = get_uri_option(r_ssl, "client_key");
      ssl_client_cert = get_uri_option(r_ssl, "client_cert");
      ssl_ca_cert     = get_uri_option(r_ssl, "ca_cert");
      ssl_ca_path     = get_uri_option(r_ssl, "ca_path");
      ssl_cipher      = get_uri_option(r_ssl, "cipher");

      assert_file_exists(ssl_client_key,  "client_key doesn't exist");
      assert_file_exists(ssl_client_cert, "client_cert doesn't exist");
      assert_file_exists(ssl_ca_cert,     "ca_cert doesn't exist");

      mysql_ssl_set(db, ssl_client_key, ssl_client_cert, ssl_ca_cert, ssl_ca_path, ssl_cipher);
    } else if(r_ssl != Qnil) {
      rb_raise(rb_eArgError, "ssl must be passed a hash");
    }
  }
#endif

  result = (MYSQL *)mysql_real_connect(
    db,
    host,
    user,
    password,
    database,
    port,
    socket,
    client_flags
  );

  if (NULL == result) {
    raise_error(self, db, Qnil);
  }

#ifdef HAVE_MYSQL_SSL_SET
  const char *ssl_cipher_used = mysql_get_ssl_cipher(db);

  if (NULL != ssl_cipher_used) {
    rb_iv_set(self, "@ssl_cipher", rb_str_new2(ssl_cipher_used));
  }
#endif

#ifdef MYSQL_OPT_RECONNECT
  my_bool reconnect = 1;
  mysql_options(db, MYSQL_OPT_RECONNECT, &reconnect);
#endif

  // Set the connections character set
  encoding = rb_iv_get(self, "@encoding");

  VALUE my_encoding = rb_hash_aref(CONST_GET(mEncoding, "MAP"), encoding);
  if(my_encoding != Qnil) {
    encoding_error = mysql_set_character_set(db, rb_str_ptr_readonly(my_encoding));
    if (0 != encoding_error) {
      raise_error(self, db, Qnil);
    } else {
#ifdef HAVE_RUBY_ENCODING_H
      rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index(rb_str_ptr_readonly(encoding))));
#endif
      rb_iv_set(self, "@my_encoding", my_encoding);
    }
  } else {
    rb_warn("Encoding %s is not a known Ruby encoding for MySQL\n", rb_str_ptr_readonly(encoding));
    rb_iv_set(self, "@encoding", rb_str_new2("UTF-8"));
#ifdef HAVE_RUBY_ENCODING_H
    rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index("UTF-8")));
#endif
    rb_iv_set(self, "@my_encoding", rb_str_new2("utf8"));
  }

  // Disable sql_auto_is_null
  cCommand_execute(Qnil, self, db, rb_str_new2("SET sql_auto_is_null = 0"));
  // removed NO_AUTO_VALUE_ON_ZERO because of MySQL bug http://bugs.mysql.com/bug.php?id=42270
  // added NO_BACKSLASH_ESCAPES so that backslashes should not be escaped as in other databases
   
  //4.x versions do not support certain session parameters  
  if(mysql_get_server_version(db) < 50000 ){
    cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_DIR_IN_CREATE,NO_UNSIGNED_SUBTRACTION'"));
  }else{
    cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'"));
  }
 
  rb_iv_set(self, "@connection", Data_Wrap_Struct(rb_cObject, 0, 0, db));
}
Пример #4
0
	void assert_files_exist(const std::vector<std::string>& file_list) const {
		for (auto file : file_list)
			assert_file_exists(file);
	}
Пример #5
0
void RBEvaluation::legacy_read_offline_data_from_files(const std::string & directory_name,
        bool read_error_bound_data,
        const bool read_binary_data)
{
    LOG_SCOPE("legacy_read_offline_data_from_files()", "RBEvaluation");

    // The reading mode: DECODE for binary, READ for ASCII
    XdrMODE mode = read_binary_data ? DECODE : READ;

    // The suffix to use for all the files that are written out
    const std::string suffix = read_binary_data ? ".xdr" : ".dat";

    // The string stream we'll use to make the file names
    std::ostringstream file_name;

    // First, find out how many basis functions we had when Greedy terminated
    unsigned int n_bfs;
    {
        file_name << directory_name << "/n_bfs" << suffix;
        assert_file_exists(file_name.str());

        Xdr n_bfs_in(file_name.str(), mode);
        n_bfs_in >> n_bfs;
        n_bfs_in.close();
    }
    resize_data_structures(n_bfs, read_error_bound_data);

    // Read in the parameter ranges
    file_name.str("");
    file_name << directory_name << "/parameter_ranges" << suffix;
    std::string continuous_param_file_name = file_name.str();

    // Read in the discrete parameter values
    file_name.str("");
    file_name << directory_name << "/discrete_parameter_values" << suffix;
    std::string discrete_param_file_name = file_name.str();
    read_parameter_data_from_files(continuous_param_file_name,
                                   discrete_param_file_name,
                                   read_binary_data);

    // Read in output data in multiple files
    for(unsigned int n=0; n<rb_theta_expansion->get_n_outputs(); n++)
    {
        for(unsigned int q_l=0; q_l<rb_theta_expansion->get_n_output_terms(n); q_l++)
        {
            file_name.str("");
            file_name << directory_name << "/output_";
            file_name << std::setw(3)
                      << std::setprecision(0)
                      << std::setfill('0')
                      << std::right
                      << n;
            file_name << "_";
            file_name << std::setw(3)
                      << std::setprecision(0)
                      << std::setfill('0')
                      << std::right
                      << q_l;
            file_name << suffix;
            assert_file_exists(file_name.str());

            Xdr output_n_in(file_name.str(), mode);

            for(unsigned int j=0; j<n_bfs; j++)
            {
                Number value;
                output_n_in >> value;
                RB_output_vectors[n][q_l](j) = value;
            }
            output_n_in.close();
        }
    }

    if(compute_RB_inner_product)
    {
        // Next read in the inner product matrix
        file_name.str("");
        file_name << directory_name << "/RB_inner_product_matrix" << suffix;
        assert_file_exists(file_name.str());

        Xdr RB_inner_product_matrix_in(file_name.str(), mode);

        for(unsigned int i=0; i<n_bfs; i++)
        {
            for(unsigned int j=0; j<n_bfs; j++)
            {
                Number value;
                RB_inner_product_matrix_in >> value;
                RB_inner_product_matrix(i,j) = value;
            }
        }
        RB_inner_product_matrix_in.close();
    }

    // Next read in the Fq vectors
    for(unsigned int q_f=0; q_f<rb_theta_expansion->get_n_F_terms(); q_f++)
    {
        file_name.str("");
        file_name << directory_name << "/RB_F_";
        file_name << std::setw(3)
                  << std::setprecision(0)
                  << std::setfill('0')
                  << std::right
                  << q_f;
        file_name << suffix;
        assert_file_exists(file_name.str());

        Xdr RB_Fq_f_in(file_name.str(), mode);

        for(unsigned int i=0; i<n_bfs; i++)
        {
            Number value;
            RB_Fq_f_in >> value;
            RB_Fq_vector[q_f](i) = value;
        }
        RB_Fq_f_in.close();
    }

    // Next read in the Aq matrices
    for(unsigned int q_a=0; q_a<rb_theta_expansion->get_n_A_terms(); q_a++)
    {
        file_name.str("");
        file_name << directory_name << "/RB_A_";
        file_name << std::setw(3)
                  << std::setprecision(0)
                  << std::setfill('0')
                  << std::right
                  << q_a;
        file_name << suffix;
        assert_file_exists(file_name.str());

        Xdr RB_Aq_a_in(file_name.str(), mode);

        for(unsigned int i=0; i<n_bfs; i++)
        {
            for(unsigned int j=0; j<n_bfs; j++)
            {
                Number  value;
                RB_Aq_a_in >> value;
                RB_Aq_vector[q_a](i,j) = value;
            }
        }
        RB_Aq_a_in.close();
    }


    if(read_error_bound_data)
    {
        // Next read in Fq representor norm data
        file_name.str("");
        file_name << directory_name << "/Fq_innerprods" << suffix;
        assert_file_exists(file_name.str());

        Xdr RB_Fq_innerprods_in(file_name.str(), mode);

        unsigned int Q_f_hat = rb_theta_expansion->get_n_F_terms()*(rb_theta_expansion->get_n_F_terms()+1)/2;
        for(unsigned int i=0; i<Q_f_hat; i++)
        {
            RB_Fq_innerprods_in >> Fq_representor_innerprods[i];
        }
        RB_Fq_innerprods_in.close();

        // Read in output data
        for(unsigned int n=0; n<rb_theta_expansion->get_n_outputs(); n++)
        {
            file_name.str("");
            file_name << directory_name << "/output_";
            file_name << std::setw(3)
                      << std::setprecision(0)
                      << std::setfill('0')
                      << std::right
                      << n;
            file_name << "_dual_innerprods" << suffix;
            assert_file_exists(file_name.str());

            Xdr output_dual_innerprods_in(file_name.str(), mode);

            unsigned int Q_l_hat = rb_theta_expansion->get_n_output_terms(n)*(rb_theta_expansion->get_n_output_terms(n)+1)/2;
            for(unsigned int q=0; q<Q_l_hat; q++)
            {
                output_dual_innerprods_in >> output_dual_innerprods[n][q];
            }
            output_dual_innerprods_in.close();
        }


        // Next read in Fq_Aq representor norm data
        file_name.str("");
        file_name << directory_name << "/Fq_Aq_innerprods" << suffix;
        assert_file_exists(file_name.str());

        Xdr RB_Fq_Aq_innerprods_in(file_name.str(), mode);

        for(unsigned int q_f=0; q_f<rb_theta_expansion->get_n_F_terms(); q_f++)
        {
            for(unsigned int q_a=0; q_a<rb_theta_expansion->get_n_A_terms(); q_a++)
            {
                for(unsigned int i=0; i<n_bfs; i++)
                {
                    RB_Fq_Aq_innerprods_in >> Fq_Aq_representor_innerprods[q_f][q_a][i];
                }
            }
        }
        RB_Fq_Aq_innerprods_in.close();

        // Next read in Aq_Aq representor norm data
        file_name.str("");
        file_name << directory_name << "/Aq_Aq_innerprods" << suffix;
        assert_file_exists(file_name.str());

        Xdr RB_Aq_Aq_innerprods_in(file_name.str(), mode);

        unsigned int Q_a_hat = rb_theta_expansion->get_n_A_terms()*(rb_theta_expansion->get_n_A_terms()+1)/2;
        for(unsigned int i=0; i<Q_a_hat; i++)
        {
            for(unsigned int j=0; j<n_bfs; j++)
            {
                for(unsigned int l=0; l<n_bfs; l++)
                {
                    RB_Aq_Aq_innerprods_in >> Aq_Aq_representor_innerprods[i][j][l];
                }
            }
        }
        RB_Aq_Aq_innerprods_in.close();
    }

    // Resize basis_functions even if we don't read them in so that
    // get_n_bfs() returns the correct value. Initialize the pointers
    // to NULL
    set_n_basis_functions(n_bfs);
    for(unsigned int i=0; i<basis_functions.size(); i++)
    {
        if(basis_functions[i])
        {
            basis_functions[i]->clear();
            delete basis_functions[i];
        }
        basis_functions[i] = libmesh_nullptr;
    }
}
Пример #6
0
void RBEvaluation::read_in_vectors_from_multiple_files(System & sys,
        std::vector< std::vector<NumericVector<Number> *> * > multiple_vectors,
        const std::vector<std::string> & multiple_directory_names,
        const std::vector<std::string> & multiple_data_names,
        const bool read_binary_vectors)
{
    LOG_SCOPE("read_in_vectors_from_multiple_files()", "RBEvaluation");

    unsigned int n_files = multiple_vectors.size();
    unsigned int n_directories = multiple_directory_names.size();
    unsigned int n_data_names = multiple_data_names.size();
    libmesh_assert( (n_files == n_directories) && (n_files == n_data_names) );

    if (n_files == 0)
        return;

    // Make sure processors are synced up before we begin
    this->comm().barrier();

    std::ostringstream file_name;
    const std::string basis_function_suffix = (read_binary_vectors ? ".xdr" : ".dat");
    struct stat stat_info;

    // Assume that all the headers are the same, hence we can just use the first one.
    file_name << multiple_directory_names[0] << "/"
              << multiple_data_names[0] << "_header" << basis_function_suffix;
    assert_file_exists(file_name.str());

    Xdr header_data(file_name.str(),
                    read_binary_vectors ? DECODE : READ);

    // set the version number in header_data from io_version_string
    // (same code as in EquationSystemsIO::_read_impl)
    std::string io_version_string = get_io_version_string();
    std::string::size_type lm_pos = io_version_string.find("libMesh");
    std::istringstream iss(io_version_string.substr(lm_pos + 8));
    int ver_major = 0, ver_minor = 0, ver_patch = 0;
    char dot;
    iss >> ver_major >> dot >> ver_minor >> dot >> ver_patch;
    header_data.set_version(LIBMESH_VERSION_ID(ver_major, ver_minor, ver_patch));

    // We need to call sys.read_header (e.g. to set _written_var_indices properly),
    // but by setting the read_header argument to false, it doesn't reinitialize the system
    sys.read_header(header_data, io_version_string, /*read_header=*/false, /*read_additional_data=*/false);

    // Following EquationSystemsIO::read, we use a temporary numbering (node major)
    // before writing out the data
    MeshTools::Private::globally_renumber_nodes_and_elements(sys.get_mesh());

    for (unsigned int data_index=0; data_index<n_directories; data_index++)
    {
        std::vector<NumericVector<Number> *> & vectors = *multiple_vectors[data_index];

        // Allocate storage for each vector
        for (unsigned int i=0; i<vectors.size(); i++)
        {
            // vectors should all be NULL, otherwise we get a memory leak when
            // we create the new vectors in RBEvaluation::read_in_vectors.
            if (vectors[i])
                libmesh_error_msg("Non-NULL vector passed to read_in_vectors_from_multiple_files");

            vectors[i] = NumericVector<Number>::build(sys.comm()).release();

            vectors[i]->init (sys.n_dofs(),
                              sys.n_local_dofs(),
                              false,
                              PARALLEL);
        }

        file_name.str("");
        file_name << multiple_directory_names[data_index]
                  << "/" << multiple_data_names[data_index]
                  << "_data" << basis_function_suffix;

        // On processor zero check to be sure the file exists
        if (this->processor_id() == 0)
        {
            int stat_result = stat(file_name.str().c_str(), &stat_info);

            if (stat_result != 0)
                libmesh_error_msg("File does not exist: " << file_name.str());
        }

        assert_file_exists(file_name.str());
        Xdr vector_data(file_name.str(),
                        read_binary_vectors ? DECODE : READ);

        // The vector_data needs to know which version to read.
        vector_data.set_version(LIBMESH_VERSION_ID(ver_major, ver_minor, ver_patch));

        sys.read_serialized_vectors (vector_data, vectors);
    }

    // Undo the temporary renumbering
    sys.get_mesh().fix_broken_node_and_element_numbering();
}
Пример #7
0
void full_connect(VALUE self, MYSQL *db) {
  VALUE r_host = rb_iv_get(self, "@host");
  const char *host = "localhost";

  if (r_host != Qnil) {
    host = StringValuePtr(r_host);
  }

  VALUE r_user = rb_iv_get(self, "@user");
  const char *user = "******";

  if (r_user != Qnil) {
    user = StringValuePtr(r_user);
  }

  VALUE r_password = rb_iv_get(self, "@password");
  char *password = NULL;

  if (r_password != Qnil) {
    password = StringValuePtr(r_password);
  }

  VALUE r_port = rb_iv_get(self, "@port");
  int port = 3306;

  if (r_port != Qnil) {
    port = NUM2INT(r_port);
  }

  VALUE r_path = rb_iv_get(self, "@path");
  char *path = NULL;
  char *database = NULL;

  if (r_path != Qnil) {
    path = StringValuePtr(r_path);
    database = strtok(path, "/"); // not threadsafe
  }

  if (!database || !*database) {
    rb_raise(eConnectionError, "Database must be specified");
  }

  VALUE r_query = rb_iv_get(self, "@query");
  char *socket = NULL;

  // Check to see if we're on the db machine.  If so, try to use the socket
  if (strcasecmp(host, "localhost") == 0) {
    socket = get_uri_option(r_query, "socket");

    if (socket) {
      rb_iv_set(self, "@using_socket", Qtrue);
    }
  }

#ifdef HAVE_MYSQL_SSL_SET
  char *ssl_client_key, *ssl_client_cert, *ssl_ca_cert, *ssl_ca_path, *ssl_cipher;
  VALUE r_ssl;

  if (rb_obj_is_kind_of(r_query, rb_cHash)) {
    r_ssl = rb_hash_aref(r_query, rb_str_new2("ssl"));

    if (rb_obj_is_kind_of(r_ssl, rb_cHash)) {
      ssl_client_key  = get_uri_option(r_ssl, "client_key");
      ssl_client_cert = get_uri_option(r_ssl, "client_cert");
      ssl_ca_cert     = get_uri_option(r_ssl, "ca_cert");
      ssl_ca_path     = get_uri_option(r_ssl, "ca_path");
      ssl_cipher      = get_uri_option(r_ssl, "cipher");

      assert_file_exists(ssl_client_key,  "client_key doesn't exist");
      assert_file_exists(ssl_client_cert, "client_cert doesn't exist");
      assert_file_exists(ssl_ca_cert,     "ca_cert doesn't exist");

      mysql_ssl_set(db, ssl_client_key, ssl_client_cert, ssl_ca_cert, ssl_ca_path, ssl_cipher);
    }
    else if (r_ssl != Qnil) {
      rb_raise(rb_eArgError, "ssl must be passed a hash");
    }
  }
#endif

  unsigned long client_flags = 0;

  MYSQL *result = mysql_real_connect(
    db,
    host,
    user,
    password,
    database,
    port,
    socket,
    client_flags
  );

  if (!result) {
    raise_error(self, db, Qnil);
  }

#ifdef HAVE_MYSQL_GET_SSL_CIPHER
  const char *ssl_cipher_used = mysql_get_ssl_cipher(db);

  if (ssl_cipher_used) {
    rb_iv_set(self, "@ssl_cipher", rb_str_new2(ssl_cipher_used));
  }
#endif

#ifdef MYSQL_OPT_RECONNECT
  my_bool reconnect = 1;
  mysql_options(db, MYSQL_OPT_RECONNECT, &reconnect);
#endif

  // We only support encoding for MySQL versions providing mysql_set_character_set.
  // Without this function there are potential issues with mysql_real_escape_string
  // since that doesn't take the character set into consideration when setting it
  // using a SET CHARACTER SET query. Since we don't want to stimulate these possible
  // issues we simply ignore it and assume the user has configured this correctly.

#ifdef HAVE_MYSQL_SET_CHARACTER_SET
  // Set the connections character set
  VALUE encoding = rb_iv_get(self, "@encoding");
  VALUE my_encoding = rb_hash_aref(do_const_get(mEncoding, "MAP"), encoding);

  if (my_encoding != Qnil) {
    int encoding_error = mysql_set_character_set(db, rb_str_ptr_readonly(my_encoding));

    if (encoding_error != 0) {
      raise_error(self, db, Qnil);
    }
    else {
#ifdef HAVE_RUBY_ENCODING_H
      rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index(rb_str_ptr_readonly(encoding))));
#endif

      rb_iv_set(self, "@my_encoding", my_encoding);
    }
  }
  else {
    rb_warn("Encoding %s is not a known Ruby encoding for MySQL\n", rb_str_ptr_readonly(encoding));
    rb_iv_set(self, "@encoding", rb_str_new2("UTF-8"));
#ifdef HAVE_RUBY_ENCODING_H
    rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index("UTF-8")));
#endif
    rb_iv_set(self, "@my_encoding", rb_str_new2("utf8"));
  }
#endif

  // Disable sql_auto_is_null
  cCommand_execute(Qnil, self, db, rb_str_new2("SET sql_auto_is_null = 0"));
  // removed NO_AUTO_VALUE_ON_ZERO because of MySQL bug http://bugs.mysql.com/bug.php?id=42270
  // added NO_BACKSLASH_ESCAPES so that backslashes should not be escaped as in other databases

// For really anscient MySQL versions we don't attempt any strictness
#ifdef HAVE_MYSQL_GET_SERVER_VERSION
  //4.x versions do not support certain session parameters
  if (mysql_get_server_version(db) < 50000) {
    cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_DIR_IN_CREATE,NO_UNSIGNED_SUBTRACTION'"));
  }
  else {
    cCommand_execute(Qnil, self, db, rb_str_new2("SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'"));
  }
#endif

  rb_iv_set(self, "@connection", Data_Wrap_Struct(rb_cObject, 0, 0, db));
}