Package com.j256.ormlite.db

Examples of com.j256.ormlite.db.DatabaseType


   */
  public FieldType(ConnectionSource connectionSource, String tableName, Field field, DatabaseFieldConfig fieldConfig,
      Class<?> parentClass) throws SQLException {
    this.connectionSource = connectionSource;
    this.tableName = tableName;
    DatabaseType databaseType = connectionSource.getDatabaseType();
    this.field = field;
    this.parentClass = parentClass;

    // post process our config settings
    fieldConfig.postProcess();

    Class<?> clazz = field.getType();
    DataPersister dataPersister;
    if (fieldConfig.getDataPersister() == null) {
      Class<? extends DataPersister> persisterClass = fieldConfig.getPersisterClass();
      if (persisterClass == null || persisterClass == VoidType.class) {
        dataPersister = DataPersisterManager.lookupForField(field);
      } else {
        Method method;
        try {
          method = persisterClass.getDeclaredMethod("getSingleton");
        } catch (Exception e) {
          throw SqlExceptionUtil.create("Could not find getSingleton static method on class "
              + persisterClass, e);
        }
        Object result;
        try {
          result = method.invoke(null);
        } catch (InvocationTargetException e) {
          throw SqlExceptionUtil.create("Could not run getSingleton method on class " + persisterClass,
              e.getTargetException());
        } catch (Exception e) {
          throw SqlExceptionUtil.create("Could not run getSingleton method on class " + persisterClass, e);
        }
        if (result == null) {
          throw new SQLException("Static getSingleton method should not return null on class "
              + persisterClass);
        }
        try {
          dataPersister = (DataPersister) result;
        } catch (Exception e) {
          throw SqlExceptionUtil.create(
              "Could not cast result of static getSingleton method to DataPersister from class "
                  + persisterClass, e);
        }
      }
    } else {
      dataPersister = fieldConfig.getDataPersister();
      if (!dataPersister.isValidForField(field)) {
        StringBuilder sb = new StringBuilder();
        sb.append("Field class ").append(clazz.getName());
        sb.append(" for field ").append(this);
        sb.append(" is not valid for type ").append(dataPersister);
        Class<?> primaryClass = dataPersister.getPrimaryClass();
        if (primaryClass != null) {
          sb.append(", maybe should be " + primaryClass);
        }
        throw new IllegalArgumentException(sb.toString());
      }
    }
    String foreignColumnName = fieldConfig.getForeignColumnName();
    String defaultFieldName = field.getName();
    if (fieldConfig.isForeign() || fieldConfig.isForeignAutoRefresh() || foreignColumnName != null) {
      if (dataPersister != null && dataPersister.isPrimitive()) {
        throw new IllegalArgumentException("Field " + this + " is a primitive class " + clazz
            + " but marked as foreign");
      }
      if (foreignColumnName == null) {
        defaultFieldName = defaultFieldName + FOREIGN_ID_FIELD_SUFFIX;
      } else {
        defaultFieldName = defaultFieldName + "_" + foreignColumnName;
      }
      if (ForeignCollection.class.isAssignableFrom(clazz)) {
        throw new SQLException("Field '" + field.getName() + "' in class " + clazz + "' should use the @"
            + ForeignCollectionField.class.getSimpleName() + " annotation not foreign=true");
      }
    } else if (fieldConfig.isForeignCollection()) {
      if (clazz != Collection.class && !ForeignCollection.class.isAssignableFrom(clazz)) {
        throw new SQLException("Field class for '" + field.getName() + "' must be of class "
            + ForeignCollection.class.getSimpleName() + " or Collection.");
      }
      Type type = field.getGenericType();
      if (!(type instanceof ParameterizedType)) {
        throw new SQLException("Field class for '" + field.getName() + "' must be a parameterized Collection.");
      }
      Type[] genericArguments = ((ParameterizedType) type).getActualTypeArguments();
      if (genericArguments.length == 0) {
        // i doubt this will ever be reached
        throw new SQLException("Field class for '" + field.getName()
            + "' must be a parameterized Collection with at least 1 type.");
      }
    } else if (dataPersister == null && (!fieldConfig.isForeignCollection())) {
      if (byte[].class.isAssignableFrom(clazz)) {
        throw new SQLException("ORMLite does not know how to store " + clazz + " for field '" + field.getName()
            + "'. byte[] fields must specify dataType=DataType.BYTE_ARRAY or SERIALIZABLE");
      } else if (Serializable.class.isAssignableFrom(clazz)) {
        throw new SQLException("ORMLite does not know how to store " + clazz + " for field '" + field.getName()
            + "'.  Use another class, custom persister, or to serialize it use "
            + "dataType=DataType.SERIALIZABLE");
      } else {
        throw new IllegalArgumentException("ORMLite does not know how to store " + clazz + " for field "
            + field.getName() + ". Use another class or a custom persister.");
      }
    }
    if (fieldConfig.getColumnName() == null) {
      this.columnName = defaultFieldName;
    } else {
      this.columnName = fieldConfig.getColumnName();
    }
    this.fieldConfig = fieldConfig;
    if (fieldConfig.isId()) {
      if (fieldConfig.isGeneratedId() || fieldConfig.getGeneratedIdSequence() != null) {
        throw new IllegalArgumentException("Must specify one of id, generatedId, and generatedIdSequence with "
            + field.getName());
      }
      this.isId = true;
      this.isGeneratedId = false;
      this.generatedIdSequence = null;
    } else if (fieldConfig.isGeneratedId()) {
      if (fieldConfig.getGeneratedIdSequence() != null) {
        throw new IllegalArgumentException("Must specify one of id, generatedId, and generatedIdSequence with "
            + field.getName());
      }
      this.isId = true;
      this.isGeneratedId = true;
      if (databaseType.isIdSequenceNeeded()) {
        this.generatedIdSequence = databaseType.generateIdSequenceName(tableName, this);
      } else {
        this.generatedIdSequence = null;
      }
    } else if (fieldConfig.getGeneratedIdSequence() != null) {
      this.isId = true;
      this.isGeneratedId = true;
      String seqName = fieldConfig.getGeneratedIdSequence();
      if (databaseType.isEntityNamesMustBeUpCase()) {
        seqName = seqName.toUpperCase();
      }
      this.generatedIdSequence = seqName;
    } else {
      this.isId = false;
View Full Code Here


   *
   * @see BaseDaoImpl#initialize()
   */
  public void configDaoInformation(ConnectionSource connectionSource, Class<?> parentClass) throws SQLException {
    Class<?> fieldClass = field.getType();
    DatabaseType databaseType = connectionSource.getDatabaseType();
    TableInfo<?, ?> foreignTableInfo;
    final FieldType foreignIdField;
    final FieldType foreignFieldType;
    final BaseDaoImpl<?, ?> foreignDao;
    final MappedQueryForId<Object, Object> mappedQueryForId;
View Full Code Here

  /**
   * Return An instantiated {@link FieldType} or null if the field does not have a {@link DatabaseField} annotation.
   */
  public static FieldType createFieldType(ConnectionSource connectionSource, String tableName, Field field,
      Class<?> parentClass) throws SQLException {
    DatabaseType databaseType = connectionSource.getDatabaseType();
    DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, tableName, field);
    if (fieldConfig == null) {
      return null;
    } else {
      return new FieldType(connectionSource, tableName, field, fieldConfig, parentClass);
View Full Code Here

  }

  @Test
  public void testCreateTableQueriesAfter() throws Exception {
    final String queryAfter = "SELECT * from foo";
    DatabaseType databaseType = new H2DatabaseType() {
      @Override
      public void appendColumnArg(String tableName, StringBuilder sb, FieldType fieldType,
          List<String> additionalArgs, List<String> statementsBefore, List<String> statementsAfter,
          List<String> queriesAfter) throws SQLException {
        super.appendColumnArg(tableName, sb, fieldType, additionalArgs, statementsBefore, statementsAfter,
View Full Code Here

  @Test
  public void testAppendArgOrValueString() throws SQLException {
    String value = "23wbdqwbdq13";
    StringBuilder sb = new StringBuilder();
    DatabaseType databaseType = createMock(DatabaseType.class);
    databaseType.appendEscapedWord(sb, value);
    replay(databaseType);
    cmpString.appendArgOrValue(databaseType, stringFieldType, sb, new ArrayList<ArgumentHolder>(), value);
    verify(databaseType);
  }
View Full Code Here

    pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL, null, null);
    assertEquals(DEFAULT_DATABASE_URL, pooled.getUrl());
    pooled.close();

    DatabaseType databaseType = DatabaseTypeUtils.createDatabaseType(DEFAULT_DATABASE_URL);
    pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL, databaseType);
    assertEquals(DEFAULT_DATABASE_URL, pooled.getUrl());
    assertSame(databaseType, pooled.getDatabaseType());
    pooled.close();
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

TOP

Related Classes of com.j256.ormlite.db.DatabaseType

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.