Package com.j256.ormlite.support

Examples of com.j256.ormlite.support.ConnectionSource


      throw SqlExceptionUtil.create("problems rolling back transaction", e);
    }
  }

  public CompiledStatement compileStatement(String statement, StatementType type, FieldType[] argFieldTypes) {
    CompiledStatement stmt = new AndroidCompiledStatement(statement, db, type);
    return stmt;
  }
View Full Code Here


  /**
   * Satisfies the {@link SQLiteOpenHelper#onCreate(SQLiteDatabase)} interface method.
   */
  @Override
  public final void onCreate(SQLiteDatabase db) {
    ConnectionSource cs = getConnectionSource();
    /*
     * The method is called by Android database helper's get-database calls when Android detects that we need to
     * create or update the database. So we have to use the database argument and save a connection to it on the
     * 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);
      }
    }
    try {
      onCreate(db, cs);
    } finally {
      if (clearSpecial) {
        cs.clearSpecialConnection(conn);
      }
    }
  }
View Full Code Here

  /**
   * Satisfies the {@link SQLiteOpenHelper#onUpgrade(SQLiteDatabase, int, int)} interface method.
   */
  @Override
  public final void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    ConnectionSource cs = getConnectionSource();
    /*
     * The method is called by Android database helper's get-database calls when Android detects that we need to
     * create or update the database. So we have to use the database argument and save a connection to it on the
     * 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);
      }
    }
    try {
      onUpgrade(db, cs, oldVersion, newVersion);
    } finally {
      if (clearSpecial) {
        cs.clearSpecialConnection(conn);
      }
    }
  }
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

    }
  }

  public static void loadShows() {
    try {
      ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:file:" + dbFileName);
      Dao<Show, Integer> showDao = DaoManager.createDao(connectionSource, Show.class);
      QueryBuilder<Show, Integer> queryBuilder = showDao.queryBuilder().orderBy("title", true);
      PreparedQuery<Show> preparedQuery = queryBuilder.prepare();
      ShowLibrary.getInstance().shows.clear();
      ShowLibrary.getInstance().shows.addAll(showDao.query(preparedQuery));
      connectionSource.close();
    } catch (SQLException e) {
      LOGGER.severe("SQLException: " + e.toString());
      System.exit(1);
    }
    int numShows = 0;
View Full Code Here

  }

  public static Settings getSettings() {
    Settings settings = null;
    try {
      ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:file:" + dbFileName);
      Dao<Settings, Integer> settingsDao = DaoManager.createDao(connectionSource, Settings.class);
      QueryBuilder<Settings, Integer> queryBuilder = settingsDao.queryBuilder();
      queryBuilder.where().isNotNull("databaseId");
      PreparedQuery<Settings> preparedQuery = queryBuilder.prepare();
      settings = settingsDao.queryForFirst(preparedQuery);
      settingsDao.update(settings);
      connectionSource.close();
    } catch (SQLException e) {
      LOGGER.severe("SQLException: " + e.toString());
      System.exit(1);
    }
    LOGGER.fine("Loaded settings from database");
View Full Code Here

    return settings;
  }

  public static void clear() {
    try {
    ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:file:" + dbFileName);
    TableUtils.dropTable(connectionSource, Show.class, true);
    TableUtils.dropTable(connectionSource, Season.class, true);
    TableUtils.dropTable(connectionSource, Episode.class, true);
    TableUtils.dropTable(connectionSource, Settings.class, true);
    TableUtils.dropTable(connectionSource, Feed.class, true);
View Full Code Here

  @SuppressWarnings("unchecked")
  @Test
  public void testForeignAutoRefresh() throws Exception {
    Field field = ForeignAutoRefresh.class.getDeclaredField("foreign");
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    DatabaseConnection connection = createMock(DatabaseConnection.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    expect(connectionSource.getReadOnlyConnection()).andReturn(connection);
    ForeignForeign foreignForeign = new ForeignForeign();
    String stuff = "21312j3213";
    int id = 4123123;
    foreignForeign.id = id;
    foreignForeign.stuff = stuff;
    expect(
        connection.queryForOne(isA(String.class), isA(Object[].class), isA(FieldType[].class),
            isA(GenericRowMapper.class))).andReturn(foreignForeign);
    connectionSource.releaseConnection(connection);
    DatabaseResults results = createMock(DatabaseResults.class);
    ForeignAutoRefresh foreign = new ForeignAutoRefresh();
    replay(results, connectionSource, connection);
    FieldType fieldType =
        FieldType.createFieldType(connectionSource, ForeignAutoRefresh.class.getSimpleName(), field,
View Full Code Here

  private final DatabaseType databaseType = new StubDatabaseType();

  @Test(expected = SQLException.class)
  public void testNoIdBuildDelete() throws Exception {
    DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    replay(connectionSource);
    MappedDeleteCollection.deleteObjects(databaseType,
        new TableInfo<NoId, Void>(connectionSource, null, NoId.class), databaseConnection,
        new ArrayList<NoId>());
  }
View Full Code Here

TOP

Related Classes of com.j256.ormlite.support.ConnectionSource

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.