1 19 20 package org.apache.cayenne.dba.frontbase; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.cayenne.CayenneRuntimeException; 29 import org.apache.cayenne.access.DataNode; 30 import org.apache.cayenne.access.QueryResult; 31 import org.apache.cayenne.dba.JdbcPkGenerator; 32 import org.apache.cayenne.map.DbEntity; 33 import org.apache.cayenne.query.SQLTemplate; 34 35 39 public class FrontBasePkGenerator extends JdbcPkGenerator { 40 41 public FrontBasePkGenerator() { 42 super(); 43 } 44 45 48 public int getPkCacheSize() { 49 return 0; 50 } 51 52 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 53 Iterator it = dbEntities.iterator(); 55 while (it.hasNext()) { 56 DbEntity ent = (DbEntity) it.next(); 57 runUpdate(node, pkCreateString(ent.getName())); 58 } 59 } 60 61 public List createAutoPkStatements(List dbEntities) { 62 List list = new ArrayList (); 63 64 Iterator it = dbEntities.iterator(); 65 while (it.hasNext()) { 66 DbEntity ent = (DbEntity) it.next(); 67 list.add(pkCreateString(ent.getName())); 68 } 69 70 return list; 71 } 72 73 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 74 } 75 76 protected String pkTableCreateString() { 77 return ""; 78 } 79 80 protected String pkDeleteString(List dbEntities) { 81 return "-- The 'Drop Primary Key Support' option is unavailable"; 82 } 83 84 protected String pkCreateString(String entName) { 85 StringBuffer buf = new StringBuffer (); 86 buf.append("SET UNIQUE = 1000000 FOR \"" + entName + "\""); 87 return buf.toString(); 88 } 89 90 protected String pkSelectString(String entName) { 91 StringBuffer buf = new StringBuffer (); 92 buf.append("SELECT UNIQUE FROM \"" + entName + "\""); 93 return buf.toString(); 94 } 95 96 protected String pkUpdateString(String entName) { 97 return ""; 98 } 99 100 protected String dropAutoPkString() { 101 return ""; 102 } 103 104 protected int pkFromDatabase(DataNode node, DbEntity entity) throws Exception { 105 String template = "SELECT #result('UNIQUE' 'int') FROM " + entity.getName(); 106 107 SQLTemplate query = new SQLTemplate(entity, template); 108 QueryResult observer = new QueryResult(); 109 node.performQueries(Collections.singleton(query), observer); 110 111 List results = observer.getFirstRows(query); 112 if (results.size() != 1) { 113 throw new CayenneRuntimeException("Error fetching PK. Expected one row, got " 114 + results.size()); 115 } 116 117 Map row = (Map ) results.get(0); 118 Number pk = (Number ) row.get("UNIQUE"); 119 return pk.intValue(); 120 } 121 } 122 | Popular Tags |