1 28 29 package com.caucho.amber.query; 30 31 import com.caucho.util.CharBuffer; 32 33 36 public class QueryCacheKey { 37 private String _sql; 38 private Object []_args; 39 private int _startRow; 40 41 public QueryCacheKey() 42 { 43 } 44 45 public QueryCacheKey(String sql, Object []args, int startRow) 46 { 47 init(sql, args, startRow); 48 } 49 50 53 public void init(String sql, Object []args, int startRow) 54 { 55 _sql = sql; 56 _args = args; 57 _startRow = startRow; 58 } 59 60 63 public int hashCode() 64 { 65 int hash = _startRow; 66 67 hash = 65537 * hash + _sql.hashCode(); 68 69 Object []args = _args; 70 for (int i = args.length - 1; i >= 0; i--) { 71 Object v = args[i]; 72 73 if (v == null) 74 hash = 65537 * hash + 17; 75 else 76 hash = 65537 * hash + 17 * v.hashCode(); 77 } 78 79 return hash; 80 } 81 82 85 public boolean equals(Object o) 86 { 87 if (this == o) 88 return true; 89 else if (o == null || o.getClass() != getClass()) 90 return false; 91 92 QueryCacheKey key = (QueryCacheKey) o; 93 94 if (! _sql.equals(key._sql)) { 95 return false; 96 } 97 98 if (_startRow != key._startRow) { 99 return false; 100 } 101 102 Object []argsA = _args; 103 Object []argsB = key._args; 104 105 if (argsA.length != argsB.length) { 106 return false; 107 } 108 109 for (int i = argsA.length - 1; i >= 0; i--) { 110 Object a = argsA[i]; 111 Object b = argsB[i]; 112 113 if (a != b && (a == null || ! a.equals(b))) { 114 return false; 115 } 116 } 117 118 return true; 119 } 120 121 public String toString() 122 { 123 CharBuffer cb = new CharBuffer(); 124 125 cb.append("QueryCacheKey["); 126 cb.append(_sql); 127 128 for (int i = 0; i < _args.length; i++) { 129 cb.append(","); 130 cb.append(_args[i]); 131 } 132 133 cb.append("]"); 134 135 return cb.toString(); 136 } 137 } 138 | Popular Tags |