home *** CD-ROM | disk | FTP | other *** search
- * main program for the FoxPro Connectivity Kit Sample Application
- * Microsoft Corp. (c) 1993
-
- PRIVATE ALL
- SET PROCEDURE TO mainprog
-
- * save settings
- IF SET("TALK") = "ON"
- SET TALK OFF
- oldtalk = "ON"
- ELSE
- oldtalk = "OFF"
- ENDIF
- oldcompat = SET("COMPATIBLE")
- SET COMPATIBLE OFF
- oldarea = SELECT()
- oldsafe = SET("SAFETY")
- SET SAFETY OFF
-
- ON KEY LABEL ALT+F4
- ON KEY LABEL ALT+F4
-
- * use the library with all of the connectivity functions
- DO CASE
- CASE _DOS
- SET LIBRARY TO LOCFILE("fpsql.plb","PLB","Where is fpsql?") ADDITIVE
- CASE _WINDOWS
- SET LIBRARY TO LOCFILE("fpsql.fll","FLL","Where is fpsql?") ADDITIVE
- ENDCASE
-
- * set up global arrays for query builder
- DIMENSION tablelist[1]
- DIMENSION fromlist[1]
- DIMENSION columnlist[1]
- DIMENSION selectlist[1]
- DIMENSION wherelist[1]
-
- * Global Variables
- mess_str = ""
- m.asynch = 0
- ckhandle = 0
- sqlselect = ""
-
- * determine which work areas are currently in use
- badnews = .f.
- on error badnews = .t.
- areacnt = 1
- do while ((not badnews) and (areacnt <= 225))
- DIMENSION used_array(areacnt)
- used_array(areacnt) = used(areacnt)
- areacnt = areacnt + 1
- enddo
- closem = .t.
- on error
-
- PUSH MENU _MSYSMENU
- * put up main menu
- DO sample.mpr
-
- * attempt to make an initial connection
- DO connect.spr
-
- * if the connection was not successful, punt
- IF ckhandle = 0
- RELEASE LIBRARY fpsql
- SET SYSMENU TO DEFAULT
- RETURN
- ENDIF
-
- * foundation read
- exitapp = .F.
- READ CYCLE VALID exitapp
-
- * close tables if desired
- if closem
- for i = 1 to alen(used_array)
- if not used_array(i)
- if used(i)
- select i
- use
- endif
- endif
- endfor
- endif
-
- * reset environment
- SELECT (oldarea)
- SET COMPATIBLE &oldcompat
- SET SAFETY &oldsafe
- SET TALK &oldtalk
- POP MENU _MSYSMENU
-
- * end of mainprog
-
-
- * build SQL statement from the selections made in build query
- FUNCTION buildsql
- PRIVATE lenlist, sqlselect
-
- sqlselect = "SELECT "
- lenlist = ALEN(selectlist) - 1
-
- * if there was at least one field selected, select 'em
- * otherwise select '*'
- IF (lenlist > 0)
- sqlselect = sqlselect + selectlist[1]
- FOR i = 2 TO lenlist
- sqlselect = sqlselect + ", " + selectlist[i] + CHR(13)
- ENDFOR
- ELSE
- sqlselect = sqlselect + "*"
- ENDIF
-
- * if there was at least table selected, use 'em
- * otherwise select return null (no statement possible)
- lenlist = ALEN(fromlist) - 1
- IF (lenlist > 0)
- sqlselect = sqlselect + " FROM "
- sqlselect = sqlselect + fromlist[1]
- FOR i = 2 TO lenlist
- sqlselect = sqlselect + ", " + fromlist[i] + CHR(13)
- ENDFOR
- ELSE
- RETURN ""
- ENDIF
-
- * if there was at least one where statement entered, use 'em
- lenlist = ALEN(wherelist) - 1
- IF (lenlist > 0)
- sqlselect = sqlselect + " WHERE " + wherelist[1]
- FOR i = 2 TO lenlist
- sqlselect = sqlselect + " " + wherelist[i] + CHR(13)
- ENDFOR
- ENDIF
-
- RETURN sqlselect
-
-
- * execute the query passed in the parameter
- PROCEDURE xquery
- PARAMETER stmt
- PRIVATE retcode
-
- * nothing to do
- IF EMPTY(stmt)
- WAIT WINDOW "No query to execute!" NOWAIT
- RETURN
- ENDIF
-
- * define a window to put the results in
- DEFINE WINDOW results ;
- AT 0.000, 0.000 ;
- SIZE 19.000,77.000 ;
- TITLE "Results" ;
- FONT "MS Sans Serif", 8 ;
- STYLE "B" ;
- FLOAT ;
- CLOSE ;
- SHADOW ;
- MINIMIZE ;
- ZOOM GROW ;
- COLOR SCHEME 10 ;
- SYSTEM
- MOVE WINDOW results CENTER
-
- IF (ckhandle == 0)
- * this sould not happen
- ?? CHR(7)
- WAIT WINDOW "No server connection"
- RETURN
- ELSE
- SELE 0
- wait window "Executing query..." nowait
- * in asynchronous mode, wait for the results
- IF (m.asynch == 1) && Asynch
- retcode = 0
- DO WHILE (retcode == 0)
- retcode = dbexec(ckhandle, stmt)
- WAIT WINDOW "Query executing" TIMEOUT 1
- ENDDO
-
- * if an error is returned, display it
- * otherwise display results.
- IF (retcode < 0)
- DO repoerro.spr WITH retcode
- ELSE
- IF USED()
- GO TOP
- BROW WINDOW results
- ELSE
- WAIT WINDOW "Done!" NOWAIT
- ENDIF
- ENDIF
- ELSE && Synchronous
- * in asynchronous mode, no need to wait for the results
- retcode = dbexec(ckhandle, stmt)
- IF (retcode < 0)
- DO repoerro.spr WITH retcode
- ELSE
- IF USED()
- GO TOP
- BROW WINDOW results
- ELSE
- WAIT WINDOW "Done!" NOWAIT
- ENDIF
- ENDIF
- ENDIF
- ENDIF
- RETURN
-
- * disconnect from current data share
- PROCEDURE disconn
- PRIVATE retcode
-
- retcode = dbdisconn(ckhandle)
- IF (retcode < 0)
- DO repoerro.spr WITH retcode
- ENDIF
- * clear global variables
- ckhandle = 0
- DIMENSION tablelist[1]
- DIMENSION fromlist[1]
- DIMENSION columnlist[1]
- DIMENSION selectlist[1]
- DIMENSION wherelist[1]
- SHOW MENU _msysmenu
- WAIT WINDOW "Disconnected From "+mess_str NOWAIT
- mess_str = "Data Source: none"
- SET MESSAGE TO mess_str
-
- RETURN
-
-
- * build a list of tables for the current database
- PROCEDURE buildtlist
- PRIVATE ntables, nobjects, retcode
- IF (ckhandle == 0)
- USE tables
- ELSE
- IF (m.asynch == 1) && Asynch
- retcode = 0
- DO WHILE (retcode == 0)
- retcode = dbtables(ckhandle)
- ENDDO
- ELSE
- retcode = dbtables(ckhandle)
- ENDIF
- IF (retcode < 0)
- DO repoerro.spr WITH retcode
- DIMENSION tablelist[1]
- return
- ENDIF
- ENDIF
- ntables = 0
- nobjects = RECCOUNT()
- IF (nobjects != 0)
- DIMENSION tablelist[nObjects]
- SCAN FOR INLIST(table_type, "TABLE", "VIEW","U","V")
- ntables = ntables + 1
- tablelist[nTables] = ALLTRIM(table_name)
- ENDSCAN
- ENDIF
- USE
- DIMENSION tablelist[nTables + 1]
- RETURN
-
- FUNCTION yes_no
- * This function simply allows the user to
- * answer yes or no to a prompt. The answer
- * is then passed back as a logical.
-
- PARAMETER cmessage, cmsghead
-
- m.choice = 1
- DO yesno.spr WITH cmsghead,cmessage
- RETURN IIF(m.choice = 1,.T.,.F.)
-