1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package org.apache.openoffice.comp.sdbc.dbtools.util;
22 
23 public class AutoRetrievingBase {
24     private String autoRetrievingStatement; // contains the statement which should be used when query for automatically generated values
25     private boolean autoRetrievingEnabled; // set to when we should allow to query for generated values
26 
isAutoRetrievingEnabled()27     public boolean isAutoRetrievingEnabled() {
28         return autoRetrievingEnabled;
29     }
30 
getAutoRetrievingStatement()31     public String getAutoRetrievingStatement() {
32         return autoRetrievingStatement;
33     }
34 
setAutoRetrievingEnabled(boolean autoRetrievingEnabled)35     public void setAutoRetrievingEnabled(boolean autoRetrievingEnabled) {
36         this.autoRetrievingEnabled = autoRetrievingEnabled;
37     }
38 
setAutoRetrievingStatement(String autoRetrivingStatement)39     public void setAutoRetrievingStatement(String autoRetrivingStatement) {
40         this.autoRetrievingStatement = autoRetrivingStatement;
41     }
42 
43     /** transform the statement to query for auto generated values
44      * @param  insertStatement
45      *     The "INSERT" statement, is used to query for column and table names
46      * @return
47      *     The transformed generated statement.
48      */
getTransformedGeneratedStatement(String insertStatement)49     public String getTransformedGeneratedStatement(String insertStatement) {
50         Osl.ensure(autoRetrievingEnabled, "Illegal call here. isAutoRetrievingEnabled() is false!");
51          insertStatement = insertStatement.toUpperCase();
52          String statement = "";
53          if (insertStatement.startsWith("INSERT")) {
54              statement = autoRetrievingStatement;
55 
56              int index = 0;
57              index = statement.indexOf("$column");
58              if (index != -1) {
59                  // we need a column
60                  // FIXME: do something?
61              }
62 
63              index = statement.indexOf("$table");
64              if (index != -1) {
65                  // we need a table
66                  int intoIndex = insertStatement.indexOf("INTO ");
67                  insertStatement = insertStatement.substring(intoIndex + 5);
68 
69                  int firstNonSpace;
70                  for (firstNonSpace = 0; firstNonSpace < insertStatement.length();) {
71                      int ch = insertStatement.codePointAt(firstNonSpace);
72                      if (ch != ' ') {
73                          break;
74                      }
75                      firstNonSpace += Character.charCount(ch);
76                  }
77                  insertStatement = insertStatement.substring(firstNonSpace);
78 
79                  int nextSpace = insertStatement.indexOf(' ');
80                  String tableName;
81                  if (nextSpace >= 0) {
82                      tableName = insertStatement.substring(0, nextSpace);
83                  } else {
84                      tableName = "";
85                  }
86 
87                  statement = statement.substring(0, index) + tableName + statement.substring(index + 6);
88              }
89          }
90          return statement;
91     }
92 }
93