|
|
The creation and retrieving of auto increment values
Version History
Problem descriptionAt the moment it is not possible to create auto increment columns and to retrieve the value of an auto increment column when a new row was inserted. Creation of auto increment columnsDue to the fact that there exists no standard way on defining auto increment columns when creating a new table, a general approach is not possible. For example: MySQL identifies an auto increment definition of a column with the key word “auto_increment”, the MS SQL Server on the other side uses the keyword “increment(1,1)” to identify the auto increment column. Retrieving of auto increment column's valuesThe retrieving of an auto increment column's value, when inserting a new row, is already defined in the JDBC API 3.0. There the statement supports an additional method which returns a result set to access the new inserted auto increment column's value. This result set also supports meta data. To check if the statement supports this mechanism, the database meta data is extended by new support method which simply returns true or false. SolutionTo allow a general approach of defining/retrieving auto increment columns/values, the user must have the possibility to define a statement which should be appended at the column definition when creating a table or should be called when fetching the new value of the auto increment column. Creation of auto increment columnsWhen creating tables for any kind of database, the current handling at the moment is that we use a general SDBCX layer for all database drivers which do not support the layer by their self. In other words the user has to take no action when the database driver supports already the SDBCX layer. For the case that the driver doesn't support the SDBCX layer and the user creates a new table, the generated SQL statement may look like this: CREATE TABLE “table1” (“id” INTEGER) The “CREATE TABLE” statement doesn't support any auto increment features, so that it isn't possible to create such columns. For MySQL this statement for an auto increment column looks like this: CREATE TABLE “table1” (“id” INTEGER AUTO_INCREMENT) Like this examples shows the user must have the opportunity to add the missing part of the statement to the “CREATE TABLE” statement. This can be done in two different ways which mustn't exclude each other.
The 3rd approach would be the one which allows the user the most flexibility to get a predefined string which he could change if necessary. The best place to store the value for the data source would be the data source administration dialog. To be more precise this would be implemented for the JDBC and the ODBC drivers (they don't support the SDBCX layer). All other drivers support the SDBCX layer. Retrieving of auto increment column's valuesAs mentioned in the problem description JDBC 3.0 already defines methods to accomplish the task but it is not possible just to forward this to the used drivers. Many drivers as also APIs like ODBC don't support this feature. To fit near all databases, a method has to be implemented that allows to fetch the new inserted auto increment value. Many databases support a special SQL statement which allows to fetch this value. Mostly these statements are database specific and not SQL conform. So a similar approach as for the creation of auto increment columns should be chosen. The data source should also store a SQL string which allows to fetch the auto increment values. This string will then be executed by the statement when asked for the auto increment values after inserting a new row. To be compatible as possible to any kind of database the SQL string, stored in the data source, must fulfill some additional specifications to be valid for different tables. In other words, the SQL string must contain some keywords like $column1 or $table1 which will be replaced with the auto increment column name or with the used table. A general string could already be inserted into the data source dialog which is not the best solution but works for a lot of drivers: “select count($column1)+1 from $table1” If the supporting of this feature should be disabled the string could be cleared by the user or an additional checkbox must be inserted into to the dialog to enable or disable this feature. When this feature is enabled the database meta data object implemented by the driver will return false when asking the general support method for the feature “AutoRetrievingEnabled” otherwise true. When true was returned the statement object implemented by the driver allows the creation of a result set to fetch the auto increment values. Data source administration dialog
Table design
|




