Package com.j256.ormlite.field

Examples of com.j256.ormlite.field.DataPersister


  protected <T, ID> void testType(Dao<T, ID> dao, T foo, Class<T> clazz, Object javaVal, Object defaultSqlVal,
      Object sqlArg, String defaultValStr, DataType dataType, String columnName, boolean isValidGeneratedType,
      boolean isAppropriateId, boolean isEscapedValue, boolean isPrimitive, boolean isSelectArgRequired,
      boolean isStreamType, boolean isComparable, boolean isConvertableId) throws Exception {
    DataPersister dataPersister = dataType.getDataPersister();
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt =
          conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      assertTrue(results.next());
      int colNum = results.findColumn(columnName);
      Field field = clazz.getDeclaredField(columnName);
      FieldType fieldType = FieldType.createFieldType(connectionSource, TABLE_NAME, field, clazz);
      Class<?>[] classes = fieldType.getDataPersister().getAssociatedClasses();
      if (classes.length > 0) {
        assertTrue(classes[0].isAssignableFrom(fieldType.getType()));
      }
      assertTrue(fieldType.getDataPersister().isValidForField(field));
      if (javaVal instanceof byte[]) {
        assertTrue(Arrays.equals((byte[]) javaVal,
            (byte[]) dataPersister.resultToJava(fieldType, results, colNum)));
      } else {
        Map<String, Integer> colMap = new HashMap<String, Integer>();
        colMap.put(columnName, colNum);
        Object result = fieldType.resultToJava(results, colMap);
        assertEquals(javaVal, result);
      }
      if (dataType == DataType.STRING_BYTES || dataType == DataType.BYTE_ARRAY
          || dataType == DataType.SERIALIZABLE) {
        try {
          dataPersister.parseDefaultString(fieldType, "");
          fail("parseDefaultString should have thrown for " + dataType);
        } catch (SQLException e) {
          // expected
        }
      } else if (defaultValStr != null) {
        assertEquals(defaultSqlVal, dataPersister.parseDefaultString(fieldType, defaultValStr));
      }
      if (sqlArg == null) {
        // noop
      } else if (sqlArg instanceof byte[]) {
        assertTrue(Arrays.equals((byte[]) sqlArg, (byte[]) dataPersister.javaToSqlArg(fieldType, javaVal)));
      } else {
        assertEquals(sqlArg, dataPersister.javaToSqlArg(fieldType, javaVal));
      }
      assertEquals(isValidGeneratedType, dataPersister.isValidGeneratedType());
      assertEquals(isAppropriateId, dataPersister.isAppropriateId());
      assertEquals(isEscapedValue, dataPersister.isEscapedValue());
      assertEquals(isEscapedValue, dataPersister.isEscapedDefaultValue());
      assertEquals(isPrimitive, dataPersister.isPrimitive());
      assertEquals(isSelectArgRequired, dataPersister.isArgumentHolderRequired());
      assertEquals(isStreamType, dataPersister.isStreamType());
      assertEquals(isComparable, dataPersister.isComparable());
      if (isConvertableId) {
        assertNotNull(dataPersister.convertIdNumber(10));
      } else {
        assertNull(dataPersister.convertIdNumber(10));
      }
      List<T> list = dao.queryForAll();
      assertEquals(1, list.size());
      assertTrue(dao.objectsEqual(foo, list.get(0)));
      // if we have a value then look for it, floats don't find any results because of rounding issues
      if (javaVal != null && dataPersister.isComparable() && dataType != DataType.FLOAT
          && dataType != DataType.FLOAT_OBJ) {
        // test for inline arguments
        list = dao.queryForMatching(foo);
        assertEquals(1, list.size());
        assertTrue(dao.objectsEqual(foo, list.get(0)));
View Full Code Here


  public void appendColumnArg(String tableName, StringBuilder sb, FieldType fieldType, List<String> additionalArgs,
      List<String> statementsBefore, List<String> statementsAfter, List<String> queriesAfter) throws SQLException {
    appendEscapedEntityName(sb, fieldType.getColumnName());
    sb.append(' ');
    DataPersister dataPersister = fieldType.getDataPersister();
    // first try the per-field width
    int fieldWidth = fieldType.getWidth();
    if (fieldWidth == 0) {
      // next try the per-data-type width
      fieldWidth = dataPersister.getDefaultWidth();
    }
    switch (dataPersister.getSqlType()) {

      case STRING :
        appendStringType(sb, fieldType, fieldWidth);
        break;

      case LONG_STRING :
        appendLongStringType(sb, fieldType, fieldWidth);
        break;

      case BOOLEAN :
        appendBooleanType(sb, fieldType, fieldWidth);
        break;

      case DATE :
        appendDateType(sb, fieldType, fieldWidth);
        break;

      case CHAR :
        appendCharType(sb, fieldType, fieldWidth);
        break;

      case BYTE :
        appendByteType(sb, fieldType, fieldWidth);
        break;

      case BYTE_ARRAY :
        appendByteArrayType(sb, fieldType, fieldWidth);
        break;

      case SHORT :
        appendShortType(sb, fieldType, fieldWidth);
        break;

      case INTEGER :
        appendIntegerType(sb, fieldType, fieldWidth);
        break;

      case LONG :
        appendLongType(sb, fieldType, fieldWidth);
        break;

      case FLOAT :
        appendFloatType(sb, fieldType, fieldWidth);
        break;

      case DOUBLE :
        appendDoubleType(sb, fieldType, fieldWidth);
        break;

      case SERIALIZABLE :
        appendSerializableType(sb, fieldType, fieldWidth);
        break;

      case BIG_DECIMAL :
        appendBigDecimalNumericType(sb, fieldType, fieldWidth);
        break;

      case UNKNOWN :
      default :
        // shouldn't be able to get here unless we have a missing case
        throw new IllegalArgumentException("Unknown SQL-type " + dataPersister.getSqlType());
    }
    sb.append(' ');

    /*
     * NOTE: the configure id methods must be in this order since isGeneratedIdSequence is also isGeneratedId and
View Full Code Here

      throw new IllegalStateException("Simulate a failure");
    }
    Field field = GeneratedId.class.getField("id");
    DatabaseType mockDb = createMock(DatabaseType.class);
    expect(mockDb.isIdSequenceNeeded()).andReturn(false);
    DataPersister dataPersister = createMock(DataPersister.class);
    expect(mockDb.getDataPersister(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
    expect(mockDb.getFieldConverter(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
    expect(mockDb.isEntityNamesMustBeUpCase()).andReturn(false);
    replay(mockDb);
    connectionSource.setDatabaseType(mockDb);
View Full Code Here

  @Test(expected = IllegalStateException.class)
  public void testBadGeneratedId() throws Exception {
    Field field = GeneratedId.class.getField("id");
    DatabaseType mockDb = createMock(DatabaseType.class);
    expect(mockDb.isIdSequenceNeeded()).andReturn(false);
    DataPersister dataPersister = createMock(DataPersister.class);
    expect(mockDb.getDataPersister(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
    expect(mockDb.getFieldConverter(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
    expect(mockDb.isEntityNamesMustBeUpCase()).andReturn(true);
    replay(mockDb);
    connectionSource.setDatabaseType(mockDb);
View Full Code Here

  @Test(expected = IllegalStateException.class)
  public void testBadGeneratedId() throws Exception {
    Field field = GeneratedId.class.getField("id");
    DatabaseType mockDb = createMock(DatabaseType.class);
    expect(mockDb.isIdSequenceNeeded()).andReturn(false);
    DataPersister dataPersister = createMock(DataPersister.class);
    expect(mockDb.getDataPersister(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
    expect(mockDb.getFieldConverter(isA(DataPersister.class), isA(FieldType.class))).andReturn(dataPersister);
    expect(mockDb.isEntityNamesMustBeUpCase()).andReturn(false);
    replay(mockDb);
    connectionSource.setDatabaseType(mockDb);
View Full Code Here

    public Object[] mapRow(DatabaseResults results) throws SQLException {
      int columnN = results.getColumnCount();
      Object[] result = new Object[columnN];
      for (int colC = 0; colC < columnN; colC++) {
        DataType dataType;
        if (colC >= columnTypes.length) {
          dataType = DataType.STRING;
        } else {
          dataType = columnTypes[colC];
        }
        result[colC] = dataType.getDataPersister().resultToJava(null, results, colC);
      }
      return result;
    }
View Full Code Here

    if (columnAnnotation == null && idAnnotation == null && oneToOneAnnotation == null
        && manyToOneAnnotation == null) {
      return null;
    }

    DatabaseFieldConfig config = new DatabaseFieldConfig();
    String fieldName = field.getName();
    if (databaseType.isEntityNamesMustBeUpCase()) {
      fieldName = fieldName.toUpperCase();
    }
    config.setFieldName(fieldName);

    if (columnAnnotation != null) {
      try {
        Method method = columnAnnotation.getClass().getMethod("name");
        String name = (String) method.invoke(columnAnnotation);
        if (name != null && name.length() > 0) {
          config.setColumnName(name);
        }
        method = columnAnnotation.getClass().getMethod("length");
        config.setWidth((Integer) method.invoke(columnAnnotation));
        method = columnAnnotation.getClass().getMethod("nullable");
        config.setCanBeNull((Boolean) method.invoke(columnAnnotation));
        method = columnAnnotation.getClass().getMethod("unique");
        config.setUnique((Boolean) method.invoke(columnAnnotation));
      } catch (Exception e) {
        throw SqlExceptionUtil.create("Problem accessing fields from the Column annotation for field " + field,
            e);
      }
    }
    if (idAnnotation != null) {
      if (generatedValueAnnotation == null) {
        config.setId(true);
      } else {
        // generatedValue only works if it is also an id according to {@link GeneratedValue)
        config.setGeneratedId(true);
      }
    }
    // foreign values are always ones we can't map as primitives (or Strings)
    config.setForeign(oneToOneAnnotation != null || manyToOneAnnotation != null);
    config.setDataPersister(DataPersisterManager.lookupForField(field));
    config.setUseGetSet(DatabaseFieldConfig.findGetMethod(field, false) != null
        && DatabaseFieldConfig.findSetMethod(field, false) != null);
    return config;
  }
View Full Code Here

    }
  }

  public static <T, ID> MappedUpdateId<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo)
      throws SQLException {
    FieldType idField = tableInfo.getIdField();
    if (idField == null) {
      throw new SQLException("Cannot update-id in " + tableInfo.getDataClass()
          + " because it doesn't have an id field");
    }
    StringBuilder sb = new StringBuilder(64);
View Full Code Here

    super(tableInfo, statement, argFieldTypes);
  }

  public static <T, ID> MappedDelete<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo)
      throws SQLException {
    FieldType idField = tableInfo.getIdField();
    if (idField == null) {
      throw new SQLException("Cannot delete from " + tableInfo.getDataClass()
          + " because it doesn't have an id field");
    }
    StringBuilder sb = new StringBuilder(64);
View Full Code Here

  /**
   * This is private because the execute is the only method that should be called here.
   */
  private static <T, ID> MappedDeleteCollection<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
      int dataSize) throws SQLException {
    FieldType idField = tableInfo.getIdField();
    if (idField == null) {
      throw new SQLException("Cannot delete " + tableInfo.getDataClass()
          + " because it doesn't have an id field defined");
    }
    StringBuilder sb = new StringBuilder(128);
View Full Code Here

TOP

Related Classes of com.j256.ormlite.field.DataPersister

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.