Define Class DBAccessController As Custom

	cServer = Null
	cSchema = Null
	cParola = Null
	oExecutor = Null
	oConn = Null
	nHandle = Null
	nSucces = 0
	lAfiseazaEroare = .T.
	***********************************************************************
	Procedure Init
		If Type('goConn') = 'O'
			This.oConn = goConn
		Else
			This.oConn = Createobject("oConn")
			Public goConn
			goConn = This.oConn
		Endif
		If Type('goExecutor') = 'O'
			This.oExecutor = goExecutor
		Else
			This.oExecutor = Createobject("oExecutor")
			Public goExecutor
			goExecutor = This.oExecutor
		Endif
		If Type('gnHandle') = 'N'
			This.nHandle = gnHandle
		Else
			This.nHandle = -1
			Public gnHandle
			gnHandle = -1
		Endif
	Endproc
	***********************************************************************
	PROCEDURE conecteaza
		Lparameters tcSchema,tcParola
		This.cSchema = tcSchema
		This.cParola = tcParola
		This.nHandle = This.oConn.Connect(This.cServer,This.cSchema,This.cParola)
	ENDPROC
	***********************************************************************
	PROCEDURE deconecteaza
		This.oConn.Disconnect(This.nHandle)
		This.cSchema = Null
		This.cParola = Null
		This.nHandle = -1
	ENDPROC
	***********************************************************************
	Procedure setServer
		Lparameters tcServer
		This.cServer = tcServer
	Endproc
	***********************************************************************
	Procedure setSchemaParola
		Lparameters tcSchema,tcParola
		If This.nHandle <> -1
			This.oConn.Disconnect(This.nHandle)
		Endif
		This.conecteaza(tcSchema,tcParola)
	Endproc
	***********************************************************************
	Procedure copiaza_structura_cursor
		Lparameters tcSursa,tcDestinatie
		LOCAL lnIndex
		If !Empty(tcSursa) And !Empty(tcDestinatie)
			If Used(tcSursa)
				Dimension laStructura(1,18)
				If Used(tcDestinatie)
					Use In (tcDestinatie)
				Endif
				Afields(laStructura,tcSursa)
*!*					lnIndex = ALEN(laStructura,1)+1
*!*					DIMENSION laStructura(lnIndex,18)
*!*					laStructura[lnIndex,1] = "RN"
*!*					laStructura[lnIndex,2] = "N"
*!*					laStructura[lnIndex,3] = 10
*!*					laStructura[lnIndex,4] = 0
*!*					laStructura[lnIndex,5] = .F.
*!*					laStructura[lnIndex,6] = laStructura[lnIndex-1,6]
*!*					laStructura[lnIndex,7] = []
*!*					laStructura[lnIndex,8] = []
*!*					laStructura[lnIndex,9] = []
*!*					laStructura[lnIndex,10] = laStructura[lnIndex-1,10] 
*!*					laStructura[lnIndex,11] = laStructura[lnIndex-1,11]
*!*					laStructura[lnIndex,12] = laStructura[lnIndex-1,12]
*!*					laStructura[lnIndex,13] = []
*!*					laStructura[lnIndex,14] = []
*!*					laStructura[lnIndex,15] = []
*!*					laStructura[lnIndex,16] = []
*!*					laStructura[lnIndex,17] = 1
*!*					laStructura[lnIndex,18] = 1
				Create Cursor (tcDestinatie) From Array laStructura
				Release laStructura
			Else
				amessagebox("Eroare interna 2 - copiaza structura cursor",16,"Eroare")
			Endif
		Else
			amessagebox("Eroare interna 1 - copiaza structura cursor",16,"Eroare")
		Endif
	Endproc
	***********************************************************************
	Function apeleaza_sql_hash
		Lparameters toHash,tcNumeCursor
		LOCAL lcSql
		lcSql = [select ] + toHash.getValue('cColumns') + ;
			[ from ] + toHash.getValue('cTables') + ;
			IIF(!Empty(toHash.getValue('cWhere')),[ where ] + toHash.getValue('cWhere'),[]) + ;
			IIF(!Empty(toHash.getValue('cGroupBy')),[ group by ] + toHash.getValue('cGroupBy'),[]) + ;
			IIF(!Empty(toHash.getValue('cOrderBy')),[ order by ] + toHash.getValue('cOrderBy'),[])
		RETURN This.apeleaza_sql(lcSql,tcNumeCursor)
	endfunc
	***********************************************************************
	Function apeleaza_sql
		Lparameters tcSql,tcNumeCursor
		Local lcNumeCursor,lcAlias,llSucces
		llSucces = .F.
		lcAlias = Alias()
		lcNumeCursor = [codbctemp]
		If Used(lcNumeCursor)
			Use In (lcNumeCursor)
		Endif
		This.nSucces = This.oExecutor.oExecute(tcSql,lcNumeCursor)
		llSucces = This.verificaSucces()
		If llSucces
			If !Used(tcNumeCursor)
				This.copiaza_structura_cursor(lcNumeCursor,tcNumeCursor)
			Endif
			Select (tcNumeCursor)
			Zap In (tcNumeCursor)
			Append From Dbf(lcNumeCursor)
			Go Top
			If Used(lcNumeCursor)
				Use In (lcNumeCursor)
			Endif
			If !Empty(lcAlias)
				Select (lcAlias)
			Endif
		Endif
		Return llSucces
	Endfunc
	***********************************************************************
	Function apeleaza_procedura
		Lparameters tcApelProcedura,tcNumeCursor
		Local lcNumeCursor,lcAlias,llSucces,lcDeclaratie
		llSucces = .F.
		lcAlias = Alias()
		If !Empty(tcNumeCursor)
			lcNumeCursor = [codbctemp]
			lcSql = [{call ] + tcApelProcedura + [}]
			If Used(lcNumeCursor)
				Use In (lcNumeCursor)
			Endif
		Else
			lcSql = [begin ] + tcApelProcedura + [; end;]
		Endif

		This.nSucces = This.oExecutor.oExecute(lcSql,lcNumeCursor)
		
		llSucces = This.verificaSucces()
		If llSucces And !Empty(tcNumeCursor)
			If !Used(tcNumeCursor)
				This.copiaza_structura_cursor(lcNumeCursor,tcNumeCursor)
			Endif
			Select (tcNumeCursor)
			Zap In (tcNumeCursor)
			Append From Dbf(lcNumeCursor)
			Go Top
			If Used(lcNumeCursor)
				Use In (lcNumeCursor)
			Endif
		Endif
		If !Empty(lcAlias)
			Select (lcAlias)
		Endif
		Return llSucces
	Endfunc
	***********************************************************************
	Function apeleaza_procedura_o
		Lparameters tcApelProcedura,tcSirTip
		Local lcNumeCursor,lcAlias,llSucces,lcNumeVariabila,lnParametri, lcTip
		Dimension laSir[1]
		llSucces = .F.
		lcAlias = Alias()
		lnParametri = Getwordcount(tcSirTip,[|])
		Dimension laSir[lnParametri]
		For i = 1 To lnParametri
			lcTip = Getwordnum(tcSirTip,i,[|])
			lcNumeVariabila = [p] + lcTip + [Parametru] + Alltrim(Str(i))
			lcDeclaratie = [Private ] + lcNumeVariabila
			&lcDeclaratie
			DO case
			 CASE UPPER(lcTip) = 'N'
			 	lcDeclaratie = lcNumeVariabila + [ = 0]
			 CASE UPPER(lcTip) = 'C'
			 	lcDeclaratie = lcNumeVariabila + [ = '']
			 Otherwise
			 	lcDeclaratie = lcNumeVariabila + [ = Null]
			endcase			  		
			&lcDeclaratie
			tcApelProcedura = Strtran(tcApelProcedura,[@p]+Padl(Alltrim(Str(i)),2,[0]),[@]+lcNumeVariabila)
		Endfor
		lcSql = [{call ] + tcApelProcedura + [}]
		This.nSucces = This.oExecutor.oExecute(lcSql)
		llSucces = This.verificaSucces()
		If llSucces
			For i = 1 To lnParametri
				lcNumeVariabila = [p] + Getwordnum(tcSirTip,i,[|]) + [Parametru] + Alltrim(Str(i))
				loVariabila = &lcNumeVariabila
				laSir[i] = loVariabila
				lcDeclaratie = [Release ] + lcNumeVariabila
				&lcDeclaratie
			Endfor
		ELSE
			For i = 1 To lnParametri
				laSir[i] = Null
			Endfor
		Endif
		If !Empty(lcAlias)
			Select (lcAlias)
		Endif
		Return laSir
	Endfunc
	***********************************************************************
	Function apeleaza_functie
		Lparameters tcApelFunctie,tcTip
		Local lcCursor,luReturn
		lcCursor = [crstaf010101]
		lcSql = [select ] + tcApelFunctie + [ as valoare from dual]
		This.nSucces = This.oExecutor.oExecute(lcSql,lcCursor)
		If This.verificaSucces()
			Select (lcCursor)
			luReturn = valoare
			If Used(lcCursor)
				Use In (lcCursor)
			Endif
			Do Case
				Case Isnull(luReturn)
					Return Null
				Case Type('luReturn') <> tcTip
					Return Val(luReturn)
				Otherwise
					Return luReturn
			Endcase
		Else
			Return Null
		Endif
	Endfunc
	***********************************************************************
	Function getEroare
		Return Iif(Between(This.oExecutor.nEroare,20000,21000),This.oExecutor.cEroare,[])
	Endfunc
	***********************************************************************
	Function getSucces
		Return This.nSucces
	Endfunc
	***********************************************************************
	Function getSchema
		Return This.cSchema
	Endfunc
	***********************************************************************
	Procedure setAfiseazaEroare
		Lparameters tlAfiseazaEroare
		This.lAfiseazaEroare = tlAfiseazaEroare
	Endproc
	***********************************************************************
	Procedure getAfiseazaEroare
		Return This.lAfiseazaEroare
	Endproc
	***********************************************************************	
	Function verificaSucces
		Local llReturn
		llReturn = .T.
		If This.nSucces < 0
			IF This.lAfiseazaEroare OR (!BETWEEN(This.oExecutor.nEroare,20000,21000))
				amessagebox(This.oExecutor.cEroare,16,"Eroare")
			ENDIF
			llReturn = .F.
		Endif
		Return llReturn
	Endfunc
	***********************************************************************
	Function setTranzactieManuala
		Local lnSucces
		lnSucces = SQLSetprop(This.nHandle,"Transactions",2)
		If lnSucces < 0
			If This.lAfiseazaEroare
				amessagebox("Programul nu a reusit sa treaca pe tranzactie manuala! Reintrati in program si incercati din nou!",16,"Eroare")
			Endif
			llReturn = .F.
		Else
			llReturn = .T.
		Endif
		Return llReturn
	Endfunc
	***********************************************************************	
	Function setTranzactieAutomata
		Local lnSucces,llReturn
		lnSucces = SQLSetprop(This.nHandle,"Transactions",1)
		If lnSucces < 0
			If This.lAfiseazaEroare
				amessagebox("Programul nu a reusit sa treaca pe tranzactie automata! Iesiti din program si intrati din nou!",16,"Eroare")
			Endif
			llReturn = .F.
		Else
			llReturn = .T.
		Endif
		Return llReturn
	Endfunc
	***********************************************************************		
	Function confirmaOperatii
		Local lnSucces,llReturn
		lnSucces = Sqlcommit(This.nHandle)
		If lnSucces < 0
			If This.lAfiseazaEroare
				amessagebox("Eroare la COMMIT!",16,"Eroare")
			Endif
			llReturn = .F.
		Else
			llReturn = .T.
		Endif
		Return llReturn
	Endfunc
	***********************************************************************
	Function revocaOperatii
		Local lnSucces,llReturn
		lnSucces = Sqlrollback(This.nHandle)
		If lnSucces < 0
			If This.lAfiseazaEroare
				amessagebox("Eroare la ROLLBACK!",16,"Eroare")
			Endif
			llReturn = .F.
		Else
			llReturn = .T.
		Endif
		Return llReturn
	Endfunc
	***********************************************************************				
Enddefine