src/cats/postgresql.c
BDB *db_init_database(JCR *jcr, const char *db_driver, const char *db_name, const char *db_user, 
                       const char *db_password, const char *db_address, int db_port, const char *db_socket, 
                       const char *db_ssl_mode, const char *db_ssl_key, const char *db_ssl_cert,
                       const char *db_ssl_ca, const char *db_ssl_capath, const char *db_ssl_cipher,
                       bool mult_db_connections, bool disable_batch_insert) 
{
   BDB_POSTGRESQL *mdb = NULL;

   if (!db_user) {
      Jmsg(jcr, M_FATAL, 0, _("A user name for PostgreSQL must be supplied.\n"));
      return NULL;
   }
   P(mutex);                          /* lock DB queue */
   if (db_list && !mult_db_connections) {
      /*
       * Look to see if DB already open
       */
      foreach_dlist(mdb, db_list) {
         if (mdb->bdb_match_database(db_driver, db_name, db_address, db_port)) {
            Dmsg1(dbglvl_info, "DB REopen %s\n", db_name);
            mdb->increment_refcount();
            goto get_out;
         }
      }
   }
   Dmsg0(dbglvl_info, "db_init_database first time\n");
   /* Create the global Bacula db context */
   mdb = New(BDB_POSTGRESQL());
   if (!mdb) goto get_out;

   /* Initialize the parent class members. */
   mdb->m_db_name = bstrdup(db_name);
   mdb->m_db_user = bstrdup(db_user);
   if (db_password) {
      mdb->m_db_password = bstrdup(db_password);
   }
   if (db_address) {
      mdb->m_db_address = bstrdup(db_address);
   }
   if (db_socket) {
      mdb->m_db_socket = bstrdup(db_socket);
   }
   if (db_ssl_mode) {
      mdb->m_db_ssl_mode = bstrdup(db_ssl_mode);
   } else {
      mdb->m_db_ssl_mode = bstrdup("prefer");
   }
   if (db_ssl_key) {
      mdb->m_db_ssl_key = bstrdup(db_ssl_key);
   }
   if (db_ssl_cert) {
      mdb->m_db_ssl_cert = bstrdup(db_ssl_cert);
   }
   if (db_ssl_ca) {
      mdb->m_db_ssl_ca = bstrdup(db_ssl_ca);
   }
   mdb->m_db_port = db_port;

   if (disable_batch_insert) { 
      mdb->m_disabled_batch_insert = true;
      mdb->m_have_batch_insert = false;
   } else { 
      mdb->m_disabled_batch_insert = false;
#ifdef USE_BATCH_FILE_INSERT
#if defined(HAVE_POSTGRESQL_BATCH_FILE_INSERT) || defined(HAVE_PQISTHREADSAFE) 
#ifdef HAVE_PQISTHREADSAFE 
      mdb->m_have_batch_insert = PQisthreadsafe();
#else 
      mdb->m_have_batch_insert = true;
#endif /* HAVE_PQISTHREADSAFE */ 
#else 
      mdb->m_have_batch_insert = true;
#endif /* HAVE_POSTGRESQL_BATCH_FILE_INSERT || HAVE_PQISTHREADSAFE */ 
#else 
      mdb->m_have_batch_insert = false;
#endif /* USE_BATCH_FILE_INSERT */ 
   } 
   mdb->m_allow_transactions = mult_db_connections;
 
   /* At this time, when mult_db_connections == true, this is for
    * specific console command such as bvfs or batch mode, and we don't
    * want to share a batch mode or bvfs. In the future, we can change
    * the creation function to add this parameter.
    */
   mdb->m_dedicated = mult_db_connections;

get_out:
   V(mutex);
   return mdb;
}
/*
 * Initialize database data structure. In principal this should
 * never have errors, or it is really fatal.
 */
Initialize database data structure. In principal this should
never have errors, or it is really fatal.
Initialize database data structure . In principal this should never have errors or it is really fatal .
BDB *db_init_database(JCR *jcr, const char *db_driver, const char *db_name, const char *db_user, 
                       const char *db_password, const char *db_address, int db_port, const char *db_socket, 
                       const char *db_ssl_mode, const char *db_ssl_key, const char *db_ssl_cert,
                       const char *db_ssl_ca, const char *db_ssl_capath, const char *db_ssl_cipher,
                       bool mult_db_connections, bool disable_batch_insert) 
{
   BDB_POSTGRESQL *mdb = NULL;
   if (!db_user) {
      Jmsg(jcr, M_FATAL, 0, _("A user name for PostgreSQL must be supplied.\n"));
      return NULL;
   }
   P(mutex);                          
   if (db_list && !mult_db_connections) {
      foreach_dlist(mdb, db_list) {
         if (mdb->bdb_match_database(db_driver, db_name, db_address, db_port)) {
            Dmsg1(dbglvl_info, "DB REopen %s\n", db_name);
            mdb->increment_refcount();
            goto get_out;
         }
      }
   }
   Dmsg0(dbglvl_info, "db_init_database first time\n");
   mdb = New(BDB_POSTGRESQL());
   if (!mdb) goto get_out;
   mdb->m_db_name = bstrdup(db_name);
   mdb->m_db_user = bstrdup(db_user);
   if (db_password) {
      mdb->m_db_password = bstrdup(db_password);
   }
   if (db_address) {
      mdb->m_db_address = bstrdup(db_address);
   }
   if (db_socket) {
      mdb->m_db_socket = bstrdup(db_socket);
   }
   if (db_ssl_mode) {
      mdb->m_db_ssl_mode = bstrdup(db_ssl_mode);
   } else {
      mdb->m_db_ssl_mode = bstrdup("prefer");
   }
   if (db_ssl_key) {
      mdb->m_db_ssl_key = bstrdup(db_ssl_key);
   }
   if (db_ssl_cert) {
      mdb->m_db_ssl_cert = bstrdup(db_ssl_cert);
   }
   if (db_ssl_ca) {
      mdb->m_db_ssl_ca = bstrdup(db_ssl_ca);
   }
   mdb->m_db_port = db_port;
   if (disable_batch_insert) { 
      mdb->m_disabled_batch_insert = true;
      mdb->m_have_batch_insert = false;
   } else { 
      mdb->m_disabled_batch_insert = false;
#ifdef USE_BATCH_FILE_INSERT
#if defined(HAVE_POSTGRESQL_BATCH_FILE_INSERT) || defined(HAVE_PQISTHREADSAFE) 
#ifdef HAVE_PQISTHREADSAFE 
      mdb->m_have_batch_insert = PQisthreadsafe();
#else 
      mdb->m_have_batch_insert = true;
#endif  
#else 
      mdb->m_have_batch_insert = true;
#endif  
#else 
      mdb->m_have_batch_insert = false;
#endif  
   } 
   mdb->m_allow_transactions = mult_db_connections;
   mdb->m_dedicated = mult_db_connections;
get_out:
   V(mutex);
   return mdb;
}
BDB * db_init_database ( JCR * jcr , const char * db_driver , const char * db_name , const char * db_user , const char * db_password , const char * db_address , int db_port , const char * db_socket , const char * db_ssl_mode , const char * db_ssl_key , const char * db_ssl_cert , const char * db_ssl_ca , const char * db_ssl_capath , const char * db_ssl_cipher , bool mult_db_connections , bool disable_batch_insert ) { BDB_POSTGRESQL * mdb = NULL ; if ( ! db_user ) { Jmsg ( jcr , M_FATAL , 0 , _ ( " \n " ) ) ; return NULL ; } P ( mutex ) ; if ( db_list && ! mult_db_connections ) { foreach_dlist ( mdb , db_list ) { if ( mdb -> bdb_match_database ( db_driver , db_name , db_address , db_port ) ) { Dmsg1 ( dbglvl_info , " \n " , db_name ) ; mdb -> increment_refcount ( ) ; goto get_out ; } } } Dmsg0 ( dbglvl_info , " \n " ) ; mdb = New ( BDB_POSTGRESQL ( ) ) ; if ( ! mdb ) goto get_out ; mdb -> m_db_name = bstrdup ( db_name ) ; mdb -> m_db_user = bstrdup ( db_user ) ; if ( db_password ) { mdb -> m_db_password = bstrdup ( db_password ) ; } if ( db_address ) { mdb -> m_db_address = bstrdup ( db_address ) ; } if ( db_socket ) { mdb -> m_db_socket = bstrdup ( db_socket ) ; } if ( db_ssl_mode ) { mdb -> m_db_ssl_mode = bstrdup ( db_ssl_mode ) ; } else { mdb -> m_db_ssl_mode = bstrdup ( " " ) ; } if ( db_ssl_key ) { mdb -> m_db_ssl_key = bstrdup ( db_ssl_key ) ; } if ( db_ssl_cert ) { mdb -> m_db_ssl_cert = bstrdup ( db_ssl_cert ) ; } if ( db_ssl_ca ) { mdb -> m_db_ssl_ca = bstrdup ( db_ssl_ca ) ; } mdb -> m_db_port = db_port ; if ( disable_batch_insert ) { mdb -> m_disabled_batch_insert = true ; mdb -> m_have_batch_insert = false ; } else { mdb -> m_disabled_batch_insert = false ; #ifdef USE_BATCH_FILE_INSERT #if defined ( HAVE_POSTGRESQL_BATCH_FILE_INSERT ) || defined ( HAVE_PQISTHREADSAFE ) #ifdef HAVE_PQISTHREADSAFE mdb -> m_have_batch_insert = PQisthreadsafe ( ) ; #else mdb -> m_have_batch_insert = true ; #endif #else mdb -> m_have_batch_insert = true ; #endif #else mdb -> m_have_batch_insert = false ; #endif } mdb -> m_allow_transactions = mult_db_connections ; mdb -> m_dedicated = mult_db_connections ; get_out : V ( mutex ) ; return mdb ; }
Initialize database data structure.
Initialize database data structure .
/* lock DB queue */ /*
       * Look to see if DB already open
       */ /* Create the global Bacula db context */ /* Initialize the parent class members. */ /* HAVE_PQISTHREADSAFE */ /* HAVE_POSTGRESQL_BATCH_FILE_INSERT || HAVE_PQISTHREADSAFE */ /* USE_BATCH_FILE_INSERT */ /* At this time, when mult_db_connections == true, this is for
    * specific console command such as bvfs or batch mode, and we don't
    * want to share a batch mode or bvfs. In the future, we can change
    * the creation function to add this parameter.
    */