X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b25ba5b701dbc822ed2aeaa9b8143a857f6de96e..6dbb903bb9410e2a2a977ad349ba7b4b3b12ac78:/docs/latex/wx/tdb.tex?ds=inline diff --git a/docs/latex/wx/tdb.tex b/docs/latex/wx/tdb.tex index db83cc7f50..a3c33edee1 100644 --- a/docs/latex/wx/tdb.tex +++ b/docs/latex/wx/tdb.tex @@ -14,7 +14,7 @@ Following is detailed overview of how to use the wxWindows ODBC classes - \helpr and \helpref{wxDbTable}{wxdbtable} and their associated functions. These are the ODBC classes donated by Remstar International, and are collectively referred to herein as the wxODBC classes. Since their initial inclusion with -wxWindows v2.x, they have become the standard wxWindows classes for database +wxWindows v2.x, they have become the recommended wxWindows classes for database access. An older version of some classes ported over from wxWindows v1.68 still exist @@ -55,6 +55,8 @@ users have successfully used the classes with the following datasources: \item INFORMIX \item VIRTUOSO \item DB2 +\item Interbase +\item Pervasive SQL \end{itemize} An up-to-date list can be obtained by looking in the comments of the function @@ -283,17 +285,17 @@ The wxWindows setup.h file has several settings in it pertaining to compiling the wxODBC classes. \begin{twocollist}\itemsep=0pt -\twocolitem{wxUSE_ODBC}{This must be set to 1 in order for the compiler to +\twocolitem{wxUSE\_ODBC}{This must be set to 1 in order for the compiler to compile the wxODBC classes. Without setting this to 1, there will be no access to any of the wxODBC classes. The default is 0.} -\twocolitem{wxODBC_FWD_ONLY_CURSORS}{When a new database connection is +\twocolitem{wxODBC\_FWD\_ONLY\_CURSORS}{When a new database connection is requested, this setting controls the default of whether the connection allows only forward scrolling cursors, or forward and backward scrolling cursors (see the section in "WHERE TO START" on cursors for more information on cursors). This default can be overridden by passing a second parameter to either the \helpref{wxDbGetConnection}{wxdbfunctions} or \helpref{wxDb constructor}{wxdbconstr}. The default is 1.} -\twocolitem{wxODBC_BACKWARD_COMPATABILITY}{Between v2.0 and 2.2, massive +\twocolitem{wxODBC\_BACKWARD\_COMPATABILITY}{Between v2.0 and 2.2, massive renaming efforts were done to the ODBC classes to get naming conventions similar to those used throughout wxWindows, as well as to preface all wxODBC classes names and functions with a wxDb preface. Because this renaming would @@ -356,23 +358,9 @@ Authorization string (password). A fourth piece of information, a default directory indicating where the data file is stored, is required for Text and dBase drivers for ODBC. -The wxWindows data structure "wxDbConnectInf" exists for holding all of these +The wxWindows data class wxDbConnectInf exists for holding all of these values, plus some others that may be desired. -\begin{verbatim} - wxDbConnectInf - HENV Henv; - char Dsn[SQL_MAX_DSN_LENGTH+1] // DataSource Name - char Uid[20+1] // User ID - char AuthStr[20+1] // Authorization (password) - - // Optional needed for some databases like dBase, FoxPro and TEXT files - char defaultDir[wxDB_PATH_MAX] // Directory that db file resides in - - char description[SQL_MAX_DSN_LENGTH+1]; - char fileType[SQL_MAX_DSN_LENGTH+1]; -\end{verbatim} - The 'Henv' member is the environment handle used to access memory for use by the ODBC driver. Use of this member is described below in the "Getting a Connection to the Datasource" section. @@ -424,17 +412,18 @@ member. \begin{verbatim} wxDbConnectInf DbConnectInf; - wxStrcpy(DbConnectInf.Dsn, "Your DSN"); - wxStrcpy(DbConnectInf.Uid, "YourUserName"); - wxStrcpy(DbConnectInf.AuthStr, "YourUserPassword"); - wxStrcpy(DbConnectInf.defaultDir, ""); + DbConnectInf.SetDsn,"MyDSN"); + DbConnectInf.SetUserID,"MyUserName"); + DbConnectInf.SetPassword("MyPassword"); + DbConnectInf.SetDefaultDir(""); \end{verbatim} To allocate an environment handle for the ODBC connection to use, the -standard SQLAllocEnv() function is used. +wxDbConnectInf class has a datasource independent method for creating +the necessary handle: \begin{verbatim} - if (SQLAllocEnv(&DbConnectInf.Henv) != SQL_SUCCESS) + if (DbConnectInf.AllocHenv()) { wxMessageBox("Unable to allocate an ODBC environment handle", "DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION); @@ -442,23 +431,38 @@ standard SQLAllocEnv() function is used. } \end{verbatim} -When the SQLAllocEnv() function is called successfully, a value of -SQL_SUCCESS will be returned. Anything other return value is a failure -to create the handle, and the handle will be undefined. +When the wxDbConnectInf::AllocHenv() function is called successfully, a +value of TRUE will be returned. A value of FALSE means allocation failed, +and the handle will be undefined. + +A shorter form of doing the above steps is encapsulated into the +long form of the constructor for wxDbConnectInf. + +\begin{verbatim} + wxDbConnectInf *DbConnectInf; + + DbConnectInf = new wxDbConnectInf(NULL, "MyDSN", "MyUserName", + "MyPassword", ""); +\end{verbatim} + +This shorthand form of initializing the constructor passes a NULL for the SQL +environment handle, telling the constructor to allocate a handle during +construction. This handle is also managed for the life of wxDbConnectInf +instance, and is freed automatically upon destruction of the instance. -Once the members of the wxDbConnectInf structure are initialized, you are -now ready to connect to the datasource. +Once the wxDbConnectInf instance is initialized, you are ready to +connect to the datasource. To manually create datasource connections, you must create a wxDb instance, and then open it. \begin{verbatim} - wxDb *db = new wxDb(DbConnectInf.Henv); - opened = db->Open(DbConnectInf.Dsn, DbConnectInf.Uid, - DbConnectInf.AuthStr); + wxDb *db = new wxDb(DbConnectInf->GetHenv()); + + opened = db->Open(DbConnectInf); \end{verbatim} -The first line does the house keeping needed to initialize everything all +The first line does the house keeping needed to initialize all the members of the wxDb class. The second line actually sends the request to the ODBC driver to open a connection to its associated datasource using the parameters supplied in the call to \helpref{wxDb::Open}{wxdbopen}. @@ -475,7 +479,7 @@ a connection to a datasource, simply call it with a single parameter of the type wxDbConnectInf: \begin{verbatim} - db = wxDbGetConnection(&DbConnectInf); + db = wxDbGetConnection(DbConnectInf); \end{verbatim} The wxDb pointer that is returned is both initialized and opened. If @@ -521,7 +525,7 @@ queried, rather than re-querying the datasource for all those same settings. One final note on creating a connection. When a connection is created, it will default to only allowing cursor scrolling to be either forward only, or both backward and forward scrolling cursors. The default behavior is -determined by the setting "wxODBC_FWD_ONLY_CURSORS" in setup.h when you +determined by the setting {\tt wxODBC\_FWD\_ONLY\_CURSORS} in setup.h when you compile the wxWindows library. The library default is to only support forward scrolling cursors only, though this can be overridden by parameters for wxDb() constructor or the \helpref{wxDbGetConnection}{wxdbfunctions} @@ -546,8 +550,8 @@ The first step in accessing data in a datasource's tables via the wxDbTable class is to create a wxDbTable instance. \begin{verbatim} - table = new wxDbTable(db, tableName, numTableColumns, 0, - !wxDB_QUERY_ONLY, NULL); + table = new wxDbTable(db, tableName, numTableColumns, "", + !wxDB_QUERY_ONLY, ""); \end{verbatim} When you create the instance, you indicate the previously established @@ -562,11 +566,11 @@ connecting to the datasource. Each of the above parameters are described in detail in the wxDbTable class' description, but one special note here about the fifth parameter - queryOnly setting. If a wxDbTable instance is created as -wxDB_QUERY_ONLY, then no inserts/deletes/updates are able to be performed +{\tt wxDB\_QUERY\_ONLY}, then no inserts/deletes/updates are able to be performed using this instance of the wxDbTable. Any calls to \helpref{wxDb::CommitTrans}{wxdbcommittrans} or \helpref{wxDb::RollbackTrans}{wxdbrollbacktrans} against the datasource connection used by this wxDbTable instance are ignored by this instance. If -the wxDbTable instance is created with "!wxDB_QUERY_ONLY" as shown above, +the wxDbTable instance is created with {\tt !wxDB\_QUERY\_ONLY} as shown above, then all the cursors and other overhead associated with being able to insert/update/delete data in the table are created, and thereby those operations can then be performed against the associated table with this @@ -613,7 +617,7 @@ variables contain valid data. It is not necessary to define column definitions for columns whose data is not going to be returned to the client. For example, if you want to query the datasource for all users with a first name of 'GEORGE', but you only want -the list of last names associated with those rows (why return the FIRST_NAME +the list of last names associated with those rows (why return the FIRST\_NAME column every time when you already know it is 'GEORGE'), you would only have needed to define one column above. @@ -630,7 +634,7 @@ house keeping of checking that the specified table exists, that the current connected user has at least SELECT privileges for accessing the table, setting up the requisite cursors, binding columns and cursors, and constructing the default INSERT statement that is used when a new row is -inserted into the table (non-wxDB_QUERY_ONLY tables only). +inserted into the table (non-wxDB\_QUERY\_ONLY tables only). \begin{verbatim} if (!table->Open()) @@ -669,11 +673,11 @@ it where to get the data from, and in what sequence we want the data returned. \end{verbatim} The above lines will be used to tell the datasource to return in the result -all the rows in the table whose column "FIRST_NAME" contains the name +all the rows in the table whose column "FIRST\_NAME" contains the name 'GEORGE' (note the required use of the single quote around the string literal) and that the result set will return the rows sorted by ascending last names (ascending is the default, and can be overridden with the -"DESC" keyword for datasources that support it - "LAST_NAME DESC"). +"DESC" keyword for datasources that support it - "LAST\_NAME DESC"). Specifying a blank WHERE clause will result in the result set containing all rows in the datasource. @@ -843,13 +847,19 @@ this example it was stored in "DbConnectInf.Henv") have been closed, then it is safe to release the environment handle: \begin{verbatim} - SQLFreeEnv(DbConnectInf.Henv); + DbConnectInf->FreeHenv()); +\end{verbatim} +Or, if the long form of the constructor was used and the constructor was allowed +to allocate its own SQL environment handle, leaving scope or destruction of the +wxDbConnectInf will free the handle automatically. + +\begin{verbatim} + delete DbConnectInf; \end{verbatim} \normalbox{Remember to never release this environment handle if there are any -connections still using the handle. -} +connections still using the handle.} \subsection{wxODBC - Known Issues}\label{wxodbcknownissues} @@ -899,7 +909,7 @@ NOTE: dBase is not a true ODBC datasource. You only have access to as much functionality as the driver can emulate. \begin{itemize}\itemsep=0pt -\item Does not support the SQL_TIMESTAMP structure +\item Does not support the SQL\_TIMESTAMP structure \item Supports only one cursor and one connect (apparently? with Microsoft driver only?) \item Does not automatically create the primary index if the 'keyField' param of SetColDef is TRUE. The user must create ALL indexes from their program with calls to \helpref{wxDbTable::CreateIndex}{wxdbtablecreateindex} \item Table names can only be 8 characters long @@ -920,7 +930,7 @@ functionality as the driver can emulate. \item Maximum row size is somewhere in the neighborhood of 1920 bytes \end{itemize} -{\it MY_SQL} +{\it mySQL} \begin{itemize}\itemsep=0pt \item If a column is part of the Primary Key, the column cannot be NULL. \item Cannot support selecting for update [\helpref{wxDbTable::CanSelectForUpdate}{wxdbtablecanselectforupdate}]. Always returns FALSE. @@ -942,10 +952,9 @@ functionality as the driver can emulate. {\bf UNICODE with wxODBC classes} -Currently there is no support for Unicode with the wxODBC classes. In -fact, Unicode builds must be disabled if wxWindows is compiled with -wxUSE_ODBC set to 1 in setup.h - +The ODBC classes support for Unicode is yet in early experimental stage and +hasn't been tested extensively. It might work for you or it might not: please +report the bugs/problems you have encountered in the latter case. \subsection{wxODBC - Sample Code #1}\label{wxodbcsamplecode1} @@ -959,42 +968,37 @@ NOTE: Not all error trapping is shown here, to reduce the size of the code and to make it more easily readable. \begin{verbatim} -struct wxDbConnectInf DbConnectInf; +wxDbConnectInf *DbConnectInf = NULL; wxDb *db = NULL; // The database connection wxDbTable *table = NULL; // The data table to access -char FirstName[50+1]; // buffer for data from column "FIRST_NAME" -char LastName[50+1]; // buffer for data from column "LAST_NAME" +wxChar FirstName[50+1]; // buffer for data from column "FIRST_NAME" +wxChar LastName[50+1]; // buffer for data from column "LAST_NAME" bool errorOccured = FALSE; -const char tableName[] = "CONTACTS"; -const int numTableColumns = 2; // Number of bound columns +const wxChar tableName[] = "CONTACTS"; +const UWORD numTableColumns = 2; // Number of bound columns FirstName[0] = 0; LastName[0] = 0; -// Initialize the ODBC Environment for database operations -// and store the handle in 'DbConnectInf.Henv' -if (SQLAllocEnv(&DbConnectInf.Henv) != SQL_SUCCESS) +DbConnectInf = new wxDbConnectInf(NULL,"MyDSN","MyUserName", "MyPassword"); + +if (!DbConnectInf || !DbConnectInf->GetHenv()) { -wxMessageBox("Unable to allocate an ODBC environment handle", - "DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION); -return; + wxMessageBox("Unable to allocate an ODBC environment handle", + "DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION); + return; } -wxStrcpy(DbConnectInf.Dsn, "Your DSN"); -wxStrcpy(DbConnectInf.Uid, "YourUserName"); -wxStrcpy(DbConnectInf.AuthStr, "YourUserPassword"); -wxStrcpy(DbConnectInf.defaultDir, ""); - // Get a database connection from the cached connections -db = wxDbGetConnection(&DbConnectInf); +db = wxDbGetConnection(DbConnectInf); // Create the table connection -table = new wxDbTable(db, tableName, numTableColumns, 0, - !wxDB_QUERY_ONLY, NULL); +table = new wxDbTable(db, tableName, numTableColumns, "", + !wxDB_QUERY_ONLY, ""); // // Bind the columns that you wish to retrieve. Note that there must be @@ -1068,12 +1072,11 @@ wxDbCloseConnections(); // Release the environment handle that was created // for use with the ODBC datasource connections -SQLFreeEnv(DbConnectInf.Henv); +delete DbConnectInf; \end{verbatim} - -\subsection{wxDatabase ODBC class overview [DEPRECATED]}\label{wxodbcoverview} +\subsection{wxDatabase ODBC class overview [DEPRECATED]}\label{oldwxodbcoverview} Classes: \helpref{wxDatabase}{wxdatabase}, \helpref{wxRecordSet}{wxrecordset}, \helpref{wxQueryCol}{wxquerycol}, \rtfsp\helpref{wxQueryField}{wxqueryfield} @@ -1305,31 +1308,31 @@ for selection and the columns returned may be specified. Examples: -\verb$SELECT * FROM Book$ +\tt{SELECT * FROM Book} Selects all rows and columns from table Book. -\verb$SELECT Title, RetailPriceAmount FROM Book WHERE RetailPriceAmount > 20.0$ +\tt{SELECT Title, RetailPriceAmount FROM Book WHERE RetailPriceAmount > 20.0} Selects columns Title and RetailPriceAmount from table Book, returning only the rows that match the WHERE clause. -\verb$SELECT * FROM Book WHERE CatCode = 'LL' OR CatCode = 'RR'$ +\tt{SELECT * FROM Book WHERE CatCode = 'LL' OR CatCode = 'RR'} Selects all columns from table Book, returning only the rows that match the WHERE clause. -\verb$SELECT * FROM Book WHERE CatCode IS NULL$ +\tt{SELECT * FROM Book WHERE CatCode IS NULL} Selects all columns from table Book, returning only rows where the CatCode column is NULL. -\verb$SELECT * FROM Book ORDER BY Title$ +\tt{SELECT * FROM Book ORDER BY Title} Selects all columns from table Book, ordering by Title, in ascending order. To specify descending order, add DESC after the ORDER BY Title clause. -\verb$SELECT Title FROM Book WHERE RetailPriceAmount >= 20.0 AND RetailPriceAmount <= 35.0$ +\tt{SELECT Title FROM Book WHERE RetailPriceAmount >= 20.0 AND RetailPriceAmount <= 35.0} Selects records where RetailPriceAmount conforms to the WHERE expression. @@ -1339,7 +1342,7 @@ Updates records in a table. Example: -\verb$UPDATE Incident SET X = 123 WHERE ASSET = 'BD34'$ +\tt{UPDATE Incident SET X = 123 WHERE ASSET = 'BD34'} This example sets a field in column `X' to the number 123, for the record where the column ASSET has the value `BD34'.