#Define crlf Chr(13) + Chr(10)

**** de verificat programul , incomplet

Set Ansi On
Set Exact On
Set Deleted On

***** sequence anulat
Public gnConnHandle

Local tcHost, tcUser, tcPassword, lcTablePath, laEroare,lnSucces

On Error
lnSucces = 0
lcTablePath = []

tcHost = [jcsserver]
tcUser = [test_alex]
tcPassword = [123]

gnConnHandle = conecteaza(tcHost, tcUser, tcPassword)
SQLSetprop(gnConnHandle,"Transactions",2)
If gnConnHandle > 0
	gcAppDir = Addbs(Justpath(Sys(16,0)))
	lcTablePath = gcAppDir + [xact]


	SQLSetprop(gnConnHandle,"Transactions",2)	&& tranzactie manuala

	*	lnSucces = makeTable(lcTablePath)			&& creaza tabela cu sau fara sequence
	*	If lnSucces > 0
	importTable(lcTablePath,.T.)
	*	Endif
Else
	Aerror(laEroare)
	Messagebox(laEroare(2))
Endif

SQLSetprop(gnConnHandle,"Transactions",1)	&& tranzactie automata
deconecteaza()
****************************** inceput importTable ******************************************

Procedure importTable
	Lparameters tcTableName, llDel

	Local i,lnNr, lcSql, lcInsert, lnSucces, tacColName, lcCols, lcVals, nr
	lnSucces = 0
	lcNumeTabela = []
	lcSir = []
	lcCols = []
	lcVals = []
	lcInsert = []

	lcNumeTabela = Substr(tcTableName,Rat("\",tcTableName))
	nr = Len(lcNumeTabela)
	lcNumeTabela = Right(lcNumeTabela,nr-1)

	If !Used(tcTableName)
		Use (tcTableName)
	Endif

	If llDel
		executaSql([delete from ] + Alltrim(lcNumeTabela))
	Endif

	lnNr = Afields(taColName)

	lcSql = [insert into ] + Alltrim(lcNumeTabela)
	For i = 1 To lnNr
		lcCols = lcCols + taColName(i,1) + [,]
	Endfor
	lcCols = Left(lcCols,Len(lcCols)-1)

	Scan
		lcVals = []
		lcInsert = []
		For i = 1 To lnNr			&& se adauga valorile din tabela , convertite
			If taColName(i,2) = "I" Or taColName(i,2) = "N"
				lcVals = lcVals + Alltrim(Str(&taColName(i,1))) + [,]
			Endif
			If taColName(i,2) = "D" Or taColName(i,2) = "T"
				lcVals = lcVals + [to_date('] + Alltrim(Dtos(&taColName(i,1))) + [','yyyymmdd'),]
			Endif
			If taColName(i,2) = "L"
				lcVals = lcVals + Iif(&taColName(i,1),[1,],[0,])
			Endif
			If taColName(i,2) = "C"
				lcVals = lcVals + ['] + Alltrim(&taColName(i,1)) + [',]
			Endif
		Endfor
		lcVals = Left(lcVals,Len(lcVals) - 1)
		lcInsert = lcSql + [(] + lcCols + [)] + [ values(] + lcVals + [)]

		lnSucces = executaSql(lcInsert,'',.T.)
		If lnSucces < 0
			Strtofile(lcInsert,[d:\afis.txt])
			Exit
		Endif
	Endscan

	If lnSucces < 0
		Sqlrollback(gnConnHandle)
		Messagebox([ eroare la insert] + crlf + [sql-ul se gaseste in d:\afis.txt], 48)
	Else
		Sqlcommit(gnConnHandle)
		Messagebox('sa facut importu')
	Endif

	SQLSetprop(gnConnHandle,"Transactions",1)

Endproc

****************************** inceput makeTable *********************************************
Function makeTable
	Lparameters tcTableName, llTrg

	Local i,lnNr, lnSucces, lcSql, lcSeq, lcTrig, lcPk, lcTable, lcTipData, lcLen, lcCol, lcNumeTabela, lcSir
	lnSucces = 0
	lcNumeTabela = []
	lcSir = []
	lcSeq = []
	lcTrig = []
	lcPk = []
	lcTipData = []
	lcLen = []
	lcCol = []
	lnNr = []
	lcSql = []

	lcNumeTabela = Substr(tcTableName,Rat("\",tcTableName))
	lnNr = Len(lcNumeTabela)
	lcNumeTabela = Right(lcNumeTabela,lnNr-1)
	*	Use (lcNumeTabela)

	If !Used(tcTableName)
		Use (tcTableName)
	Endif

	lnNr = Afields(taColName)		&& get table data

	lcTable = [create table ] + lcNumeTabela +[(]
	For i = 1 To lnNr						&& creare tabela
		lcTipData = Iif(taColName(i,2)="I" Or taColName(i,2)="N", [number],lcTipData )
		lcTipData = Iif(taColName(i,2)="D" Or taColName(i,2)="T",[date],lcTipData )
		lcTipData = Iif(taColName(i,2)="L",[number],lcTipData )
		lcTipData = Iif(taColName(i,2)="F" ,[binary_float],lcTipData )
		lcTipData = Iif(taColName(i,2)="B",[binary_double],lcTipData )
		lcTipData = Iif(taColName(i,2)="C" Or taColName(i,2)="M" Or taColName(i,2)="V",[varchar2],lcTipData )

		lcLen = Iif(lcTipData=[number] And taColName(i,2)<>"L", ;
			[(]+ Alltrim(Str(taColName(i,3))) +[)],lcLen )
		lcLen = Iif(lcTipData=[number] And taColName(i,2)="L",[(1)],lcLen )
		lcLen = Iif(lcTipData=[varchar2] And (taColName(i,2)="C" Or taColName(i,2)="V" Or taColName(i,2)="M"),  ;
			[(]+Alltrim(Str(taColName(i,3)))+[)],lcLen )

		*		lcCol = lcCol + [ ?taColName(i,1) ?lcTipData ?lcLen] + Iif(taColName(i,5),[ not null ],[]) + [,]
		lcCol = lcCol + taColName(i,1)+ [ ] + lcTipData + lcLen + Iif(taColName(i,5),[ not null ],[]) + [,]
		lcTipData = []
		lcLen = []
	Endfor

	lcTable = lcTable + Left(lcCol,Len(lcCol)-1) + [);]
	lnSucces = executaSql(lcTable,'',.T.)

	*!*		If lnSucces > 0
	*!*			lcPk = [alter table ]+ lcNumeTabela  + ;		&&primary key
	*!*			[ add constraint PK_]+lcNumeTabela +[ primary KEY(]+taColName(1,1) +[); ]
	*!*			lnSucces = executaSql(lcPk)

	*!*			If lnSucces > 0 									&&trigger
	*!*				If llTrg
	*!*					lcSeq = [create sequence SEQ_] + lcNumeTabela  + ;
	*!*						[ minvalue 1 ] + ;
	*!*						[ maxvalue 99999999999999 ]+ ;
	*!*						[ start with 1 ] + ;
	*!*						[ increment by 1 ] + ;
	*!*						[ nocache; ]
	*!*					lnSucces = executaSql(lcSeq,'',.t.)
	*!*				Endif
	*!*				If lnSucces > 0
	*!*					lcTrig = [create or replace trigger TRG_]+lcNumeTabela + ;
	*!*						CHR(13)+Chr(10) + [ before insert on ] + lcNumeTabela  + ;
	*!*						CHR(13)+Chr(10) + [ referencing old as old new as new] + ;
	*!*						CHR(13)+Chr(10) + [ for each row] + ;
	*!*						[ begin ] + ;
	*!*						CHR(13)+Chr(10) + [  if :new.] + taColName(1,1)+ [ is null then ] + ;
	*!*						CHR(13)+Chr(10) + [    select SEQ_] + lcNumeTabela + ;
	*!*						[.NEXTVAL into :new.] + taColName(1,1)+ [ from dual; ] + ;
	*!*						CHR(13)+Chr(10) + [  end if; ] + ;
	*!*						CHR(13)+Chr(10) + [ end; ]
	*!*					lnSucces = executaSql(lcTrig,'',.t.)
	*!*				Endif
	*!*			Endif
	*!*		ENDIF

	If lnSucces < 0
		Sqlrollback(gnConnHandle)
	Else
		Strtofile(lcTable,[D:\roa\ROACASA\COMUN\script.sql],1)
		Sqlcommit(gnConnHandle)
	Endif

	Return lnSucces
Endfunc



************************	inceput conecteaza ***************************
Procedure conecteaza
	Lparameters tcHost, tcUser, tcPassword

	gnConnHandle = SQLConnect(tcHost, tcUser, tcPassword)
	If gnConnHandle < 1
		Aerror(laEroare)
		Messagebox(laEroare(2))
	Endif

	Return gnConnHandle
Endproc

**************************** inceput executaSql ***************************
Function executaSql
	Lparameters cmdSql, crsName, tlShowErr
	Local lnSucces

	lnSucces = -1

	If Pcount() < 2 And !m.tlShowErr
		crsName=""
	Endif

	If gnConnHandle > 0
		lnSucces = SQLExec(gnConnHandle,cmdSql,crsName)

		If lnSucces < 0 And m.tlShowErr
			Aerror(laEroare)
			eroaresql(@laEroare,cmdSql)
		Endif
	Endif

	Return lnSucces
Endfunc

*************************	inceput deconecteaza ************************
Function  deconecteaza
	*Lparameters lnConn
	Local lnSucces

	lnSucces = -1
	If gnConnHandle > 0
		lnSucces = SQLDisconnect(gnConnHandle)
	Endif

	On Error
	Return lnSucces
Endfunc

*******************************	inceput eroareSql **********************************
Procedure eroaresql
	Lparameters laErr, lcSql

	External Array laErr

	Local lcMesaj

	lcMesaj = [Eroare # : ] + Alltrim(Str(laErr(1))) + crlf + ;
		[Mesajul : ] + laErr(2) + ;
		IIF(!Empty(laErr(3)), [Eroare OLE : ] + laErr(3) ,[]) + ;
		[Aplicatie : ] + laErr(4) + ;
		IIF(Pcount() = 2, crlf + lcSql, [])
	Messagebox(lcMesaj,0+16)

Endproc




*!*	Procedure importTable
*!*		Lparameters tcTableName, llDel

*!*		Local i,lnNr, lcSql, lnSucces, lcSql,tacColName, nr
*!*		lnSucces = 0
*!*		lcNumeTabela = []
*!*		lcSir = []

*!*		lcNumeTabela = Substr(tcTableName,Rat("\",tcTableName))
*!*		nr = Len(lcNumeTabela)
*!*		lcNumeTabela = Right(lcNumeTabela,nr-1)
*!*		*	Use (lcNumeTabela)

*!*		If !Used(tcTableName)
*!*			Use (tcTableName)
*!*		Endif

*!*		lnNr = Afields(taColName)

*!*		lcSql = [insert into ] + Alltrim(lcNumeTabela)
*!*		For i = 1 To lnNr
*!*			lcSir = lcSir + taColName(i,1) + [,]
*!*		Endfor
*!*		lcSir = Left(lcSir,Len(lcSir)-1)

*!*		lcSql = lcSql + "("+ lcSir + ") values("

*!*		lcSir = []
*!*		For i = 2 To lnNr
*!*			lcSir = lcSir + "?" + taColName(i,1) + ","
*!*		Endfor
*!*		lcSir = Left(lcSir,Len(lcSir)-1)
*!*		lcSql = lcSql + lcSir +")"

*!*		If llDel
*!*			executaSql([delete from ]+Alltrim(lcNumeTabela))
*!*		ENDIF

*!*		Scan
*!*			lnsucces = executaSql(lcSql,'',.t.)
*!*			IF lnsucces < 0
*!*				EXIT
*!*			endif
*!*		ENDSCAN
*!*
*!*
*!*
*!*		IF lnsucces < 0
*!*			SQLROLLBACK(gnconnhandle)
*!*		ELSE
*!*			SQLCOMMIT(gnconnhandle)
*!*		endif

*!*		SQLSETPROP(gnconnhandle,"Transactions",1)
*!*		deconecteaza()
*!*	Endproc

****************************** inceput makeTable *********************************************
