@Test
 public void testGeneratedIdSequenceAutoName() throws Exception {
   TableInfo<GeneratedIdSequenceAutoName, Integer> tableInfo =
       new TableInfo<GeneratedIdSequenceAutoName, Integer>(
           connectionSource, null, GeneratedIdSequenceAutoName.class);
   assertEquals(2, tableInfo.getFieldTypes().length);
   FieldType idField = tableInfo.getFieldTypes()[0];
   StringBuilder sb = new StringBuilder();
   List<String> additionalArgs = new ArrayList<String>();
   List<String> statementsBefore = new ArrayList<String>();
   databaseType.appendColumnArg(null, sb, idField, additionalArgs, statementsBefore, null, null);
   databaseType.addPrimaryKeySql(
       new FieldType[] {idField}, additionalArgs, statementsBefore, null, null);
   String seqName =
       databaseType.generateIdSequenceName(
           GeneratedIdSequenceAutoName.class.getSimpleName().toLowerCase(), idField);
   assertTrue(
       sb + " should contain gen-id-seq-name stuff",
       sb.toString().contains(" GENERATED BY DEFAULT AS IDENTITY "));
   // sequence, sequence table, insert
   assertEquals(1, statementsBefore.size());
   assertTrue(statementsBefore.get(0).contains(seqName.toUpperCase()));
   assertEquals(1, additionalArgs.size());
   assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
 }
Example #2
0
 public static <T, ID> MappedUpdate<T, ID> build(
     DatabaseType databaseType, TableInfo<T, ID> tableInfo) throws SQLException {
   FieldType idField = tableInfo.getIdField();
   if (idField == null) {
     throw new SQLException(
         "Cannot update " + tableInfo.getDataClass() + " because it doesn't have an id field");
   }
   StringBuilder sb = new StringBuilder(64);
   appendTableName(databaseType, sb, "UPDATE ", tableInfo.getTableName());
   boolean first = true;
   int argFieldC = 0;
   FieldType versionFieldType = null;
   int versionFieldTypeIndex = -1;
   // first we count up how many arguments we are going to have
   for (FieldType fieldType : tableInfo.getFieldTypes()) {
     if (isFieldUpdatable(fieldType, idField)) {
       if (fieldType.isVersion()) {
         versionFieldType = fieldType;
         versionFieldTypeIndex = argFieldC;
       }
       argFieldC++;
     }
   }
   // one more for where id = ?
   argFieldC++;
   if (versionFieldType != null) {
     // one more for the AND version = ?
     argFieldC++;
   }
   FieldType[] argFieldTypes = new FieldType[argFieldC];
   argFieldC = 0;
   for (FieldType fieldType : tableInfo.getFieldTypes()) {
     if (!isFieldUpdatable(fieldType, idField)) {
       continue;
     }
     if (first) {
       sb.append("SET ");
       first = false;
     } else {
       sb.append(", ");
     }
     appendFieldColumnName(databaseType, sb, fieldType, null);
     argFieldTypes[argFieldC++] = fieldType;
     sb.append("= ?");
   }
   sb.append(' ');
   appendWhereFieldEq(databaseType, idField, sb, null);
   argFieldTypes[argFieldC++] = idField;
   if (versionFieldType != null) {
     sb.append(" AND ");
     appendFieldColumnName(databaseType, sb, versionFieldType, null);
     sb.append("= ?");
     argFieldTypes[argFieldC++] = versionFieldType;
   }
   return new MappedUpdate<T, ID>(
       tableInfo, sb.toString(), argFieldTypes, versionFieldType, versionFieldTypeIndex);
 }
 @Test
 public void testBoolean() throws Exception {
   TableInfo<AllTypes, Integer> tableInfo =
       new TableInfo<AllTypes, Integer>(connectionSource, null, AllTypes.class);
   assertEquals(9, tableInfo.getFieldTypes().length);
   FieldType booleanField = tableInfo.getFieldTypes()[1];
   assertEquals("booleanField", booleanField.getColumnName());
   StringBuilder sb = new StringBuilder();
   List<String> additionalArgs = new ArrayList<String>();
   List<String> statementsBefore = new ArrayList<String>();
   databaseType.appendColumnArg(
       null, sb, booleanField, additionalArgs, statementsBefore, null, null);
   assertTrue(sb.toString().contains("BIT"));
 }
 @Test
 public void testGneratedIdLong() throws Exception {
   TableInfo<GeneratedIdLong, Long> tableInfo =
       new TableInfo<GeneratedIdLong, Long>(connectionSource, null, GeneratedIdLong.class);
   assertEquals(2, tableInfo.getFieldTypes().length);
   FieldType idField = tableInfo.getFieldTypes()[0];
   assertEquals("genId", idField.getColumnName());
   StringBuilder sb = new StringBuilder();
   List<String> additionalArgs = new ArrayList<String>();
   List<String> statementsBefore = new ArrayList<String>();
   databaseType.appendColumnArg(null, sb, idField, additionalArgs, statementsBefore, null, null);
   assertEquals(1, statementsBefore.size());
   StringBuilder sb2 = new StringBuilder();
   sb2.append("CREATE SEQUENCE ");
   databaseType.appendEscapedEntityName(sb2, LONG_SEQ_NAME.toUpperCase());
   sb2.append(" AS BIGINT");
   assertTrue(
       statementsBefore.get(0) + " should contain the right stuff",
       statementsBefore.get(0).contains(sb2.toString()));
 }
 @Override
 @Test
 public void testGeneratedIdSequence() throws Exception {
   TableInfo<GeneratedIdSequence, Integer> tableInfo =
       new TableInfo<GeneratedIdSequence, Integer>(
           connectionSource, null, GeneratedIdSequence.class);
   assertEquals(2, tableInfo.getFieldTypes().length);
   StringBuilder sb = new StringBuilder();
   List<String> additionalArgs = new ArrayList<String>();
   List<String> statementsBefore = new ArrayList<String>();
   databaseType.appendColumnArg(
       null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null, null);
   databaseType.addPrimaryKeySql(
       tableInfo.getFieldTypes(), additionalArgs, statementsBefore, null, null);
   assertTrue(
       sb + " should contain autoincrement stuff",
       sb.toString().contains(" GENERATED BY DEFAULT AS IDENTITY "));
   // sequence, sequence table, insert
   assertEquals(1, statementsBefore.size());
   assertTrue(statementsBefore.get(0).contains(GENERATED_ID_SEQ.toUpperCase()));
   assertEquals(1, additionalArgs.size());
   assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
 }
Example #6
0
 Where(
     TableInfo<T, ID> tableInfo,
     StatementBuilder<T, ID> statementBuilder,
     DatabaseType databaseType) {
   // limit the constructor scope
   this.tableInfo = tableInfo;
   this.statementBuilder = statementBuilder;
   this.idFieldType = tableInfo.getIdField();
   if (idFieldType == null) {
     this.idColumnName = null;
   } else {
     this.idColumnName = idFieldType.getColumnName();
   }
   this.databaseType = databaseType;
 }
Example #7
0
 private FieldType findColumnFieldType(String columnName) {
   return tableInfo.getFieldTypeByColumnName(columnName);
 }