' com_ado.pb — ADO database access simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL connState AS LONG
    LOCAL recordCount AS LONG
    LOCAL fieldCount AS LONG
    LOCAL connStr AS STRING
    LOCAL tableName AS STRING
    LOCAL queryType AS LONG
    LOCAL rowData(100) AS LONG
    LOCAL rowID(100) AS LONG
    LOCAL rowValues(100) AS LONG
    LOCAL numRows AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL totalValue AS LONG
    LOCAL connResult AS LONG
    LOCAL queryResult AS LONG
    LOCAL colSum AS LONG
    LOCAL avgVal AS LONG
    LOCAL maxVal AS LONG
    LOCAL minVal AS LONG
    LOCAL iter AS LONG
    LOCAL numQueries AS LONG

    PRINT "=== ADO Database Simulation ==="
    connStr = "Provider=SQLOLEDB;Data Source=localhost;"
    connState = 1

    IF connState = 1 THEN
        PRINT "Connected: " + connStr
    ELSE
        PRINT "Connection failed"
    END IF

    tableName = "Employees"
    numRows = 5

    rowID(0) = 1
    rowValues(0) = 50000
    rowID(1) = 2
    rowValues(1) = 62000
    rowID(2) = 3
    rowValues(2) = 48000
    rowID(3) = 4
    rowValues(3) = 75000
    rowID(4) = 5
    rowValues(4) = 55000
    numRows = 5

    PRINT "Table: " + tableName + " Rows: " + STR$(numRows)

    FOR numQueries = 1 TO 4
        queryType = numQueries

        IF queryType = 1 THEN
            PRINT "  SELECT * FROM " + tableName
            FOR i = 0 TO numRows - 1
                PRINT "    Row " + STR$(rowID(i)) + ": $" + STR$(rowValues(i))
                totalValue = totalValue + rowValues(i)
            NEXT i
        END IF

        IF queryType = 2 THEN
            PRINT "  SELECT SUM(salary) FROM " + tableName
            colSum = 0
            FOR i = 0 TO numRows - 1
                colSum = colSum + rowValues(i)
            NEXT i
            PRINT "    Sum: $" + STR$(colSum)
        END IF

        IF queryType = 3 THEN
            PRINT "  SELECT AVG(salary) FROM " + tableName
            colSum = 0
            FOR i = 0 TO numRows - 1
                colSum = colSum + rowValues(i)
            NEXT i
            avgVal = colSum / numRows
            PRINT "    Avg: $" + STR$(avgVal)
        END IF

        IF queryType = 4 THEN
            PRINT "  SELECT MAX(salary), MIN(salary) FROM " + tableName
            maxVal = rowValues(0)
            minVal = rowValues(0)
            FOR i = 1 TO numRows - 1
                IF rowValues(i) > maxVal THEN maxVal = rowValues(i)
                IF rowValues(i) < minVal THEN minVal = rowValues(i)
            NEXT i
            PRINT "    Max: $" + STR$(maxVal) + " Min: $" + STR$(minVal)
        END IF
    NEXT numQueries

    PRINT "Closing connection"
    connState = 0
    PRINT "=== End ADO ==="
    FUNCTION = 0
END FUNCTION
