Example #1
1
 public String tableColumns(String tableName, String columnPrefix) throws SQLException {
   ResultSet columns = executeQuery(driver.getQueries().getColumns(tableName));
   StringBuilder columnsBuilder = new StringBuilder();
   while (columns.next()) {
     if (columnsBuilder.length() > 0) columnsBuilder.append(',');
     columnsBuilder.append(columnPrefix).append(columns.getString("property.name"));
   }
   return columnsBuilder.toString();
 }
Example #2
0
  public void transformProcedure(Procedure proc) {
    // For each variable and field, look for that variable-field access
    //  combination, and replace it with the correct representation.
    for (Dim3Var var : implicitvars) {
      for (int entry = 0; entry < var.getNumEntries(); entry++) {
        AccessExpression old =
            // *cetus-1.1*  new AccessExpression(new Identifier(var.getString()),
            new AccessExpression(
                new NameID(var.getString()),
                AccessOperator.MEMBER_ACCESS,
                // *cetus-1.1*  new Identifier(var.getDimEntry(entry)));
                new NameID(var.getDimEntry(entry)));

        Expression replacement = var.getId(entry);

        if (Driver.getOptionValue("CEAN") != null && var == MCUDAUtils.Tidx)
          if (Driver.getOptionValue("CEANv2") != null)
            for (Expression e : MCUDAUtils.getBdim())
              replacement = new ArrayAccess(replacement, new ArraySlice());
          else replacement = new ArrayAccess(replacement, new ArraySlice());

        Tools.replaceAll(proc, old, replacement);
      }
    }
  }
  private String findDriverByUrlImpl(String url) {
    for (int i = 0; i < _driverList.size(); i++) {
      try {
        Driver driver = (Driver) _driverList.get(i);

        if (driver.acceptsURL(url)) return driver.getClass().getName();
      } catch (Exception e) {
        log.log(Level.FINE, e.toString(), e);
      }
    }

    return null;
  }
  private void initDriverList() {
    try {
      Thread thread = Thread.currentThread();
      ClassLoader loader = thread.getContextClassLoader();

      Enumeration iter = loader.getResources("META-INF/services/java.sql.Driver");
      while (iter.hasMoreElements()) {
        URL url = (URL) iter.nextElement();

        ReadStream is = null;
        try {
          is = Vfs.lookup(url.toString()).openRead();

          String filename;

          while ((filename = is.readLine()) != null) {
            int p = filename.indexOf('#');

            if (p >= 0) filename = filename.substring(0, p);

            filename = filename.trim();
            if (filename.length() == 0) continue;

            try {
              Class cl = Class.forName(filename, false, loader);
              Driver driver = null;

              if (Driver.class.isAssignableFrom(cl)) driver = (Driver) cl.newInstance();

              if (driver != null) {
                log.fine(L.l("DatabaseManager adding driver '{0}'", driver.getClass().getName()));

                _driverList.add(driver);
              }
            } catch (Exception e) {
              log.log(Level.FINE, e.toString(), e);
            }
          }
        } catch (Exception e) {
          log.log(Level.FINE, e.toString(), e);
        } finally {
          if (is != null) is.close();
        }
      }
    } catch (Exception e) {
      log.log(Level.FINE, e.toString(), e);
    }
  }
Example #5
0
  public Sql2oTest(Driver driverToRegister, String url, String user, String pass, String testName) {

    if (driverToRegister != null) {
      try {
        DriverManager.registerDriver(driverToRegister);
      } catch (SQLException e) {
        throw new RuntimeException(
            "could not register driver '" + driverToRegister.getClass().getName() + "'", e);
      }
    }

    this.sql2o = new Sql2o(url, user, pass);

    HashMap<String, String> defaultColumnMap = new HashMap<String, String>();
    defaultColumnMap.put("ID", "id");
    defaultColumnMap.put("NAME", "name");
    defaultColumnMap.put("EMAIL", "email");
    defaultColumnMap.put("TEXT", "text");
    defaultColumnMap.put("ANUMBER", "aNumber");
    defaultColumnMap.put("ALONGNUMBER", "aLongNumber");
    sql2o.setDefaultColumnMappings(defaultColumnMap);

    this.url = url;
    this.user = user;
    this.pass = pass;

    if ("HyperSQL DB test".equals(testName)) {
      sql2o.createQuery("set database sql syntax MSS true").executeUpdate();
    }
  }
Example #6
0
 public Iterable<Expression> returnProperties(String tableName, String columnPrefix)
     throws SQLException {
   ResultSet columns = executeQuery(driver.getQueries().getColumns(tableName));
   List<Expression> properties = new ArrayList<Expression>();
   while (columns.next()) {
     properties.add(
         CypherQuery.identifier(columnPrefix).property(columns.getString("property.name")));
   }
   return properties;
 }
Example #7
0
  public void testMajorVersion() {
    try {
      Properties props = P6TestUtil.loadProperties("P6Test.properties");
      String url = props.getProperty("url");

      Driver driver = DriverManager.getDriver(url);

      // make sure you have a p6 driver
      if (!(driver instanceof P6SpyDriverCore)) {
        fail("Expected to get back a p6spy driver, got back a " + driver);
      }

      // but make sure it's bound to something
      // these numbers will likely change over time :)
      assertEquals(1, driver.getMajorVersion());
      assertEquals(0, driver.getMinorVersion());

    } catch (Exception e) {
      e.printStackTrace(System.out);
      fail("unexpected exception: " + e);
    } finally {
    }
  }
Example #8
0
 @XmlTransient
 public Driver getDriver() throws ScriptException {
   String driverClass = getPeristencePropertiesMap().get("hibernate.connection.driver_class");
   if (driverClass == null) {
     return Driver.derby;
     //            return Driver.h2;
   }
   Driver[] drivers = Driver.values();
   for (int i = 0; i < drivers.length; i++) {
     if (drivers[i].driverClass.equals(driverClass)) {
       return drivers[i];
     }
   }
   throw new IllegalArgumentException("Unsupported driver: " + driverClass);
 }
Example #9
0
  private Connection openDB(final DBConfiguration dbConfiguration) throws DatabaseException {
    final String connectionURL = dbConfiguration.getConnectionString();
    final String jdbcClassName = dbConfiguration.getDriverClassname();

    try {
      final byte[] jdbcDriverBytes = dbConfiguration.getJdbcDriver();
      if (jdbcDriverBytes != null) {
        LOGGER.debug("loading JDBC database driver stored in configuration");
        final JarClassLoader jarClassLoader = new JarClassLoader();
        jarClassLoader.add(new ByteArrayInputStream(jdbcDriverBytes));
        final JclObjectFactory jclObjectFactory = JclObjectFactory.getInstance();

        // Create object of loaded class
        driver = (Driver) jclObjectFactory.create(jarClassLoader, jdbcClassName);

        LOGGER.debug(
            "successfully loaded JDBC database driver '"
                + jdbcClassName
                + "' from application configuration");
      }
    } catch (Throwable e) {
      final String errorMsg =
          "error registering JDBC database driver stored in configuration: " + e.getMessage();
      final ErrorInformation errorInformation =
          new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
      LOGGER.error(errorMsg, e);
      throw new DatabaseException(errorInformation);
    }

    if (driver == null) {
      try {
        LOGGER.debug("loading JDBC database driver from classpath: " + jdbcClassName);
        driver = (Driver) Class.forName(jdbcClassName).newInstance();

        LOGGER.debug("successfully loaded JDBC database driver from classpath: " + jdbcClassName);
      } catch (Throwable e) {
        final String errorMsg =
            e.getClass().getName()
                + " error loading JDBC database driver from classpath: "
                + e.getMessage();
        final ErrorInformation errorInformation =
            new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
        throw new DatabaseException(errorInformation);
      }
    }

    try {
      LOGGER.debug("opening connection to database " + connectionURL);
      final Properties connectionProperties = new Properties();
      if (dbConfiguration.getUsername() != null && !dbConfiguration.getUsername().isEmpty()) {
        connectionProperties.setProperty("user", dbConfiguration.getUsername());
      }
      if (dbConfiguration.getPassword() != null) {
        connectionProperties.setProperty(
            "password", dbConfiguration.getPassword().getStringValue());
      }
      final Connection connection = driver.connect(connectionURL, connectionProperties);

      final Map<PwmAboutProperty, String> debugProps = getConnectionDebugProperties(connection);
      ;
      LOGGER.debug(
          "successfully opened connection to database "
              + connectionURL
              + ", properties: "
              + JsonUtil.serializeMap(debugProps));

      connection.setAutoCommit(true);
      return connection;
    } catch (Throwable e) {
      final String errorMsg =
          "error connecting to database: " + Helper.readHostileExceptionMessage(e);
      final ErrorInformation errorInformation =
          new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
      if (e instanceof IOException) {
        LOGGER.error(errorInformation);
      } else {
        LOGGER.error(errorMsg, e);
      }
      throw new DatabaseException(errorInformation);
    }
  }
Example #10
0
  /**
   * get train info from apix web
   *
   * @param from start station code
   * @param to destinatioin station code
   * @param date query date formated like '2015-10-22'
   * @return json data
   */
  private static String getFromAPIX(String from, String to, String date) throws IOException {
    String httpUrl = "http://a.apix.cn/apixlife/ticket/rest";
    String httpArg = "from=" + from + "&to=" + to + "&date=" + date;

    return Driver.requestAPIX(httpUrl, httpArg, APIKEY);
  }