Exemple #1
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();
  }
Exemple #2
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();
    }
  }
Exemple #3
0
  /**
   * @param tableName
   * @param newTableName
   * @param newColumnDef
   * @return
   * @throws SqlJetException
   */
  private ISqlJetTableDef alterTableSafe(final SqlJetAlterTableDef alterTableDef)
      throws SqlJetException {

    assert (null != alterTableDef);
    String tableName = alterTableDef.getTableName();
    String newTableName = alterTableDef.getNewTableName();
    ISqlJetColumnDef newColumnDef = alterTableDef.getNewColumnDef();

    if (null == tableName) {
      throw new SqlJetException(SqlJetErrorCode.MISUSE, "Table name isn't defined");
    }

    if (null == newTableName && null == newColumnDef) {
      throw new SqlJetException(SqlJetErrorCode.MISUSE, "Not defined any altering");
    }

    boolean renameTable = false;
    if (null != newTableName) {
      renameTable = true;
    } else {
      newTableName = tableName;
    }

    if (renameTable && tableDefs.containsKey(newTableName)) {
      throw new SqlJetException(
          SqlJetErrorCode.MISUSE, String.format("Table \"%s\" already exists", newTableName));
    }

    final SqlJetTableDef tableDef = (SqlJetTableDef) tableDefs.get(tableName);
    if (null == tableDef) {
      throw new SqlJetException(
          SqlJetErrorCode.MISUSE, String.format("Table \"%s\" not found", tableName));
    }

    List<ISqlJetColumnDef> columns = tableDef.getColumns();
    if (null != newColumnDef) {

      final String fieldName = newColumnDef.getName();
      if (tableDef.getColumn(fieldName) != null) {
        throw new SqlJetException(
            SqlJetErrorCode.MISUSE,
            String.format("Field \"%s\" already exists in table \"%s\"", fieldName, tableName));
      }

      final List<ISqlJetColumnConstraint> constraints = newColumnDef.getConstraints();
      if (null != constraints && 0 != constraints.size()) {
        boolean notNull = false;
        boolean defaultValue = false;
        for (final ISqlJetColumnConstraint constraint : constraints) {
          if (constraint instanceof ISqlJetColumnNotNull) {
            notNull = true;
          } else if (constraint instanceof ISqlJetColumnDefault) {
            defaultValue = true;
          } else {
            throw new SqlJetException(
                SqlJetErrorCode.MISUSE,
                String.format("Invalid constraint: %s", constraint.toString()));
          }
        }
        if (notNull && !defaultValue) {
          throw new SqlJetException(
              SqlJetErrorCode.MISUSE, "NOT NULL requires to have DEFAULT value");
        }
      }

      columns = new ArrayList<ISqlJetColumnDef>(columns);
      columns.add(newColumnDef);
    }

    final int page = tableDef.getPage();
    final long rowId = tableDef.getRowId();

    final SqlJetTableDef alterDef =
        new SqlJetTableDef(
            newTableName,
            null,
            tableDef.isTemporary(),
            false,
            columns,
            tableDef.getConstraints(),
            page,
            rowId);

    final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);
    try {
      schemaTable.lock();
      try {

        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 || !TABLE_TYPE.equals(typeField)) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }
        if (null == nameField || !tableName.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);
        }

        final String alteredSql = getTableAlteredSql(schemaTable.getSqlField(), alterTableDef);

        db.getOptions().changeSchemaVersion();

        schemaTable.insertRecord(TABLE_TYPE, newTableName, newTableName, page, alteredSql);

        if (renameTable && !tableName.equals(newTableName)) {
          renameTablesIndices(
              schemaTable, tableName, newTableName, getAlterTableName(alterTableDef));
        }

        tableDefs.remove(tableName);
        tableDefs.put(newTableName, alterDef);

        return alterDef;

      } finally {
        schemaTable.unlock();
      }
    } finally {
      schemaTable.close();
    }
  }
Exemple #4
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);
      }
    }
  }