public synchronized void delete() {
    // Drop main table and any other tables with the main table prefix

    // First get a list of the tables to drop
    ArrayList<String> tablesToDrop = new ArrayList<String>();

    Connection con;
    try {
      con = connectionPool.getConnection();
    } catch (ConnectionException e) {
      throw new BeanFactoryException(e);
    }

    try {
      Statement stmt = con.createStatement();
      if (printSQL != null) printDebug("deleteTable: SHOW TABLES");
      ResultSet rs = stmt.executeQuery("SHOW TABLES");

      while (rs.next()) {
        String s = rs.getString(1);
        if (File.separatorChar == '\\') {
          // It's windows...case insensitive matching
          String lower = s.toLowerCase();
          if (lower.equalsIgnoreCase(tableName)) tablesToDrop.add(s);
          if (lower.startsWith(tableName.toLowerCase() + "_")) tablesToDrop.add(s);
        } else {
          // It's Unix...case counts
          if (s.equals(tableName)) tablesToDrop.add(s);
          if (s.startsWith(tableName + "_")) tablesToDrop.add(s);
        }
      }

      rs.close();
      stmt.close();

      for (String name : tablesToDrop) {
        stmt = con.createStatement();
        String sql = "DROP TABLE " + name;
        if (printSQL != null) printDebug("deleteTable: " + sql);
        stmt.executeUpdate(sql);
        stmt.close();
      }

      connectionPool.releaseConnection(con);
    } catch (SQLException e) {
      try {
        con.close();
      } catch (SQLException e2) {
      }
      throw new BeanFactoryException(e);
    }
  }
  private String[] getPrimaryKeyNamesFromTable() {
    Connection con;
    try {
      con = connectionPool.getConnection();
    } catch (ConnectionException e) {
      throw new BeanFactoryException(e);
    }

    try {
      Statement stmt = con.createStatement();
      String sql = "DESCRIBE " + tableName;
      if (printSQL != null) printDebug("getPrimaryKeyNamesFromTable: " + sql);
      ResultSet rs = stmt.executeQuery(sql);

      ArrayList<String> list = new ArrayList<String>();
      while (rs.next()) {
        String name = rs.getString(1);
        // String sqlType = rs.getString(2);
        // boolean nonNull = !rs.getBoolean(3);
        boolean primaryKey = rs.getString(4).equalsIgnoreCase("PRI");
        if (primaryKey) {
          if (!name.contains(Property.META_SEPARATOR)) {
            list.add(name);
          } else {
            String prefix = name.substring(0, name.indexOf(Property.META_SEPARATOR));
            if (!list.contains(prefix)) list.add(prefix);
          }
        }
      }

      stmt.close();
      connectionPool.releaseConnection(con);

      // Don't check anymore now that tables with only one row and no primary key are allowed.
      //          if (list.size() == 0) throw new BeanFactoryException("Could not find any primary
      // key in table: "+tableName);

      return list.toArray(new String[list.size()]);
    } catch (SQLException e) {
      try {
        con.close();
      } catch (SQLException e2) {
      }
      throw new BeanFactoryException(e);
    }
  }
  public boolean exists() {
    // Returns true if main table exists.
    // If main table exists, but aux tables are missing, then the world is inconsistent
    // ...you'll get an error during creation allowing admin to examine existing tables before
    // dropping them.
    // Likewise if main table doesn't exist, but aux tables do.

    Connection con;
    try {
      con = connectionPool.getConnection();
    } catch (ConnectionException e) {
      throw new BeanFactoryException(e);
    }

    try {
      Statement stmt = con.createStatement();
      if (printSQL != null) printDebug("tableExists(" + tableName + "): SHOW TABLES");
      ResultSet rs = stmt.executeQuery("SHOW TABLES");

      boolean answer = false;
      while (rs.next() && !answer) {
        String s = rs.getString(1);
        if (tableName.equalsIgnoreCase(s)) answer = true;
      }

      stmt.close();
      connectionPool.releaseConnection(con);

      return answer;
    } catch (SQLException e) {
      try {
        con.close();
      } catch (SQLException e2) {
      }
      throw new BeanFactoryException(e);
    }
  }
Esempio n. 4
0
  private void withConnection(URI uri, Properties props, ConnectionCallback cb)
      throws SQLException {
    ConnectionPool cp;

    synchronized (connectionPools) {
      ConnectionKey key = new ConnectionKey(uri, props);

      cp = connectionPools.get(key);

      if (cp == null) {
        cp = new ConnectionPool(uri, props);
        connectionPools.put(key, cp);
      }
    }

    // If this thread is already inside withConnection(), re-use the
    // same PooledConnection as the outermost withConnection() call
    // returned.
    PerThreadConnection ptc = perThreadConnection.get();

    if (ptc.refCounter == 0) {
      ++ptc.refCounter;
      ptc.pooledConnection = cp.getConnection();
    }

    try {
      cb.execute(ptc.pooledConnection);
    } finally {
      --ptc.refCounter;

      if (ptc.refCounter == 0) {
        cp.releaseConnection(ptc.pooledConnection);
        ptc.pooledConnection = null;
      }
    }
  }
  public synchronized void create(String... primaryKeyNames) {
    PrimaryKeyInfo<B> primaryKeyInfo =
        PrimaryKeyInfo.getInstance(beanClass, primaryKeyNames, referencedFactories);
    Property[] primaryKeyProperties = primaryKeyInfo.getProperties();
    Property[] properties =
        Property.deriveProperties(beanClass, primaryKeyInfo, referencedFactories);

    String primaryKeyColumnNamesCommaSeparated =
        appendColumnNamesCommaSeparated(new StringBuffer(), primaryKeyInfo.getProperties())
            .toString();

    // Make main table
    StringBuffer b = new StringBuffer();
    b.append("create table ");
    b.append(tableName);
    b.append(" (");
    for (int i = 0; i < properties.length; i++) {
      if (i > 0) b.append(", ");
      Property prop = properties[i];
      if (prop.isPrimaryKeyProperty()
          && prop.getType() == int.class
          && primaryKeyInfo.getProperties().length == 1) {
        b.append(prop.getName());
        b.append(" INT NOT NULL AUTO_INCREMENT");
      } else if (prop.isPrimaryKeyProperty()
          && prop.getType() == long.class
          && primaryKeyInfo.getProperties().length == 1) {
        b.append(prop.getName());
        b.append(" BIGINT NOT NULL AUTO_INCREMENT");
      } else if (prop.isArray()) {
        b.append(prop.getName());
        b.append(" BOOLEAN NOT NULL DEFAULT 0");
      } else {
        String[] columnNames = prop.getColumnNames();
        Class<?>[] columnTypes = prop.getColumnTypes();
        int[] columnStrLens = prop.getColumnMaxStrLens();
        for (int j = 0; j < columnNames.length; j++) {
          if (j > 0) b.append(", ");
          b.append(columnNames[j]);
          b.append(' ');
          b.append(javaToSql(columnTypes[j], prop, columnStrLens[j]));
        }
      }
    }

    if (primaryKeyProperties.length > 0) {
      b.append(", PRIMARY KEY(");
      b.append(primaryKeyColumnNamesCommaSeparated);
      b.append(')');
    }

    b.append(')');

    Connection con;
    try {
      con = connectionPool.getConnection();
    } catch (ConnectionException e) {
      throw new BeanFactoryException(e);
    }

    try {
      Statement stmt = con.createStatement();
      if (printSQL != null) printDebug("createTable: " + b);
      stmt.executeUpdate(b.toString());
      stmt.close();
    } catch (SQLException e) {
      try {
        con.close();
      } catch (SQLException e2) {
        /* ignore */
      }
      throw new BeanFactoryException(
          "Error creating table \"" + tableName + "\": " + e.getMessage());
    }

    // If there are any array columns, make auxiliary tables to hold their data
    for (Property prop : properties) {
      if (prop.isArray()) {
        try {
          Statement stmt = con.createStatement();
          StringBuffer sql = new StringBuffer();
          sql.append("CREATE TABLE ").append(tableName).append('_');
          sql.append(prop.getName().toLowerCase()).append(" (");
          for (int i = 0; i < primaryKeyProperties.length; i++) {
            if (i > 0) sql.append(", ");
            String[] priKeyColumnNames = primaryKeyProperties[i].getColumnNames();
            Class<?>[] priKeyColumnTypes = primaryKeyProperties[i].getColumnTypes();
            int[] priKeyColStrLens = primaryKeyProperties[i].getColumnMaxStrLens();
            for (int j = 0; j < priKeyColumnNames.length; j++) {
              if (j > 0) sql.append(", ");
              sql.append(priKeyColumnNames[j]);
              sql.append(' ');
              sql.append(
                  javaToSql(priKeyColumnTypes[j], primaryKeyProperties[i], priKeyColStrLens[j]));
            }
          }
          sql.append(", ");
          sql.append(ARRAY_POS_COLUMN_NAME);
          sql.append(" INTEGER NOT NULL");
          String[] columnNames = prop.getColumnNames();
          Class<?>[] columnTypes = prop.getColumnTypes();
          int[] columnStrLens = prop.getColumnMaxStrLens();
          for (int j = 0; j < columnNames.length; j++) {
            sql.append(", ");
            sql.append(columnNames[j]);
            sql.append(' ');
            sql.append(javaToSql(columnTypes[j], prop, columnStrLens[j]));
          }
          sql.append(", PRIMARY KEY(");
          sql.append(primaryKeyColumnNamesCommaSeparated);
          sql.append(',');
          sql.append(ARRAY_POS_COLUMN_NAME);
          sql.append("))");
          if (printSQL != null) printDebug("createTable: " + sql);
          stmt.executeUpdate(sql.toString());
          stmt.close();
        } catch (SQLException e) {
          try {
            con.close();
          } catch (SQLException e2) {
            /* ignore */
          }
          throw new BeanFactoryException(
              "Error creating auxiliary table \""
                  + tableName
                  + '_'
                  + prop.getName()
                  + "\": "
                  + e.getMessage());
        }
      }
    }
    connectionPool.releaseConnection(con);
  }