DEFINE CLASS cData As Custom

	nErrorNo = 0
	sErrorMssg = ""
	cOldErrHand = "" 
	nDataSource = -1
	cConnectionString = ""
	nLineNo = 0
	sProgam = ""
	sLineTxt = ""
	sOtvorene = ""
	sProgramChain = ""
	sSqlPrefix = ""
	tichyRezim = .T.

	PROTECTED oLogToFile
	


	PROTECTED ARRAY tablesList(100,1) &&Pole mien a ciest tabuliek
	PROTECTED nlastIndex

	
	PROCEDURE init
	LPARAMETERS m.constr
	LOCAL m.lRet
	
		IF PARAMETERS() < 1
			this.cConnectionString = ""
		ELSE
			this.cConnectionString = m.constr	
		ENDIF

		this.nDataSource = -1
		this.nlastIndex = 0
		this.clearError()
		
		this.cOldErrHand = ON("ERROR")		
		this.tichyRezim = .T.
		
		this.oLogToFile = .NULL.
		
		m.lRet = this.myConnect(this.cConnectionString)
		
		*** L.L. (Pozor, vzdy musim vratit .T., aby mi vznikol object a aby som mohol zisit chybu)
		RETURN .T.
	ENDPROC
	
	PROCEDURE setLogToFile
	LPARAMETERS m.pLogFilePrefix, m.pLogPath, m.pLogLevel, m.pMaxLogsDay 
	LOCAL lret	
		m.pLogFilePrefix	= IIF(PARAMETERS() < 1, "SQL_", m.pLogFilePrefix)		
		m.pLogPath    		= IIF(PARAMETERS() < 2, "", m.pLogPath)		
		m.pLogLevel   		= IIF(PARAMETERS() < 3, 0, m.pLogLevel)		
		m.pMaxLogsDay		= IIF(PARAMETERS() < 4, 5, m.pMaxLogsDay)		
		
		IF !EMPTY(m.pLogPath) AND m.pLogLevel>0 &&mam nastavenu cestu na logovanie a level logovania je  > 0 => logujem
			this.oLogToFile = CREATEOBJECT("clogtofile",pLogPath, pLogFilePrefix, "LOG", pLogLevel, pMaxLogsDay)	
			IF TYPE("this.ologtofile") == "O" .and. !isNull(this.ologtofile)
				m.lRet = .T.
			ELSE
				m.lRet = .F.
				this.oLogToFile = .NULL.
			ENDIF
		ELSE
			m.lRet = .F.
			this.oLogToFile = .NULL.
		ENDIF
		
		RETURN m.lRet
	ENDPROC

	PROCEDURE nastavTichyRezim
		this.tichyRezim = .T.
	ENDPROC
	
	PROCEDURE zrusTichyRezim
		this.tichyRezim = .F.
	ENDPROC

	PROCEDURE nastavRezim
	LPARAMETERS lTichyRezim
		this.tichyRezim = lTichyRezim
	ENDPROC

	
	PROCEDURE myConnect
	LPARAMETERS m.constr

		IF PARAMETERS() > 0
			IF !EMPTY(m.constr)
				this.cConnectionString = m.constr	
			ENDIF
		ENDIF

		this.cOldErrHand = ON("ERROR")
		this.clearError()

		*** L.L. (11.11.2017.02) nDataSource nenastavujem na -1. Ak je totiz<>-1 => som pripojeny
		*this.ndatasource = -1
		
		RETURN .T.
	ENDPROC

	PROCEDURE myExec
	LPARAMETERS m.lcExec, m.lcAlias

		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC


	PROCEDURE myInsert
	LPARAMETERS m.lcTableName, m.lcCoulmns, m.lcValues

		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC	

	PROCEDURE myUpdate
	LPARAMETERS m.lcTableName, m.lcSets, m.lcCondidion

		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC	

	PROCEDURE mySelect
	LPARAMETERS m.lcCommand, m.lcDestName
	
		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC

	PROCEDURE dajTabulku
	LPARAMETERS m.lcTableName, m.lcAlias, m.lToTable, m.lcOrder, m.lcPodmienka
		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC	

	PROCEDURE myAppendFromNative
	LPARAMETERS m.lcFrom, m.cTo, m.cCondition
		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC
	

	PROCEDURE myAppendFromMemvar
	LPARAMETERS m.cTo
		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC


	PROCEDURE isExistTable
	LPARAMETERS m.cTableName

		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN .F.
	ENDPROC

	PROCEDURE commit
		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC
		
	PROCEDURE rollback
		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC

	PROCEDURE setTransaction
	PARAMETERS lnSetTO
		this.cOldErrHand = ON("ERROR")
		this.clearError()
		
		RETURN 1
	ENDPROC


	PROCEDURE myDisconnect
		this.clearError()
		RETURN .T.
	ENDPROC
	

	PROCEDURE vyhodTag
	LPARAMETERS m.cString, m.cTag
	LOCAL m.cStrngBezTagu, m.nZaciatokTagu, m.nKoniecTagu, m.cPomString
		m.cStrngBezTagu = m.cString
		DO WHILE .T.
			m.cPomString = m.cStrngBezTagu
			m.nZaciatokTagu =  AT("<"+m.cTag+">", m.cPomString)
			m.nKoniecTagu = AT("</"+m.cTag+">",m.cPomString)
			IF m.nZaciatokTagu > 0
				m.cStrngBezTagu = LEFT(m.cPomString,nZaciatokTagu-1) 
				IF m.nKoniecTagu > 0
					m.cStrngBezTagu = m.cStrngBezTagu+SUBSTR(m.cPomString,m.nKoniecTagu+LEN(ctag)+3)
				ENDIF
			ELSE
				EXIT
			ENDIF
		ENDDO
		RETURN m.cStrngBezTagu
	ENDPROC


	PROCEDURE setSqlPrefix
	LPARAMETERS m.psSqlPrefix
		this.sSqlPrefix = m.psSqlPrefix
	ENDPROC

	PROCEDURE setPrefix
	LPARAMETERS m.cString	
		RETURN STRTRAN(m.cString, "<PREFIX>",this.sSqlPrefix+".")
	ENDPROC 
	
	
	PROCEDURE addTableName
	LPARAMETERS tableName, tablePath
		
		m.tableName = ALLTRIM(UPPER(m.tableName))
		IF !tableExist(tableName)
			m.tablePath = ADDBS(m.tablePath)
			this.nlastIndex = this.nlastIndex + 1
			this.tablesList(this.nlastIndex,1) = m.tableName
			this.tablesList(this.nlastIndex,2) = m.tablePath 
		ENDIF
		
	ENDPROC 

	PROCEDURE tableExist
	LPARAMETERS tableName
		RETURN this.getIndexOfTable(tableName) <> 0
	ENDPROC
		
	PROCEDURE getIndexOfTable
	LPARAMETERS tableName
		RETURN ASCAN(this.tablesList, UPPER(tableName), 1, ALEN(this.tablesList,1), 1, 14)
	ENDPROC
	
	PROCEDURE getTablePath
	LPARAMETERS tableName
	LOCAL tablePath, nIndex		
		m.nIndex = ASCAN(this.tablesList, UPPER(tableName), 1, ALEN(this.tablesList,1), 1, 14)
		IF m.nIndex <> 0
			tablePath = this.tablesList(m.nIndex, 2)
		ELSE
			tablePath = this.cConnectionString 
		ENDIF
	ENDPROC 


	PROCEDURE onRecordInsert
	LPARAMETERS pNAktualnyZoznam, pnCelkovyPocetZaznamov
		IF !this.tichyRezim
			WAIT ALLTRIM(STR(pNAktualnyZoznam,10,0))+"/"+ALLTRIM(STR(pnCelkovyPocetZaznamov,10,0)) windows NOWAIT 
		ENDIF
	ENDPROC

	PROCEDURE viewMessage
	LPARAMETERS m.pnType, m.psMessage
		this.writeToLog(m.pnType, m.psMessage)
		*WAIT m.psMessage WINDOW NOWAIT 
	ENDPROC

	PROCEDURE writeToLog
	LPARAMETERS pnTypSpr, radek
	LOCAL lret
		IF TYPE("this.ologtofile") == "O" .and. !isNull(this.ologtofile)
			TRY
				this.ologtofile.zapisspravudologu(pnTypSpr-100, TTOC(DATETIME())+" "+m.radek)
				m.lret =.T.
			CATCH
				m.lRet = .F.
			ENDTRY
		ELSE
			m.lRet = .T.					
		ENDIF
				
		RETURN m.lRet
	ENDPROC

	PROCEDURE clearError
		this.nErrorNo = 0
		this.sErrorMssg = ""
		this.nLineNo = 0
		this.sProgam = ""
		this.sLineTxt = ""
		this.sOtvorene = ""
		this.sProgramChain = ""
	ENDPROC 
	
	PROCEDURE getErrorNo
		RETURN this.nErrorNo
	ENDPROC 

	PROCEDURE getErrorMssg
	LOCAL cErrorMssg	
		RETURN this.sErrorMssg
	ENDPROC 

	PROCEDURE getLastErrorNO
	LOCAL m.nLastErrorNo
		m.nLastErrorNo = this.nErrorNo
		this.nErrorNo = 0
		
		RETURN m.nLastErrorNo
	ENDPROC 
	
	PROCEDURE Error
	LPARAMETERS nErrorno, cProcedure, nLineNo
	LOCAL oldErrorHand, lnIndex
	
		this.nErrorNo = nErrorno
		this.sErrorMssg =  MESSAGE() 
		this.nLineNo = nLineNo
		this.sProgam = cProcedure
		this.sLineTxt = MESSAGE(1)
		this.sOtvorene = DBF() + IIF(EMPTY(DBF()), "", " as " + ALIAS() + IIF(EMPTY(ORDER()), " not ordered", " tag " + ORDER()))
		this.sOtvorene = SUBSTR(STRTR(this.sOtvorene, ".DBF", ""), 4)
		
		this.sProgramChain = PROGRAM(2)
			
		m.lnIndex = 3
		DO WHILE (PROGRAM(m.lnIndex) # PROGRAM()) .AND. (m.lnIndex < 32)
			this.sProgramChain  = ALLT(IIF ("..." $ PROGRAM(m.lnIndex), "", PROGRAM(m.lnIndex) + " ") + this.sProgramChain)
			m.lnIndex= m.lnIndex + 1
		ENDDO

		
		oldErrorHand= this.cOldErrHand 
		&oldErrorHand
	ENDPROC
	
	
	
	PROCEDURE viewErrorMessage
	PRIVATE m.lsMessage, m.line_txt, sta_debug, can_susp

		PUSH KEY CLEAR

		sta_debug = "DEB" $ m.food_debug
		can_susp  = .NOT. "LIBRARY" $ UPPER(VERSION())

		m.lsMessage = STR(this.nErrorNo, 5) + ":" + this.sErrorMssg
		m.line_txt = STR(this.nLineNo, 5) + ":" + ALLTRIM(this.sLineTxt)

		DO CASE
			CASE (this.nErrorNo = 165) .AND. ("ST_SYSMENU" $ UPPER(m.line_txt))
				POP KEY                              && Nedefinovan‚ syst‚mov‚ menu
				ON KEY LABEL SHIFT+f12
				RETURN
			OTHERWISE
				MESSAGEBOX('Err: '+ALLTRIM(m.lsMessage)+CHR(10)+;
					   'Line: '+ALLTRIM(m.line_txt)+CHR(10)+;
					   'Program: '+ALLTRIM(this.sProgramChain)+CHR(10)+CHR(10)+;
					   'Program bude ukončený';
				   		, 16, 'Chyba')

		ENDCASE
		this.viewMessage(100, 'Err: '+ALLTRIM(m.lsMessage)+'Line: '+ALLTRIM(m.line_txt)+CHR(10)+ 'Program: '+ALLTRIM(this.sProgramChain)+CHR(10)+CHR(10))
	ENDPROC

	PROCEDURE release
			
		this.clearError()
		TRY 
			this.myDisconnect()
		CATCH
		ENDTRY
	ENDPROC

ENDDEFINE




DEFINE CLASS cDataSql AS cData	

	PROCEDURE sqlError	
	LOCAL laerrors(1), m.prog_chain, lnI
	
		AERROR(laerrors)
		this.nErrorNo = laerrors(1)
		this.sErrorMssg = "SqlError: "+laerrors(2)	
		
		m.prog_chain = PROGRAM(2)
		m.lnI = 3
		DO WHILE (PROGRAM(m.lnI) # PROGRAM()) .AND. (m.lnI < 32)
			m.prog_chain = ALLT(IIF ("..." $ PROGRAM(m.lnI), "", PROGRAM(m.lnI) + " ") + m.prog_chain)
			m.lnI = m.lnI + 1
		ENDDO
		this.sProgramChain = ALLTRIM(m.prog_chain)
		this.nLineNo = LINE()
		this.sLineTxt = "" 
		this.viewMessage(100, 'Err: '+ALLTRIM(STR(this.nErrorNo,10,0))+"-"+ALLTRIM(this.sErrorMssg)+'Line: '+ALLTRIM(this.sLineTxt)+CHR(10)+ 'Program: '+ALLTRIM(this.sProgramChain)+CHR(10)+CHR(13))
	ENDPROC
	
						
	PROCEDURE myConnect
	LPARAMETERS m.constr

		DODEFAULT(m.constr)

		*** L.L. (11.11.2017.02) Ak som uz pripojeny, tak sa nepripajam znovu	 		
 		IF this.ndatasource<1  &&este nie som pripojeny
			this.viewmessage(103, "Connecting to SQL server...") 
			=SQLSETPROP(0,"Displogin",3)
			this.ndatasource = SQLSTRINGCONNECT(ALLTRIM(this.cConnectionString))				
			*** AK je v ODBC chyba, tak vo VFP je to OK, takze musim chybu zistit rucne ***
			IF this.ndatasource<1				
				this.sqlError()					
			ENDIF				
			WAIT CLEAR
		ENDIF
			
		RETURN IIF(this.ndatasource < 1, .F., .T.)
	ENDPROC
	

	PROCEDURE myDisconnect
		this.viewmessage(103, "Disconnecting from SQL server....") 
		IF this.ndatasource > 0			
			SQLDISCONNECT(this.ndatasource)
			*** L.L. (11.11.2017.02) Po diskonekte nastavim ndatasource na -1, aby som v pripade dalsieho myconnectu vedel, ze sa musim znovu pripojit
			this.ndatasource = -1
		ENDIF
		WAIT CLEAR 
	ENDPROC
	
	
	**
	PROCEDURE myExec
	LPARAMETERS m.lcExec, m.lcAlias
	LOCAL olderrhandle, m.nret, lcOutputPath, lcOutputFile

		m.lcAlias = IIF(PARAMETERS()<2, "", m.lcAlias)
		
		DODEFAULT(m.lcExec, m.lcAlias)
		
		*** naskor vyhodim cestu, ktora je potrebna len pre FREETABLE ***
		m.lcExexBezPath = this.vyhodTag(lcExec, "PATH")
		m.lcExexBezPath = this.setPrefix(m.lcExexBezPath)
		mlcExexBezPath = STRTRAN(m.lcExexBezPath,"<??>","?")
		
		IF !EMPTY(m.lcAlias)
			IF !EMPTY(JUSTPATH(m.lcAlias))  &&id o cursor
				m.lcOutputPath = JUSTPATH(m.lcAlias)
				m.lcOutputFile = JUSTFNAME(m.lcAlias)
				m.lcAlias = "POM_CUR"
			ELSE
				m.lcOutputPath = ""
				m.lcOutputFile = ""
			ENDIF
		ENDIF		
		
		*** Uprava RECNO(). Postgres nepozna recno(), tak to tu nahradim row_number() over() 
		*** Asi by bolo rozumnejsie urbot zase fukciu, ktora podla typu sql server urobi konverziu, ale kedze zatial 
		*** aj tak robime len z Posgresom, tak to dam rovno tu
		m.lcExexBezPath = STRTRAN(m.lcExexBezPath, "recno()", "row_number() over()", 1, -1, 1)
		
		this.viewmessage(101, m.lcExexBezPath)				
		m.nret = SQLEXEC(this.ndatasource, m.lcExexBezPath, m.lcAlias)
		
		IF m.nret<0 &&Nastala nejaka chyba
			this.sqlError()
		ELSE
			IF !EMPTY(m.lcOutputPath)
				SELECT POM_CUR
				SELECT * FROM POM_CUR INTO TABLE (m.lcOutputPath+"\"+m.lcOutputFile)
				USE IN POM_CUR
			ENDIF
		ENDIF
						
		RETURN m.nret
	ENDPROC


	PROCEDURE myInsert
	LPARAMETERS m.lcTableName, m.lcCoulmns, m.lcValues
	LOCAL m.lcTableBezPath
		
		DODEFAULT(m.lcTableName, m.lcCoulmns, m.lcValues)

		m.lcTableBezPath = this.vyhodTag(m.lcTableName, "PATH")  &&ak by bola cesta riesena tagom
		m.lcTableBezPath = JUSTFNAME(m.lcTableBezPath) 
		m.lcTableBezPath = FORCEEXT(m.lcTableBezPath, "")
		m.lcTableBezPath = IIF(AT("<PREFIX>", m.lcTableBezPath) # 0, this.setPrefix(m.lcTableBezPath), this.sSqlPrefix+"."+m.lcTableBezPath)			
		
		m.lcValues = STRTRAN(lcValues ,"<??>","?")
		
		nRet = this.myExec("INSERT INTO "+m.lcTableBezPath+" ("+m.lcCoulmns+") VALUES ("+m.lcValues+")")
		
		RETURN nRet
	ENDPROC 	

	PROCEDURE myUpdate
	LPARAMETERS m.lcTableName, m.lcSets, m.lcCondition
	LOCAL m.lcTableBezPath
		DODEFAULT(m.lcTableName, m.lcSets, m.lcCondition)

		m.lcTableBezPath = this.vyhodTag(m.lcTableName, "PATH")  &&ak by bola cesta riesena tagom
		m.lcTableBezPath = JUSTFNAME(m.lcTableBezPath) 
		m.lcTableBezPath = FORCEEXT(m.lcTableBezPath, "")
		m.lcTableBezPath = IIF(AT("<PREFIX>", m.lcTableBezPath) # 0, this.setPrefix(m.lcTableBezPath), this.sSqlPrefix+"."+m.lcTableBezPath)			
		
		m.lcCondition = STRTRAN(lcCondition ,"<??>","?")
		
		nRet = this.myExec("UPDATE "+m.lcTableBezPath+" SET "+m.lcSets+" WHERE "+m.lcCondition + ";")
		
		RETURN nRet
	ENDPROC 	


	PROCEDURE mySelect
	LPARAMETERS m.lcCommand, m.lcDestName
	LOCAL lcTableName, lcTablePath, lcPomFileName, lCmd, nRet, lcCursorName

		m.lcDestName = IIF(PARAMETERS()<2, "", m.lcDestName)
		m.lcCommand  = IIF(PARAMETERS()<1, "", m.lcCommand)
		
		IF !EMPTY(m.lcCommand)
			
			DODEFAULT(m.lcCommand, m.lcDestName)
			m.lcDestName  = this.vyhodTag(m.lcDestName,"PATH")
			m.lcTableName = FORCEEXT(JUSTFNAME(m.lcDestName),"")
			m.lcTablePath = ADDBS(JUSTPATH(lcDestName))			
			m.lcCommand   = this.vyhodTag(m.lcCommand, "PATH")
			m.lcCommand   = IIF(AT("<PREFIX>", m.lcCommand) # 0, this.setPrefix(m.lcCommand), this.sSqlPrefix+"."+m.lcCommand)			
			m.lcCommand   = STRTRAN(m.lcCommand,"<??>","?")
			
			IF !EMPTY(m.lcTablePath)
				m.lcCursorName = m.lcTableName +"cur"				
			ELSE
				m.lcCursorName = m.lcTableName 
			ENDIF

			IF this.myExec(m.lcCommand, m.lcCursorName) == 1
				IF !EMPTY(m.lcTablePath)
					SELECT * FROM (m.lcCursorName) INTO TABLE (m.lcTablePath+m.lcTableName)
					USE IN (m.lcCursorName)
				ENDIF
			ELSE
				m.nRet = -1
			ENDIF			
		ELSE
			this.nErrorNo = 10002
			this.sErrorMssg = "Wrong function call MYSELECT. Empty parameter command."
		ENDIF					
		
		IF this.nErrorNo <> 0   &&nastala nejaka chyba
			nRet = -1
		ELSE
			nRet = 1
		ENDIF		
	
		RETURN nRet								
	ENDPROC


	*** L.L. (05.11.2017.01) Pokus o zakomponovanie WHERE podmienky rovno do SQLExecu. Vyhodou je, ze sa prenasa ovela menej dat
	*** pridany parameetr lcSQLPodmienka
	PROCEDURE dajTabulku
	LPARAMETERS m.lcTableName, m.lcAlias, m.lToTable, m.lcOrder, lcPodmienka, lcSQLPodmienka
	LOCAL nRet, lcDestTable, m.lcTableBezPath, m.lcTableBezPrefix, lcDestTableName, lcTableBezPrefixOrig, m.lcSqlCommand 
		m.lcSQLPodmienka = IIF(PARAMETERS()<6, "", m.lcSQLPodmienka)
		m.lcPodmienka  = IIF(PARAMETERS()<5, "", m.lcPodmienka)
		m.lcOrder      = IIF(PARAMETERS()<4, "", m.lcOrder)
		m.lToTable     = IIF(PARAMETERS()<3, .T., m.lToTable)
		m.lcAlias      = IIF(PARAMETERS()<2, "", m.lcAlias)
		m.lcTableName  = IIF(PARAMETERS()<1, "", m.lcTableName)
		
		IF !EMPTY(m.lcTableName)

			DODEFAULT(m.lcTableName, m.lcAlias, m.lToTable, m.lcOrder, lcPodmienka)

			IF !EMPTY(m.lcPodmienka)
				m.lcPodmienka = " WHERE "+m.lcPodmienka
			ENDIF
			
			m.lcTableBezPath = this.vyhodTag(m.lcTableName, "PATH")  &&ak by bola cesta riesena tagom
			m.lcTableBezPath = JUSTFNAME(m.lcTableBezPath) 
			m.lcTableBezPath = FORCEEXT(m.lcTableBezPath, "")
			m.lcDestTable = IIF(EMPTY(m.lcAlias), STRTRAN(m.lcTableBezPath, "<PREFIX>",""), m.lcAlias)
			m.lcTableBezPrefix = STRTRAN(m.lcTableBezPath, "<PREFIX>","")
			m.lcTableBezPath = IIF(AT("<PREFIX>", m.lcTableBezPath) # 0, this.setPrefix(m.lcTableBezPath), this.sSqlPrefix+"."+m.lcTableBezPath)			
			m.lcDestTableName = JUSTFNAME(m.lcDestTable)
						
			DO CASE 
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="KARTY" AND this.isExistTable("skaskarty")
					m.lcTableBezPrefix = "skaskarty" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="KCENY" AND this.isExistTable("skaskceny")
					m.lcTableBezPrefix = "skaskceny" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="POLOZKY" AND this.isExistTable("skaspolozky")
					m.lcTableBezPrefix = "skaspolozky" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="KLAPKY" AND this.isExistTable("skasklapky")
					m.lcTableBezPrefix = "skasklapky" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 			
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="KL_DON" AND this.isExistTable("sdonklapky")
					m.lcTableBezPrefix = "sdonklapky" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 			
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="UMISTENI" AND this.isExistTable("skasumisteni")
					m.lcTableBezPrefix = "skasumisteni" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 								
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="UMDRUHY" AND this.isExistTable("skasumdruhy")
					m.lcTableBezPrefix = "skasumdruhy" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 								
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="HRAS_SET" AND this.isExistTable("skashras_set")
					m.lcTableBezPrefix = "skashras_set" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 								
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="FRAS_SET" AND this.isExistTable("skasfras_set")
					m.lcTableBezPrefix = "skasfras_set" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 								
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="GRAS_SET" AND this.isExistTable("skasgras_set")
					m.lcTableBezPrefix = "skasgras_set" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 								
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="ARAS_SET" AND this.isExistTable("skasaras_set")
					m.lcTableBezPrefix = "skasaras_set" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 								
				CASE ALLTRIM(UPPER(lcTableBezPrefix))=="HOR_SET" AND this.isExistTable("skashor_set")
					m.lcTableBezPrefix = "skashor_set" 
					m.lcTableBezPath   = this.sSqlPrefix+"."+m.lcTableBezPrefix 								
			ENDCASE
				
			m.lcSqlCommand = "SELECT * FROM "+m.lcTableBezPath + IIF(!EMPTY(m.lcOrder), " ORDER BY " + m.lcOrder, "") +  IIF(!EMPTY(m.lcSQLPodmienka), " WHERE " + m.lcSQLPodmienka, "")	
			this.viewmessage(101, m.lcSqlCommand )									
			m.nret = SQLEXEC(this.ndatasource, m.lcSqlCommand, "pom_"+m.lcDestTableName)
			IF this.nErrorNo = 0 
				IF nRet # -1
					TRY 
						IF lToTable     
							SELECT * FROM ("pom_"+m.lcDestTableName) INTO TABLE  (m.lcDestTable) &lcPodmienka
						ELSE
							SELECT * FROM ("pom_"+m.lcDestTableName) INTO CURSOR (m.lcDestTableName) READWRITE &lcPodmienka
						ENDIF
						USE IN ("pom_"+m.lcDestTableName)
					CATCH TO oErr
						this.nErrorNo   = oErr.ErrorNo		
						this.sErrorMssg = oErr.Message
						m.nRet = -1
					ENDTRY
					
					IF m.nRet <> -1
						SELECT(lcDestTableName)
						=this.changeColumns(m.lcTableBezPrefix)
						=this.vyplnStlpec(m.lcTableBezPrefix)
						*=this.doplnStlpce(m.lcTableBezPrefix)
						*** Index ma zmysel vytvarat len pre original tabulku nie pre pomocny cursor. Preto ma zmysel volat len ak mam
						*** naozaj otvorenu original tabulku a teda lcDestTable=nazov tabulky
						=this.zmazStlpec(m.lcTableBezPrefix)
						=this.vytvorIndex(m.lcTableBezPrefix)
						IF lToTable     														
							ERASE JUSTPATH(m.lcDestTable)+"\"+JUSTSTEM(m.lcDestTable)+".BAK"
						ENDIF
					ENDIF
				ELSE
					IF m.nret<0 &&Nastala nejaka chyba
						this.sqlError()
					ENDIF
				ENDIF
			ENDIF
		ELSE
			this.nErrorNo = 10002
			this.sErrorMssg = "Wrong function call DAJTABULKU. Empty table name."
		ENDIF					
		
		IF this.nErrorNo <> 0   &&nastala nejaka chyba
			nRet = -1
		ELSE
			nRet = 1
		ENDIF		
	
		RETURN nRet
	ENDPROC


	PROCEDURE vyplnStlpec
	PARAMETERS m.sTableName
	LOCAL m.lRet, cAlias

		m.cAlias = ALIAS()
		*** Uprava stlpcov pre jednotlive tabulky ***
		DO CASE 
			CASE ALLTRIM(UPPER(m.sTableName)) == "SKASKARTY"
				REPLACE ALL naz1 WITH "" FOR ISNULL(naz1)
				REPLACE ALL naz2 WITH "" FOR ISNULL(naz2)
				REPLACE ALL naz3 WITH "" FOR ISNULL(naz3)
				REPLACE ALL naz4 WITH "" FOR ISNULL(naz4)
				REPLACE ALL naz5 WITH "" FOR ISNULL(naz5)
				m.lRet = .T.
			CASE ALLTRIM(UPPER(m.sTableName)) == "KARTY"
				ALTER TABLE &cAlias ADD  COLUMN druh C(10)
				ALTER TABLE &cAlias ADD  COLUMN druh_skl C(10)
				ALTER TABLE &cAlias ADD  COLUMN druh_kalk C(10)						
				REPLACE ALL naz1 WITH "" FOR ISNULL(naz1)
				REPLACE ALL naz2 WITH "" FOR ISNULL(naz2)
				REPLACE ALL naz3 WITH "" FOR ISNULL(naz3)
				REPLACE ALL naz4 WITH "" FOR ISNULL(naz4)
				REPLACE ALL naz5 WITH "" FOR ISNULL(naz5)
				IF this.dajTabulku("skupiny", "POM_SKUP", .F.) == 1
					INDEX ON c_druh TAG c_druh
					SELECT &cAlias
					INDEX on c_Druh tag c_druh
					SET RELATION TO c_Druh INTO pom_skup
					REPLACE ALL druh WITH pom_skup.druh
					SET RELATION OFF INTO pom_skup
					INDEX on c_Druh tag c_druhs
					SET RELATION TO c_Druhs INTO pom_skup
					REPLACE ALL druh_skl WITH pom_skup.druh
					SET RELATION OFF INTO pom_skup
					INDEX on c_Druhk tag c_druhk
					SET RELATION TO c_Druhk INTO pom_skup
					REPLACE ALL druh_kalk WITH pom_skup.druh
					SET RELATION OFF INTO pom_skup
					USE IN POM_SKUP
					m.lRet = .T.
				ELSE
					m.lRet = .F.							
				ENDIF
			CASE ALLTRIM(UPPER(m.sTableName)) == "UMISTENI"
				ALTER TABLE &cAlias ADD  COLUMN druh_kalk C(10)						
				IF this.dajTabulku("skupiny", "POM_SKUP", .F.) == 1
					INDEX ON c_druh TAG c_druh
					SELECT &cAlias
					INDEX on c_Druhk tag c_druhk
					SET RELATION TO c_Druhk INTO pom_skup
					REPLACE ALL druh_kalk WITH pom_skup.druh
					SET RELATION OFF INTO pom_skup
					USE IN POM_SKUP
					m.lRet = .T.
				ELSE
					m.lRet = .F.							
				ENDIF
			CASE ALLTRIM(UPPER(m.sTableName)) == "UMDRUHY"
				ALTER TABLE &cAlias ADD  COLUMN druh C(10)						
				IF this.dajTabulku("skupiny", "POM_SKUP", .F.) == 1
					INDEX ON c_druh TAG c_druh
					SELECT &cAlias
					INDEX on c_Druhk tag c_druhk
					SET RELATION TO c_Druhk INTO pom_skup
					REPLACE ALL druh WITH pom_skup.druh
					SET RELATION OFF INTO pom_skup
					USE IN POM_SKUP
					m.lRet = .T.
				ELSE
					m.lRet = .F.							
				ENDIF
			CASE ALLTRIM(UPPER(m.sTableName)) == "POLOZKY"
				ALTER TABLE &cAlias ADD  COLUMN pocet N(8,4)					
				IF this.dajTabulku("karty", "POM_KARTY", .F.) == 1
					INDEX ON c_karty TAG c_karty
					SELECT &cAlias
					INDEX on c_karty tag c_karty
					SET RELATION TO c_karty INTO pom_karty
					REPLACE ALL pocet WITH pom_karty.pocet
					SET RELATION OFF INTO pom_karty
					USE IN pom_karty
					m.lRet = .T.
				ELSE
					m.lRet = .F.							
				ENDIF
			CASE ALLTRIM(UPPER(m.sTableName)) == "KLAPKY"   &&bohuzial pokladn pocita zo stlpcom Nazev. Kvoli ftp prenosom to musim urobit takto
				ALTER TABLE &cAlias ADD  COLUMN nazev C(30)					
				IF this.dajTabulku("karty", "POM_KARTY", .F.) == 1
					INDEX ON c_karty TAG c_karty
					SELECT &cAlias
					INDEX on c_karty tag c_karty
					SET RELATION TO c_karty INTO pom_karty
					REPLACE ALL nazev WITH pom_karty.nazev
					SET RELATION OFF INTO pom_karty
					USE IN pom_karty
					m.lRet = .T.
				ELSE
					m.lRet = .F.							
				ENDIF
				*** L.L. (07.06.2017) Pre page nastavit nazev
				REPLACE ALL nazev WITH caption FOR ALLTRIM(LOWER(typ))=="page"
			*** L.L. (29.05.2017) Doplnene klapky don, ktore su v samostatnej tabulke
			CASE UPPER(sTableName) == "KL_DON"
				ALTER TABLE &cAlias ADD  COLUMN nazev C(30)					
				IF this.dajTabulku("karty", "POM_KARTY", .F.) == 1
					INDEX ON c_karty TAG c_karty
					SELECT &cAlias
					INDEX on c_karty tag c_karty
					SET RELATION TO c_karty INTO pom_karty
					REPLACE ALL nazev WITH pom_karty.nazev
					SET RELATION OFF INTO pom_karty
					USE IN pom_karty
					m.lRet = .T.
				ELSE
					m.lRet = .F.							
				ENDIF
				REPLACE ALL nazev WITH caption FOR ALLTRIM(LOWER(typ))=="page"
			CASE INLIST(ALLTRIM(UPPER(m.sTableName)), "HRAS_SET", "HOR_SET", "PRAS_SET", "FRAS_SET", "GRAS_SET", "FRONTSET", "HOT_SET", "ARAS_SET", "FID_SET")
				ALTER TABLE &cAlias ADD  COLUMN druh C(10)						
				IF this.dajTabulku("skupiny", "POM_SKUP", .F.) == 1
					INDEX ON c_druh TAG c_druh
					SELECT &cAlias
					INDEX on c_Druh tag c_druh
					SET RELATION TO c_Druh INTO pom_skup
					REPLACE ALL druh WITH pom_skup.druh
					SET RELATION OFF INTO pom_skup
					USE IN POM_SKUP
					SELECT &cAlias
					REPLACE druh WITH "TIP" FOR c_druh=-100
					m.lRet = .T.
				ELSE
					m.lRet = .F.							
				ENDIF
			*** L.L. (05.11.2017) Prepojenie na Externy kreditny system ALTOPOSTGRES
			CASE ALLTRIM(UPPER(m.sTableName)) == "HOT_KRED"   
				ALTER TABLE &cAlias ADD  COLUMN datum C(10)
				ALTER TABLE &cAlias ADD  COLUMN cas C(8)
				ALTER TABLE &cAlias ADD  COLUMN jmeno C(30)						
				ALTER TABLE &cAlias ADD  COLUMN adresa C(40)						
				ALTER TABLE &cAlias ADD  COLUMN adr_Zam C(40)						
				ALTER TABLE &cAlias ADD  COLUMN limit N(11,2)						
				SELECT &cAlias
				REPLACE ALL st_datum WITH CTOD("") FOR ISNULL(st_Datum)
				REPLACE ALL last_dat_o WITH CTOD("") FOR ISNULL(last_dat_o)								
				REPLACE ALL exp_Date WITH SUBSTR(exp_Date,7,4)+"/"+SUBSTR(exp_Date,4,2)+"/"+LEFT(exp_Date,2) FOR !EMPTY(exp_Date)
				REPLACE exp_date WITH "" FOR ISNULL(exp_date) OR EMPTY(exp_Date)
				REPLACE ALL datum WITH DTOS(TTOD(datcas))				
				REPLACE ALL cas WITH TTOC(datcas,2)								
				REPLACE ALL jmeno WITH ALLTRIM(ALLTRIM(priezvisko)+" "+ALLTRIM(meno))
				REPLACE ALL adresa WITH ALLTRIM(ulica)+IIF(!EMPTY(psc),", "+ALLTRIM(psc),"")+IIF(!EMPTY(mesto),", "+ALLTRIM(mesto),"")				
				REPLACE ALL adr_zam WITH ALLTRIM(ulica_zam)+IIF(!EMPTY(psc_zam),", "+ALLTRIM(psc_zam),"")+IIF(!EMPTY(mesto_Zam),", "+ALLTRIM(mesto_zam),"")				
				REPLACE ALL limit WITH limit_kred
			*** L.L. (05.11.2017) Prepojenie na Externy kreditny system ALTOPOSTGRES
			CASE ALLTRIM(UPPER(m.sTableName)) == "HK_OSKAR"   
				SELECT &cAlias								
				REPLACE ALL dat_naroz WITH CTOD("") FOR ISNULL(dat_naroz)
				REPLACE ALL exp_Date WITH SUBSTR(exp_Date,7,4)+"/"+SUBSTR(exp_Date,4,2)+"/"+LEFT(exp_Date,2)  FOR !EMPTY(exp_Date)
				REPLACE exp_date WITH "" FOR ISNULL(exp_date) OR EMPTY(exp_Date)
				REPLACE datumdo WITH CTOT("") FOR ISNULL(datumdo)
				REPLACE ALL meno WITH ALLTRIM(ALLTRIM(priezvisko)+" "+ALLTRIM(meno))
			CASE ALLTRIM(UPPER(m.sTableName)) == "HKREDOBR" OR ALLTRIM(UPPER(m.sTableName)) == "SKASHKOBR"
				SELECT &cAlias								
				ALTER TABLE &cAlias ADD  COLUMN datum C(10)
				ALTER TABLE &cAlias ADD  COLUMN cas C(8)
				REPLACE ALL datum WITH DTOS(TTOD(datcas))				
				REPLACE ALL cas WITH TTOC(datcas,2)								
				REPLACE ALL rupdate WITH CTOT("") FOR ISNULL(rupdate)
				REPLACE ALL zmena WITH CTOT("") FOR ISNULL(zmena)
			CASE UPPER(m.sTableName) == "HK_TYPK" OR UPPER(m.sTableName) == "SKASHKTYPK"     			
				*REPLACE ALL id WITH 0 FOR ISNULL(id)
				REPLACE ALL povol_oper WITH "" FOR ISNULL(povol_oper)
			CASE UPPER(m.sTableName) == "HK_PTYPK" OR UPPER(m.sTableName) == "SKASHKPTYPK"     
				*REPLACE ALL id WITH 0 FOR ISNULL(id)
				REPLACE ALL zakaz_oper WITH "" FOR ISNULL(zakaz_oper)
				REPLACE ALL druhy_pl WITH "" FOR ISNULL(druhy_pl)
				REPLACE ALL zakaz_drpl WITH "" FOR ISNULL(zakaz_drpl)
				REPLACE ALL spart WITH "ALL" FOR ISNULL(spart)				
			*** L.L. (05.11.2017) Koniec: Prepojenie na Externy kreditny system ALTOPOSTGRES					
			OTHERWISE
				m.lRet = .T.
		ENDCASE 
	RETURN m.lRet



	PROCEDURE doplnStlpec
	PARAMETERS sNovyStlpec, sTyp, sPovodnyStlpec
	LOCAL cAlias

		m.cAlias = ALIAS()
		ALTER TABLE &cAlias ADD COLUMN &sNovyStlpec &sTyp
		IF !EMPTY(sPovodnyStlpec)
			REPLACE ALL &sNovyStlpec WITH &sPovodnyStlpec 
		ENDIF
	ENDPROC 

	*** Podla tabulky zavola funkciu, ktora meni typ stlpaca z N(1) na L 
	*** V postgrese pouzivame totiz SMALLINT namiesto L
	PROCEDURE changeColumns
	PARAMETERS m.sTableName
		
			DO CASE 
				CASE UPPER(m.sTableName) == "SK_FIN"
					=this.alterColumnSIToL("POHLADAVKA")
				CASE UPPER(m.sTableName) == "SK_POL"
					=this.alterColumnSIToL("KOMPEZ")
				CASE UPPER(m.sTableName) == "SK_PLT"
					=this.alterColumnSIToL("KOMPEZ")
				CASE UPPER(m.sTableName) == "SK_FIN"
					=this.alterColumnSIToL("POHLADAVKA")
				CASE UPPER(m.sTableName) == "KARTY" OR 	UPPER(m.sTableName) == "SKASKARTY"		
					=this.alterColumnSIToL("LABEL")
				CASE UPPER(m.sTableName) == "SK_SEZ"
					=this.alterColumnCToI("C_MENA")
				CASE UPPER(m.sTableName) == "SKUPINY"
					=this.alterColumnSIToL("DOTACIA")
					=this.alterColumnSIToL("SUMAR")
				CASE UPPER(m.sTableName) == "DRUHY_PL"
					=this.alterColumnSIToL("TEXT")
					=this.alterColumnSIToL("DO_RECEPCE")
					=this.alterColumnSIToL("DO_LIMITU")
					=this.alterColumnSIToL("ORDER_EN")
					=this.alterColumnSIToL("NEMEN_CENU")
					=this.alterColumnSIToL("PRPLMENCEN")
					=this.alterColumnSIToL("NFPL")
				CASE UPPER(m.sTableName) == "FOOD_DAT"    
					=this.alterColumnSIToL("KONT_DBF")
				CASE UPPER(m.sTableName) == "FOOD_SET"    
					=this.alterColumnSIToL("SUPERVISOR")
				CASE UPPER(m.sTableName) == "HCRC_SET"    
					=this.alterColumnSIToL("PO_UCTOCH")
					=this.alterColumnSIToL("PO_TIMATR")
				CASE UPPER(m.sTableName) == "KLAPKY" OR UPPER(m.sTableName) == "SKASKLAPKY"
					=this.alterColumnSIToL("UMISTENO")
					=this.alterColumnCToI("STRANA")
					=this.alterColumnCToI("POZICE")
					=this.alterColumnCToI("VAZBA")
					=this.alterColumnCToI("COLO")
				*** L.L. (29.05.2017) Doplnene klapky DON
				CASE UPPER(m.sTableName) == "KL_DON" OR UPPER(m.sTableName) == "SDONKLAPKY"    
					=this.alterColumnCToI("STRANA")
					=this.alterColumnCToI("POZICE")
					=this.alterColumnCToI("VAZBA")
				CASE UPPER(m.sTableName) == "PRN_DEF"    
					=this.alterColumnSIToL("IS_NETPRN")
					=this.alterColumnSIToL("P_HEADER")
				CASE UPPER(m.sTableName) == "UMISTENI" OR UPPER(m.sTableName) == "SKASUMISTENI"       
					=this.alterColumnSIToL("OBLASTA")
					=this.alterColumnSIToL("OBLASTB")
					=this.alterColumnSIToL("OBLASTC")
					=this.alterColumnCToI("C_KARTY")
					=this.alterColumnCToI("C_DRUHK")
				CASE UPPER(m.sTableName) == "POLOZKY" OR UPPER(m.sTableName) == "SKASPOLOZKY"               
					=this.alterColumnCToI("C_KARTY")
					=this.alterColumnCToI("C_POLOZ")
				CASE UPPER(m.sTableName) == "UMDRUHY" OR UPPER(m.sTableName) == "SKASUMDRUHY"           
					=this.alterColumnCToI("C_DRUHK")
				CASE UPPER(m.sTableName) == "EANY"    
					=this.alterColumnCToI("C_KARTY")
				CASE UPPER(m.sTableName) == "USERS"    
					=this.alterColumnSIToL("IS_ADMIN")
				CASE UPPER(m.sTableName) == "GR___"    
					=this.alterColumnSIToL("G0")
					=this.alterColumnSIToL("G1")
					=this.alterColumnSIToL("G2")
					=this.alterColumnSIToL("G3")
					=this.alterColumnSIToL("G4")
					=this.alterColumnSIToL("G5")
					=this.alterColumnSIToL("G6")
					=this.alterColumnSIToL("G7")
					=this.alterColumnSIToL("G8")
					=this.alterColumnSIToL("G9")
				CASE UPPER(m.sTableName) == "UG___"    
					=this.alterColumnSIToL("G0")
					=this.alterColumnSIToL("G1")
					=this.alterColumnSIToL("G2")
					=this.alterColumnSIToL("G3")
					=this.alterColumnSIToL("G4")
					=this.alterColumnSIToL("G5")
					=this.alterColumnSIToL("G6")
					=this.alterColumnSIToL("G7")
					=this.alterColumnSIToL("G8")
					=this.alterColumnSIToL("G9")
				CASE UPPER(m.sTableName) == "HH_HLAV"
					=this.alterColumnSIToL("AKTIVNA")
					=this.alterColumnSIToL("ZMAZANA")
					=this.alterColumnSIToL("DEN1")
					=this.alterColumnSIToL("DEN2")
					=this.alterColumnSIToL("DEN3")
					=this.alterColumnSIToL("DEN4")
					=this.alterColumnSIToL("DEN5")
					=this.alterColumnSIToL("DEN6")
					=this.alterColumnSIToL("DEN7")
					=this.alterColumnSIToL("MES01")
					=this.alterColumnSIToL("MES02")
					=this.alterColumnSIToL("MES03")
					=this.alterColumnSIToL("MES04")
					=this.alterColumnSIToL("MES05")
					=this.alterColumnSIToL("MES06")
					=this.alterColumnSIToL("MES07")
					=this.alterColumnSIToL("MES08")
					=this.alterColumnSIToL("MES09")
					=this.alterColumnSIToL("MES10")
					=this.alterColumnSIToL("MES11")
					=this.alterColumnSIToL("MES12")
					=this.alterColumnSIToL("VS_DRUHY")
				CASE UPPER(m.sTableName) == "HH_KALKY"    
					=this.alterColumnCToI("ID_HH_HLAV")				
				CASE UPPER(m.sTableName) == "HH_DRUHY"    
					=this.alterColumnCToI("ID_HH_HLAV")
				CASE UPPER(m.sTableName) == "H10CRC_SET"    
					=this.alterColumnSIToL("PO_UCTOCH")
				*** L.L. (05.11.2017) Prepojenie na Externy kreditny system ALTOPOSTGRES					
				CASE UPPER(m.sTableName) == "HOT_KRED"    
					=this.alterColumnSIToL("storno")
					=this.alterColumnSIToL("smazana")
					=this.alterColumnDToC("exp_date")
					=this.alterColumnSIToL("label")
				CASE UPPER(m.sTableName) == "HK_OSKAR"    
					=this.alterColumnSIToL("storno")
					=this.alterColumnSIToL("blok")
					=this.alterColumnDToC("exp_date")
					=this.alterColumnSIToL("platneako")					
				CASE UPPER(m.sTableName) == "ORGSTR"    
					=this.alterColumnSIToL("label")
					=this.alterColumnSIToL("blokovana")
					=this.alterColumnSIToL("pr_zostato")
					=this.alterColumnSIToL("pr_pohyb")
					=this.alterColumnSIToL("pr_sp_dnes")
					=this.alterColumnSIToL("pr_sp_mes")
					=this.alterColumnSIToL("pr_zu_dnes")
					=this.alterColumnSIToL("pr_zos_jed")
					=this.alterColumnSIToL("pr_sp_jed")
				CASE UPPER(m.sTableName) == "HK_TYPK" OR UPPER(m.sTableName) == "SKASHKTYPK"               
					=this.alterColumnSIToL("zmazane")
					=this.alterColumnSIToL("hk_ptypk")
				CASE UPPER(m.sTableName) == "HK_PTYPK" OR UPPER(m.sTableName) == "SKASHKPTYPK"                   
					=this.alterColumnSIToL("sucast_uct")
					=this.alterColumnSIToL("sled_poc_j")
				CASE UPPER(m.sTableName) == "HK_SLEVY" 
					=this.alterColumnSIToL("povolene")
				*** L.L. (05.11.2017) Koniec: Prepojenie na Externy kreditny system ALTOPOSTGRES					
			ENDCASE 
	ENDPROC 


	PROCEDURE alterColumnSIToL
	PARAMETERS lColumnName
	LOCAL cAlias, pocetpokusov
		m.pocetpokusov=0
		m.cAlias = ALIAS()
		IF FSIZE(lColumnName) <> 0 &&ak stlpec existuje
			DO WHILE FSIZE("pomcul_l") = 0
				ALTER TABLE &cAlias ADD COLUMN pomcul_l L	
				m.pocetpokusov=m.pocetpokusov+1
				IF m.pocetpokusov>10
					EXIT 
				ENDIF 
			ENDDO  
			IF FSIZE("pomcul_l")>0
				REPLACE ALL pomcul_l WITH .T. FOR &lColumnName == 1
				ALTER TABLE &cAlias ALTER COLUMN &lColumnName L
				REPLACE ALL &lColumnName WITH pomcul_l
				ALTER TABLE &cAlias DROP COLUMN pomcul_l
			ELSE
				=MESSAGEBOX("Nepodarilo sa upraviť štruktúru tabuľky "+m.cAlias+", stĺpec "+lColumnName,64,"FoodMan")
			ENDIF 
		ENDIF
	ENDPROC 
		
	PROCEDURE alterColumnCToI
	PARAMETERS lColumnName
	LOCAL cAlias, pocetpokusov
		m.pocetpokusov=0
		IF FSIZE(lColumnName) <> 0 &&ak stlpec existuje
			IF TYPE(lColumnName) == "C"  &&Ak ide o charakter, tak az potom menim
				m.cAlias = ALIAS()
				DO WHILE FSIZE("pomcul_i") = 0
					ALTER TABLE &cAlias ADD COLUMN pomcul_i i	
					m.pocetpokusov=m.pocetpokusov+1
					IF m.pocetpokusov>10
						EXIT 
					ENDIF 
				ENDDO  
				IF FSIZE("pomcul_i")>0
					REPLACE ALL pomcul_i WITH VAL(&lColumnName)
					ALTER TABLE &cAlias ALTER COLUMN &lColumnName I
					REPLACE ALL &lColumnName WITH pomcul_i
					ALTER TABLE &cAlias DROP COLUMN pomcul_i
				ELSE
					=MESSAGEBOX("Nepodarilo sa upraviť štruktúru tabuľky "+m.cAlias+", stĺpec "+lColumnName,64,"FoodMan")
				ENDIF 
			ENDIF
		ENDIF
	ENDPROC 

	PROCEDURE alterColumnDToC
	PARAMETERS lColumnName
	LOCAL cAlias, pocetpokusov
		m.pocetpokusov=0
		IF FSIZE(lColumnName) <> 0 &&ak stlpec existuje
			IF TYPE(lColumnName) == "D"  &&Menim len v pripade, ze je povodny typ naozaj datum
				m.cAlias = ALIAS()
				DO WHILE FSIZE("pomcul_c") = 0
					ALTER TABLE &cAlias ADD COLUMN pomcul_c c(10)	
					m.pocetpokusov=m.pocetpokusov+1
					IF m.pocetpokusov>10
						EXIT 
					ENDIF 
				ENDDO  
				IF FSIZE("pomcul_c")>0
					REPLACE ALL pomcul_c WITH DTOC(&lColumnName) FOR !ISNULL(&lColumnName)
					ALTER TABLE &cAlias ALTER COLUMN &lColumnName C(10)
					REPLACE ALL &lColumnName WITH pomcul_c
					ALTER TABLE &cAlias DROP COLUMN pomcul_c
				ELSE
					=MESSAGEBOX("Nepodarilo sa upraviť štruktúru tabuľky "+m.cAlias+", stĺpec "+lColumnName,64,"FoodMan")
				ENDIF 
			ENDIF
		ENDIF
	ENDPROC 
	

	PROCEDURE alterColumnSMToL
	PARAMETERS lColumnName
	LOCAL cAlias, pocetpokusov
		m.pocetpokusov=0

		IF FSIZE(lColumnName) <> 0 &&ak stlpec existuje
			IF TYPE(lColumnName) == "N"  &&Ak ide o charakter, tak az potom menim
				m.cAlias = ALIAS()
				DO WHILE FSIZE("pomcul_l") = 0
					ALTER TABLE &cAlias ADD COLUMN pomcul_l L	
					m.pocetpokusov=m.pocetpokusov+1
					IF m.pocetpokusov>10
						EXIT 
					ENDIF 
				ENDDO  
				IF FSIZE("pomcul_l")>0
					REPLACE ALL pomcul_l WITH .T. FOR &lColumnName == 1
					ALTER TABLE &cAlias ALTER COLUMN &lColumnName L
					REPLACE ALL &lColumnName WITH pomcul_l
					ALTER TABLE &cAlias DROP COLUMN pomcul_l
				ELSE
					=MESSAGEBOX("Nepodarilo sa upraviť štruktúru tabuľky "+m.cAlias+", stĺpec "+lColumnName,64,"FoodMan")
				ENDIF 
			ENDIF
		ENDIF
	ENDPROC 

	PROCEDURE zmazStlpec	
	PARAMETERS sTableName
	LOCAL cAlias	
		m.cAlias = ALIAS()
		DO CASE 
			CASE UPPER(sTableName) == "DEFSUBOR"	
				IF FSIZE("modul") <> 0	
					ALTER TABLE &cAlias DROP COLUMN modul
				ENDIF
				IF FSIZE("setup_skup") <> 0	
					ALTER TABLE &cAlias DROP COLUMN setup_skup
				ENDIF
			CASE UPPER(sTableName) == "GR___"
				IF FSIZE("modul") <> 0	
					ALTER TABLE &cAlias DROP COLUMN modul
				ENDIF
			CASE UPPER(sTableName) == "UG___"
				IF FSIZE("modul") <> 0	
					ALTER TABLE &cAlias DROP COLUMN modul
				ENDIF
		ENDCASE
	ENDPROC
				
					
	PROCEDURE vytvorIndex
	PARAMETERS sTableName
		DO CASE 
			CASE UPPER(sTableName) == "KARTY"  OR 	UPPER(m.sTableName) == "SKASKARTY"	
				INDEX on kod TAG kod
				INDEX on c_karty TAG c_karty
				INDEX on druh_kalk TAG druh_kalk 
				INDEX on druh TAG druh
				INDEX on druh_skl TAG druh_skl
				INDEX on c_druhk TAG c_druhk
				INDEX on c_druh TAG c_druh
				INDEX on c_druhs TAG c_druhs
				INDEX on nazev TAG nazev
				INDEX on nazev1 TAG nazev1			
			CASE UPPER(sTableName) == "KARTY"  OR 	UPPER(m.sTableName) == "SKASKARTY"	
				INDEX on c_karty TAG c_karty
			CASE UPPER(sTableName) == "KCENY"  OR 	UPPER(m.sTableName) == "SKASKCENY"	
				INDEX on c_karty TAG c_karty
			CASE UPPER(sTableName) == "UMISTENI" OR UPPER(sTableName) == "SKASUMISTENI"
				INDEX on c_karty TAG c_karty
				INDEX on nazev TAG nazev
				INDEX on id_kas TAG id_kas
				INDEX on druh_kalk tag druh_kalk
			CASE UPPER(sTableName) == "SKUPINY"
				INDEX on SPART TAG SPART
				INDEX on DRUH TAG DRUH 
				INDEX on TYP_SKUP TAG TYP_SKUP 
				INDEX on TYP_KALK TAG TYP_KALK 
				INDEX on TYP_SKL TAG TYP_SKL 
				INDEX on C_DRUH TAG C_DRUH 
			CASE UPPER(sTableName) == "POLOZKY" OR UPPER(sTableName) == "SKASPOLOZKY"           
				INDEX ON c_poloz TAG c_poloz
				INDEX on c_karty TAG c_karty				
			CASE UPPER(sTableName) == "UMDRUHY" OR UPPER(sTableName) == "SKASUMDRUHY"
				INDEX on ID_KAS TAG ID_KAS 	
				INDEX on DRUH TAG DRUH 
			CASE UPPER(sTableName) == "KLAPKY" OR UPPER(sTableName) == "SKASKLAPKY"
				INDEX on VAZBA TAG VAZBA 
				INDEX on NAZEV TAG NAZEV 
				INDEX on pozice TAG Page FOR typ="page"
				INDEX on TRANSFORM(strana,"@L 999")+TRANSFORM(pozice,"@L 99") TAG STRPOZ		
			*** L.L. (29.05.2017) Doplnene klapky don, ktore su v samostatnej tabulke
			CASE UPPER(sTableName) == "KL_DON" OR  UPPER(sTableName) == "SDONKLAPKY"
				INDEX on NAZEV TAG NAZEV 
				INDEX on TRANSFORM(strana,"@L 9999")+TRANSFORM(pozice,"@L 999") TAG STRPOZ		
			CASE UPPER(sTableName) == "EANY"
				INDEX on C_KARTY TAG C_KARTY 
				INDEX on ALLTRIM(ean) TAG EAN
			CASE UPPER(sTableName) == "USERS"
				INDEX on USER_ID TAG USER_ID 
			CASE UPPER(sTableName) == "GR___"
				INDEX on KLIC TAG KLIC 
			CASE UPPER(sTableName) == "UG___"
				*** Nema ziaden index ***			
			CASE UPPER(sTableName) == "KURSY"
				INDEX on STAT TAG STAT 
				INDEX on MENA TAG MENA 
			CASE UPPER(sTableName) == "FOOD_DAT"
				INDEX on ID TAG ID
			CASE UPPER(sTableName) == "ZPRAVY"
				INDEX ON zprava TAG zprava
			CASE UPPER(sTableName) == "STOL_CHL"
				INDEX ON stol TAG stol 
			CASE INLIST(ALLTRIM(UPPER(sTableName)), "HRAS_SET", "HOR_SET", "PRAS_SET", "FRAS_SET", "GRAS_SET", "FRONTSET", "HOT_SET", "ARAS_SET", "FID_SET")
				INDEX ON druh TAG druh
			CASE INLIST(ALLTRIM(UPPER(sTableName)), "SKASHRAS_SET", "SKASHOR_SET", "SKASPRAS_SET", "SKASFRAS_SET", "SKASGRAS_SET", "SKASFRONTSET", "SKASHOT_SET", "SKASARAS_SET", "SKASFID_SET")
				INDEX ON druh TAG druh
			CASE UPPER(sTableName) == "_TMSKFIN" .OR. UPPER(sTableName) == "STMSKFIN" 
				INDEX ON u_cis TAG u_cis
				INDEX ON id_kas TAG id_kas
				INDEX ON AUTOR + uzcis TAG uzcis
			CASE UPPER(sTableName) == "_TMSKFIS" .OR. UPPER(sTableName) == "STMSKFIS" 
				INDEX ON u_cis TAG u_cis
				INDEX ON id_kas TAG id_kas
			CASE UPPER(sTableName) == "_TMSKVKLVY" .OR. UPPER(sTableName) == "STMSKVKLVY" 
				INDEX ON u_cis TAG u_cis
				INDEX ON id_kas TAG id_kas
			CASE UPPER(sTableName) == "_TMSKPLT" .OR. UPPER(sTableName) == "STMSKPLT"
				INDEX ON u_cis TAG u_cis
				INDEX ON uzaverka TAG uzaverka
			CASE UPPER(sTableName) == "_TMSKTXT" .OR. UPPER(sTableName) == "STMSKTXT"
				INDEX ON u_cis + TRAN(poradi,'@L 99') TAG u_cispor
				INDEX ON u_cis TAG u_cis 
			CASE UPPER(sTableName) == "_TMSKPOL" .OR. UPPER(sTableName) == "STMSKPOL"
				INDEX ON u_cis TAG u_cis
				INDEX ON uzaverka TAG uzaverka
				INDEX ON nazev TAG nazev
				INDEX ON id_man TAG id_man
				INDEX ON id_kas TAG id_kas
			CASE UPPER(sTableName) == "C_SKPOL"  &&ked vyriesim inak _block, cize semafory, tak to mozem vyhodit
				INDEX ON u_cis TAG u_cis
			CASE UPPER(sTableName) == "HH_DRUHY"  
				INDEX ON id_hh_hlav TAG id_hlav
			CASE UPPER(sTableName) == "HH_KALKY"  
				INDEX ON id_hh_hlav TAG id_hlav
			*** L.L. (04.11.2016) Index pre h10_set.dbf a h10crc_set.dbf
			CASE UPPER(sTableName) == "H10_SET"  
				INDEX ON c_druh TAG c_druh
			CASE UPPER(sTableName) == "H10CRC_SET"  
				INDEX ON druh_pl TAG druh_pl			
			CASE UPPER(sTableName) == "HCRC_SET"  
				INDEX ON druh_pl TAG druh_pl			
			*** L.L. (05.11.2017) Prepojenie na Externy kreditny system ALTOPOSTGRES					
			CASE UPPER(m.sTableName) == "HOT_KRED"    
				INDEX ON jmeno TAG jmeno
				INDEX ON datum TAG datum
				INDEX ON id_ucet TAG id_ucet
				INDEX on typ TAG typ 
			CASE UPPER(m.sTableName) == "HK_OSKAR"    
				INDEX on cis_kred TAG cis_kred
				INDEX on id_ucet TAG id_ucet
				INDEX on id_karty TAG id_karty 
			CASE UPPER(m.sTableName) == "ORGSTR"
				INDEX on cislo_org TAG cislo_org
				INDEX ON STR(cislo_org,10,0)+ALLTRIM(dic_org) TAG org
				INDEX on dic_org TAG dic_org				
			CASE UPPER(m.sTableName) == "HK_TYPK" OR UPPER(m.sTableName) == "SKASHKTYPK"                   
				INDEX ON typ TAG typ
			CASE UPPER(m.sTableName) == "HK_PTYPK"  OR UPPER(m.sTableName) == "SKASHKPTYPK"                       
				INDEX ON poradie TAG poradie
			*** L.L. (05.11.2017) Koniec: Prepojenie na Externy kreditny system ALTOPOSTGRES					
			OTHERWISE
		ENDCASE			
	ENDPROC 



	PROCEDURE myAppendFromMemvar
	LPARAMETERS m.cTo
	LOCAL nRet, m.oldSel, m.sqlCmd, m.lnPocetStlpcov, m.sqlCmdColumns, m.sqlCmdValues, nIndex, lPocetCasti, cCastSqlCmd
	
		DODEFAULT(m.cTo)

		*SUSPEND 
				
		m.cTo = this.vyhodTag(m.cTo, "PATH")
		m.cTo = JUSTFNAME(m.cTo) 
		m.cTo = IIF(AT("<PREFIX>", m.cTo) # 0, this.setPrefix(m.cTo), this.sSqlPrefix+"."+m.cTo)					
		
		m.oldSel = SELECT()

		m.iii = SQLCOLUMNS(this.ndatasource, m.cTo, "FOXPRO", "flist")		
		IF m.iii > 0
			m.iii = SQLCOLUMNS(this.ndatasource, m.cTo, "NATIVE", "flistnat")		
			COPY all to (m.pri_path+"flistnat")
			USE (m.pri_path+"flistnat") EXCLUSIVE 
			SELECT flist
			ALTER table flist ADD COLUMN prenasat L
			ALTER table flist ADD COLUMN ftype_nati C(1)
			ALTER table flist ADD COLUMN autoinc L				
			INDEX on field_name tag fname 
			SELECT flistnat
			REPLACE ALL flistnat.column_nam WITH UPPER(flistnat.column_nam)
			ALTER table flistnat ALTER COLUMN column_nam C(99)
			INDEX on column_nam tag fname
			SELECT flist
			SET RELATION TO field_name INTO flistnat				
			REPLACE ALL flist.autoinc WITH IIF(flistnat.auto_incre == 1, .T., .F.)
			REPLACE ALL ftype_nati WITH field_type		&& je to dobre???
			SET RELATION OFF INTO flistnat
			
			REPLACE ALL prenasat WITH .T. FOR TYPE("m."+ALLTRIM(flist.field_name)) # "U" .and. !autoinc
			SELECT flist			
			COUNT FOR prenasat TO m.lnPocetStlpcov
			
			IF m.lnPocetStlpcov > 0
				*** Musim to rozdelit na dve casti, pretoze potom vznikal dlhy retazec, na ktory neslo urobot &
				m.sqlCmdColumns = this.tvorSqlCommand(m.cTo, .F., .T.)
				m.sqlCmdValues = this.tvorSqlCommand(m.cTo, .T., .T.)

				m.sqlCmd = sqlCmdColumns+ " "
				m.lPocetCasti = OCCURS("<STOP>", m.sqlCmdValues)

				IF m.lPocetCasti = 0
					m.sqlCmd = m.sqlCmd+" "+&sqlCmdValues
				ELSE
					FOR nIndex = 0 TO m.lPocetCasti-1
						IF m.nIndex = 0
							m.cCastSqlCmd = STREXTRACT(sqlCmdValues, "", "<STOP>", nIndex)								
						ELSE
							m.cCastSqlCmd = STREXTRACT(sqlCmdValues, "<STOP>", "<STOP>", nIndex)
						ENDIF
						m.sqlCmd = m.sqlCmd + " "+&cCastSqlCmd 
					ENDFOR
					m.cCastSqlCmd = STREXTRACT(sqlCmdValues, "<STOP>", "", m.lPocetCasti)	
					m.sqlCmd = m.sqlCmd + " "+&cCastSqlCmd 
				ENDIF
								
				this.viewmessage(101, m.sqlCmd)									
				
				*SUSPEND 
				
				m.nret = SQLEXEC(this.ndatasource,m.sqlCmd )
				IF m.nret<0 &&Nastala nejaka chyba
					this.sqlError()
				ENDIF					

			ENDIF

			IF USED("flist")
				USE IN flist
			ENDIF
			IF USED("flistnat")
				USE IN flistnat
			ENDIF
			ERASE(m.pri_path+"flistnat.dbf")
			ERASE(m.pri_path+"flistnat.cdx")			
			SELECT (m.oldSel)
			IF USED("struct")
				USE IN struct
			ENDIF
			ERASE(m.pri_path+"struct.dbf")
			ERASE(m.pri_path+"struct.cdx")

			IF this.nErrorNo <> 0   &&nastala nejaka chyba
				nRet = -1
			ELSE
				nRet = 1
			ENDIF
		ELSE
			nRet = -1
		ENDIF 		
	
		RETURN nRet
	ENDPROC			


	PROCEDURE myAppendFromNative
	LPARAMETERS m.lcFrom, m.cTo, m.cCondition
	LOCAL nRet, m.lcAlias, m.oldSel, m.sqlCmd, m.lnPocetStlpcov, m.sqlCmdColumns, m.sqlCmdValues, nIndex, lPocetCasti, cCastSqlCmd, lCloseFrom, lcFromBezPath, nCelkovyPocetZaznamov, nAktualnyZaznam
	
		IF PARAMETERS()<3
			m.cCondition = " ALL"
		ELSE
			IF EMPTY(m.cCondition)
				m.cCondition = " ALL"
			ELSE
				m.cCondition = " FOR "+m.cCondition 
			ENDIF
		ENDIF	

		DODEFAULT(m.lcFrom, m.cTo, m.cCondition)

		m.lcAlias = ""
				
		*** Pre Nativne tabulky musim vyhadzat <PATH> </PATH> a vsetko co je medzi nimi
		m.cTo = this.vyhodTag(m.cTo, "PATH")
		m.cTo = JUSTFNAME(m.cTo) 
		m.cTo = IIF(AT("<PREFIX>", m.cTo) # 0, this.setPrefix(m.cTo), this.sSqlPrefix+"."+m.cTo)			
		
		m.lcFromBezPath = this.vyhodTag(m.lcFrom, "PATH")
		m.lcFromBezPath = JUSTFNAME(m.lcFromBezPath)
		
		m.oldSel = SELECT()

		TRY	
			IF ALLTRIM(m.lcFromBezPath) == ALLTRIM(m.lcFrom) && je to len alias bez PAth, takze moze byt otvoreny
				IF USED(m.lcFromBezPath)
					m.lCloseFrom = .F.
					SELECT(m.lcFromBezPath) 
				ELSE
					m.lCloseFrom = .T.
					SELECT 0		
					USE(m.lcFrom)				
				ENDIF
			ELSE
				m.lCloseFrom = .T.
				SELECT 0		
				USE(m.lcFrom)							
			ENDIF
			m.lcAlias = ALIAS()
		
			SQLCOLUMNS(this.ndatasource, m.cTo, "FOXPRO", "flist")		
			SQLCOLUMNS(this.ndatasource, m.cTo, "NATIVE", "flistnat")		
			COPY all to (m.pri_path+"flistnat")
			USE (m.pri_path+"flistnat") EXCLUSIVE 
			SELECT flist
			ALTER table flist ADD COLUMN prenasat L
			ALTER table flist ADD COLUMN ftype_nati C(1)
			ALTER table flist ADD COLUMN autoinc L				
			INDEX on field_name tag fname 
			SELECT flistnat
			REPLACE ALL flistnat.column_nam WITH UPPER(flistnat.column_nam)			
			ALTER table flistnat ALTER COLUMN column_nam C(99)
			INDEX on column_nam tag fname
			SELECT flist
			SET RELATION TO field_name INTO flistnat				
			REPLACE ALL flist.autoinc WITH IIF(flistnat.auto_incre == 1, .T., .F.)
			SET RELATION OFF INTO flistnat
			SELECT (m.lcAlias)
			COPY STRUCTURE EXTENDED TO (m.pri_path+"struct.dbf")
			SELECT 0
			USE (m.pri_path+"struct.dbf") EXCLUSIVE 
			ALTER table struct alter COLUMN field_name c(99)
			INDEX on field_name tag field_name
			SELECT flist
			SET RELATION TO field_name INTO struct ADDITIVE			
			REPLACE ALL prenasat WITH .T. FOR !EMPTY(struct.field_name) .and. !autoinc
			REPLACE ALL ftype_nati WITH struct.field_type
			SET RELATION OFF INTO struct
			SELECT flist			
			COUNT FOR prenasat TO m.lnPocetStlpcov
			IF m.lnPocetStlpcov > 0
				*** Musim to rozdelit na dve casti, pretoze potom vznikal dlhy reteaza, na ktory neslo urobot &
				m.sqlCmdColumns = this.tvorSqlCommand(m.cTo, .F., .F.)
				m.sqlCmdValues = this.tvorSqlCommand(m.cTo, .T., .F.)
				SELECT (m.lcAlias)
				COUNT &cCondition TO nCelkovyPocetZaznamov
				m.nAktualnyZaznam = 1
				IF m.nCelkovyPocetZaznamov > 0
					SCAN &cCondition 
						this.onRecordInsert(m.nAktualnyZaznam, nCelkovyPocetZaznamov)
						m.sqlCmd = sqlCmdColumns+ " "
						m.lPocetCasti = OCCURS("<STOP>", m.sqlCmdValues)
						IF m.lPocetCasti = 0
							m.sqlCmd = m.sqlCmd+" "+&sqlCmdValues
						ELSE
							FOR nIndex = 0 TO m.lPocetCasti-1
								IF m.nIndex = 0
									m.cCastSqlCmd = STREXTRACT(sqlCmdValues, "", "<STOP>", nIndex)								
								ELSE
									m.cCastSqlCmd = STREXTRACT(sqlCmdValues, "<STOP>", "<STOP>", nIndex)
								ENDIF
								m.sqlCmd = m.sqlCmd + " "+&cCastSqlCmd 
							ENDFOR
							m.cCastSqlCmd = STREXTRACT(sqlCmdValues, "<STOP>", "", m.lPocetCasti)	
							m.sqlCmd = m.sqlCmd + " "+&cCastSqlCmd 							
						ENDIF
						
						this.viewmessage(101,m.sqlCmd)				
						m.nret = SQLEXEC(this.ndatasource,m.sqlCmd )
						m.nAktualnyZaznam = m.nAktualnyZaznam + 1
						IF m.nret<0 &&Nastala nejaka chyba
							this.sqlError()
							EXIT
						ENDIF					
					ENDSCAN
				ENDIF
			ENDIF
		CATCH TO oErr
			this.nErrorNo = oErr.ErrorNo		
			this.sErrorMssg =  oErr.Message

			oldErrorHand= this.cOldErrHand 
			&oldErrorHand
		FINALLY 
			IF USED("flist")
				USE IN flist
			ENDIF
			IF USED("flistnat")
				USE IN flistnat
			ENDIF
			ERASE(m.pri_path+"flistnat.*")
			IF !EMPTY(m.lcAlias) AND m.lCloseFrom
				IF USED(m.lcAlias)
					USE IN (m.lcAlias)
				ENDIF
			ENDIF
			SELECT (m.oldSel)
			IF USED("struct")
				USE IN struct
			ENDIF
			ERASE(m.pri_path+"struct.*")
		ENDTRY

		IF this.nErrorNo <> 0   &&nastala nejaka chyba
			nRet = -1
		ELSE
			nRet = 1
		ENDIF		
	
		RETURN nRet
	ENDPROC	
		
	PROCEDURE tvorSqlCommand
	PARAMETERS m.cTo, lCastValues, m.lFromMemvar
	LOCAL sqlCmd, lcPridajSql, lnAktualLen

		SELECT flist
	
		IF ! m.lCastValues
			m.sqlCmd = "INSERT INTO "+m.cTo +" ("
								
			SCAN FOR prenasat
				IF !RIGHT(m.sqlCmd,1) == "("   &&nejde o prvy stlpec
					m.sqlCmd = m.sqlCmd + ","
				ENDIF  
				m.sqlCmd = m.sqlCmd + ALLTRIM(flist.field_name)
			ENDSCAN
			m.sqlCmd = m.sqlCmd + ")" 
		ELSE
			m.sqlCmd = " 'VALUES ("
			m.lnAktualLen = LEN(m.sqlCmd)
	
			SCAN FOR prenasat
				IF !RIGHT(m.sqlCmd,1) == "("   &&nejde o prvy stlpec
					m.sqlCmd = m.sqlCmd + ","
					m.lnAktualLen = m.lnAktualLen + 1
				ENDIF  
				IF ALLTRIM(flist.ftype_nati) == "D"  &&ak ide o datum, tak to musim upravit					
					lcPridajSql = "'"+"+IIF(EMPTY("+ALLTRIM(flist.field_name)+"),'DEFAULT', '?DTOS("+IIF(m.lFromMemvar, "m.","")+ALLTRIM(flist.field_name)+")')+"+"'"
					*lcPridajSql = "?CAST("+IIF(m.lFromMemvar, "m.","")+ALLTRIM(flist.field_name)+" AS date)"
				ELSE
					**Asi nieco traba aj pre DATTIME
					IF ALLTRIM(flist.ftype_nati) == "T"  &&ak ide o datum cas, tak to musim upravit	
						*** L.L. (04.12.2017) Oprava - povodne to bolo ako pri datume DTOS, ael tu musi byt TTOC				
						lcPridajSql = "'"+"+IIF(EMPTY("+ALLTRIM(flist.field_name)+"),'DEFAULT', '?TTOC("+IIF(m.lFromMemvar, "m.","")+ALLTRIM(flist.field_name)+")')+"+"'"
						*lcPridajSql = "?CAST("+IIF(m.lFromMemvar, "m.","")+ALLTRIM(flist.field_name)+" AS date)"
					ELSE
						IF ALLTRIM(flist.ftype_nati) == "XXXX"   &&ak ide o char, tak to orezem ako foxka
							lcPridajSql = "?ALLTRIM(LEFT("+IIF(m.lFromMemvar, "m.","")+ALLTRIM(flist.field_name)+","+ ALLTRIM(STR(flist.field_len,10,0))+"))"
						ELSE
							IF ALLTRIM(flist.ftype_nati) == "L"   &&ak ide o logicku hodnotu
								lcPridajSql = "?CAST("+IIF(m.lFromMemvar, "m.","")+ALLTRIM(flist.field_name)+" AS integer)"
							ELSE
								lcPridajSql = "?"+IIF(m.lFromMemvar, "m.","")+ALLTRIM(flist.field_name)
							ENDIF
						ENDIF
					ENDIF
				ENDIF
				*** Kedze neskor potrebujem urobit EVAL (&) a ten gunguje len na reteazce cca do 250znakov, tak si to rozdelim
				*** musim vediet, ked to mozem rozdelit, takze si tam dam znacku
				IF m.lnAktualLen + LEN(lcPridajSql) > 240
					m.lnAktualLen = LEN(lcPridajSql)
					m.sqlCmd = m.sqlCmd + "'<STOP>'"
				ELSE
					m.lnAktualLen = m.lnAktualLen + LEN(lcPridajSql)
				ENDIF
				m.sqlCmd = m.sqlCmd + lcPridajSql 
			ENDSCAN
			m.sqlCmd = m.sqlCmd + ")'"
		ENDIF

	RETURN sqlCmd 
	


	PROCEDURE converTableName
	LPARAMETER m.cTableName
	LOCAL m.cConvertedName
	
		DO CASE
			CASE UPPER(m.cTableName) == "SKFIN" .or. UPPER(m.cTableName) == "_TMSKFIN"
				m.cConvertedName = "stmskfin"
			CASE UPPER(m.cTableName) == "SKPOL" .or. UPPER(m.cTableName) == "_TMSKPOL"
				m.cConvertedName = "stmskpol"
			CASE UPPER(m.cTableName) == "SKPLT" .or. UPPER(m.cTableName) == "_TMSKPLT"
				m.cConvertedName = "stmskplt"
			CASE UPPER(m.cTableName) == "SKTXT" .or. UPPER(m.cTableName) == "_TMSKTXT"
				m.cConvertedName = "stmsktxt"
			CASE UPPER(m.cTableName) == "SKVKLVY" .or. UPPER(m.cTableName) == "_TMSKVKLVY"
				m.cConvertedName = "stmskvklvy"
			CASE UPPER(m.cTableName) == "SKFIS" .or. UPPER(m.cTableName) == "_TMSKFIS"
				m.cConvertedName = "stmskfis"				
			OTHERWISE
				m.cConvertedName = m.cTableName
		ENDCASE
	RETURN m.cConvertedName


	PROCEDURE isExistTable
	LPARAMETERS m.cTableName
	LOCAL m.lRet, m.nPocet


		DODEFAULT(m.cTableName)
		
		IF this.myExec("SELECT CAST(COUNT(*) AS integer) AS pocet FROM information_schema.tables WHERE  table_schema = '"+this.sSqlPrefix+"' AND table_name = '"+m.cTableName+"'" , "TSTEX"+LEFT(m.cTableName,5)) == 1
			SUM pocet TO nPocet
			IF nPocet > 0
				m.lRet = .T.
			ELSE
				m.lRet = .F.
			ENDIF
			USE IN "TSTEX"+LEFT(m.cTableName,5)
		ELSE
			m.lRet = .F.
		ENDIF					
		
		IF this.nErrorNo <> 0   &&nastala nejaka chyba
			m.lRet = .F.
		ENDIF		

		
		RETURN m.lRet
	ENDPROC


	PROCEDURE commit
	LOCAL nRet

		DODEFAULT()

		SQLCOMMIT(this.ndatasource)

		IF this.nErrorNo <> 0   &&nastala nejaka chyba
			nRet = -1
		ELSE
			nRet = 1
		ENDIF		
	
		RETURN nRet
	ENDPROC

		
	PROCEDURE rollback
	LOCAL nRet

		DODEFAULT()

		SQLROLLBACK(this.ndatasource)

		IF this.nErrorNo <> 0   &&nastala nejaka chyba
			nRet = -1
		ELSE
			nRet = 1
		ENDIF		
	
		RETURN nRet
	ENDPROC
	
	PROCEDURE setTransaction
	PARAMETERS lnSetTO
	LOCAL nRet

		DODEFAULT(lnSetTO)

		SQLSETPROP(this.ndatasource, 'Transactions', lnSetTO)

		IF this.nErrorNo <> 0   &&nastala nejaka chyba
			nRet = -1
		ELSE
			nRet = 1
		ENDIF		
	
		RETURN nRet
	ENDPROC 
	
ENDDEFINE 


*** Neviem ako funguje navratova hodnota, ked chcem vratit objekt, takze radsej 
*** vytvorim PUBLIC premennu oDataSrc
*** L.L. (05.11.2017) Funcia prerobena, tak aby sa dala pouzivat aj pre vytvorenie ineho ako  spojenia na datovy server - pridany parameter pDataSrc, 
*** cez ktory sa vrati objekt na spojenie na server
*** L.L. (05.11.2017) Prepojenie na extreny kreditny system - potrebujem potlacit hlasenie chyby, doplneny parameter pHlasit
PROCEDURE createDataConection
LPARAMETERS m.sdatatype, m.constr, pPrefix, pDataSrc, pHlasit, pClass
LOCAL m.lRet

	m.sdatatype = IIF(PARAMETERS()<1, "", m.sdatatype)
	m.constr = IIF(PARAMETERS()<2, "", m.constr)
	m.pPrefix = IIF(PARAMETERS()<3, "food600", m.pPrefix)
	m.pHlasit = IIF(PARAMETERS()<5, .T., m.pHlasit )
	m.pClass = IIF(PARAMETERS()<6, "cDataSql", m.pClass)
				
	pDataSrc = .NULL.
	m.lRet = .T.
		
	DO CASE
		*** Pripojenie cez ODBC ***
		CASE UPPER(m.sdatatype) == "ODBC"			
			m.pDataSrc = CREATEOBJECT(m.pClass, m.constr)
			*** L.L. (13.03.2018) Prefix nastavujem aj ked vo chvili vytvarania spojenia nie je mozne sa pripojit
			*IF pDataSrc.getErrorno() == 0
			IF TYPE("pDataSrc") == "O" AND !ISNULL(m.pDataSrc)
				pDataSrc.setSqlPrefix(ALLTRIM(m.pPrefix))				
			ENDIF
		*** FoxPro Databaza
		CASE UPPER(m.sdatatype) == "NATIVE"
			m.pDataSrc = CREATEOBJECT("cDataNative", m.constr)
		*** FREETABLE***
		CASE UPPER(m.sdatatype) == "FREETABLE"   
			m.pDataSrc = CREATEOBJECT("cDataFreeTable", m.constr)
		OTHERWISE
			m.lRet = .F.
			=MESSAGEBOX("Wrong Food600 datatype!" + " ("+ALLTRIM(UPPER(m.sdatatype))+")", 16, "User info")
	ENDCASE		

	m.lRet = (pDataSrc.getErrorno() == 0)
	IF !m.lRet
		IF pHlasit
			=MESSAGEBOX("Connection error no. "+ALLTRIM(STR(pDataSrc.getLastErrorNO(),10,0))+" - "+pDataSrc.getErrorMssg(), 16, "User info")		
		ENDIF
	ENDIF

	RETURN m.lRet 
ENDPROC

PROCEDURE releaseDataConnection
	IF TYPE("oDataSrc") == "O"
		TRY
			DO closeDataConnection
			oDataSrc.release()		
		CATCH
		ENDTRY
	ENDIF
ENDPROC

*** L.L. (05.11.2017) Prepojenie na extreny kreditny system - potrebujem potlacit hlasenie chyby, doplneny parameter pHlasit + prehadzane parametre
PROCEDURE openDataConnection
LPARAMETERS pDataSrc, pPrefix, pHlasit, m.sdatatype, m.constr
LOCAL m.lRet

	m.pHlasit = IIF(PARAMETERS()<3, .T., m.pHlasit)
	m.pPrefix = IIF(PARAMETERS()<2, "food600", m.pPrefix)
	m.pDataSrc = IIF(PARAMETERS()<1, oDataSrc, m.pDataSrc)
	
	IF EMPTY(m.pPrefix)
		m.pPrefix = "food600"
	ENDIF
	
	IF TYPE("pDataSrc") == "O"
		pDataSrc.myconnect(pDataSrc.cConnectionString)
		m.lRet = (pDataSrc.getErrorno() == 0)
		IF !m.lRet
			IF m.pHlasit
				=MESSAGEBOX("Connection error no. "+ALLTRIM(STR(pDataSrc.getLastErrorNO(),10,0))+" - "+pDataSrc.getErrorMssg(), 16, "User info")		
			ENDIF
		ENDIF
	ELSE
		m.lRet = createDataConection(m.sdatatype, m.constr, pPrefix, pDataSrc, pHlasit)
	ENDIF

RETURN m.lRet		
ENDPROC

PROCEDURE closeDataConnection
LPARAMETERS pDataSrc

	m.pDataSrc = IIF(PARAMETERS()<1, oDataSrc, m.pDataSrc)
	
	IF TYPE("pDataSrc") == "O"
		TRY
			pDataSrc.myDisconnect()		
		CATCH
		ENDTRY
	ENDIF
ENDPROC




	
	
	*!*****************************************************************************
	*!      Procedure: CODE_PWD
	*!*****************************************************************************
PROC code_pwd
	PARAMETERS _str, dekodovat
	PRIVATE _str, ret, ii, jj, seed
	seed = "#5)0% A+'@^&*("
	ret = ""
	FOR ii = 1 TO LEN(_str)
		IF PARAMETERS() = 1
			_str = UPPER(m._str)
			jj = ASC(SUBSTR(m._str, m.ii, 1)) + m.ii + ASC(SUBSTR(m.seed, MOD(m.ii,10), 1))
			jj = IIF(m.jj > 255, m.jj - 255, m.jj)
		ELSE
			jj = ASC(SUBSTR(m._str, m.ii, 1)) - m.ii - ASC(SUBSTR(m.seed, MOD(m.ii,10), 1))
			jj = IIF(m.jj < 0,   m.jj + 255, m.jj)
		ENDIF
		ret = ret + CHR(m.jj)
	ENDFOR
	RETURN ret
	
	
	
