Source Code Cross Referenced for SequenceManagerMSSQLGuidImpl.java in  » Database-ORM » db-ojb » org » apache » ojb » broker » util » sequence » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » db ojb » org.apache.ojb.broker.util.sequence 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.broker.util.sequence;
002:
003:        /* Copyright 2003-2005 The Apache Software Foundation
004:         *
005:         * Licensed under the Apache License, Version 2.0 (the "License");
006:         * you may not use this file except in compliance with the License.
007:         * You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        import org.apache.commons.lang.SystemUtils;
019:        import org.apache.ojb.broker.PersistenceBroker;
020:        import org.apache.ojb.broker.PersistenceBrokerException;
021:        import org.apache.ojb.broker.metadata.JdbcTypesHelper;
022:        import org.apache.ojb.broker.util.logging.LoggerFactory;
023:        import org.apache.ojb.broker.accesslayer.ResultSetAndStatement;
024:        import org.apache.ojb.broker.metadata.FieldDescriptor;
025:        import org.apache.ojb.broker.metadata.JdbcType;
026:        import org.apache.ojb.broker.query.Query;
027:
028:        import java.sql.SQLException;
029:
030:        /**
031:         * An Implementation Class that will retrieve a valid new value
032:         * for a PK field that is of type 'uniqueidentifier'. Since values
033:         * for these types are generated through a 'newid()' call to
034:         * MSSQL Server, this class is only valid for MSSQL Server 7.0 and up.
035:         * <br/>
036:         * This SequenceManager can be used for any classes that have their PK
037:         * defined as a 'uniqueidetifier'
038:         *
039:         * @author <a href="mailto:aclute825@hotmail.com">Andrew Clute</a>
040:         * @version $Id: SequenceManagerMSSQLGuidImpl.java,v 1.4.2.2 2005/12/21 22:28:41 tomdz Exp $
041:         */
042:        public class SequenceManagerMSSQLGuidImpl extends
043:                AbstractSequenceManager {
044:            private static final JdbcType JDBC_TYPE_VARCHAR = JdbcTypesHelper
045:                    .getJdbcTypeByName("varchar");
046:
047:            /**
048:             * Constructor used by
049:             * {@link org.apache.ojb.broker.util.sequence.SequenceManagerFactory}
050:             *
051:             * @param broker  PB instance to perform the
052:             * id generation.
053:             */
054:            public SequenceManagerMSSQLGuidImpl(PersistenceBroker broker) {
055:                super (broker);
056:            }
057:
058:            public Object getUniqueValue(FieldDescriptor field)
059:                    throws SequenceManagerException {
060:                // only works for VARCHAR fields
061:                if (!field.getJdbcType().equals(JDBC_TYPE_VARCHAR)) {
062:                    throw new SequenceManagerException(
063:                            "This implementation only works with fields defined"
064:                                    + " as VARCHAR, but given field was "
065:                                    + (field != null ? field.getJdbcType()
066:                                            : null));
067:                }
068:                Object result = getUniqueString(field);
069:                // perform a sql to java conversion here, so that clients do
070:                // not see any db specific values
071:                result = field.getFieldConversion().sqlToJava(result);
072:                return result;
073:            }
074:
075:            /**
076:             * returns a unique String for given field.
077:             * the returned uid is unique accross all tables.
078:             */
079:            protected String getUniqueString(FieldDescriptor field)
080:                    throws SequenceManagerException {
081:                ResultSetAndStatement rsStmt = null;
082:                String returnValue = null;
083:                try {
084:                    rsStmt = getBrokerForClass().serviceJdbcAccess()
085:                            .executeSQL("select newid()",
086:                                    field.getClassDescriptor(),
087:                                    Query.NOT_SCROLLABLE);
088:                    if (rsStmt.m_rs.next()) {
089:                        returnValue = rsStmt.m_rs.getString(1);
090:                    } else {
091:                        LoggerFactory.getDefaultLogger().error(
092:                                this .getClass()
093:                                        + ": Can't lookup new oid for field "
094:                                        + field);
095:                    }
096:                } catch (PersistenceBrokerException e) {
097:                    throw new SequenceManagerException(e);
098:                } catch (SQLException e) {
099:                    throw new SequenceManagerException(e);
100:                }
101:
102:                finally {
103:                    // close the used resources
104:                    if (rsStmt != null)
105:                        rsStmt.close();
106:                }
107:                return returnValue;
108:            }
109:
110:            /**
111:             * Returns a new unique int for the given Class and fieldname.
112:             */
113:            protected int getUniqueId(FieldDescriptor field)
114:                    throws SequenceManagerException {
115:                throw new SequenceManagerException(
116:                        SystemUtils.LINE_SEPARATOR
117:                                + "Failure attempting to retrieve a Guid for a field that is an int -- field should be returned as a VARCHAR");
118:            }
119:
120:            /**
121:             * Returns a new unique int for the given Class and fieldname.
122:             */
123:            protected long getUniqueLong(FieldDescriptor field)
124:                    throws SequenceManagerException {
125:                throw new SequenceManagerException(
126:                        SystemUtils.LINE_SEPARATOR
127:                                + "Failure attempting to retrieve a Guid for a field that is a long -- field should be returned as a VARCHAR");
128:            }
129:
130:            // TODO: never used? remove?
131:            //    /**
132:            //     * returns a unique Object for class clazz and field fieldName.
133:            //     * the returned Object is unique accross all tables in the extent of clazz.
134:            //     */
135:            //    protected Object getUniqueObject(FieldDescriptor field) throws SequenceManagerException
136:            //    {
137:            //        return getUniqueString(field);
138:            //    }
139:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.