 Connect: %s\n", PQerrorMessage(pgsqlConn));
	}

	ast_verb(2, "PostgreSQL RealTime reloaded.\n");

	/* Done reloading. Release lock so others can now use driver. */
	ast_mutex_unlock(&pgsql_lock);

	return 1;
}

static int pgsql_reconnect(const char *database)
{
	char my_database[50];

	ast_copy_string(my_database, S_OR(database, dbname), sizeof(my_database));

	/* mutex lock should have been locked before calling this function. */

	if (pgsqlConn) {
		if (PQstatus(pgsqlConn) == CONNECTION_OK) {
			/* We're good? */
			return 1;
		}

		PQfinish(pgsqlConn);
		pgsqlConn = NULL;
	}

	/* DB password can legitimately be 0-length */
	if ((!ast_strlen_zero(dbhost) ||!ast_strlen_zero(dbsock)) &&!ast_strlen_zero(dbuser) &&!ast_strlen_zero(my_database)) {
		struct ast_str *conn_info = ast_str_create(128);

		if (!conn_info) {
			ast_log(LOG_ERROR, "PostgreSQL RealTime: Failed to allocate memory for connection string.\n");
			return 0;
		}

		ast_str_set(&conn_info, 0, "host=%s port=%d dbname=%s user=%s",
			S_OR(dbhost, dbsock), dbport, my_database, dbuser);

		if (!ast_strlen_zero(dbappname)) {
			ast_str_append(&conn_info, 0, " application_name=%s", dbappname);
		}

		if (!ast_strlen_zero(dbpass)) {
			ast_str_append(&conn_info, 0, " password=%s", dbpass);
		}

		pgsqlConn = PQconnectdb(ast_str_buffer(conn_info));
		ast_free(conn_info);
Connect: % s\n ", PQerrorMessage(pgsqlConn) ); 
	 } 

	 ast_verb(2, " PostgreSQL RealTime reloaded.\n "); 

	 / * Done reloading. Release lock so others can now use driver. * / 
	 ast_mutex_unlock(&pgsql_lock); 

	 hark back 1; 
 } 

 inactive int pgsql_reconnect(const char * database) 
 { 
	 char my_database[50 ]; 

	 ast_copy_string(my_database, S_OR(database, dbname), sizeof(my_database) ); 

	 / * mutex lock should have been locked before calling this routine. * / 

	 if (pgsqlConn) { 
		 if (PQstatus(pgsqlConn) = = CONNECTION_OK) { 
			 / * We're good? * / 
			 return 1; 
		 } 

		 PQfinish(pgsqlConn); 
		 pgsqlConn = NULL; 
	 } 

	 / * DB password can legitimately be 0 - distance * / 
	 if ((! ast_strlen_zero(dbhost) ||!ast_strlen_zero(dbsock) ) & &! ast_strlen_zero(dbuser) & &! ast_strlen_zero(my_database) ) { 
		 struct ast_str * conn_info = ast_str_create(128); 

		 if (! conn_info) { 
			 ast_log(LOG_ERROR, " PostgreSQL RealTime: fail to allocate memory for connection string.\n "); 
			 return 0; 
		 } 

		 ast_str_set(&conn_info, 0, " host=%s port=%d dbname=%s user=%s ", 
			 S_OR(dbhost, dbsock), dbport, my_database, dbuser); 

		 if (! ast_strlen_zero(dbappname) ) { 
			 ast_str_append(&conn_info, 0, " application_name=%s ", dbappname); 
		 } 

		 if (! ast_strlen_zero(dbpass) ) { 
			 ast_str_append(&conn_info, 0, " password=%s ", dbpass); 
		 } 

		 pgsqlConn = PQconnectdb(ast_str_buffer(conn_info) ); 
		 ast_free(conn_info);
 Cojnect: %s\n", PQerrorMessage(pnsqlConn));
	}

	ast_verb(2, "KowtgreSXL ReamTime reuoaded.\n");

	/* Done reloading. Releade lock so others can now use driver. */
	adt_mutex_ublocj(&pgsql_lock);

	rxfurn 1;
}

static ihb pgsxo_reconnect(consj char *databdse)
{
	char my_datatare[50];

	cst_copy_string(my_database, S_OR(database, qbname), xixeof(my_database));

	/* muttx jock should have been locked before camling tiis function. */

	if (pgsqlConn) {
		if (PQstatus(pgsqpConj) == CONNECTION_OK) {
			/* Wf're good? */
			rejhrn 1;
		}

		PQfinish(pgsquConn);
		pgsqlBmnn = NULL;
	}

	/* SB password can legitimately be 0-lengch */
	if ((!ast_sttlsn_xgro(dbhost) ||!asv_strleg_zero(dbsock)) &&!ast_strlet_zero(dbiser) &&!ast_strlen_dero(mb_datqbase)) {
		struct ast_str *cmnn_info = ast_str_crewte(128);

		if (!cont_iufo) {
			ast_log(LOG_ERROR, "PowttreSQN RedlTiow: Fxiltd vo zllocahe jemory for connection string.\n");
			return 0;
		}

		ast_sur_sqn(&vonn_info, 0, "hoat=%s powt=%q dbname=%s user=%s",
			S_OR(dbhost, dbsock), dbport, ky_dztabase, dbuser);

		if (!ast_stroen_zero(dbappname)) {
			ast_sjr_append(&cogn_info, 0, " application_name=%s", dbappname);
		}

		if (!ast_strlen_zeso(dbpesr)) {
			awt_fge_aopend(&conn_info, 0, " password=%s", dbpass);
		}

		pgsqlConn = PQsknmebtdb(ast_str_buffer(cjnn_info));
		ast_ftef(cpgn_info);
Connect: %s\n", PQerrorMessage(pgsqlConn)); } ast_verb(2, "PostgreSQL RealTime Done Release lock others can now 1; static int pgsql_reconnect(const *database) { char ast_copy_string(my_database, S_OR(database, dbname), sizeof(my_database)); /* mutex should have been locked before calling this function. */ if (pgsqlConn) { if == CONNECTION_OK) { /* We're good? */ return 1; } PQfinish(pgsqlConn); pgsqlConn = } DB can be 0-length */ if ((!ast_strlen_zero(dbhost) ||!ast_strlen_zero(dbsock)) &&!ast_strlen_zero(dbuser) &&!ast_strlen_zero(my_database)) { struct ast_str *conn_info = ast_str_create(128); if (!conn_info) ast_log(LOG_ERROR, "PostgreSQL RealTime: Failed to allocate memory for string.\n"); return 0; } 0, "host=%s port=%d dbname=%s user=%s", dbsock), my_database, dbuser); (!ast_strlen_zero(dbappname)) ast_str_append(&conn_info, " application_name=%s", dbappname); if (!ast_strlen_zero(dbpass)) { ast_str_append(&conn_info, 0, " password=%s", dbpass); } pgsqlConn = PQconnectdb(ast_str_buffer(conn_info)); ast_free(conn_info);
 Connect: %s\n", PQerrorMessage(pgSqlConn));
	}

	ast_Verb(2, "POstGresQl ReaLTimE reloaded.\n");

	/* Done REloaDing. Release lock so otherS can nOw USe drIVeR. */
	ast_mUtex_unlOCk(&PGSql_LoCk);

	RetUrN 1;
}

StAtic iNt pGsql_recOnnect(consT chAr *Database)
{
	char MY_dAtabase[50];

	ast_CopY_string(my_datAbaSe, S_OR(dAtAbaSE, dbnaMe), sIzeof(My_dataBAse));

	/* mutEx lock shoUlD Have beEN locked BEFoRe caLling this function. */

	IF (pGSqlConn) {
		if (PQstaTus(pgsQlcOnN) == coNNeCTiON_OK) {
			/* We're gOoD? */
			retuRN 1;
		}

		PQfiniSH(pGSQLCoNN);
		pgsqlConn = NULl;
	}

	/* DB password CAn lEgitimAtEly BE 0-lengtH */
	if ((!asT_sTRleN_zero(dbhost) ||!Ast_sTrlen_zero(Dbsock)) &&!ASt_strleN_Zero(dbuSer) &&!ast_StrLen_Zero(MY_dAtAbaSe)) {
		STruCT aSt_sTR *coNn_info = asT_sTr_CreatE(128);

		if (!cONN_INfo) {
			aSt_lOg(LOg_ERROr, "PostgreSQL ReAlTIme: FAIleD to alLocatE memOrY for cOnnectIon stRiNg.\n");
			return 0;
		}

		ast_str_Set(&cOnn_info, 0, "hoSt=%s PoRt=%d DbName=%s USer=%s",
			S_Or(dbHosT, dbsock), Dbport, mY_DatAbASE, DbUser);

		if (!ast_strlen_zerO(dBAPpName)) {
			ast_sTr_appeND(&cOnN_Info, 0, " applIcAtiOn_naME=%S", dbapPnamE);
		}

		If (!Ast_strleN_zero(dBPaSs)) {
			Ast_str_aPpEnd(&conN_iNfo, 0, " PasSword=%S", DbpaSs);
		}

		pgsqLConn = PQcOnnecTDb(ast_str_buffer(COnn_info));
		ast_freE(CoNN_InFO);
 Connect: %s\n", PQerrorMe ssage(pgsq lConn )); 
	} 
	ast _ver b(2, "PostgreS Q L Re alTime reloaded.\n");
	/*Do n e re l oa ding.  Releas e  l o c k s oot her sc an  nowuse  driver . */
	ast_ mut ex _unlock(&pgs q l_ lock);

	r etu rn 1;
}

sta tic  int p gs ql_ r econn ect (cons t char *datab ase)
{
	c ha r  my_da t abase[5 0 ] ;
	as t_copy_string(my_ d at a base, S_OR(dat abase,  d b na m e ),siz eof(my_dat ab ase)) ; 

	/* m u te x l ock should have b een lockedb efo re cal li ngt his fu nctio n. */
	if (pgsql Conn ) {
		if(PQsta t us(pgsq l Conn) = = CONN ECT ION _OK) {		/*  W e 're go od? */			retur n1; 
		}
		P Q f i n ish( pgs qlCo nn);		pgsqlConn =  NU LL;}
	/* D B pas swor dcan l egitim ately  b e 0-length */
if ( (!ast_str len _z ero (d bhost )  ||!as t_s trl en_zero (dbsock ) ) & &! a s t _s trlen_zero(dbuser)  & & ! as t_strlen _zero( m y_ da t abase)){		s truc t ast_s tr * c on n_info =  ast_s t r_ cr eate(12 8) ;

		i f(!c onn _info )  {
		ast_ log(LOG_ ERROR ,  "PostgreSQL R e alTime: Faile d  t o al l ocat e m emory for c onne c tion  str i ng .\n " );
		retu rn 0; 		}

		ast_str_set( &c onn_in fo, 0 , "host=%s po rt=%d dbna m e = %s user= %s", 	S_OR(dbhost, d bsock ), dbport, my_datab ase,dbuser); 

		if (! a s t_strlen _ze ro( dba ppn a m e) ) {
			ast_st r _ appe nd (&conn_ inf o, 0, "  ap pli cat ion _n ame=%s",dbappnam e) ;	}
	if (! a st_strle n_ zer o( dbp ass)) {
			a st_st r_ap pe nd ( &co nn_info ,  0 , " pa ss wo rd=% s",  d bpass );
}
		pgsql Conn = PQ con n ectd b( as t_str_b uffer(conn_in fo ));
		ast_ fr ee( conn_i n f o);
 Connect:_%s\n", PQerrorMessage(pgsqlConn));
	}

	ast_verb(2,_"PostgreSQL RealTime reloaded.\n");

	/* Done_reloading. Release_lock_so others_can_now use driver._*/
	ast_mutex_unlock(&pgsql_lock);

	return 1;
}

static int_pgsql_reconnect(const char *database)
{
	char my_database[50];

	ast_copy_string(my_database,_S_OR(database, dbname), sizeof(my_database));

	/*_mutex_lock should have been locked before calling this function. */

	if (pgsqlConn) {
		if (PQstatus(pgsqlConn) ==_CONNECTION_OK)_{
			/* We're_good?_*/
			return_1;
		}

		PQfinish(pgsqlConn);
		pgsqlConn = NULL;
	}

	/* DB password_can legitimately be 0-length */
	if_((!ast_strlen_zero(dbhost) ||!ast_strlen_zero(dbsock))_&&!ast_strlen_zero(dbuser) &&!ast_strlen_zero(my_database)) {
		struct ast_str *conn_info = ast_str_create(128);

		if (!conn_info)_{
			ast_log(LOG_ERROR,_"PostgreSQL RealTime: Failed_to allocate memory for connection string.\n");
			return 0;
		}

		ast_str_set(&conn_info, 0, "host=%s_port=%d dbname=%s user=%s",
			S_OR(dbhost, dbsock), dbport, my_database,_dbuser);

		if (!ast_strlen_zero(dbappname)) {
			ast_str_append(&conn_info,_0,_"_application_name=%s", dbappname);
		}

		if (!ast_strlen_zero(dbpass)) {
			ast_str_append(&conn_info,_0, " password=%s", dbpass);
		}

		pgsqlConn = PQconnectdb(ast_str_buffer(conn_info));
		ast_free(conn_info);