1 29 30 package com.caucho.amber.field; 31 32 import com.caucho.amber.type.RelatedType; 33 import com.caucho.amber.type.Type; 34 import com.caucho.config.ConfigException; 35 import com.caucho.java.JavaWriter; 36 import com.caucho.log.Log; 37 import com.caucho.util.CharBuffer; 38 import com.caucho.util.L10N; 39 40 import java.io.IOException ; 41 import java.util.logging.Logger ; 42 43 46 public class VersionField extends PropertyField { 47 private static final L10N L = new L10N(VersionField.class); 48 protected static final Logger log = Log.open(VersionField.class); 49 50 public VersionField(RelatedType entityType, String name) 51 throws ConfigException 52 { 53 super(entityType, name); 54 55 setLazy(false); 56 } 57 58 public VersionField(RelatedType entityType) 59 { 60 super(entityType); 61 62 setLazy(false); 63 } 64 65 68 public String generateIsNull() 69 { 70 String getter = generateSuperGetter(); 71 Type type = getColumn().getType(); 72 73 return type.generateIsNull(getter); 74 } 75 76 79 public void generateIncrementVersion(JavaWriter out) 80 throws IOException 81 { 82 int dirtyGroup = getIndex() / 64; 83 String dirtyVar = "__caucho_dirtyMask_" + dirtyGroup; 84 85 long dirtyMask = 1L << (getIndex() % 64); 86 87 String getter = generateSuperGetter(); 88 Type type = getColumn().getType(); 89 90 out.println(); 92 out.println("if (" + generateIsNull() + ")"); 93 out.println(" " + generateSuperSetter("new Integer(1)") + ";"); 94 out.println("else"); 95 out.println(" " + generateSuperSetter(type.generateIncrementVersion(getter)) + ";"); 96 97 out.println(); 98 out.println("long oldMask = " + dirtyVar + ";"); 99 out.println(dirtyVar + " |= " + dirtyMask + "L;"); 100 out.println(); 101 out.println("if (__caucho_session != null && oldMask == 0)"); 102 out.println(" __caucho_session.update(this);"); 103 } 104 105 108 public String generateMatchArgWhere(String id) 109 { 110 return getColumn().generateMatchArgWhere(id); 111 } 112 113 116 public void generatePostConstructor(JavaWriter out) 117 throws IOException 118 { 119 121 String setter = getSetterName(); 122 String typeName = getJavaTypeName(); 123 Object initialValue = getColumn().getType().toObject(1); 124 125 out.println("if (" + generateIsNull() + ");"); 126 out.println(" __caucho_increment_version();"); 127 } 128 129 132 public void generateSetVersion(JavaWriter out, 133 String pstmt, 134 String index) 135 throws IOException 136 { 137 String value = generateGet("super"); 138 Type type = getColumn().getType(); 139 getColumn().generateSetVersion(out, pstmt, index, value); } 142 143 146 public void generateUpdate(CharBuffer sql) 147 { 148 sql.append(getColumn().generateUpdateSet()); 149 } 150 151 154 public void generateUpdate(JavaWriter out, 155 String maskVar, 156 String pstmt, 157 String index) 158 throws IOException 159 { 160 168 generateSetVersion(out, pstmt, index); 169 170 } 173 } 174 | Popular Tags |