|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +using System.Data.Common; |
| 4 | +using NHibernate.Engine; |
| 5 | +using NHibernate.SqlTypes; |
| 6 | +using NHibernate.UserTypes; |
| 7 | + |
| 8 | +namespace NHibernate.Test.NHSpecificTest.GH1963 |
| 9 | +{ |
| 10 | + [Serializable] |
| 11 | + public class SqlBoolean : IUserType |
| 12 | + // Uncomment for checking implementing this solves the query exception |
| 13 | + //, IEnhancedUserType |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Compare two instances of the class mapped by this type for persistent "equality" |
| 17 | + /// ie. equality of persistent state |
| 18 | + /// </summary> |
| 19 | + /// <param name="x"/><param name="y"/> |
| 20 | + /// <returns/> |
| 21 | + public new bool Equals(object x, object y) |
| 22 | + { |
| 23 | + if (x == y) return true; |
| 24 | + if (x == null || y == null) return false; |
| 25 | + |
| 26 | + return x.Equals(y); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Get a hashcode for the instance, consistent with persistence "equality" |
| 31 | + /// </summary> |
| 32 | + public int GetHashCode(object x) |
| 33 | + { |
| 34 | + return x.GetHashCode(); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Retrieve an instance of the mapped class from a JDBC resultset. |
| 39 | + /// Implementors should handle possibility of null values. |
| 40 | + /// </summary> |
| 41 | + /// <param name="rs">a IDataReader</param><param name="names">column names</param> |
| 42 | + /// <param name="session"></param> |
| 43 | + /// <param name="owner">the containing entity</param> |
| 44 | + /// <returns/> |
| 45 | + /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception> |
| 46 | + public object NullSafeGet(DbDataReader rs, string[] names, ISessionImplementor session, object owner) |
| 47 | + { |
| 48 | + object result = NHibernateUtil.String.NullSafeGet(rs, names[0], session, owner); |
| 49 | + |
| 50 | + if (result == null) |
| 51 | + return default(bool?); |
| 52 | + else |
| 53 | + return result.ToString() == "1"; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Write an instance of the mapped class to a prepared statement. |
| 58 | + /// Implementors should handle possibility of null values. |
| 59 | + /// A multi-column type should be written to parameters starting from index. |
| 60 | + /// </summary> |
| 61 | + /// <param name="cmd">a IDbCommand</param><param name="value">the object to write</param> |
| 62 | + /// <param name="index">command parameter index</param> |
| 63 | + /// <param name="session"></param> |
| 64 | + /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception> |
| 65 | + public void NullSafeSet(DbCommand cmd, object value, int index, ISessionImplementor session) |
| 66 | + { |
| 67 | + var result = value != null ? Convert.ToInt16(bool.Parse(value.ToString())) : default(short?); |
| 68 | + |
| 69 | + NHibernateUtil.Int16.NullSafeSet(cmd, result, index, session); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Return a deep copy of the persistent state, stopping at entities and at collections. |
| 74 | + /// </summary> |
| 75 | + /// <param name="value">generally a collection element or entity field</param> |
| 76 | + /// <returns> |
| 77 | + /// a copy |
| 78 | + /// </returns> |
| 79 | + public object DeepCopy(object value) |
| 80 | + { |
| 81 | + return value; |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// During merge, replace the existing (<paramref name="target"/>) value in the entity |
| 86 | + /// we are merging to with a new (<paramref name="original"/>) value from the detached |
| 87 | + /// entity we are merging. For immutable objects, or null values, it is safe to simply |
| 88 | + /// return the first parameter. For mutable objects, it is safe to return a copy of the |
| 89 | + /// first parameter. For objects with component values, it might make sense to |
| 90 | + /// recursively replace component values. |
| 91 | + /// </summary> |
| 92 | + /// <param name="original">the value from the detached entity being merged</param> |
| 93 | + /// <param name="target">the value in the managed entity</param> |
| 94 | + /// <param name="owner">the managed entity</param> |
| 95 | + /// <returns> |
| 96 | + /// the value to be merged |
| 97 | + /// </returns> |
| 98 | + public object Replace(object original, object target, object owner) |
| 99 | + { |
| 100 | + return DeepCopy(original); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Reconstruct an object from the cacheable representation. At the very least this |
| 105 | + /// method should perform a deep copy if the type is mutable. (optional operation) |
| 106 | + /// </summary> |
| 107 | + /// <param name="cached">the object to be cached</param><param name="owner">the owner of the cached object</param> |
| 108 | + /// <returns> |
| 109 | + /// a reconstructed object from the cachable representation |
| 110 | + /// </returns> |
| 111 | + public object Assemble(object cached, object owner) |
| 112 | + { |
| 113 | + return DeepCopy(cached); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Transform the object into its cacheable representation. At the very least this |
| 118 | + /// method should perform a deep copy if the type is mutable. That may not be enough |
| 119 | + /// for some implementations, however; for example, associations must be cached as |
| 120 | + /// identifier values. (optional operation) |
| 121 | + /// </summary> |
| 122 | + /// <param name="value">the object to be cached</param> |
| 123 | + /// <returns> |
| 124 | + /// a cacheable representation of the object |
| 125 | + /// </returns> |
| 126 | + public object Disassemble(object value) |
| 127 | + { |
| 128 | + return DeepCopy(value); |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// The SQL types for the columns mapped by this type. |
| 133 | + /// </summary> |
| 134 | + public SqlType[] SqlTypes |
| 135 | + { |
| 136 | + get { return new[] { new SqlType(DbType.Int16) }; } |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// The type returned by <c>NullSafeGet()</c> |
| 141 | + /// </summary> |
| 142 | + public System.Type ReturnedType |
| 143 | + { |
| 144 | + get { return typeof(bool?); } |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Are objects of this type mutable? |
| 149 | + /// </summary> |
| 150 | + public bool IsMutable |
| 151 | + { |
| 152 | + get { return false; } |
| 153 | + } |
| 154 | + |
| 155 | + public object FromXMLString(string xml) |
| 156 | + { |
| 157 | + if (bool.TryParse(xml, out var value)) |
| 158 | + return value; |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + public string ObjectToSQLString(object value) |
| 163 | + { |
| 164 | + var val = value as bool?; |
| 165 | + switch (val) |
| 166 | + { |
| 167 | + case true: |
| 168 | + return "1"; |
| 169 | + case false: |
| 170 | + return "0"; |
| 171 | + default: |
| 172 | + return "null"; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public string ToXMLString(object value) |
| 177 | + { |
| 178 | + return value?.ToString(); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments