Package com.j256.ormlite.jdbc

Examples of com.j256.ormlite.jdbc.JdbcConnectionSource


     * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource().
     */
    DatabaseConnection conn = cs.getSpecialConnection();
    boolean clearSpecial = false;
    if (conn == null) {
      conn = new AndroidDatabaseConnection(db, true);
      try {
        cs.saveSpecialConnection(conn);
        clearSpecial = true;
      } catch (SQLException e) {
        throw new IllegalStateException("Could not save special connection", e);
View Full Code Here


     * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource().
     */
    DatabaseConnection conn = cs.getSpecialConnection();
    boolean clearSpecial = false;
    if (conn == null) {
      conn = new AndroidDatabaseConnection(db, true);
      try {
        cs.saveSpecialConnection(conn);
        clearSpecial = true;
      } catch (SQLException e) {
        throw new IllegalStateException("Could not save special connection", e);
View Full Code Here

   * You should use {@link FieldType#createFieldType} to instantiate one of these field if you have a {@link Field}.
   */
  public FieldType(ConnectionSource connectionSource, String tableName, Field field, DatabaseFieldConfig fieldConfig,
      Class<?> parentClass) throws SQLException {
    this.connectionSource = connectionSource;
    DatabaseType databaseType = connectionSource.getDatabaseType();
    this.field = field;
    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 = (DataPersister) 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 new SQLException(
              "Could not cast result of static getSingleton method to DataPersister from class "
                  + persisterClass);
        }
      }
    } else {
      dataPersister = fieldConfig.getDataPersister();
      if (!dataPersister.isValidForField(field)) {
        throw new IllegalArgumentException("Field class " + clazz + " for field " + this
            + " is not valid for data persister " + dataPersister);
      }
    }
    String defaultFieldName = field.getName();
    if (fieldConfig.isForeign() || fieldConfig.isForeignAutoRefresh()) {
      if (dataPersister != null && dataPersister.isPrimitive()) {
        throw new IllegalArgumentException("Field " + this + " is a primitive class " + clazz
            + " but marked as foreign");
      }
      defaultFieldName = defaultFieldName + FOREIGN_ID_FIELD_SUFFIX;
    } 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 can't store unknown class " + 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 can't store unknown class " + clazz + " for field '" + field.getName()
            + "'. Serializable fields must specify dataType=DataType.SERIALIZABLE");
      } else {
        throw new IllegalArgumentException("ORMLite does not know how to store field class " + clazz
            + " for field " + this);
      }
    }
    if (fieldConfig.getColumnName() == null) {
      this.dbColumnName = defaultFieldName;
    } else {
      this.dbColumnName = 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<?> clazz = field.getType();
    DatabaseType databaseType = connectionSource.getDatabaseType();
    TableInfo<?, ?> foreignTableInfo;
    final FieldType foreignIdField;
    final Constructor<?> foreignConstructor;
    final FieldType foreignFieldType;
    final Dao<?, ?> foreignDao;
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

    }
  }

  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

  public static void initializeDB() {
    LOGGER.info("Initialized database");
    System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "error");
    try {
      ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:file:" + dbFileName);
      TableUtils.createTableIfNotExists(connectionSource, Show.class);
      TableUtils.createTableIfNotExists(connectionSource, Season.class);
      TableUtils.createTableIfNotExists(connectionSource, Episode.class);
      TableUtils.createTableIfNotExists(connectionSource, Feed.class);
      TableUtils.createTableIfNotExists(connectionSource, Settings.class);
      TableUtils.createTableIfNotExists(connectionSource, Search.class);
      connectionSource.close();
    } catch (SQLException e) {
      LOGGER.severe("SQLException: " + e.toString());
      System.exit(1);
    }
  }
View Full Code Here

    }
  }

  public static void saveData() {
    try {
      ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:file:" + dbFileName);
      Dao<Show, Integer> showDao = DaoManager.createDao(connectionSource, Show.class);
      Dao<Season, Integer> seasonDao = DaoManager.createDao(connectionSource, Season.class);
      Dao<Episode, Integer> episodeDao = DaoManager.createDao(connectionSource, Episode.class);
      int numShows = 0;
      int numSeasons = 0;
      int numEpisodes = 0;
      ArrayList<Show> shows = ShowLibrary.getInstance().shows;
      for(Show show : shows) {
        showDao.createOrUpdate(show);
        numShows++;
        for(Season season : show.seasons) {
          seasonDao.createOrUpdate(season);
          numSeasons++;
          for(Episode episode : season.getEpisodes()) {
            episodeDao.createOrUpdate(episode);
            numEpisodes++;
          }
        }
      }
      for(Show dbShow : showDao.queryForAll()) {
        Show myShow = ShowLibrary.getInstance().getShow(dbShow.getTitle());
        if(myShow==null) {
          for(Season season : dbShow.seasons) {
            episodeDao.delete(season.getEpisodes());
          }
          seasonDao.delete(dbShow.seasons);
          showDao.delete(dbShow);
        }
      }
      Dao<Settings, Integer> settingsDao = DaoManager.createDao(connectionSource, Settings.class);
      Dao<Feed, Integer> feedDao = DaoManager.createDao(connectionSource, Feed.class);
      Dao<Search, Integer> searchDao = DaoManager.createDao(connectionSource, Search.class);
      Settings settings = Settings.getInstance();
      for(Feed feed : settings.getFeeds()) {
        feedDao.createOrUpdate(feed);
      }
      for(Search search : settings.getSearches()) {
        searchDao.createOrUpdate(search);
      }
      settingsDao.createOrUpdate(settings);
      LOGGER.fine("Stored " + numShows + " shows, " + numSeasons + " seasons, and " + numEpisodes + " episodes in database.");
      connectionSource.close();
    } catch (SQLException e) {
      LOGGER.severe("SQLException: " + e.toString());
      System.exit(1);
    }
  }
View Full Code Here

TOP

Related Classes of com.j256.ormlite.jdbc.JdbcConnectionSource

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.