示例#1
0
  /**
   * @param schemaTable
   * @param name
   * @param generateAutoIndexName
   * @throws SqlJetException
   */
  private boolean doDropIndex(String indexName, boolean allowAutoIndex, boolean throwIfFial)
      throws SqlJetException {

    if (!indexDefs.containsKey(indexName)) {
      if (throwIfFial) throw new SqlJetException(SqlJetErrorCode.MISUSE);
      return false;
    }
    final SqlJetBaseIndexDef indexDef = (SqlJetBaseIndexDef) indexDefs.get(indexName);

    if (!allowAutoIndex && indexDef.isImplicit()) {
      if (throwIfFial)
        throw new SqlJetException(
            SqlJetErrorCode.MISUSE, String.format(CANT_DELETE_IMPLICIT_INDEX, indexName));
      return false;
    }

    final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);

    try {

      schemaTable.lock();

      try {

        if (!schemaTable.goToRow(indexDef.getRowId())
            || !INDEX_TYPE.equals(schemaTable.getTypeField())) {
          if (throwIfFial) throw new SqlJetException(SqlJetErrorCode.INTERNAL);
          return false;
        }
        final String n = schemaTable.getNameField();
        if (null == n || !indexName.equals(n)) {
          if (throwIfFial) throw new SqlJetException(SqlJetErrorCode.INTERNAL);
          return false;
        }

        if (!allowAutoIndex && schemaTable.isNull(ISqlJetBtreeSchemaTable.SQL_FIELD)) {
          if (throwIfFial)
            throw new SqlJetException(
                SqlJetErrorCode.MISUSE, String.format(CANT_DELETE_IMPLICIT_INDEX, indexName));
          return false;
        }

        schemaTable.delete();

      } finally {
        schemaTable.unlock();
      }

    } finally {
      schemaTable.close();
    }

    final int page = indexDef.getPage();
    final int moved = btree.dropTable(page);
    if (moved != 0) {
      movePage(page, moved);
    }

    return true;
  }
示例#2
0
 /**
  * @param schemaTable
  * @param generateAutoIndexName
  * @throws SqlJetException
  */
 private SqlJetBaseIndexDef createAutoIndex(
     ISqlJetBtreeSchemaTable schemaTable, String tableName, String autoIndexName)
     throws SqlJetException {
   final int page = btree.createTable(BTREE_CREATE_INDEX_FLAGS);
   final SqlJetBaseIndexDef indexDef = new SqlJetBaseIndexDef(autoIndexName, tableName, page);
   indexDef.setRowId(schemaTable.insertRecord(INDEX_TYPE, autoIndexName, tableName, page, null));
   indexDefs.put(autoIndexName, indexDef);
   return indexDef;
 }
示例#3
0
  private void dropTableSafe(String tableName) throws SqlJetException {

    if (null == tableName || "".equals(tableName))
      throw new SqlJetException(SqlJetErrorCode.MISUSE, "Table name must be not empty");

    if (!tableDefs.containsKey(tableName))
      throw new SqlJetException(SqlJetErrorCode.MISUSE, "Table not found: " + tableName);
    final SqlJetTableDef tableDef = (SqlJetTableDef) tableDefs.get(tableName);

    dropTableIndexes(tableDef);

    final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);

    try {

      schemaTable.lock();

      try {

        db.getOptions().changeSchemaVersion();

        if (!schemaTable.goToRow(tableDef.getRowId())
            || !TABLE_TYPE.equals(schemaTable.getTypeField()))
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        final String n = schemaTable.getNameField();
        if (null == n || !tableName.equals(n)) throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        schemaTable.delete();

      } finally {
        schemaTable.unlock();
      }

    } finally {
      schemaTable.close();
    }

    final int page = tableDef.getPage();
    final int moved = btree.dropTable(page);
    if (moved != 0) {
      movePage(page, moved);
    }

    tableDefs.remove(tableName);
  }
示例#4
0
  private ISqlJetIndexDef createIndexSafe(String sql) throws SqlJetException {

    final ParserRuleReturnScope parseIndex = parseIndex(sql);
    final CommonTree ast = (CommonTree) parseIndex.getTree();

    final SqlJetIndexDef indexDef = new SqlJetIndexDef(ast, 0);

    if (null == indexDef.getName()) throw new SqlJetException(SqlJetErrorCode.ERROR);
    final String indexName = indexDef.getName();
    if ("".equals(indexName)) throw new SqlJetException(SqlJetErrorCode.ERROR);

    checkNameReserved(indexName);

    if (indexDefs.containsKey(indexName)) {
      if (indexDef.isKeepExisting()) {
        return indexDefs.get(indexName);
      } else {
        throw new SqlJetException(
            SqlJetErrorCode.ERROR, "Index \"" + indexName + "\" exists already");
      }
    }

    checkNameConflict(SqlJetSchemaObjectType.INDEX, indexName);

    if (null == indexDef.getTableName()) throw new SqlJetException(SqlJetErrorCode.ERROR);
    final String tableName = indexDef.getTableName();
    if ("".equals(tableName)) throw new SqlJetException(SqlJetErrorCode.ERROR);

    final List<ISqlJetIndexedColumn> columns = indexDef.getColumns();
    if (null == columns) throw new SqlJetException(SqlJetErrorCode.ERROR);

    final ISqlJetTableDef tableDef = getTable(tableName);
    if (null == tableDef) throw new SqlJetException(SqlJetErrorCode.ERROR);

    for (final ISqlJetIndexedColumn column : columns) {
      if (null == column.getName()) throw new SqlJetException(SqlJetErrorCode.ERROR);
      final String columnName = column.getName();
      if ("".equals(columnName)) throw new SqlJetException(SqlJetErrorCode.ERROR);
      if (null == tableDef.getColumn(columnName))
        throw new SqlJetException(
            SqlJetErrorCode.ERROR,
            "Column \"" + columnName + "\" not found in table \"" + tableName + "\"");
    }

    final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);
    final String createIndexSQL =
        indexDef.isUnique() ? getCreateIndexUniqueSql(parseIndex) : getCreateIndexSql(parseIndex);

    try {

      schemaTable.lock();

      try {

        db.getOptions().changeSchemaVersion();

        final int page = btree.createTable(BTREE_CREATE_INDEX_FLAGS);

        final long rowId =
            schemaTable.insertRecord(INDEX_TYPE, indexName, tableName, page, createIndexSQL);

        indexDef.setPage(page);
        indexDef.setRowId(rowId);
        indexDef.bindColumns(tableDef);
        indexDefs.put(indexName, indexDef);

        final SqlJetBtreeIndexTable indexTable =
            new SqlJetBtreeIndexTable(btree, indexDef.getName(), true);
        try {
          indexTable.reindex(this);
        } finally {
          indexTable.close();
        }
        return indexDef;

      } finally {
        schemaTable.unlock();
      }

    } finally {
      schemaTable.close();
    }
  }
示例#5
0
  private ISqlJetTableDef createTableSafe(String sql, boolean internal) throws SqlJetException {

    final RuleReturnScope parseTable = parseTable(sql);
    final CommonTree ast = (CommonTree) parseTable.getTree();

    if (isCreateVirtualTable(ast)) {
      throw new SqlJetException(SqlJetErrorCode.ERROR);
    }

    final SqlJetTableDef tableDef = new SqlJetTableDef(ast, 0);
    if (null == tableDef.getName()) throw new SqlJetException(SqlJetErrorCode.ERROR);
    final String tableName = tableDef.getName();
    if ("".equals(tableName)) throw new SqlJetException(SqlJetErrorCode.ERROR);

    if (!internal) {
      checkNameReserved(tableName);
    }

    if (tableDefs.containsKey(tableName)) {
      if (tableDef.isKeepExisting()) {
        return tableDefs.get(tableName);
      } else {
        throw new SqlJetException(
            SqlJetErrorCode.ERROR, "Table \"" + tableName + "\" exists already");
      }
    }

    checkNameConflict(SqlJetSchemaObjectType.TABLE, tableName);
    checkFieldNamesRepeatsConflict(tableDef.getName(), tableDef.getColumns());

    final List<ISqlJetColumnDef> columns = tableDef.getColumns();
    if (null == columns || 0 == columns.size()) throw new SqlJetException(SqlJetErrorCode.ERROR);

    final String createTableSql = getCreateTableSql(parseTable);

    final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);

    try {

      schemaTable.lock();

      try {

        db.getOptions().changeSchemaVersion();

        final int page = btree.createTable(BTREE_CREATE_TABLE_FLAGS);
        final long rowId =
            schemaTable.insertRecord(TABLE_TYPE, tableName, tableName, page, createTableSql);

        addConstraints(schemaTable, tableDef);

        tableDef.setPage(page);
        tableDef.setRowId(rowId);
        tableDefs.put(tableName, tableDef);
        return tableDef;

      } finally {
        schemaTable.unlock();
      }

    } finally {
      schemaTable.close();
    }
  }