*:*****************************************************************************
*:
*:	Procedure file: EXPORT_FROM_SQL
*:
*: Vyexportuje tabulku z SQL do DBF
*:  __src_table: meno tabulky, ktora sa ma vyexportovat
*:  __kam  : cesta kam se ma kopirovat
*:  priklad: do copy_dbf with "i:\d_xxx\data\sk_???", "c:\ftp\data\dbf\"
*:
*:  2012-12-28: zrusen 3. parametr, ktery urcoval, zda kopirovat s klauzuli FOX2X
*:              on error nahrazeno kontrukci try-catch
*:	2019-10-30: pridan parametr sqljmenoobjektu kvuli full sql
*:*****************************************************************************
PROCEDURE export_From_sql
PARAMETERS __sourcefile, __kam, __podmienka, __sqljmenoobjektu
LOCAL m.isOk, m.destinationFileName,__koliksouboru 

IF PARAMETERS()<4
	__sqljmenoobjektu = "oDataSrc"
ENDIF 

IF PARAMETERS()<3
	__podmienka = ""
ENDIF

__kam = __kam + IIF(RIGHT(m.__kam, 1)="\", "", "\")
if ! DIRECTORY(m.__kam)
   MKDIR (m.__kam)
ENDIF
m.isOk = .T.
m.__koliksouboru = 0

DO CASE
	CASE UPPER(m.__sourcefile) == "DEFSUBOR"
		IF &__sqljmenoobjektu..myExec("SELECT DISTINCT modul, setup_skup FROM <PREFIX>defsubor"+IIF(EMPTY(m.__podmienka),""," WHERE "+m.__podmienka), "DEF_MODULY") > 0
			SELECT def_moduly
			SCAN ALL 
				IF	UPPER(ALLTRIM(def_moduly.modul)) == "FOOD_SET"
					m.destinationFileName = "FOOD_SET"
				ELSE
					m.destinationFileName = "s_"+ALLTRIM(def_moduly.modul)+"_"+ALLTRIM(def_moduly.setup_skup)
				ENDIF
				IF &__sqljmenoobjektu..dajTabulku("defsubor", m.__kam+m.destinationFileName, .T., "", "modul='"+ALLTRIM(def_moduly.modul)+"' AND setup_skup='"+ALLTRIM(def_moduly.setup_skup)+"'"+IIF(EMPTY(m.__podmienka),""," AND "+m.__podmienka)) > 0
					USE IN m.destinationFileName
					m.isOk = .T.
					m.__koliksouboru = __koliksouboru + 1
				ELSE
					m.isOk = .F.
					EXIT
				ENDIF
			ENDSCAN
			USE IN def_moduly
		ELSE
			m.isOk = .F.
		ENDIF
	CASE UPPER(m.__sourcefile) == "GR___" OR UPPER(m.__sourcefile) == "UG___" 
		IF &__sqljmenoobjektu..myExec("SELECT DISTINCT modul FROM <PREFIX>"+m.__sourcefile+IIF(EMPTY(m.__podmienka),""," WHERE "+m.__podmienka), "MODULY") > 0
			SELECT MODULY
			SCAN ALL 
				m.destinationFileName = LEFT(__sourcefile,5)+ALLTRIM(moduly.modul)
				IF &__sqljmenoobjektu..dajTabulku(LEFT(__sourcefile,5), m.__kam+m.destinationFileName, .T., "", "modul='"+ALLTRIM(moduly.modul)+"'"+IIF(EMPTY(m.__podmienka),""," AND "+m.__podmienka)) > 0
					USE IN m.destinationFileName
					m.isOk = .T.
					m.__koliksouboru = __koliksouboru + 1
				ELSE
					m.isOk = .F.
					EXIT
				ENDIF
			ENDSCAN
			USE IN moduly
		ELSE
			m.isOk = .F.
		ENDIF		
	CASE UPPER(m.__sourcefile) == "KLAPKY" 
		IF &__sqljmenoobjektu..myExec("SELECT DISTINCT id, mod, oblast FROM <PREFIX>klapky"+IIF(EMPTY(m.__podmienka),""," WHERE "+m.__podmienka), "MODULY") > 0
			SELECT MODULY
			SCAN ALL
				LOCAL nVysledok
				IF !EMPTY(mod) 
					m.destinationFileName = "klapk"+ALLTRIM(moduly.mod)+ALLTRIM(id)
					nVysledok = &__sqljmenoobjektu..dajTabulku("klapky",m.__kam+m.destinationFileName, .T., "", "id='"+ALLTRIM(moduly.id)+"' AND mod='"+ALLTRIM(moduly.mod)+"' AND EMPTY(mod)"+IIF(EMPTY(m.__podmienka),""," AND "+m.__podmienka))
				ELSE
					m.destinationFileName = "klap_"+ALLTRIM(moduly.oblast)+ALLTRIM(id)
					nVysledok = &__sqljmenoobjektu..dajTabulku("klapky",m.__kam+m.destinationFileName, .T., "", "id='"+ALLTRIM(moduly.id)+"' AND oblast='"+ALLTRIM(moduly.oblast)+"' AND EMPTY(mod)"+IIF(EMPTY(m.__podmienka),""," AND "+m.__podmienka))
				ENDIF		
				IF  m.nVysledok > 0
					USE IN m.destinationFileName
					m.isOk = .T.
					m.__koliksouboru = __koliksouboru + 1
				ELSE
					m.isOk = .F.
					EXIT					
				ENDIF				
			ENDSCAN
			USE IN moduly
		ELSE
			m.isOk = .F.
		ENDIF		
	CASE UPPER(m.__sourcefile) == "KL_DON" 
		IF &__sqljmenoobjektu..myExec("SELECT DISTINCT id FROM <PREFIX>kl_don"+IIF(EMPTY(m.__podmienka),""," WHERE "+m.__podmienka), "MODULY") > 0
			SELECT MODULY
			SCAN ALL
				LOCAL nVysledok
				m.destinationFileName = "kl_don"+ALLTRIM(moduly.id)
				nVysledok = &__sqljmenoobjektu..dajTabulku("kl_don",m.__kam+m.destinationFileName, .T., "", "id='"+ALLTRIM(moduly.id)+"'"+IIF(EMPTY(m.__podmienka),""," AND "+m.__podmienka))			
				IF  m.nVysledok > 0
					USE IN m.destinationFileName
					m.isOk = .T.
					m.__koliksouboru = __koliksouboru + 1
				ELSE
					m.isOk = .F.
					EXIT					
				ENDIF				
			ENDSCAN
			USE IN moduly
		ELSE
			m.isOk = .F.
		ENDIF		
	OTHERWISE
		IF &__sqljmenoobjektu..dajTabulku(m.__sourcefile, m.__kam+m.__sourcefile, .T., "", m.__podmienka) > 0	 
			USE IN m.__sourcefile
			m.isOk = .T.
			m.__koliksouboru = __koliksouboru + 1
		ELSE
			m.isOk = .F.
		ENDIF
ENDCASE		    

RETURN m.isOk	


