001package gudusoft.gsqlparser.sqlenv; 002 003 004import gudusoft.gsqlparser.util.SQLUtil; 005 006import java.util.*; 007 008/** 009 * In the most sql-implementation, Catalog is the anonymous of the Database. 010 * So, use this class represents a database. 011 */ 012public class TSQLCatalog extends TSQLObject { 013 014 /** 015 * create a catalog and add to the SQL environment. 016 * 017 * @param sqlEnv SQL environment 018 * @param catalogName catalog name 019 */ 020 public TSQLCatalog(TSQLEnv sqlEnv, String catalogName){ 021 super(sqlEnv,catalogName,ESQLDataObjectType.dotCatalog); 022 this.sqlEnv.doAddCatalog(this); 023 } 024 025 /** 026 * schema list in this catalog 027 * 028 * @return schema list 029 */ 030 public List<TSQLSchema> getSchemaList() { 031 List<TSQLSchema> schemaList = new LinkedList<>(); 032 for(String schema: schemaMap.keySet()){ 033 schemaList.add(schemaMap.get(schema)); 034 } 035 return schemaList; 036 } 037 038 private Map<String, TSQLSchema> schemaMap = Collections.synchronizedMap(new LinkedHashMap<String, TSQLSchema>( )); 039 040 protected TSQLSchema searchSchema(String schemaName){ 041 String name = SQLUtil.getIdentifierNormalName(this.sqlEnv.getDBVendor(), schemaName, ESQLDataObjectType.dotSchema); 042 return schemaMap.get(name); 043 } 044 045 protected void addSchema(TSQLSchema sqlSchema) { 046 if (searchSchema(sqlSchema.name) == null) { 047 String name = SQLUtil.getIdentifierNormalName(this.sqlEnv.getDBVendor(), sqlSchema.name, ESQLDataObjectType.dotSchema); 048 schemaMap.put(name, sqlSchema); 049 } 050 } 051 052 /** 053 * get a schema instance from the catalog, create a new schema if this schema is not exist and the createIfNotExist parameter is true. 054 * 055 * @param schemaName schema name 056 * @param createIfNotExist create a new schema if this schema is not exist and the createIfNotExist parameter is true. 057 * @return the schema instance 058 */ 059 public TSQLSchema getSchema(String schemaName, boolean createIfNotExist){ 060 TSQLSchema result = searchSchema(schemaName); 061 if ((result == null)&&(createIfNotExist)){ 062 result = new TSQLSchema(this,schemaName); 063 } 064 065 return result; 066 } 067 068 /** 069 * create a schema under this catalog 070 * 071 * @param schemaName schema name 072 * @return schema instance 073 */ 074 public TSQLSchema createSchema(String schemaName){ 075 return getSchema(schemaName,true); 076 } 077 078}