en GUI, Harbour

Herramienta para MySQL – Código Liberado II

MySQLInfo.prg – Parte II

*------------------------------------------------------------*
PROCEDURE MainFin()
*------------------------------------------------------------*
    IF MsgYesNo( "¿Desea cerrar el programa?    ", _GEN_PROGRAMA )
        Desconectar( .F. )
        DoMethod( "wndPrincipal", "Release" )
    ENDIF
RETURN

*------------------------------------------------------------*
PROCEDURE GetError( oServer, nError, lInternal )
*------------------------------------------------------------*
    LOCAL cText := ""

    setMessage( "Error al conectar con el servidor MySql..." , 1 )

    cText += oServer:ErrorTxt() + Hb_OSNewLine()
    cText += "ERROR No: " + Str( nError ) + Hb_OSNewLine()
    cText += "Internal: " + If( lInternal, "Yes", "No" ) + Hb_OSNewLine()

    MsgInfo( cText, _GEN_PROGRAMA )

RETURN

*------------------------------------------------------------*
PROCEDURE Desconectar( lVerMensaje )
*------------------------------------------------------------*
    // Se desconecta del servidor de datos.--------------------------------
    IF _lConectado
        IF lVerMensaje
            IF MsgYesNo( "¿Desea desconectarse del servidor MySql?    ", _GEN_PROGRAMA )
                oServidor:End()
                wndPrincipal.Title := _GEN_PROGRAMA
                _lConectado := .F.
                UpdateTree()
                UpdateVars()
                UpdateStatus()
                UpdateMain()
                setMessage()
            ENDIF
        ELSE
            oServidor:End()
            _lConectado := .F.
        ENDIF
    ENDIF
    wndPrincipal.tabInfo.Enabled:=.f.
    wndPrincipal.grdTabla.DeleteAllItems
    wndPrincipal.grdDatos.DeleteAllItems
RETURN

*------------------------------------------------------------*
Procedure EditaTabla()
*------------------------------------------------------------*
    If IsControlDefined("treArbol","wndPrincipal")
        nItem := getProperty( "wndPrincipal", "treArbol", "Value" )
        If !Empty(nItem)
            If nItem >= 10000 .AND. nItem <= 99999
                cBase := getProperty( "wndPrincipal", "treArbol", "Item", nItem )
                MigABM( oServidor, cBase )
            Endif
        Endif
    Endif
Return

*------------------------------------------------------------*
STATIC PROCEDURE UpdateTree()
*------------------------------------------------------------*
    // Declaracion de variables locales.-----------------------------------
    LOCAL i                 AS NUMERIC
    LOCAL j                 AS NUMERIC
    LOCAL oDb               AS OBJECT
    LOCAL oQuery            AS OBJECT
    LOCAL nNodoBase         AS NUMERIC
    LOCAL nNodoTabla        AS NUMERIC

    // Actualiza el el tree.-----------------------------------------------
    IF _lConectado

        // Inicializa el arbol de vistas...............................
        wndPrincipal.treArbol.DeleteAllItems
        oTree:AddItem( oServidor:cUser + "@" + _cServidor, oTree:SelectionID(), 1 )

        aDatabases:= oServidor:ListDBs()
        If oServidor:lError
            MsGExclamation("Error verifying database list: " + oServidor:ErrorTxt())
            Return
        endif

        // Actualiza las bases de datos................................
        FOR i := 1 TO Len( aDatabases )
            nNodoBase := Val( "1" + PadL( i, 2, "0" ) )
            wndPrincipal.treArbol.AddItem( aDatabases[i], 1, nNodoBase )

            // Actualiza las tablas de cada base de datos..........
            oServidor:SelectDb( aDatabases[i] )
            If oServidor:lError
                MsGExclamation("Error connecting to database: " + oServidor:ErrorTxt())
                Return
            endif

            aTablas:=oServidor:ListTables()
            If oServidor:lError
                MsGExclamation("Error verifying tables list: " + oServidor:ErrorTxt())
                Return
            endif

            FOR j := 1 TO Len( aTablas )
                nNodoTabla := Val( AllTrim( Str( nNodoBase ) + PadL( j, 2, "0" ) ) )
                wndPrincipal.treArbol.AddItem( aTablas[j], nNodoBase, nNodoTabla )
            NEXT

        NEXT

        // Expande los items de primer nivel...........................
        wndPrincipal.treArbol.Expand( 1 )
    ELSE
        doMethod( "wndPrincipal", "treArbol", "DeleteAllItems" )
    ENDIF

RETURN

*------------------------------------------------------------*
PROCEDURE UpdateVars()
*------------------------------------------------------------*
    // Declaracion de variables.-------------------------------------------
    LOCAL oQuery         AS OBJECT
    LOCAL i              AS NUMERIC
    // Borra las variables.------------------------------------------------
    wndPrincipal.grdVariables.DeleteAllItems

    // Actualiza las variables si esta conectado---------------------------
    IF _lConectado
        setMessage( "Ejecutando consulta: SHOW VARIABLES...", 1 )
        oQuery := oServidor:Query( "show variables" )
        IF !(oServidor:lError)
            oQuery:Gotop()
            aFila := oQuery:FillArray()
            FOR i := 1 to oQuery:LastRec()
                IF !Empty( aFila[1] )
                    wndPrincipal.grdVariables.addItem( aFila[i] )
                ENDIF
            NEXT
        ENDIF
        oQuery:End()
        setMessage()
    ENDIF

RETURN

*------------------------------------------------------------*
PROCEDURE UpdateStatus()
*------------------------------------------------------------*
    // Declaracion de variables.-------------------------------------------
    LOCAL oQuery         AS OBJECT
    LOCAL i              AS NUMERIC

    // Borra las variables.------------------------------------------------
    wndPrincipal.grdEstado.DeleteAllItems

    // Actualiza el estado si esta conectado-------------------------------
    IF _lConectado
        setMessage( "Ejecutando consulta: SHOW STATUS...", 1 )
        oQuery := oServidor:Query( "show status" )
        IF !(oServidor:lError())
            oQuery:Gotop()
            aFila := oQuery:FillArray()
            FOR i := 1 TO oQuery:LastRec()
                IF !Empty( aFila[1] )
                    wndPrincipal.grdEstado.addItem( aFila[i] )
                ENDIF
            NEXT
        ENDIF
        oQuery:End()
        setMessage()
    ENDIF

RETURN

*------------------------------------------------------------*
STATIC PROCEDURE UpdateTable( cTabla )
*------------------------------------------------------------*
    // Declaracion de variables.-------------------------------------------
    LOCAL oQuery         AS OBJECT
    LOCAL i              AS NUMERIC

    // Borra las variables.------------------------------------------------
    wndPrincipal.grdTabla.DeleteAllItems

    // Actualiza el estado si esta conectado-------------------------------
    IF _lConectado
        setMessage( "Ejecutando consulta: DESCRIBE " + Upper( cTabla ) + "..." , 1 )
        oQuery := oServidor:Query( "describe " + cTabla )
        IF !(oServidor:lError)
            oQuery:Gotop()
            aFila := oQuery:FillArray()
            FOR i := 1 TO oQuery:LastRec()
                IF !Empty( aFila[1] )
                    wndPrincipal.grdTabla.addItem( aFila[i] )
                ENDIF
            NEXT
        ENDIF
        oQuery:End()
        setMessage()
    ENDIF

RETURN

*------------------------------------------------------------*
STATIC PROCEDURE UpdateData( cTabla )
*------------------------------------------------------------*
    // Declaracion de variables.-------------------------------------------
    LOCAL oQuery            AS OBJECT
    LOCAL i                 AS NUMERIC
    LOCAL aCampos           AS ARRAY
    LOCAL aAnchos           AS ARRAY
    LOCAL aFila             AS ARRAY
    LOCAL aTipos            AS ARRAY
    LOCAL nColumnas         AS NUMERIC

    // Inicializacion de variables.----------------------------------------
    nColumnas := Len( getProperty( "wndPrincipal", "grdDatos", "Item", 1 ) )
    // Borra las columnas existentes.......................................
    DO WHILE nColumnas != 0
        wndPrincipal.grdDatos.DeleteColumn( nColumnas )
        nColumnas--
    ENDDO

    // Crea la consulta y obtiene la informacion.--------------------------
    setMessage( "Ejecutando consulta: SELECT * FROM " + Upper( cTabla) + " LIMIT 1000...", 1 )
    oQuery := oServidor:Query( "select * from " + cTabla + " limit 50" )

    IF !(oServidor:lError)
        aCampos := ARRAY( oQuery:FCount() )
        aAnchos := ARRAY( oQuery:FCount() )
        aTipos  := ARRAY( oQuery:FCount() )

        FOR i := 1 TO oQuery:FCount()
            aCampos[i] := oQuery:FieldName( i )
            aTipos[i]  := oQuery:FieldType( i )
            aAnchos[i] := iif( oQuery:FieldLen( i ) > Len( oQuery:FieldName( i ) ),  ;
                oQuery:FieldLen( i ) * 14, Len( oQuery:FieldName( i ) ) * 14 )
            aAnchos[i] := iif( aAnchos[i] > 250, 250, aAnchos[i] )
        NEXT
    ELSE
        RETURN
    ENDIF

    // Actualiza la informacion del grid.----------------------------------
    // Crea las nuevas columnas............................................
    FOR i := 1 TO oQuery:FCount()
        oGrid4:AddColumn( i, aCampos[i], aAnchos[i], iif(aTipos[i]=="N", 1, 0) )
    NEXT

    // Actualiza la informacion de la consulta.............................
    oQuery:GoTop()

    aFila := oQuery:FillArray()

    FOR i := 1 TO oQuery:LastRec()
        if (i%100)==0
            do events
            setmessage( "Record: "+str(i,5), 2 )
        endif

        IF !Empty( aFila[1] )
            wndPrincipal.grdDatos.addItem( ItemChar( aFila[i], aTipos ) )
        ENDIF

    NEXT

    oQuery:End()
    setMessage()

RETURN

*------------------------------------------------------------*
function ItemChar( aLine, aType )    //SELECT residents FROM country WHERE name = 'France'
*------------------------------------------------------------*
    local aRet:={}, i:=0, l:=0

    Iif( ValType(aLine) == 'A', aRet := array( len(aLine) ) , aRet := {""} )

    l := Len( aRet )

    for i := 1 to l
        do case

        case aType[i]=="N"
            If l = 1
                aRet[i] := hb_Cstr(aLine)
            Else
                aRet[i] := hb_Cstr( aLine[i] )
            Endif
        case aType[i]=="D"
            If Empty( aLine[i] )
                aRet[i] := aLine[i]
            ElseIf l = 1
                aRet[i] := DtoC( aLine )
            Else
                aRet[i] := dtoc(aLine[i])
            Endif
        case aType[i]=="L"
            If l = 1
                aRet[i] := iif( aLine, ".T.", ".F." )
            Else
                aRet[i] := iif(aLine[i], ".T.", ".F.")
            Endif
        otherwise
            If l = 1
                aRet[i] := hb_Cstr( aLine )
            Else
                aRet[i] := hb_Cstr( aLine[i] )
            Endif
        endcase
    next
Return( aRet )