⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.java

📁 Java写的含有一个jdbc驱动的小型数据库数据库引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param c
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processDrop(Tokenizer c,
			       Channel channel) throws SQLException {
	channel.checkReadWrite();
	channel.checkAdmin();

	String sToken = c.getString();

	if (sToken.equals("TABLE")) {
	    sToken = c.getString();

	    if (sToken.equals("IF")) {
		sToken = c.getString();    // EXISTS
		sToken = c.getString();    // <table>

		dropTable(sToken, true);
	    } else {
		dropTable(sToken, false);
	    }
	    channel.commit();
	} else if (sToken.equals("USER")) {
	    aAccess.dropUser(c.getStringToken());
	} else if (sToken.equals("TRIGGER")) {
	    dropTrigger(c.getString());
	} else if (sToken.equals("INDEX")) {
	    sToken = c.getString();

	    if (!c.wasLongName()) {
		throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
	    }

	    String table = c.getLongNameFirst();
	    String index = c.getLongNameLast();
	    Table  t = getTable(table, channel);

	    t.checkDropIndex(index);

	    Table tn = t.moveDefinition(index);

	    tn.moveData(t);
	    dropTable(table);
	    linkTable(tn);
	    channel.commit();
	} else {
	    throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
	}

	return new Result();
    }

    /**
     * Method declaration
     *
     *
     * @param c
     * @param channel
     * @param grant
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processGrantOrRevoke(Tokenizer c, Channel channel,
					boolean grant) throws SQLException {
	channel.checkReadWrite();
	channel.checkAdmin();

	int    right = 0;
	String sToken;

	do {
	    String sRight = c.getString();

	    right |= Access.getRight(sRight);
	    sToken = c.getString();
	} while (sToken.equals(","));

	if (!sToken.equals("ON")) {
	    throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
	}

	String table = c.getString();

	if (table.equals("CLASS")) {

	    // object is saved as 'CLASS "java.lang.Math"'
	    // tables like 'CLASS "xy"' should not be created
	    table += " \"" + c.getString() + "\"";
	} else {
	    getTable(table, channel);    // to make sure the table exists
	}

	c.getThis("TO");

	String user = c.getStringToken();
	String command;

	if (grant) {
	    aAccess.grant(user, table, right);

	    command = "GRANT";
	} else {
	    aAccess.revoke(user, table, right);

	    command = "REVOKE";
	}

	return new Result();
    }

    /**
     * Method declaration
     *
     *
     * @param c
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processConnect(Tokenizer c,
				  Channel channel) throws SQLException {
	c.getThis("USER");

	String username = c.getStringToken();

	c.getThis("PASSWORD");

	String password = c.getStringToken();
	User   user = aAccess.getUser(username, password);

	channel.commit();
	channel.setUser(user);

	return new Result();
    }

    /**
     * Method declaration
     *
     *
     * @param c
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processDisconnect(Tokenizer c,
				     Channel channel) throws SQLException {
	if (!channel.isClosed()) {
	    channel.disconnect();
	    cChannel.setElementAt(null, channel.getId());
	}

	return new Result();
    }

    /**
     * Method declaration
     *
     *
     * @param c
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processSet(Tokenizer c,
			      Channel channel) throws SQLException {
	String sToken = c.getString();

	if (sToken.equals("PASSWORD")) {
	    channel.checkReadWrite();
	    channel.setPassword(c.getStringToken());
	} else if (sToken.equals("READONLY")) {
	    channel.commit();
	    channel.setReadOnly(processTrueOrFalse(c));
	} else if (sToken.equals("LOGSIZE")) {
	    channel.checkAdmin();

	    int i = Integer.parseInt(c.getString());

	    if (lLog != null) {
		lLog.setLogSize(i);
	    }
	} else if (sToken.equals("IGNORECASE")) {
	    channel.checkAdmin();

	    bIgnoreCase = processTrueOrFalse(c);
	} else if (sToken.equals("MAXROWS")) {
	    int i = Integer.parseInt(c.getString());

	    channel.setMaxRows(i);
	} else if (sToken.equals("AUTOCOMMIT")) {
	    channel.setAutoCommit(processTrueOrFalse(c));
	} else if (sToken.equals("TABLE")) {
	    channel.checkReadWrite();
	    channel.checkAdmin();

	    Table t = getTable(c.getString(), channel);

	    c.getThis("INDEX");
	    c.getString();
	    t.setIndexRoots((String) c.getAsValue());
	} else if (sToken.equals("REFERENCIAL_INTEGRITY")
		   || sToken.equals("REFERENTIAL_INTEGRITY")) {
	    channel.checkAdmin();

	    bReferentialIntegrity = processTrueOrFalse(c);
	} else if (sToken.equals("WRITE_DELAY")) {
	    channel.checkAdmin();

	    boolean delay = processTrueOrFalse(c);

	    if (lLog != null) {
		lLog.setWriteDelay(delay);
	    }
	} else {
	    throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
	}

	return new Result();
    }

    /**
     * Method declaration
     *
     *
     * @param c
     *
     * @return
     *
     * @throws SQLException
     */
    private boolean processTrueOrFalse(Tokenizer c) throws SQLException {
	String sToken = c.getString();

	if (sToken.equals("TRUE")) {
	    return true;
	} else if (sToken.equals("FALSE")) {
	    return false;
	} else {
	    throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param c
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processCommit(Tokenizer c,
				 Channel channel) throws SQLException {
	String sToken = c.getString();

	if (!sToken.equals("WORK")) {
	    c.back();
	}

	channel.commit();

	return new Result();
    }

    /**
     * Method declaration
     *
     *
     * @param c
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processRollback(Tokenizer c,
				   Channel channel) throws SQLException {
	String sToken = c.getString();

	if (!sToken.equals("WORK")) {
	    c.back();
	}

	channel.rollback();

	return new Result();
    }

    /**
     * Method declaration
     *
     */
    public void finalize() {
	try {
	    close(0);
	} catch (SQLException e) {

	    // it's too late now
	}
    }

    /**
     * Method declaration
     *
     *
     * @param type
     *
     * @throws SQLException
     */
    private void close(int type) throws SQLException {
	if (lLog == null) {
	    return;
	}

	lLog.stop();

	if (type == -1) {
	    lLog.shutdown();
	} else if (type == 0) {
	    lLog.close(false);
	} else if (type == 1) {
	    lLog.close(true);
	}

	lLog = null;
	bShutdown = true;
    }

    /**
     * Method declaration
     *
     *
     * @param c
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processShutdown(Tokenizer c,
				   Channel channel) throws SQLException {
	channel.checkAdmin();

	// don't disconnect system user; need it to save database
	for (int i = 1; i < cChannel.size(); i++) {
	    Channel d = (Channel) cChannel.elementAt(i);

	    if (d != null) {
		d.disconnect();
	    }
	}

	cChannel.removeAllElements();

	String token = c.getString();

	if (token.equals("IMMEDIATELY")) {
	    close(-1);
	} else if (token.equals("COMPACT")) {
	    close(1);
	} else {
	    c.back();
	    close(0);
	}

	processDisconnect(c, channel);

	return new Result();
    }

    /**
     * Method declaration
     *
     *
     * @param channel
     *
     * @return
     *
     * @throws SQLException
     */
    private Result processCheckpoint(Channel channel) throws SQLException {
	channel.checkAdmin();

	if (lLog != null) {
	    lLog.checkpoint();
	}

	return new Result();
    }

    /**
    /**
     * Method declaration
     *
     *
     * @param name
     *
     * @throws SQLException
     */
    private void dropTable(String name) throws SQLException {
	dropTable(name, false);
    }

    /**
     * Method declaration
     *
     *
     * @param name
     * @param bExists
     *
     * @throws SQLException
     */
    private void dropTable(String name, boolean bExists) throws SQLException {
	for (int i = 0; i < tTable.size(); i++) {
	    Table o = (Table) tTable.elementAt(i);

	    if (o.getName().equals(name)) {
		tTable.removeElementAt(i);

		return;
	    }
	}

	if (!bExists) {
	    throw Trace.error(Trace.TABLE_NOT_FOUND, name);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param name
     *
     * @throws SQLException
     */
    private void dropTrigger(String name) throws SQLException {

	// look in each trigger list of each type of trigger for each table
	for (int i = 0; i < tTable.size(); i++) {
	    Table t = (Table) tTable.elementAt(i);
	    int   numTrigs = TriggerDef.numTrigs();

	    for (int tv = 0; tv < numTrigs; tv++) {
		Vector v = t.vTrigs[tv];

		for (int tr = 0; tr < v.size(); tr++) {
		    TriggerDef td = (TriggerDef) v.elementAt(tr);

		    if (td.name.equals(name)) {
			v.removeElementAt(tr);

			if (Trace.TRACE) {
			    Trace.trace("Trigger dropped " + name);
			}

			return;
		    }
		}
	    }
	}

	throw Trace.error(Trace.TRIGGER_NOT_FOUND, name);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -