KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > sqlserver > SQLServerBatchAction


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba.sqlserver;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.apache.cayenne.access.OperationObserver;
29 import org.apache.cayenne.access.QueryLogger;
30 import org.apache.cayenne.access.jdbc.BatchAction;
31 import org.apache.cayenne.dba.DbAdapter;
32 import org.apache.cayenne.map.DbAttribute;
33 import org.apache.cayenne.map.EntityResolver;
34 import org.apache.cayenne.query.BatchQuery;
35 import org.apache.cayenne.query.InsertBatchQuery;
36
37 /**
38  * @since 1.2
39  * @author Andrus Adamchik
40  */

41 public class SQLServerBatchAction extends BatchAction {
42
43     public SQLServerBatchAction(BatchQuery batchQuery, DbAdapter adapter,
44             EntityResolver entityResolver) {
45         super(batchQuery, adapter, entityResolver);
46     }
47
48     public void performAction(Connection JavaDoc connection, OperationObserver observer)
49             throws SQLException JavaDoc, Exception JavaDoc {
50
51         // this condition checks if identity columns are present in the query and adapter
52
// is not ready to process them... e.g. if we are using a MS driver...
53
boolean identityOverride = expectsToOverrideIdentityColumns();
54         if (identityOverride) {
55             setIdentityInsert(connection, true);
56         }
57
58         try {
59             super.performAction(connection, observer);
60         }
61         finally {
62
63             // important: turn off IDENTITY_INSERT as SQL Server won't be able to process
64
// other identity columns in the same transaction
65

66             // TODO: if an error happens here this would mask the parent error
67
if (identityOverride) {
68                 setIdentityInsert(connection, false);
69             }
70         }
71     }
72     
73     protected void setIdentityInsert(Connection JavaDoc connection, boolean on)
74             throws SQLException JavaDoc {
75
76         String JavaDoc flag = on ? " ON" : " OFF";
77         String JavaDoc configSQL = "SET IDENTITY_INSERT "
78                 + query.getDbEntity().getFullyQualifiedName()
79                 + flag;
80
81         QueryLogger.logQuery(configSQL, Collections.EMPTY_LIST);
82
83         Statement JavaDoc statement = connection.createStatement();
84         try {
85             statement.execute(configSQL);
86         }
87         finally {
88             try {
89                 statement.close();
90             }
91             catch (Exception JavaDoc e) {
92             }
93         }
94     }
95  
96     /**
97      * Returns whether a table has identity columns.
98      */

99     protected boolean expectsToOverrideIdentityColumns() {
100         // jTDS driver supports identity columns, no need for tricks...
101
if (getAdapter().supportsGeneratedKeys()) {
102             return false;
103         }
104
105         if (!(query instanceof InsertBatchQuery) || query.getDbEntity() == null) {
106             return false;
107         }
108
109         // find identity attributes
110
Iterator JavaDoc it = query.getDbEntity().getAttributes().iterator();
111         while (it.hasNext()) {
112             DbAttribute attribute = (DbAttribute) it.next();
113             if (attribute.isGenerated()) {
114                 return true;
115             }
116         }
117
118         return false;
119     }
120 }
121
Popular Tags