@Override public Value createBlob(InputStream in, long maxLength) { init(); int type = Value.BLOB; if (maxLength < 0) { maxLength = Long.MAX_VALUE; } int max = (int) Math.min(maxLength, database.getMaxLengthInplaceLob()); try { if (max != 0 && max < Integer.MAX_VALUE) { BufferedInputStream b = new BufferedInputStream(in, max); b.mark(max); byte[] small = new byte[max]; int len = IOUtils.readFully(b, small, max); if (len < max) { if (len < small.length) { small = Arrays.copyOf(small, len); } return ValueLobDb.createSmallLob(type, small); } b.reset(); in = b; } if (maxLength != Long.MAX_VALUE) { in = new LimitInputStream(in, maxLength); } return createLob(in, type); } catch (IllegalStateException e) { throw DbException.get(ErrorCode.OBJECT_CLOSED, e); } catch (IOException e) { throw DbException.convertIOException(e, null); } }
@Override public Value convertPrecision(long precision, boolean force) { if (this.precision <= precision) { return this; } ValueLobDb lob; if (type == CLOB) { if (handler == null) { try { int p = MathUtils.convertLongToInt(precision); String s = IOUtils.readStringAndClose(getReader(), p); byte[] data = s.getBytes(Constants.UTF8); lob = ValueLobDb.createSmallLob(type, data, s.length()); } catch (IOException e) { throw DbException.convertIOException(e, null); } } else { lob = ValueLobDb.createTempClob(getReader(), precision, handler); } } else { if (handler == null) { try { int p = MathUtils.convertLongToInt(precision); byte[] data = IOUtils.readBytesAndClose(getInputStream(), p); lob = ValueLobDb.createSmallLob(type, data, data.length); } catch (IOException e) { throw DbException.convertIOException(e, null); } } else { lob = ValueLobDb.createTempBlob(getInputStream(), precision, handler); } } return lob; }
/** * Returns a substring. * * @param pos the position (the first character is at position 1) * @param length the number of characters * @return the string */ public String getSubString(long pos, int length) throws SQLException { try { if (isDebugEnabled()) { debugCode("getSubString(" + pos + ", " + length + ");"); } checkClosed(); if (pos < 1) { throw DbException.getInvalidValueException("pos", pos); } if (length < 0) { throw DbException.getInvalidValueException("length", length); } StringWriter writer = new StringWriter(Math.min(Constants.IO_BUFFER_SIZE, length)); Reader reader = value.getReader(); try { IOUtils.skipFully(reader, pos - 1); IOUtils.copyAndCloseInput(reader, writer, length); } finally { reader.close(); } return writer.toString(); } catch (Exception e) { throw logAndConvert(e); } }
/** * Called to flush the output after data has been sent to the server and just before receiving * data. This method also reads the status code from the server and throws any exception the * server sent. * * @param transfer the transfer object * @throws DbException if the server sent an exception * @throws IOException if there is a communication problem between client and server */ public void done(Transfer transfer) throws IOException { transfer.flush(); int status = transfer.readInt(); if (status == STATUS_ERROR) { String sqlstate = transfer.readString(); String message = transfer.readString(); String sql = transfer.readString(); int errorCode = transfer.readInt(); String stackTrace = transfer.readString(); JdbcSQLException s = new JdbcSQLException(message, sql, sqlstate, errorCode, null, stackTrace); if (errorCode == ErrorCode.CONNECTION_BROKEN_1) { // allow re-connect IOException e = new IOException(s.toString(), s); throw e; } throw DbException.convert(s); } else if (status == STATUS_CLOSED) { transferList = null; } else if (status == STATUS_OK_STATE_CHANGED) { sessionStateChanged = true; } else if (status == STATUS_OK) { // ok } else { throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "unexpected status " + status); } }
@Override public int update() { session.getUser().checkAdmin(); session.commit(true); Database db = session.getDatabase(); if (db.findRole(userName) != null) { throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName); } if (db.findUser(userName) != null) { if (ifNotExists) { return 0; } throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName); } int id = getObjectId(); User user = new User(db, id, userName, false); user.setAdmin(admin); user.setComment(comment); if (hash != null && salt != null) { user.setSaltAndHash(getByteArray(salt), getByteArray(hash)); } else if (password != null) { char[] passwordChars = getCharArray(password); byte[] userPasswordHash; if (userName.length() == 0 && passwordChars.length == 0) { userPasswordHash = new byte[0]; } else { userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars); } user.setUserPasswordHash(userPasswordHash); } else { throw DbException.throwInternalError(); } db.addDatabaseObject(session, user); return 0; }
/** * Create a connection info object. * * @param u the database URL (must start with jdbc:h2:) * @param info the connection properties */ public ConnectionInfo(String u, Properties info) { u = remapURL(u); this.originalURL = u; // originalURL不会再变 if (!u.startsWith(Constants.START_URL)) { // "jdbc:h2:" throw DbException.getInvalidValueException("url", u); } this.url = u; // url在接下来的代码中会再变,去掉参数 readProperties(info); readSettingsFromURL(); setUserName(removeProperty("USER", "")); convertPasswords(); // 去掉"jdbc:h2:",比如jdbc:h2:tcp://localhost:9092/test9 // name = tcp://localhost:9092/test9 name = url.substring(Constants.START_URL.length()); parseName(); String recoverTest = removeProperty("RECOVER_TEST", null); if (recoverTest != null) { FilePathRec.register(); try { Utils.callStaticMethod("org.h2.store.RecoverTester.init", recoverTest); } catch (Exception e) { throw DbException.convert(e); } name = "rec:" + name; } }
@Override public void prepare() { if (columns == null) { // 如INSERT INTO InsertTest DEFAULT VALUES if (list.size() > 0 && list.get(0).length == 0) { // special case where table is used as a sequence columns = new Column[0]; } else { // 如INSERT INTO InsertTest(SELECT * FROM tmpSelectTest) columns = table.getColumns(); } } if (list.size() > 0) { for (Expression[] expr : list) { if (expr.length != columns.length) { throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); } for (int i = 0, len = expr.length; i < len; i++) { Expression e = expr[i]; if (e != null) { e = e.optimize(session); if (e instanceof Parameter) { Parameter p = (Parameter) e; p.setColumn(columns[i]); } expr[i] = e; } } } } else { query.prepare(); if (query.getColumnCount() != columns.length) { throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); } } }
public Index getScanIndex(Session session) { if (createException != null) { String msg = createException.getMessage(); throw DbException.get(ErrorCode.VIEW_IS_INVALID_2, createException, getSQL(), msg); } PlanItem item = getBestPlanItem(session, null); return item.getIndex(); }
private void copyData() { if (table.isTemporary()) { throw DbException.getUnsupportedException("TEMP TABLE"); } Database db = session.getDatabase(); String baseName = table.getName(); String tempName = db.getTempTableName(baseName, session); Column[] columns = table.getColumns(); ArrayList<Column> newColumns = New.arrayList(); Table newTable = cloneTableStructure(columns, db, tempName, newColumns); try { // check if a view would become invalid // (because the column to drop is referenced or so) checkViews(table, newTable); } catch (DbException e) { execute("DROP TABLE " + newTable.getName(), true); throw DbException.get(ErrorCode.VIEW_IS_INVALID_2, e, getSQL(), e.getMessage()); } String tableName = table.getName(); ArrayList<TableView> views = table.getViews(); if (views != null) { views = New.arrayList(views); for (TableView view : views) { table.removeView(view); } } execute("DROP TABLE " + table.getSQL() + " IGNORE", true); db.renameSchemaObject(session, newTable, tableName); for (DbObject child : newTable.getChildren()) { if (child instanceof Sequence) { continue; } String name = child.getName(); if (name == null || child.getCreateSQL() == null) { continue; } if (name.startsWith(tempName + "_")) { name = name.substring(tempName.length() + 1); SchemaObject so = (SchemaObject) child; if (so instanceof Constraint) { if (so.getSchema().findConstraint(session, name) != null) { name = so.getSchema().getUniqueConstraintName(session, newTable); } } else if (so instanceof Index) { if (so.getSchema().findIndex(session, name) != null) { name = so.getSchema().getUniqueIndexName(session, newTable, name); } } db.renameSchemaObject(session, so, name); } } if (views != null) { for (TableView view : views) { String sql = view.getCreateSQL(true, true); execute(sql, true); } } }
/** * Create a duplicate key exception with a message that contains the index name. * * @param key the key values * @return the exception */ protected DbException getDuplicateKeyException(String key) { String sql = getName() + " ON " + table.getSQL() + "(" + getColumnListSQL() + ")"; if (key != null) { sql += " VALUES " + key; } DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, sql); e.setSource(this); return e; }
/** * Undo all operations back to the log position of the given savepoint. * * @param name the savepoint name */ public void rollbackToSavepoint(String name) { checkCommitRollback(); if (savepoints == null) { throw DbException.get(ErrorCode.SAVEPOINT_IS_INVALID_1, name); } Savepoint savepoint = savepoints.get(name); if (savepoint == null) { throw DbException.get(ErrorCode.SAVEPOINT_IS_INVALID_1, name); } rollbackTo(savepoint, false); }
/** * INTERNAL. Check if this connection is closed. * * @param write if the next operation is possibly writing * @throws SQLException if the connection or session is closed */ protected void checkClosed(boolean write) throws SQLException { if (session == null) { throw DbException.get(ErrorCode.OBJECT_CLOSED); } if (session.isClosed()) { throw DbException.get(ErrorCode.DATABASE_CALLED_AT_SHUTDOWN); } if (session.isReconnectNeeded(write)) { trace.debug("reconnect"); closePreparedCommands(); session = session.reconnect(write); setTrace(session.getTrace()); } }
/** * Automatically re-connect if necessary and if configured to do so. * * @param count the retry count index * @return true if reconnected */ private boolean autoReconnect(int count) { if (!isClosed()) { return false; } if (!autoReconnect) { return false; } if (!cluster && !autoCommit) { return false; } if (count > SysProperties.MAX_RECONNECT) { return false; } lastReconnect++; while (true) { try { embedded = connectEmbeddedOrServer(false); break; } catch (DbException e) { if (e.getErrorCode() != ErrorCode.DATABASE_IS_IN_EXCLUSIVE_MODE) { throw e; } // exclusive mode: re-try endlessly try { Thread.sleep(500); } catch (Exception e2) { // ignore } } } if (embedded == this) { // connected to a server somewhere else embedded = null; } else { // opened an embedded connection now - // must connect to this database in server mode // unfortunately connectEmbeddedOrServer(true); } recreateSessionState(); if (eventListener != null) { eventListener.setProgress( DatabaseEventListener.STATE_RECONNECTED, databaseName, count, SysProperties.MAX_RECONNECT); } return true; }
@Override public void add(Session session, Row row) { if (closed) { throw DbException.throwInternalError(); } TreeNode i = new TreeNode(row); TreeNode n = root, x = n; boolean isLeft = true; while (true) { if (n == null) { if (x == null) { root = i; rowCount++; return; } set(x, isLeft, i); break; } Row r = n.row; int compare = compareRows(row, r); if (compare == 0) { if (indexType.isUnique()) { if (!containsNullAndAllowMultipleNull(row)) { throw getDuplicateKeyException(row.toString()); } } compare = compareKeys(row, r); } isLeft = compare < 0; x = n; n = child(x, isLeft); } balance(x, isLeft); rowCount++; }
@Override public Value getValue(Session session) { query.setSession(session); ResultInterface result = query.query(2); try { int rowcount = result.getRowCount(); if (rowcount > 1) { throw DbException.get(ErrorCode.SCALAR_SUBQUERY_CONTAINS_MORE_THAN_ONE_ROW); } Value v; if (rowcount <= 0) { v = ValueNull.INSTANCE; } else { result.next(); Value[] values = result.currentRow(); if (result.getVisibleColumnCount() == 1) { v = values[0]; } else { v = ValueArray.get(values); } } return v; } finally { result.close(); } }
/** * Commit or roll back the given transaction. * * @param transactionName the name of the transaction * @param commit true for commit, false for rollback */ public void setPreparedTransaction(String transactionName, boolean commit) { if (currentTransactionName != null && currentTransactionName.equals(transactionName)) { if (commit) { commit(false); } else { rollback(); } } else { ArrayList<InDoubtTransaction> list = database.getInDoubtTransactions(); int state = commit ? InDoubtTransaction.COMMIT : InDoubtTransaction.ROLLBACK; boolean found = false; if (list != null) { for (InDoubtTransaction p : list) { if (p.getTransactionName().equals(transactionName)) { p.setState(state); found = true; break; } } } if (!found) { throw DbException.get(ErrorCode.TRANSACTION_NOT_FOUND_1, transactionName); } } }
/** * Add an undo log entry to this session. * * @param table the table * @param operation the operation type (see {@link UndoLogRecord}) * @param row the row */ public void log(Table table, short operation, Row row) { if (table.isMVStore()) { return; } if (undoLogEnabled) { UndoLogRecord log = new UndoLogRecord(table, operation, row); // called _after_ the row was inserted successfully into the table, // otherwise rollback will try to rollback a not-inserted row if (SysProperties.CHECK) { int lockMode = database.getLockMode(); if (lockMode != Constants.LOCK_MODE_OFF && !database.isMultiVersion()) { String tableType = log.getTable().getTableType(); if (locks.indexOf(log.getTable()) < 0 && !Table.TABLE_LINK.equals(tableType) && !Table.EXTERNAL_TABLE_ENGINE.equals(tableType)) { DbException.throwInternalError(); } } } undoLog.add(log); } else { if (database.isMultiVersion()) { // see also UndoLogRecord.commit ArrayList<Index> indexes = table.getIndexes(); for (int i = 0, size = indexes.size(); i < size; i++) { Index index = indexes.get(i); index.commit(operation, row); } row.commit(); } } }
static { ArrayList<String> list = SetTypes.getTypes(); HashSet<String> set = KNOWN_SETTINGS; set.addAll(list); String[] connectionTime = { "ACCESS_MODE_DATA", "AUTOCOMMIT", "CIPHER", "CREATE", "CACHE_TYPE", "FILE_LOCK", "IGNORE_UNKNOWN_SETTINGS", "IFEXISTS", "INIT", "PASSWORD", "RECOVER", "RECOVER_TEST", "USER", "AUTO_SERVER", "AUTO_SERVER_PORT", "NO_UPGRADE", "AUTO_RECONNECT", "OPEN_NEW", "PAGE_SIZE", "PASSWORD_HASH", "JMX" }; for (String key : connectionTime) { if (SysProperties.CHECK && set.contains(key)) { DbException.throwInternalError(key); } set.add(key); } }
private void sendError(Throwable t) { try { SQLException e = DbException.convert(t).getSQLException(); StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); String trace = writer.toString(); String message; String sql; if (e instanceof JdbcSQLException) { JdbcSQLException j = (JdbcSQLException) e; message = j.getOriginalMessage(); sql = j.getSQL(); } else { message = e.getMessage(); sql = null; } transfer .writeInt(SessionRemote.STATUS_ERROR) .writeString(e.getSQLState()) .writeString(message) .writeString(sql) .writeInt(e.getErrorCode()) .writeString(trace) .flush(); } catch (Exception e2) { if (!transfer.isClosed()) { server.traceError(e2); } // if writing the error does not work, close the connection stop = true; } }
@Override public Index getScanIndex(Session session) { if (getStep(session) == 0) { throw DbException.get(ErrorCode.STEP_SIZE_MUST_NOT_BE_ZERO); } return new RangeIndex(this, IndexColumn.wrap(columns)); }
/** * Remove all pages until the given data page. * * @param trunkPage the first trunk page * @param firstDataPageToKeep the first data page to keep * @return the trunk page of the data page to keep */ private int removeUntil(int trunkPage, int firstDataPageToKeep) { trace.debug("log.removeUntil " + trunkPage + " " + firstDataPageToKeep); int last = trunkPage; while (true) { Page p = store.getPage(trunkPage); PageStreamTrunk t = (PageStreamTrunk) p; if (t == null) { throw DbException.throwInternalError( "log.removeUntil not found: " + firstDataPageToKeep + " last " + last); } logKey = t.getLogKey(); last = t.getPos(); if (t.contains(firstDataPageToKeep)) { return last; } trunkPage = t.getNextTrunk(); IntArray list = new IntArray(); list.add(t.getPos()); for (int i = 0; ; i++) { int next = t.getPageData(i); if (next == -1) { break; } list.add(next); } freeLogPages(list); pageOut.free(t); } }
/** * Prepare a transaction. * * @param session the session * @param transaction the name of the transaction */ void prepareCommit(Session session, String transaction) { if (trace.isDebugEnabled()) { trace.debug("log prepare commit s: " + session.getId() + ", " + transaction); } if (store.getDatabase().getPageStore() == null) { // database already closed return; } // store it on a separate log page int pageSize = store.getPageSize(); pageOut.flush(); pageOut.fillPage(); Data buffer = getBuffer(); buffer.writeByte((byte) PREPARE_COMMIT); buffer.writeVarInt(session.getId()); buffer.writeString(transaction); if (buffer.length() >= PageStreamData.getCapacity(pageSize)) { throw DbException.getInvalidValueException("transaction name (too long)", transaction); } write(buffer); // store it on a separate log page flushOut(); pageOut.fillPage(); if (store.getDatabase().getFlushOnEachCommit()) { flush(); } }
/** * Get the value of the given property. * * @param key the property key * @param defaultValue the default value * @return the value as a String */ public String getProperty(String key, String defaultValue) { if (SysProperties.CHECK && !isKnownSetting(key)) { DbException.throwInternalError(key); } String s = getProperty(key); return s == null ? defaultValue : s; }
/** * The maximum number of rows, including uncommitted rows of any session. * * @return the maximum number of rows */ public long getRowCountMax() { try { return dataMap.sizeAsLongMax(); } catch (IllegalStateException e) { throw DbException.get(ErrorCode.OBJECT_CLOSED, e); } }
private ParameterInterface getParameter(int param) { checkClosed(); if (param < 1 || param > paramCount) { throw DbException.getInvalidValueException("param", param); } return parameters.get(param - 1); }
/** * Parse and prepare the given SQL statement. This method also checks if the connection has been * closed. * * @param sql the SQL statement * @return the prepared statement */ public Command prepareLocal(String sql) { if (closed) { throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "session closed"); } Command command; if (queryCacheSize > 0) { if (queryCache == null) { queryCache = SmallLRUCache.newInstance(queryCacheSize); modificationMetaID = database.getModificationMetaId(); } else { long newModificationMetaID = database.getModificationMetaId(); if (newModificationMetaID != modificationMetaID) { queryCache.clear(); modificationMetaID = newModificationMetaID; } command = queryCache.get(sql); if (command != null && command.canReuse()) { command.reuse(); return command; } } } Parser parser = new Parser(this); command = parser.prepareCommand(sql); if (queryCache != null) { if (command.isCacheable()) { queryCache.put(sql, command); } } return command; }
/** * Changes the current transaction isolation level. Calling this method will commit an open * transaction, even if the new level is the same as the old one, except if the level is not * supported. Internally, this method calls SET LOCK_MODE. The following isolation levels are * supported: * * <ul> * <li>Connection.TRANSACTION_READ_UNCOMMITTED = SET LOCK_MODE 0: no locking (should only be * used for testing). * <li>Connection.TRANSACTION_SERIALIZABLE = SET LOCK_MODE 1: table level locking. * <li>Connection.TRANSACTION_READ_COMMITTED = SET LOCK_MODE 3: table level locking, but read * locks are released immediately (default). * </ul> * * This setting is not persistent. Please note that using TRANSACTION_READ_UNCOMMITTED while at * the same time using multiple connections may result in inconsistent transactions. * * @param level the new transaction isolation level: Connection.TRANSACTION_READ_UNCOMMITTED, * Connection.TRANSACTION_READ_COMMITTED, or Connection.TRANSACTION_SERIALIZABLE * @throws SQLException if the connection is closed or the isolation level is not supported */ public void setTransactionIsolation(int level) throws SQLException { try { debugCodeCall("setTransactionIsolation", level); checkClosed(); int lockMode; switch (level) { case Connection.TRANSACTION_READ_UNCOMMITTED: lockMode = Constants.LOCK_MODE_OFF; break; case Connection.TRANSACTION_READ_COMMITTED: lockMode = Constants.LOCK_MODE_READ_COMMITTED; break; case Connection.TRANSACTION_REPEATABLE_READ: case Connection.TRANSACTION_SERIALIZABLE: lockMode = Constants.LOCK_MODE_TABLE; break; default: throw DbException.getInvalidValueException("" + level, "level"); } commit(); setLockMode = prepareCommand("SET LOCK_MODE ?", setLockMode); setLockMode.getParameters().get(0).setValue(ValueInt.get(lockMode), false); setLockMode.executeUpdate(); } catch (Exception e) { throw logAndConvert(e); } }
private static String remapURL(String url) { // 比如System.setProperty("h2.urlMap", "E:/H2/my-h2/my-h2-src/my/test/h2.urlMap.properties"); // 假设url="my.url",那么可以在h2.urlMap.properties中重新映射: // my.url=my.url=jdbc:h2:tcp://localhost:9092/test9 // 最后返回的url实际是jdbc:h2:tcp://localhost:9092/test9 String urlMap = SysProperties.URL_MAP; if (urlMap != null && urlMap.length() > 0) { try { SortedProperties prop; prop = SortedProperties.loadProperties(urlMap); String url2 = prop.getProperty(url); if (url2 == null) { prop.put(url, ""); prop.store(urlMap); } else { url2 = url2.trim(); if (url2.length() > 0) { return url2; } } } catch (IOException e) { throw DbException.convert(e); } } return url; }
/** * Parse a string to a ValueTime. * * @param s the string to parse * @return the time */ public static ValueTime parse(String s) { try { return fromNanos(DateTimeUtils.parseTimeNanos(s, 0, s.length(), false)); } catch (Exception e) { throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "TIME", s); } }
/** * Returns the current transaction isolation level. * * @return the isolation level. * @throws SQLException if the connection is closed */ public int getTransactionIsolation() throws SQLException { try { debugCodeCall("getTransactionIsolation"); checkClosed(); getLockMode = prepareCommand("CALL LOCK_MODE()", getLockMode); ResultInterface result = getLockMode.executeQuery(0, false); result.next(); int lockMode = result.currentRow()[0].getInt(); result.close(); int transactionIsolationLevel; switch (lockMode) { case Constants.LOCK_MODE_OFF: transactionIsolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED; break; case Constants.LOCK_MODE_READ_COMMITTED: transactionIsolationLevel = Connection.TRANSACTION_READ_COMMITTED; break; case Constants.LOCK_MODE_TABLE: case Constants.LOCK_MODE_TABLE_GC: transactionIsolationLevel = Connection.TRANSACTION_SERIALIZABLE; break; default: throw DbException.throwInternalError("lockMode:" + lockMode); } return transactionIsolationLevel; } catch (Exception e) { throw logAndConvert(e); } }