en GUI, Harbour

Herramienta para MySQL – Código Liberado III

MySQLInfo.prg – Parte III

*------------------------------------------------------------*
PROCEDURE UpdateMain()
*------------------------------------------------------------*
    // Actualiza los controles.--------------------------------------------
    IF _lConectado
        setProperty( "wndPrincipal", "treArbol", "Enabled", .T. )
        setProperty( "wndPrincipal", "tabInfo", "Enabled", .T. )
    ELSE
        setProperty( "wndPrincipal", "treArbol", "Enabled", .F. )
        setProperty( "wndPrincipal", "tabInfo", "Enabled", .F. )
    ENDIF

    // Actualiza la barra de herramientas.---------------------------------
    IF _lConectado
        setProperty( "wndPrincipal", "tbbConectar", "Enabled", .F. )
        setProperty( "wndPrincipal", "tbbDesconectar", "Enabled", .T. )
        setProperty( "wndPrincipal", "tbbConsulta", "Enabled", .T. )

        nItem := getProperty( "wndPrincipal", "treArbol", "Value" )
        If !Empty(nItem)
            If nItem >= 10000 .AND. nItem <= 99999
                setProperty( "wndPrincipal", "tbbEditar", "Enabled", .T. )
                setProperty( "wndPrincipal", "tbbExportar", "Enabled", .T. )
                setProperty( "wndPrincipal", "tbbImportar", "Enabled", .F. )
                setProperty( "wndPrincipal", "tbbEliminar", "Enabled", .T. )

            ElseIf nItem >= 100 .AND. nItem <= 999
                setProperty( "wndPrincipal", "tbbEditar", "Enabled", .F. )
                setProperty( "wndPrincipal", "tbbExportar", "Enabled", .F. )
                setProperty( "wndPrincipal", "tbbImportar", "Enabled", .T. )
                setProperty( "wndPrincipal", "tbbEliminar", "Enabled", .F. )
            Else
                setProperty( "wndPrincipal", "tbbEditar", "Enabled", .F. )
                setProperty( "wndPrincipal", "tbbExportar", "Enabled", .F. )
                setProperty( "wndPrincipal", "tbbImportar", "Enabled", .F. )
                setProperty( "wndPrincipal", "tbbEliminar", "Enabled", .F. )
            Endif
        Else
            setProperty( "wndPrincipal", "tbbEditar", "Enabled", .F. )
            setProperty( "wndPrincipal", "tbbExportar", "Enabled", .F. )
            setProperty( "wndPrincipal", "tbbImportar", "Enabled", .F. )
            setProperty( "wndPrincipal", "tbbEliminar", "Enabled", .F. )
        Endif

    ELSE
        setProperty( "wndPrincipal", "tbbConectar", "Enabled", .T. )
        setProperty( "wndPrincipal", "tbbDesconectar", "Enabled", .F. )
        setProperty( "wndPrincipal", "tbbConsulta", "Enabled", .F. )
        setProperty( "wndPrincipal", "tbbEditar", "Enabled", .F. )
        setProperty( "wndPrincipal", "tbbExportar", "Enabled", .F. )
        setProperty( "wndPrincipal", "tbbImportar", "Enabled", .F. )
        setProperty( "wndPrincipal", "tbbEliminar", "Enabled", .F. )
    ENDIF

    // Actualiza la barra de estado.---------------------------------------
    IF _lConectado
        setProperty( "wndPrincipal", "StatusBar", "Icon", 3, "ICO_32_LEDON" )
    ELSE
        setProperty( "wndPrincipal", "StatusBar", "Icon", 3, "ICO_32_LEDOFF" )
    ENDIF

    // Actualiza el menu principal.----------------------------------------
    IF _lConectado
        setProperty( "wndPrincipal", "mnuConectar", "Enabled", .F. )
        setProperty( "wndPrincipal", "mnuDesconectar", "Enabled", .T. )
        setProperty( "wndPrincipal", "mnuSql", "Enabled", .T. )
    ELSE
        setProperty( "wndPrincipal", "mnuConectar", "Enabled", .T. )
        setProperty( "wndPrincipal", "mnuDesconectar", "Enabled", .F. )
        setProperty( "wndPrincipal", "mnuSql", "Enabled", .F. )
    ENDIF


RETURN

*------------------------------------------------------------*
PROCEDURE MainCreditos()
*------------------------------------------------------------*
    ShellAbout( "", _GEN_PROGRAMA + CRLF + _GEN_AUTOR )
RETURN

*------------------------------------------------------------*
STATIC PROCEDURE ArbolChange()
*------------------------------------------------------------*
    // Declaracion de variables locales.-----------------------------------
    LOCAL nItem     AS NUMERIC
    LOCAL cBase     AS CHARACTER
    LOCAL cTabla    AS CHARACTER
    LOCAL oDb       AS OBJECT

    // Inicializacion de variables.----------------------------------------
    nItem := getProperty( "wndPrincipal", "treArbol", "Value" )

    // Ejecuta segun el item seleccionado.
    DO CASE
    CASE nItem >= 1 .AND. nItem <= 9                // Nodo principal.
        setMessage( "El servidor contiene " + AllTrim( Str( Len( oServidor:ListDBs ) ) ) + " bases de datos", 1 )

    CASE nItem >= 100 .AND. nItem <= 999            // Base de datos.

        // Selecciona la base de datos.................................
        cBase := getProperty( "wndPrincipal", "treArbol", "Item", nItem )
        oServidor:SelectDB( cBase )
        aTablas:=oServidor:ListTables()
        setMessage( "La base de datos " + Upper( cBase ) + " contiene " + AllTrim( Str( Len( aTablas ) ) ) + " tablas", 1 )

    CASE nItem >= 10000 .AND. nItem <= 99999        // Tabla.

        // Selecciona la base de datos.................................
        cTabla := getProperty( "wndPrincipal", "treArbol", "Item", nItem )
        nItem := Val( SubStr( AllTrim( Str( nItem ) ), 1, 3 ) )
        cBase := getProperty( "wndPrincipal", "treArbol", "Item", nItem )
        oServidor:SelectDB( cBase )

        // Actualiza la información de la tabla........................
        UpdateTable( cTabla )
        UpdateData( cTabla )

        // Actualiza la barra de estado................................
        oQuery := oServidor:Query( "select count(*) from " + cTabla )
        IF !(oServidor:lError)
            oQuery:Gotop()
            nRegistros := oServidor:GetRowsFromTable(cTabla)
            setMessage( "La tabla " + Upper( cTabla ) + " contiene " + Alltrim( Str(nRegistros) ) + " registros", 1 )
            oQuery:End()
        ELSE
            RETURN
        ENDIF
    ENDCASE

RETURN

*------------------------------------------------------------*
STATIC PROCEDURE Sql()
*------------------------------------------------------------*
    // Declaracion de variables.-------------------------------------------
    LOCAL nFila             AS NUMERIC
    LOCAL nColumna          AS NUMERIC

    // Inicialización de variables.----------------------------------------
    nFila    := getProperty( "wndPrincipal", "Row" ) + 87
    nColumna := getProperty( "wndPrincipal", "Col" )

    // Crea la ventana de dialogo.-----------------------------------------
    IF !isWindowDefined( wndSql )
        DEFINE window wndSql OBJ oWndSQL    ;
            at        nFila, nColumna       ;
            width     600                   ;
            height    330                   ;
            title     "Consulta SQL"        ;
            ON lostfocus {|| setMessage() }

            oWndSQL:Anchor := "TOPLEFTBOTTOMRIGHT"

            // Define la barra de herramientas.............................
            DEFINE splitbox
                DEFINE toolbar tbConsulta       ;
                    buttonsize 40, 32       ;
                    flat                    ;
                    righttext

                button tbbCerrar                                    ;
                    caption "Cerrar"                                ;
                    picture "BMP_32_CERRAR"                         ;
                    action {|| doMethod( "wndSql", "Release" ) }    ;
                    separator

                button tbbEjecutar                              ;
                    caption "Ejecutar consulta"             ;
                    picture "BMP_32_CONSULTAEX"             ;
                    action {|| UpdateSql() }
                END toolbar
            END splitbox

            // Defincicion del frame.......................................
            @ 55, 10 frame frmConsulta OBJ oFrameSQL      ;
                caption ""                      ;
                width   565                     ;
                height  230                     ;
                font    FNT_FRAME_NAME          ;
                size    FNT_FRAME_SIZE          ;
                bold

            oFrameSQL:Anchor := "TOPLEFTBOTTOMRIGHT"


            // Crea el cuadro de edicion...................................
            @ 75 , 25 editbox  edbSql OBJ oEdit                                                 ;
                width        540                                                                ;
                height       60                                                                 ;
                font         FNT_TEXT_NAME                                                      ;
                size         FNT_TEXT_SIZE                                                      ;
                ON gotfocus  {|| setControl( .T. ),                                             ;
                setMessage( "Introduzca la consulta y pulse ejecutar", 1 ) }      ;
                ON lostfocus {|| setControl( .F. ),                                             ;
                setMessage() }

            oEdit:Anchor := "TOPLEFTRIGHT"


            // Crea el grid................................................
            @ 150, 25 grid grdSql OBJ oGridSQL          ;
                width        540                        ;
                height       125                        ;
                headers      { "" }                     ;
                widths       { 100 }                    ;
                font         FNT_TEXT_NAME              ;
                size         FNT_TEXT_SIZE              ;
                ON gotfocus  {|| setControl( .T. ) }    ;
                ON lostfocus {|| setControl( .F. ) }

            oGridSQL:Anchor := "TOPLEFTBOTTOMRIGHT"

        END window

        // Activa el dialogo.--------------------------------------------------
        center window wndSql
        activate window wndSql
    ELSE
        // Actualiza las propiedades de fila y columna y muestra.......
        setProperty( "wndSql", "Row", nFila )
        setProperty( "wndSql", "Col", nColumna )
        doMethod( "wndSql", "Show" )
    ENDIF

    // Restraura el mensaje de la barra de estado.
    setMessage()

    RETURN

    STATIC PROCEDURE UpdateSql()

    // Declaracion de variables.-------------------------------------------
    LOCAL cConsulta         AS CHARACTER
    LOCAL oQuery            AS OBJECT
    LOCAL i                 AS NUMERIC
    LOCAL aCampos           AS ARRAY
    LOCAL aAnchos           AS ARRAY
    LOCAL aFila             AS ARRAY
    LOCAL nTiempo1          AS NUMERIC
    LOCAL nTiempo2          AS NUMERIC
    LOCAL cTexto            AS CHARACTER

    // Inicializacion de variables.----------------------------------------
    cConsulta := AllTrim( getProperty( "wndSql", "edbSql", "Value" ) )
    IF Empty( cConsulta )
        MsgInfo( "Escriba primero la consulta" )
        RETURN
    ENDIF

    // Crea la consulta y obtiene la informacion.--------------------------
    nTiempo1 := Seconds()
    oQuery := oServidor:Query( cConsulta )
    IF !(oServidor:lError)
        nTiempo2 := Seconds()
        cTexto := AllTrim( Str( oQuery:LastRec() ) ) + " registros "
        cTexto += " en " + PadR( nTiempo2 - nTiempo1, 6, "0" ) + " segundos"
        setProperty( "wndPrincipal", "StatusBar", "Item", 1, cTexto )
        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 ) * 14 ) < 150, oQuery:FieldLen( i ) * 14, 150 )
        NEXT
    ELSE
        RETURN
    ENDIF

    // Actualiza la informacion del grid.----------------------------------
    // Crea de nuevo el objeto grid........................................
    nWdt := oGridSQL:Width
    nHgt := oGridSQL:Height
    doMethod( "wndSql", "grdSql", "Release" )
    @ 150, 25 grid grdSql OBJ oGridSQL          ;
        of           wndSql                     ;
        width        nWdt                       ;
        height       nHgt                       ;
        headers      aCampos                    ;
        widths       aAnchos                    ;
        font         FNT_TEXT_NAME              ;
        size         FNT_TEXT_SIZE              ;
        ON gotfocus  {|| setControl( .T. ) }    ;
        ON lostfocus {|| setControl( .F. ) }

    oGridSQL:Anchor := "TOPLEFTBOTTOMRIGHT"

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

    FOR i := 1 TO oQuery:LastRec()
        IF !Empty( aFila[1] )
            wndSql.grdSql.additem( ItemChar( aFila[i], aTipos ) )
        ENDIF
    NEXT
    oQuery:End()

RETURN

*------------------------------------------------------------*
PROCEDURE setControl( lValue )
*------------------------------------------------------------*
    // Control de parametros.----------------------------------------------
    IF ValType( lValue ) != "L"
        lValue := .F.
    ENDIF

    // Cambia el color de fondo del control.-------------------------------
    IF lValue
        setProperty( thisWindow.Name, this.Name, "BackColor", { 255, 255, 200 } )
    ELSE
        setProperty( thisWindow.Name, this.Name, "BackColor", { 255, 255, 255 } )
    ENDIF

RETURN