Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c59903b5a170bca0190152445b1d5ae4795ea494 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/***************************************************************************
 * Copyright (c) 2008 - 2008 Martin Taal and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Martin Taal - initial api
 **************************************************************************/
package org.eclipse.emf.cdo.server.internal.hibernate.tuplizer;

import org.eclipse.emf.cdo.protocol.id.CDOID;
import org.eclipse.emf.cdo.protocol.id.CDOIDTemp;
import org.eclipse.emf.cdo.protocol.revision.CDORevision;
import org.eclipse.emf.cdo.server.hibernate.CDOIDHibernate;
import org.eclipse.emf.cdo.server.internal.hibernate.CDOHibernateUtil;
import org.eclipse.emf.cdo.server.internal.hibernate.CDOIDHibernateImpl;

import org.hibernate.Hibernate;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
 * Persists a cdoid in the db.
 */
public class CDOIDUserType implements UserType
{

  // second varchar is just for informational purposes
  private static final int[] SQL_TYPES = { Types.VARCHAR, Types.VARCHAR, Types.VARBINARY };

  public int[] sqlTypes()
  {
    return SQL_TYPES;
  }

  public Class<?> returnedClass()
  {
    return CDOID.class;
  }

  public boolean isMutable()
  {
    return false;
  }

  public CDOIDUserType()
  {
  }

  public Object deepCopy(Object value)
  {
    return value;
  }

  public boolean equals(Object x, Object y)
  {
    if (x == y)
    {
      return true;
    }
    if (x == null || y == null)
    {
      return false;
    }
    return x.equals(y);
  }

  public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws SQLException
  {
    final String entityName = (String)Hibernate.STRING.nullSafeGet(rs, names[0]);
    if (rs.wasNull() || entityName == null)
    {
      return null;
    }
    final byte[] content = (byte[])Hibernate.BINARY.nullSafeGet(rs, names[2]);
    final CDOIDHibernate cdoID = new CDOIDHibernateImpl();
    cdoID.setContent(content);
    return cdoID;
  }

  public void nullSafeSet(PreparedStatement statement, Object value, int index) throws SQLException
  {
    if (value == null)
    {
      statement.setNull(index, Types.VARCHAR);
      statement.setNull(index, Types.VARCHAR);
      statement.setNull(index, Types.VARBINARY);
    }
    if (value != null)
    {
      final CDOIDHibernate cdoID;
      if (value instanceof CDOIDTemp)
      {
        cdoID = CDOHibernateUtil.getInstance().getCDOIDHibernate((CDOID)value);
      }
      else if (value instanceof CDORevision)
      {
        cdoID = (CDOIDHibernate)((CDORevision)value).getID();
      }
      else
      {
        cdoID = (CDOIDHibernate)value;
      }
      statement.setString(index, cdoID.getEntityName());
      statement.setString(index + 1, cdoID.getId().toString());
      statement.setBytes(index + 2, cdoID.getContent());
    }
  }

  public Serializable disassemble(Object value)
  {
    return (Serializable)value;
  }

  public Object assemble(Serializable cachedValue, Object owner)
  {
    return cachedValue;
  }

  public Object replace(Object original, Object target, Object owner)
  {
    return original;
  }

  public int hashCode(Object x)
  {
    return x.hashCode();
  }
}

Back to the top