/** * @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; }
private ISqlJetVirtualTableDef createVirtualTableSafe(String sql, int page) throws SqlJetException { final RuleReturnScope parseTable = parseTable(sql); final CommonTree ast = (CommonTree) parseTable.getTree(); if (!isCreateVirtualTable(ast)) { throw new SqlJetException(SqlJetErrorCode.ERROR); } final SqlJetVirtualTableDef tableDef = new SqlJetVirtualTableDef(ast, 0); if (null == tableDef.getTableName()) throw new SqlJetException(SqlJetErrorCode.ERROR); final String tableName = tableDef.getTableName(); if ("".equals(tableName)) { throw new SqlJetException(SqlJetErrorCode.ERROR); } checkNameReserved(tableName); checkFieldNamesRepeatsConflict(tableDef.getTableName(), tableDef.getModuleColumns()); if (virtualTableDefs.containsKey(tableName)) { throw new SqlJetException( SqlJetErrorCode.ERROR, "Virtual table \"" + tableName + "\" exists already"); } checkNameConflict(SqlJetSchemaObjectType.VIRTUAL_TABLE, tableName); final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true); final String createVirtualTableSQL = getCreateVirtualTableSql(parseTable); try { schemaTable.lock(); try { db.getOptions().changeSchemaVersion(); final long rowId = schemaTable.insertRecord(TABLE_TYPE, tableName, tableName, page, createVirtualTableSQL); tableDef.setPage(page); tableDef.setRowId(rowId); virtualTableDefs.put(tableName, tableDef); return tableDef; } finally { schemaTable.unlock(); } } finally { schemaTable.close(); } }
private void init() throws SqlJetException { if (db.getOptions().getSchemaVersion() == 0) return; final ISqlJetBtreeSchemaTable table = openSchemaTable(false); try { table.lock(); try { readShema(table); } finally { table.unlock(); } } finally { table.close(); } }
/** * @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; }
/** * @param virtualTableName * @param indexName * @return * @throws SqlJetException */ private ISqlJetIndexDef createIndexForVirtualTableSafe(String virtualTableName, String indexName) throws SqlJetException { if (null == virtualTableName || "".equals(virtualTableName)) throw new SqlJetException(SqlJetErrorCode.ERROR); if (null == indexName || "".equals(indexName)) throw new SqlJetException(SqlJetErrorCode.ERROR); checkNameReserved(indexName); if (indexDefs.containsKey(indexName)) { throw new SqlJetException( SqlJetErrorCode.ERROR, "Index \"" + indexName + "\" exists already"); } checkNameConflict(SqlJetSchemaObjectType.INDEX, indexName); final ISqlJetVirtualTableDef tableDef = getVirtualTable(virtualTableName); if (null == tableDef) throw new SqlJetException(SqlJetErrorCode.ERROR); final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true); try { schemaTable.lock(); try { db.getOptions().changeSchemaVersion(); final ISqlJetIndexDef indexDef = createAutoIndex(schemaTable, tableDef.getTableName(), indexName); indexDefs.put(indexName, indexDef); return indexDef; } finally { schemaTable.unlock(); } } finally { schemaTable.close(); } }
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); }
/** * @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(); } }
/** * @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(); } }
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(); } }
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(); } }
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(); }
/** * @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); } } }