Beispiel #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;
  }
Beispiel #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;
 }
Beispiel #3
0
  private void readShema(ISqlJetBtreeSchemaTable table) throws SqlJetException {
    for (table.first(); !table.eof(); table.next()) {
      final String type = table.getTypeField();
      if (null == type) {
        continue;
      }
      final String name = table.getNameField();
      if (null == name) {
        continue;
      }
      final int page = table.getPageField();

      if (TABLE_TYPE.equals(type)) {
        String sql = table.getSqlField();
        // System.err.println(sql);
        final CommonTree ast = (CommonTree) parseTable(sql).getTree();
        if (!isCreateVirtualTable(ast)) {
          final SqlJetTableDef tableDef = new SqlJetTableDef(ast, page);
          if (!name.equals(tableDef.getName())) {
            throw new SqlJetException(SqlJetErrorCode.CORRUPT);
          }
          tableDef.setRowId(table.getRowId());
          tableDefs.put(name, tableDef);
        } else {
          final SqlJetVirtualTableDef virtualTableDef = new SqlJetVirtualTableDef(ast, page);
          if (!name.equals(virtualTableDef.getTableName())) {
            throw new SqlJetException(SqlJetErrorCode.CORRUPT);
          }
          virtualTableDef.setRowId(table.getRowId());
          virtualTableDefs.put(name, virtualTableDef);
        }
      } else if (INDEX_TYPE.equals(type)) {
        final String tableName = table.getTableField();
        final String sql = table.getSqlField();
        if (null != sql) {
          // System.err.println(sql);
          final CommonTree ast = (CommonTree) parseIndex(sql).getTree();
          final SqlJetIndexDef indexDef = new SqlJetIndexDef(ast, page);
          if (!name.equals(indexDef.getName())) {
            throw new SqlJetException(SqlJetErrorCode.CORRUPT);
          }
          if (!tableName.equals(indexDef.getTableName())) {
            throw new SqlJetException(SqlJetErrorCode.CORRUPT);
          }
          indexDef.setRowId(table.getRowId());
          indexDefs.put(name, indexDef);
        } else {
          SqlJetBaseIndexDef indexDef = new SqlJetBaseIndexDef(name, tableName, page);
          indexDef.setRowId(table.getRowId());
          indexDefs.put(name, indexDef);
        }
      }
    }
    bindIndexes();
  }
Beispiel #4
0
  /**
   * @param page
   * @param moved
   * @throws SqlJetException
   */
  private void movePage(final int page, final int moved) throws SqlJetException {
    final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);
    try {
      schemaTable.lock();
      try {
        for (schemaTable.first(); !schemaTable.eof(); schemaTable.next()) {
          final long pageField = schemaTable.getPageField();
          if (pageField == moved) {
            final String nameField = schemaTable.getNameField();
            schemaTable.updateRecord(
                schemaTable.getRowId(),
                schemaTable.getTypeField(),
                nameField,
                schemaTable.getTableField(),
                page,
                schemaTable.getSqlField());
            final ISqlJetIndexDef index = getIndex(nameField);
            if (index != null) {
              if (index instanceof SqlJetBaseIndexDef) {
                ((SqlJetBaseIndexDef) index).setPage(page);
              }
            } else {
              final ISqlJetTableDef table = getTable(nameField);
              if (table != null) {
                if (table instanceof SqlJetTableDef) {
                  ((SqlJetTableDef) table).setPage(page);
                }
              }
            }
            return;
          }
        }
      } finally {
        schemaTable.unlock();
      }

    } finally {
      schemaTable.close();
    }
  }
Beispiel #5
0
  /**
   * @param schemaTable
   * @param newTableName
   * @param tableName
   * @param string
   * @throws SqlJetException
   */
  private void renameTablesIndices(
      final ISqlJetBtreeSchemaTable schemaTable,
      String tableName,
      String newTableName,
      String alterTableName)
      throws SqlJetException {

    final Set<ISqlJetIndexDef> indexes = getIndexes(tableName);
    if (null == indexes || 0 == indexes.size()) {
      return;
    }

    int i = 0;
    for (final ISqlJetIndexDef index : indexes) {
      if (index instanceof SqlJetBaseIndexDef) {

        final SqlJetBaseIndexDef indexDef = (SqlJetBaseIndexDef) index;
        final String indexName = indexDef.getName();
        final long rowId = indexDef.getRowId();
        final int page = indexDef.getPage();

        if (!schemaTable.goToRow(rowId)) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }

        final String typeField = schemaTable.getTypeField();
        final String nameField = schemaTable.getNameField();
        final String tableField = schemaTable.getTableField();
        final int pageField = schemaTable.getPageField();

        if (null == typeField || !INDEX_TYPE.equals(typeField)) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }
        if (null == nameField || !indexName.equals(nameField)) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }
        if (null == tableField || !tableName.equals(tableField)) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }
        if (0 == pageField || pageField != page) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }

        indexDef.setTableName(newTableName);

        String newIndexName = indexName;
        String alteredIndexSql = null;

        if (index.isImplicit()) {
          newIndexName = generateAutoIndexName(tableName, ++i);
          indexDef.setName(newIndexName);
          indexDefs.remove(indexName);
          indexDefs.put(newIndexName, indexDef);
        } else {
          alteredIndexSql = getAlteredIndexSql(schemaTable.getSqlField(), alterTableName);
        }

        schemaTable.insertRecord(INDEX_TYPE, newIndexName, newTableName, page, alteredIndexSql);

      } else {
        throw new SqlJetException(SqlJetErrorCode.INTERNAL);
      }
    }
  }