Jump to content
Fivewin Brasil

Questão Postgres- Sqlrdd - Tabelas iguais bd's diferentes server único (Resolvido)


Jorge Andrade

Recommended Posts

Aos Geek's de plantão, uma ajuda ou esclarecimento (Resolvido)

 

Tenho um server postgres com vários bd's, conforme os trechos abaixo, tento fazer um select da mesma tabela em bd's diferentes, evidencio cada bd pela conexão mas ele continua na conexão 1, ou talvez a 2 não esteja sendo ativada, alguém, tem uma dica??

Obs: O código é bastante rústico, justamente pelo fato de ser um testee foi extraído deste exemplo do manual da sqlrdd, a única diferença são os sgbd's (mysql/firebird)

? nCnn1 := SR_AddConnection( CONNECT_MYSQL, cConnString1 )     // 1
? nCnn2 := SR_AddConnection( CONNECT_FIREBIRD, cConnString2 )  // 2
? SR_GetActiveConnection()                                     // 2
? SR_SetActiveConnection( nCnn1 )                              // 2 (valor anterior)
? SR_GetActiveConnection()                                     // 1
? SR_SetActiveConnection( 3 )                                  // -1 (conexão inválida)

Meu código:

?"estabelecendo conexao 1"

ConnString := "PGS=" + xServIp + ";" + "UID=" + xUserCon + ";" + "PWD=" + xPassCon +";" + "DTB=" + xDbSql1 + ";" + "PRT=" + xPortCon
cConnection1 := SR_AddConnection( CONNECT_POSTGRES , ConnString )

?"estabelendo conexao 2"

ConnString := "PGS=" + xServIp + ";" + "UID=" + xUserCon + ";" + "PWD=" + xPassCon +";" + "DTB=" + xDbSql2 + ";" + "PRT=" + xPortCon
cConnection2 := SR_AddConnection( CONNECT_POSTGRES , ConnString )

?"alterando conexao ativa de 2 para 1"

?SR_SetActiveConnection( cConnection1 )
?SR_GetActiveConnection()
?SR_GetConnection()

xConsSql1 := "SELECT * FROM CONTROLEPEDIDO"
dbUseArea(.t., "SQLRDD", xConsSql1, "xCons1")

SELECT("xCons1")
BROWSE()


?"alterando conexao ativa de 1 para 2"
?SR_SetActiveConnection( cConnection2 )
?SR_GetActiveConnection()
?SR_GetConnection()

xConsSql2 := "SELECT * FROM CONTROLEPEDIDO"
dbUseArea(.t., "SQLRDD", xConsSql2, "xCons2")

SELECT("xCons2")
BROWSE()

SR_EndConnection(cConnection1)
SR_EndConnection(cConnection2)

 

Nota: Continuando os testes, conseguir altenar a conexão, porém o resultado da tabela continua a mesma, ou seja, as mesmas informações da tabela do bd1.

[]s,

 

Link to comment
Share on other sites

  • 3 weeks later...

Cara sempre que preciso fazer isso faço a seguinte sequencia:

 

→ nConexao := Abre a conexão

→SR_SetActiveConnection (nConexao)

→ Abre a tabela

→ nConexao2:=Abre a conexão 2

→SR_SetActiveConnection (nConexao2)

→ Abre a tabela

→SR_SetActiveConnection (nConexao)

Depois que abri as tabelas cada qual em sua conexao pode ser feliz.

 

Uso isso muito na consultas de produtos com estoques de matriz e filiais

**PS: nConexaoPrincipal e nConexaoAtual são usadas para controlar o nº da conexão que estou trabalhando

	   nConexaoPrincipal := nConexaoAtual
	   select cemp
	   go top
	   While !Eof()
	      if !Empty(cemp->cip) .and. alltrim(upper(cemp->cip)) == alltrim(upper(cIp)) .and. vcodiemp # cemp->cod_emp
	         if ConectarOutraBase(cemp->cdatabase)
	            SR_SetActiveConnection(nConexaoAtual)
					if SR_ExistIndex("PRODUTOS")
	               cAliasNovo:=cGetNewAlias("PRO")
	               DbUseArea(.t.,"SQLRDD","PRODUTOS",cAliasNovo,.T.,.F.)
	               IF NetErr()
	                  MsgInfo("Arquivo Não Disponível no Momento" + CRLF + "PRODUTOS","Tente Novamente-Filial")
	                  Dbcloseall()
	                  Return .F.
	               Endif
	               (cAliasNovo)->(DbSetIndex())
	               aadd(aALIAS_PRO,cAliasNovo)
	               aadd(aConex_PRO,nConexaoAtual)
	            else
	               MsgWait("Não existe Índices para produtos na filial")
	            endif
	         endif
	      endif
	      select cemp
	      skip
	   End
	   nConexaoAtual := nConexaoPrincipal
	   SR_SetActiveConnection(nConexaoAtual)
Function ConectarOutraBase(pDatabase)
	lRetorno := .t.	   
	
   fileMysql:=MemoRead("sql.ini")
   aDadosMysql:=HB_ATokens(fileMysql,CRLF)
   if len(aDadosMysql) > 5
      tcIp:=aDadosMysql[1]
      tcUser:=aDadosMysql[2]
      tcPassword:=aDadosMysql[3]
      tcPort:=aDadosMysql[5]
   else
      return .f.
   endif

   scriptConectarMysql:="MySQL="+alltrim(tcIp)
   scriptConectarMysql+=";UID=" +alltrim(tcUser)
   scriptConectarMysql+=";PWD=" +alltrim(tcPassword)
   scriptConectarMysql+=";DTB=" +alltrim(pDatabase)
   scriptConectarMysql+=";PRT=" +alltrim(tcPort)
   *
	nConexaoAnterior := nConexaoAtual
	*
   nConexaoAtual := SR_AddConnection(CONNECT_MYSQL,scriptConectarMysql)
   
   IF nConexaoAtual > 0
      Try
         SR_CheckConnection()
      catch
         lRetorno := .f.
      end
      SR_SetExclusiveManagement(.t.)
      SR_MaxRowCache(200)
      SR_FetchSize( 100 )
   ELSE
      lRetorno := .f.
   ENDIF
   return lRetorno

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...