PUBLIC ARRAY aodbcdata[1]
LOCAL testheslo

gclocal_path = m.gcpri_path 

sqldriver=getodbcdriver()
IF EMPTY(m.sqldriver)
	MESSAGEBOX(jaz("form_chyba_odbc_driver"),16,"FoodMan")
    prenastav_issql()
ENDIF

IF m.issql .OR. m.is_kongres

	getodbc()
	DO WHILE .T.
		sqlvyber = get_ini("SQL Spojenie", "Meno"+ALLTRIM(STR(m.pocetsqlspojeni+1,5,0)))
		IF EMPTY(m.sqlvyber)
			IF m.pocetsqlspojeni=0
				pocetsqlspojeni=1
				sqlvyber="SQL"
			ENDIF
			EXIT
		ELSE
			pocetsqlspojeni=m.pocetsqlspojeni+1
		ENDIF
	ENDDO
	PUBLIC ARRAY sqlspojenia(m.pocetsqlspojeni,2)
	FOR i=1 TO m.pocetsqlspojeni
		sqlspojenia(m.i,1)=get_ini("SQL Spojenie", "Meno"+ALLTRIM(STR(m.i,5,0)))
		IF EMPTY(sqlspojenia(m.i,1)) AND m.i=1
			sqlspojenia(m.i,1)="Default"
			put_ini("SQL Spojenie", "Meno1", "Default")
		ENDIF
		sqlspojenia(m.i,2)=get_ini("SQL Spojenie", "Adresa"+ALLTRIM(STR(m.i,5,0)))
		IF EMPTY(sqlspojenia(m.i,2))
			IF  m.i=1
				sqlspojenia(m.i,2)="SQL"
				= put_ini("SQL Spojenie", "Adresa1", "SQL")
			ELSE
				sqlspojenia(m.i,2)=sqlspojenia(m.i,1)
				put_ini("SQL Spojenie", "Adresa"+ALLTRIM(STR(m.i,5,0)),sqlspojenia(m.i,1))
			ENDIF
		ENDIF
	ENDFOR
	IF EMPTY(m.menospojenia)
		IF m.pocetsqlspojeni=1
			sqlvyber=sqlspojenia(1,2)
			sqlmenospojenia=sqlspojenia(1,1)
		ELSE
			DO FORM sqlvyberspojenia TO m.i
			IF EMPTY(m.i)
				IF m.issql
					MESSAGEBOX(jaz("form_nevybrane_ziadne_data"),16,"FoodMan")
					issql=.F.
					DO sh_koniec
				ELSE
					is_kongres=.F.
				ENDIF
			ELSE
				sqlvyber=sqlspojenia(m.i,2)
				sqlmenospojenia=sqlspojenia(m.i,1)
			ENDIF
		ENDIF
	ELSE
		sqlvyber = m.menospojenia
	ENDIF

	IF m.issql .OR. m.is_kongres
		sqladresa		= get_ini(m.sqlvyber, "Server")
		sqlserver		= get_ini(m.sqlvyber, "ODBC")
		sqldatabaza		= get_ini(m.sqlvyber, "Databaza")
		sqlmeno			= get_ini(m.sqlvyber, "User")
		sqlheslo		= get_ini(m.sqlvyber, "Heslo")
		sqltyp			= get_ini(m.sqlvyber, "TypServera")
		sqlport			= get_ini(m.sqlvyber, "Port")
		IF EMPTY(m.sqlport)
			sqlport = "5432"
		ENDIF
		sqlsslmode		= get_ini(m.sqlvyber, "SSLMode")
		IF EMPTY(m.sqlsslmode) OR !INLIST(m.sqlsslmode,"prefered","allow","require")
			m.sqlsslmode="prefered"
		ENDIF 
		sqltypspojenia    = get_ini(m.sqlvyber, "TypSpojenia")
		DO CASE
			CASE m.sqltypspojenia = "1"
				sqltypspojenia = 1
            CASE m.sqltypspojenia = "2"
				sqltypspojenia = 2
			CASE !EMPTY(m.sqlserver)
				sqltypspojenia = 1
			CASE !EMPTY(m.sqladresa)
				sqltypspojenia = 2
			OTHERWISE
				sqltypspojenia = 1
		ENDCASE

		DO CASE
			CASE "mysql"$LOWER(m.sqltyp)
				sqlprefix="my_"
				IF EMPTY(m.sqlmeno)
					sqlmeno="root"
				ENDIF
			OTHERWISE
				sqlprefix="ps_"
				IF EMPTY(m.sqlmeno)
					sqlmeno="postgres"
				ENDIF
		ENDCASE
		IF EMPTY(m.sqlheslo)
			sqlheslo=pwd("asuprak")
		ENDIF
		IF EMPTY(m.sqldatabaza)
			sqldatabaza="food600"
		ENDIF

		IF m.sqltypspojenia = 1
            DO pripoj_typ1
		ELSE
            DO pripoj_typ2
		ENDIF
	ENDIF
ENDIF


*!*****************************************************************************
PROCEDURE pripoj_typ1
IF EMPTY(m.sqlserver)
    DO FORM sqlspojenie TO m.sqlconnection
    IF m.sqlconnection<=0
		WAIT jaz("form_nepripojene") WINDOW TIMEOUT 2
        prenastav_issql()
    ENDIF
ELSE
    DO open_database_food
    sqlconnectold=DBGETPROP("AltoNet","CONNECTION","ConnectString")
    testheslo = dekoduj_heslo(m.sqlheslo)
    sqlconnectstring="DSN="+ALLTRIM(m.sqlserver)+";Database="+ALLTRIM(m.sqldatabaza)+";Uid="+ALLTRIM(m.sqlmeno)+";Pwd="+m.testheslo
    DBSETPROP("AltoNet","CONNECTION","ConnectString",m.sqlconnectstring)
    sqlconnection=SQLCONNECT("AltoNet")
    IF m.sqlconnection>0
        SQLDISCONNECT(m.sqlconnection)
        IF m.is_kongres
            DO open_database_front
            DBSETPROP("frontpost","CONNECTION","ConnectString",m.sqlconnectstring)
        ENDIF 
        DO open_database_food
    ELSE
        DO FORM sqlspojenie TO m.sqlconnection
        IF m.sqlconnection<=0
			WAIT jaz("form_nepripojene") WINDOW TIMEOUT 2
            IF m.sqlconnectold<>m.sqlconnectstring
                DBSETPROP("AltoNet"  ,"CONNECTION","ConnectString",m.sqlconnectold)
                IF m.is_kongres
                    DO open_database_front
                    DBSETPROP("frontpost","CONNECTION","ConnectString",m.sqlconnectstring)
                ENDIF 
                DO open_database_food
            ENDIF
            prenastav_issql()
        ENDIF
    ENDIF
    CLOSE DATABASES
ENDIF


*!*****************************************************************************
PROCEDURE pripoj_typ2
IF EMPTY(m.sqladresa)
    DO FORM sqlspojenie TO m.sqlconnection
    IF m.sqlconnection<=0
		WAIT jaz("form_nepripojene") WINDOW TIMEOUT 2
        IF m.sqlconnectold<>m.sqlconnectstring
            DBSETPROP("AltoNet","CONNECTION"  ,"ConnectString",m.sqlconnectold)
            IF m.is_kongres
                DO open_database_front
                DBSETPROP("frontpost","CONNECTION","ConnectString",m.sqlconnectold)
            ENDIF 
            DO open_database_food
        ENDIF
        prenastav_issql()
    ENDIF
ELSE
    DO open_database_food
    sqlconnectold=DBGETPROP("AltoNet","CONNECTION","ConnectString")
    testheslo = dekoduj_heslo(m.sqlheslo)

    IF m.sqldatabaza = "*"
        sqldatabaza = "postgres"
        sqlconnectstring="DRIVER={"+m.sqldriver+"};DATABASE=" + ALLTRIM(m.sqldatabaza) + ";SERVER=" + ALLTRIM(m.sqladresa) + ";PORT=" + ALLTRIM(m.sqlport) + ";UID=" + ALLTRIM(m.sqlmeno) + ";PWD=" + m.testheslo + IIF(!EMPTY(m.sqlsslmode), ";SSLMODE=" + ALLTRIM(m.sqlsslmode), "")
        DBSETPROP("AltoNet","CONNECTION","ConnectString",m.sqlconnectstring)
        sqlconnection=SQLCONNECT("AltoNet")
        IF m.sqlconnection > 0

            sqldatabaza = m.sqlmeno
            sql_select("SELECT usename, usesuper FROM pg_catalog.pg_user ", "list", "usename C(66), usesuper C(5)")
            SELECT list
            LOCATE FOR usename = m.sqlmeno .and. VAL(usesuper) > 0
            IF FOUND()                   && jsem superuser

                sql_select("SELECT datname FROM pg_database WHERE datname LIKE 'food%' ORDER BY datname ", "list", "datname C(99)")
                COUNT TO pocet_databazi
                IF m.pocet_databazi = 1
                    GO TOP 
                    sqldatabaza = ALLTRIM(list.datname)
                ELSE 
                    DEFINE POPUP hom_path MARGIN RELATIVE SHADOW COLOR SCHEME 4 FROM 5, 55 TITLE "Vyber databazi na serveru " + UPPER(m.sqladresa) FONT "Courier New"

                    IF FILE(".\tmp\posledni_spustena_databaze.txt")
                        posledni_spustena_databaze = FILETOSTR(".\tmp\posledni_spustena_databaze.txt")
                        DEFINE BAR 2 OF hom_path PROMPT m.posledni_spustena_databaze
                        DEFINE BAR 3 OF hom_path PROMPT "\-"
                    ELSE
                        posledni_spustena_databaze = "F-4\\H^4+56"
                    ENDIF 

                    radek_menu = 4
                    SCAN
                        cmd = "DEFINE BAR " + STR(m.radek_menu, 2) + " OF hom_path PROMPT '" + ALLTRIM(datname) + "'"
                        &cmd
                        radek_menu = m.radek_menu + 1
                    ENDSCAN 

                    ON SELECTION POPUP hom_path deacti POPUP 
                    ACTIVATE POPUP hom_path
                    vybrana_databaza = ALLTRIM(PROMPT())
                    RELEASE POPUP hom_path
                    IF .not. EMPTY(m.vybrana_databaza )
                        sqldatabaza = m.vybrana_databaza 
                        DO CASE 
                        CASE ATC("kancl", m.sqladresa) > 0
                            pokracovatt = .T.

                        CASE ATC("localhost", m.sqladresa) > 0
                            pokracovatt = .T.

                        OTHERWISE 
                            pokracovatt = MESSAGEBOX(CHR(13) + ALLTRIM(m.sqladresa) + " : " + UPPER(ALLTRIM(m.sqldatabaza)), 3+32, "chcete spustit SQL data?") = 6
                        
                        ENDCASE 
                        
                        IF m.pokracovatt
                            mk_dir( ".\tmp" )
                            STRTOFILE(m.sqldatabaza, ".\tmp\posledni_spustena_databaze.txt")
                        
                            IF ATC(":\food_sql\", m.gchom_path ) > 0
                               gchom_path = gchom_path + STRTRAN(m.sqldatabaza, "food_", "") + "\"
                               mk_dir( m.gchom_path )
                            ENDIF 
                        ELSE
                            sqldatabaza = "qwe349857"
                            CANCEL 
                        ENDIF                         

                    ENDIF 
                ENDIF 
            ENDIF 
            SQLDISCONNECT(m.sqlconnection)
        ENDIF 
    ENDIF 

    sqlconnectstring="DRIVER={"+m.sqldriver+"};DATABASE=" + ALLTRIM(m.sqldatabaza) + ";SERVER=" + ALLTRIM(m.sqladresa) + ";PORT=" + ALLTRIM(m.sqlport) + ";UID=" + ALLTRIM(m.sqlmeno) + ";PWD=" + m.testheslo + IIF(!EMPTY(m.sqlsslmode), ";SSLMODE=" + ALLTRIM(m.sqlsslmode), "")
    DBSETPROP("AltoNet","CONNECTION","ConnectString",m.sqlconnectstring)
    sqlconnection=SQLCONNECT("AltoNet")
    IF m.sqlconnection>0
        SQLDISCONNECT(m.sqlconnection)
        IF m.is_kongres
            DO open_database_front
            DBSETPROP("frontpost","CONNECTION","ConnectString",m.sqlconnectstring)
        ENDIF 
        DO open_database_food
    ELSE
        DO FORM sqlspojenie TO m.sqlconnection
        IF m.sqlconnection<=0
			WAIT jaz("form_nepripojene") WINDOW TIMEOUT 2
            IF m.sqlconnectold # m.sqlconnectstring
                DBSETPROP("AltoNet"  ,"CONNECTION","ConnectString",m.sqlconnectold)
                IF m.is_kongres
                    DO open_database_front
                    DBSETPROP("frontpost","CONNECTION","ConnectString",m.sqlconnectold)
                ENDIF 
                DO open_database_food
            ENDIF
            prenastav_issql()
        ELSE 
            TRY 
                SQLDISCONNECT(m.sqlconnection)
            CATCH
            ENDTRY 
            IF m.is_kongres
                DO open_database_front
                DBSETPROP("frontpost","CONNECTION","ConnectString",m.sqlconnectstring)
            ENDIF 
            DO open_database_food
        ENDIF
    ENDIF
ENDIF


*!*****************************************************************************
PROCEDURE prenastav_issql
IF m.issql
    issql=.F.
    DO sh_koniec
ELSE
    is_kongres=.F.
ENDIF


*!*****************************************************************************
FUNCTION dekoduj_heslo (hhhh)
LOCAL retv
TRY
    retv = depwd(m.hhhh)
CATCH
    retv = "aaa"
ENDTRY
RETURN m.retv


*!*****************************************************************************
PROCEDURE sync_ini
LOCAL sqladresa, sqlserver, sqldatabaza, sqlmeno, sqlheslo, sqltyp, sqlport
PUBLIC ARRAY aodbcdata[1]
DO setpath
m.sqldriver=getodbcdriver()
IF EMPTY(m.sqldriver)
	MESSAGEBOX(jaz("form_chyba_odbc_driver"),16,"FoodMan")
	m.gcsyncid=""
	RETURN 
ENDIF
=getodbc()
sqladresa     = get_ini("Synchronizacia", "Server")
sqlserver     = get_ini("Synchronizacia", "ODBC")
sqldatabaza   = get_ini("Synchronizacia", "Databaza")
sqlmeno       = get_ini("Synchronizacia", "User")
sqlheslo      = get_ini("Synchronizacia", "Heslo")
sqltyp        = get_ini("Synchronizacia", "TypServera")
sqlport       = get_ini("Synchronizacia", "Port")
sqlsslmode    = get_ini("Synchronizacia", "SSLMode")
IF EMPTY(m.sqlsslmode) OR !INLIST(m.sqlsslmode,"prefered","allow","require")
	m.sqlsslmode="prefered"
ENDIF 
m.sqltypspojenia=2
IF EMPTY(m.sqladresa)
	DO FORM sqlpripojenie TO m.sqlconnection
	IF m.sqlconnection<=0
		WAIT jaz("form_nepripojene") WINDOW TIMEOUT 2
		m.gcsyncid=""
	ENDIF
ELSE
	DO open_database
	sqlconnectold=DBGETPROP("SyncNet","CONNECTION","ConnectString")
	LOCAL m.testheslo
    testheslo = dekoduj_heslo(m.sqlheslo)
	sqlconnectstring="DRIVER={"+m.sqldriver+"};DATABASE="+ALLTRIM(m.sqldatabaza)+";SERVER="+ALLTRIM(m.sqladresa)+";PORT="+ALLTRIM(m.sqlport)+";UID="+ALLTRIM(m.sqlmeno)+";PWD="+m.testheslo+IIF(!EMPTY(m.sqlsslmode), ";SSLMODE="+ALLTRIM(m.sqlsslmode),"")
	DBSETPROP("SyncNet","CONNECTION","ConnectString",m.sqlconnectstring)
	sqlconnection=SQLCONNECT("SyncNet")
	IF m.sqlconnection>0
		=SQLDISCONNECT(m.sqlconnection)
	ELSE
		DO FORM sqlpripojenie TO m.sqlconnection
		IF EMPTY(m.sqlconnection)
			WAIT jaz("form_nepripojene") WINDOW TIMEOUT 2
			IF m.sqlconnectold<>m.sqlconnectstring
				DBSETPROP("SyncNet","CONNECTION","ConnectString",m.sqlconnectold)
			ENDIF
			gcsyncid=""
		ELSE 
			TRY 
				=SQLDISCONNECT(m.sqlconnection)
			CATCH
			ENDTRY 
		ENDIF
	ENDIF 
ENDIF 
