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

📄 database.java

📁 這是一個油Java實作的資料庫系統 是個入門的好材料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // Thats the create of the Table does not need in the Synchronized.
        Table table = new Table( this, con, name, columns, indexes, foreignKeys);
        synchronized(tableViews){
            tableViews.put( name, table);
        }
    }


    /**
     * It is used to create temp Table for ALTER TABLE and co.
     */
    Table createTable(SSConnection con, String tableName, Columns columns, IndexDescriptions oldIndexes, IndexDescriptions newIndexes, ForeignKeys foreignKeys) throws Exception{
        checkForeignKeys( con, foreignKeys );
        Table table = new Table( this, con, tableName, columns, oldIndexes, newIndexes, foreignKeys);
        synchronized(tableViews){
            tableViews.put( tableName, table);
        }
        return table;
    }
    
    
	void createView(String name, String sql) throws Exception{
		// createFile() can run only one Thread success (it is atomic)
		// Thats the create of the View does not need in the Synchronized.
		new View( this, name, sql);
	}


    /**
     * Create a list of all available Databases from the point of the current 
     * Database or current working directory
     * @param database - current database
     * @return
     */
    static Object[][] getCatalogs(Database database){
    	List catalogs = new ArrayList();
    	File baseDir = (database != null) ?
    					database.directory.getParentFile() :
						new File(".");
		File dirs[] = baseDir.listFiles();
		if(dirs != null)
			for(int i=0; i<dirs.length; i++){
				if(dirs[i].isDirectory()){
					if(new File(dirs[i], Utils.MASTER_FILENAME).exists()){
						Object[] catalog = new Object[1];
						catalog[0] = dirs[i].getPath();
						catalogs.add(catalog);
					}
				}
			}
		Object[][] result = new Object[catalogs.size()][];
		catalogs.toArray(result);
		return result;
    }
	
    
	Strings getTables(String tablePattern){
		Strings list = new Strings();
		File dirs[] = directory.listFiles();    
		if(dirs != null)
			if(tablePattern == null) tablePattern = "%"; 
			tablePattern += Utils.TABLE_VIEW_EXTENTION;
			for(int i=0; i<dirs.length; i++){
				String name = dirs[i].getName();
				if(Utils.like(name, tablePattern)){
					list.add(name.substring( 0, name.length()-Utils.TABLE_VIEW_EXTENTION.length() ));
				}
			}
    	return list;
    }
	
    
    Object[][] getColumns( SSConnection con, String tablePattern, String colPattern) throws Exception{
    	List rows = new ArrayList();
		Strings tables = getTables(tablePattern);
    	for(int i=0; i<tables.size(); i++){
    		String tableName = tables.get(i);
			try{
	    		TableView tab = getTableView( con, tableName);
	    		Columns cols = tab.columns;
	    		for(int c=0; c<cols.size(); c++){
	    			Column col = cols.get(c);
					Object[] row = new Object[18];
					row[0] = getName(); 			//TABLE_CAT
								   					//TABLE_SCHEM
					row[2] = tableName;				//TABLE_NAME	
					row[3] = col.getName();			//COLUMN_NAME	
					row[4] = Utils.getShort( SQLTokenizer.getSQLDataType( col.getDataType() )); //DATA_TYPE  
					row[5] = SQLTokenizer.getKeyWord( col.getDataType() );	//TYPE_NAME
					row[6] = Utils.getInteger(col.getColumnSize());//COLUMN_SIZE
													//BUFFER_LENGTH
					row[8] = Utils.getInteger(col.getScale());//DECIMAL_DIGITS
					row[9] = Utils.getInteger(10);		//NUM_PREC_RADIX
					row[10]= Utils.getInteger(col.isNullable() ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls); //NULLABLE
													//REMARKS
					row[12]= col.getDefaultDefinition(); //COLUMN_DEF
													//SQL_DATA_TYPE
													//SQL_DATETIME_SUB
					row[15]= row[6];				//CHAR_OCTET_LENGTH
					row[16]= Utils.getInteger(i); 	//ORDINAL_POSITION		
					row[17]= col.isNullable() ? "YES" : "NO"; //IS_NULLABLE					
					rows.add(row);
	    		}
			}catch(Exception e){
				//invalid Tables and View will not show 
			}
    	}
		Object[][] result = new Object[rows.size()][];
		rows.toArray(result);
		return result;
    }
	
	
	Object[][] getReferenceKeys(SSConnection con, String pkTable, String fkTable) throws SQLException{
		List rows = new ArrayList();
		Strings tables = (pkTable != null) ? getTables(pkTable) : getTables(fkTable);
		for(int t=0; t<tables.size(); t++){
    		String tableName = tables.get(t);
    		TableView tab = getTableView( con, tableName);
			if(!(tab instanceof Table)) continue;
			ForeignKeys references = ((Table)tab).references;
			for(int i=0; i<references.size(); i++){
				ForeignKey foreignKey = references.get(i);
				IndexDescription pk = foreignKey.pk;
				IndexDescription fk = foreignKey.fk;
				if((pkTable == null || pkTable.equals(foreignKey.pkTable)) &&
				   (fkTable == null || fkTable.equals(foreignKey.fkTable))){
					Strings columnsPk = pk.getColumns();
					Strings columnsFk = fk.getColumns();
					for(int c=0; c<columnsPk.size(); c++){
						Object[] row = new Object[14];
						row[0] = getName();				//PKTABLE_CAT
														//PKTABLE_SCHEM
						row[2] = foreignKey.pkTable;	//PKTABLE_NAME
						row[3] = columnsPk.get(c);		//PKCOLUMN_NAME
						row[4] = getName();				//FKTABLE_CAT
														//FKTABLE_SCHEM
						row[6] = foreignKey.fkTable;	//FKTABLE_NAME
						row[7] = columnsFk.get(c);		//FKCOLUMN_NAME
						row[8] = Utils.getShort(c+1);	//KEY_SEQ
						row[9] = Utils.getShort(foreignKey.updateRule);//UPDATE_RULE
						row[10]= Utils.getShort(foreignKey.deleteRule); //DELETE_RULE
						row[11]= fk.getName();	//FK_NAME
						row[12]= pk.getName();	//PK_NAME
						row[13]= Utils.getShort(DatabaseMetaData.importedKeyNotDeferrable); //DEFERRABILITY
						rows.add(row);
					}
				}
			}
		}
		Object[][] result = new Object[rows.size()][];
		rows.toArray(result);
		return result;		
	}
	
	
	Object[][] getBestRowIdentifier(SSConnection con, String table) throws SQLException{
		List rows = new ArrayList();
		Strings tables = getTables(table);
		for(int t=0; t<tables.size(); t++){
    		String tableName = tables.get(t);
    		TableView tab = getTableView( con, tableName);
			if(!(tab instanceof Table)) continue;
			IndexDescriptions indexes = ((Table)tab).indexes;
			for(int i=0; i<indexes.size(); i++){
				IndexDescription index = indexes.get(i);
				if(index.isUnique()){
					Strings columns = index.getColumns();
					for(int c=0; c<columns.size(); c++){
						String columnName = columns.get(c);
						Column column = tab.findColumn(columnName);
						Object[] row = new Object[8];
						row[0] = Utils.getShort(DatabaseMetaData.bestRowSession);//SCOPE
						row[1] = columnName;			//COLUMN_NAME
						final int dataType = column.getDataType();
						row[2] = Utils.getInteger(dataType);//DATA_TYPE
						row[3] = SQLTokenizer.getKeyWord(dataType);//TYPE_NAME
						row[4] = Utils.getInteger(column.getPrecision());	//COLUMN_SIZE
														//BUFFER_LENGTH
						row[6] = Utils.getShort(column.getScale());		//DECIMAL_DIGITS
						row[7] = Utils.getShort(DatabaseMetaData.bestRowNotPseudo);//PSEUDO_COLUMN
						rows.add(row);
					}
				}
			}
		}
		Object[][] result = new Object[rows.size()][];
		rows.toArray(result);
		return result;		
	}

	
	Object[][] getPrimaryKeys(SSConnection con, String table) throws SQLException{
		List rows = new ArrayList();
		Strings tables = getTables(table);
		for(int t=0; t<tables.size(); t++){
    		String tableName = tables.get(t);
    		TableView tab = getTableView( con, tableName);
			if(!(tab instanceof Table)) continue;
			IndexDescriptions indexes = ((Table)tab).indexes;
			for(int i=0; i<indexes.size(); i++){
				IndexDescription index = indexes.get(i);
				if(index.isPrimary()){
					Strings columns = index.getColumns();
					for(int c=0; c<columns.size(); c++){
						Object[] row = new Object[6];
						row[0] = getName(); 			//TABLE_CAT
														//TABLE_SCHEM
						row[2] = tableName;				//TABLE_NAME
						row[3] = columns.get(c);		//COLUMN_NAME
						row[4] = Utils.getShort(c+1);	//KEY_SEQ
						row[5] = index.getName();		//PK_NAME
						rows.add(row);
					}
				}
			}
		}
		Object[][] result = new Object[rows.size()][];
		rows.toArray(result);
		return result;		
	}
	
	
	Object[][] getIndexInfo( SSConnection con, String table, boolean unique) throws SQLException {
		List rows = new ArrayList();
		Strings tables = getTables(table);
		Short type = Utils.getShort( DatabaseMetaData.tableIndexOther );
		for(int t=0; t<tables.size(); t++){
    		String tableName = tables.get(t);
    		TableView tab = getTableView( con, tableName);
			if(!(tab instanceof Table)) continue;
			IndexDescriptions indexes = ((Table)tab).indexes;
			for(int i=0; i<indexes.size(); i++){
				IndexDescription index = indexes.get(i);
				Strings columns = index.getColumns();
				for(int c=0; c<columns.size(); c++){
					Object[] row = new Object[13];
					row[0] = getName(); 			//TABLE_CAT
													//TABLE_SCHEM
					row[2] = tableName;				//TABLE_NAME
					row[3] = Boolean.valueOf(!index.isUnique());//NON_UNIQUE
													//INDEX_QUALIFIER
					row[5] = index.getName();		//INDEX_NAME
					row[6] = type;					//TYPE
					row[7] = Utils.getShort(c+1);	//ORDINAL_POSITION
					row[8] = columns.get(c);		//COLUMN_NAME
													//ASC_OR_DESC
													//CARDINALITY
													//PAGES
													//FILTER_CONDITION
					rows.add(row);
				}
			}
    	}
		Object[][] result = new Object[rows.size()][];
		rows.toArray(result);
		return result;
	}
}

⌨️ 快捷键说明

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