@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { checkCreate(qc); // URL to relational database final String url = string(toToken(exprs[0], qc)); final JDBCConnections jdbc = jdbc(qc); try { if (exprs.length > 2) { // credentials final String user = string(toToken(exprs[1], qc)); final String pass = string(toToken(exprs[2], qc)); if (exprs.length == 4) { // connection options final Options opts = toOptions(3, Q_OPTIONS, new Options(), qc); // extract auto-commit mode from options boolean ac = true; final HashMap<String, String> options = opts.free(); final String commit = options.get(AUTO_COMM); if (commit != null) { ac = Strings.yes(commit); options.remove(AUTO_COMM); } // connection properties final Properties props = connProps(options); props.setProperty(USER, user); props.setProperty(PASS, pass); // open connection final Connection conn = getConnection(url, props); // set auto/commit mode conn.setAutoCommit(ac); return Int.get(jdbc.add(conn)); } return Int.get(jdbc.add(getConnection(url, user, pass))); } return Int.get(jdbc.add(getConnection(url))); } catch (final SQLException ex) { throw BXSQ_ERROR_X.get(info, ex); } }
/** * Initializes the database context, based on the initial servlet context. Parses all context * parameters and passes them on to the database context. * * @param sc servlet context * @throws IOException I/O exception */ public static synchronized void init(final ServletContext sc) throws IOException { // check if HTTP context has already been initialized if (init) return; init = true; // set web application path as home directory and HTTPPATH final String webapp = sc.getRealPath("/"); Options.setSystem(Prop.PATH, webapp); Options.setSystem(GlobalOptions.WEBPATH, webapp); // bind all parameters that start with "org.basex." to system properties final Enumeration<String> en = sc.getInitParameterNames(); while (en.hasMoreElements()) { final String key = en.nextElement(); if (!key.startsWith(Prop.DBPREFIX)) continue; String val = sc.getInitParameter(key); if (key.endsWith("path") && !new File(val).isAbsolute()) { // prefix relative path with absolute servlet path Util.debug(key.toUpperCase(Locale.ENGLISH) + ": " + val); val = new IOFile(webapp, val).path(); } Options.setSystem(key, val); } // create context, update options if (context == null) { context = new Context(false); } else { context.globalopts.setSystem(); context.options.setSystem(); } // start server instance if (!context.globalopts.get(GlobalOptions.HTTPLOCAL)) new BaseXServer(context); }