001package gudusoft.gsqlparser.sqlenv;
002
003
004import gudusoft.gsqlparser.util.SQLUtil;
005
006import java.util.*;
007
008/**
009 * SQL schema, contains a list of schema objects.
010 */
011public class TSQLSchema extends TSQLObject {
012
013
014    public String getQualifiedName(){
015        return catalog.name+"."+name;
016    }
017
018    private TSQLCatalog catalog;
019
020    public TSQLCatalog getCatalog() {
021        return catalog;
022    }
023
024    /**
025     * Create a new instance of the schema, added to the catalog.
026     *
027     * @param sqlCatalog catalog contains this schema
028     * @param schemaName name of the schema
029     */
030    public TSQLSchema(TSQLCatalog sqlCatalog,String schemaName){
031        super(sqlCatalog.getSqlEnv(), schemaName,ESQLDataObjectType.dotSchema);
032        this.catalog = sqlCatalog;
033        this.catalog.addSchema(this);
034    }
035
036    public List<TSQLSchemaObject> getSchemaObjectList() {
037        List<TSQLSchemaObject> schemaObjectList = new LinkedList<>();
038        for(String schemaObjectKey: schemaObjectMap.keySet()){
039            TSQLSchemaObject schemaObject = schemaObjectMap.get(schemaObjectKey);
040            if(schemaObject instanceof TSQLProcedure){
041                if(((TSQLProcedure)schemaObject).isOraclePackageProcedure()){
042                    continue;
043                }
044            }
045            schemaObjectList.add(schemaObject);
046        }
047        return schemaObjectList;
048    }
049
050    public List<TSQLSchemaObject> getPackageObjectList(TSQLOraclePackage oraclePackage) {
051        List<TSQLSchemaObject> schemaObjectList = new LinkedList<>();
052        for (String schemaObjectKey : schemaObjectMap.keySet()) {
053            TSQLSchemaObject schemaObject = schemaObjectMap.get(schemaObjectKey);
054            if (schemaObject instanceof TSQLProcedure) {
055                if (!((TSQLProcedure) schemaObject).isOraclePackageProcedure()) {
056                    continue;
057                }
058                TSQLProcedure procedure = (TSQLProcedure) schemaObject;
059                if (procedure.getOraclePackage() == oraclePackage) {
060                    schemaObjectList.add(schemaObject);
061                }
062            }
063        }
064        return schemaObjectList;
065    }
066
067    private Map<String, TSQLSchemaObject> schemaObjectMap = Collections.synchronizedMap(new LinkedHashMap<String, TSQLSchemaObject>( ));
068
069//    public boolean addTable(TSQLTable sqlTable){
070//        return addDataObject(sqlTable);
071//    }
072//
073//    public boolean addProcedure(TSQLProcedure sqlProcedure){
074//        return addDataObject(sqlProcedure);
075//    }
076//
077//    public boolean addFunction(TSQLFunction sqlFunction){
078//        return addDataObject(sqlFunction);
079//    }
080//
081//    public boolean addTrigger(TSQLTrigger sqlTrigger){
082//        return addDataObject(sqlTrigger);
083//    }
084
085    /**
086     * add database object
087     *
088     * @param schemaObject
089     * @return true if data object added to the list successfully
090     */
091    protected void addSchemaObject(TSQLSchemaObject schemaObject){
092        String normalName = SQLUtil.getIdentifierNormalName(this.sqlEnv.getDBVendor(), schemaObject.name, schemaObject.getDataObjectType());
093        if (schemaObject.getDataObjectType() == ESQLDataObjectType.dotFunction)
094        {
095            normalName = normalName+"$function";
096        }
097        else if (schemaObject.getDataObjectType() == ESQLDataObjectType.dotProcedure)
098        {
099            normalName = normalName+"$procedure";
100        }
101        else if (schemaObject.getDataObjectType() == ESQLDataObjectType.dotTrigger)
102        {
103            normalName = normalName+"$trigger";
104        }
105        else if (schemaObject.getDataObjectType() == ESQLDataObjectType.dotOraclePackage)
106        {
107            normalName = normalName+"$package";
108        }
109        schemaObjectMap.put(normalName, schemaObject);
110        this.catalog.getSqlEnv().putSchemaObject(schemaObject.getQualifiedName(),schemaObject);
111    }
112
113    TSQLSchemaObject findSchemaObject(ESQLDataObjectType dataObjectType, String schemaObjectName) {
114        String normalName = SQLUtil.getIdentifierNormalName(this.sqlEnv.getDBVendor(), schemaObjectName, dataObjectType);
115        if (dataObjectType == ESQLDataObjectType.dotFunction)
116        {
117            normalName = normalName+"$function";
118        }
119        else if (dataObjectType == ESQLDataObjectType.dotProcedure)
120        {
121            normalName = normalName+"$procedure";
122        }
123        else if (dataObjectType == ESQLDataObjectType.dotTrigger)
124        {
125            normalName = normalName+"$trigger";
126        }
127        else if (dataObjectType == ESQLDataObjectType.dotOraclePackage)
128        {
129            normalName = normalName+"$package";
130        }
131        return schemaObjectMap.get(normalName);
132    }
133
134    /**
135     * create a oracle package belong to this schema
136     *
137     * @param oraclePackageName oracle package name
138     * @return an instance of the oracle package
139     */
140    public TSQLOraclePackage createOraclePackage(String oraclePackageName){
141        return (TSQLOraclePackage)createSchemaObject(oraclePackageName,ESQLDataObjectType.dotOraclePackage);
142    }
143
144    /**
145     * create a procedure belong to this schema
146     *
147     * @param procedureName procedure name
148     * @return an instance of the SQL procedure
149     */
150    public TSQLProcedure createProcedure(String procedureName){
151        return (TSQLProcedure)createSchemaObject(procedureName,ESQLDataObjectType.dotProcedure);
152    }
153
154    public TSQLProcedure createProcedure(String procedureName, ESQLDataObjectType type){
155        return (TSQLProcedure)createSchemaObject(procedureName,type);
156    }
157
158    /**
159     * create a function belong to this schema
160     *
161     * @param functionName function name
162     * @return an instance of the SQL function.
163     */
164    public TSQLFunction createFunction(String functionName){
165        return (TSQLFunction)createSchemaObject(functionName,ESQLDataObjectType.dotFunction);
166    }
167
168    /**
169     * create a trigger belong to this schema
170     *
171     * @param triggerName trigger name
172     * @return an instance of the SQL trigger.
173     */
174    public TSQLTrigger createTrigger(String triggerName){
175        return (TSQLTrigger)createSchemaObject(triggerName,ESQLDataObjectType.dotTrigger);
176    }
177
178    /**
179     * create a synonyms belong to this schema
180     *
181     * @param synonymsName synonyms name
182     * @return an instance of the SQL synonyms
183     */
184    public TSQLSynonyms createSynonyms(String synonymsName){
185        return (TSQLSynonyms)createSchemaObject(synonymsName,ESQLDataObjectType.dotSynonyms);
186    }
187
188    /**
189     * create a table belong to this schema. If table with the same name already exists in the schema
190     * return the existing table.
191     *
192     * @param tableName table name
193     * @return an instance of the SQL table
194     */
195    public TSQLTable createTable(String tableName){
196        return (TSQLTable)createSchemaObject(tableName,ESQLDataObjectType.dotTable);
197    }
198
199    public TSQLTable createTable(String tableName, int priority){
200        return (TSQLTable)createSchemaObject(tableName,ESQLDataObjectType.dotTable, priority);
201    }
202
203        public boolean containsTable(String tableName) {
204                return schemaObjectMap.containsKey(
205                                SQLUtil.getIdentifierNormalName(this.sqlEnv.getDBVendor(), tableName, ESQLDataObjectType.dotTable));
206        }
207
208    public TSQLTable findTable(String tableName) {
209        return (TSQLTable)schemaObjectMap.get(
210                SQLUtil.getIdentifierNormalName(this.sqlEnv.getDBVendor(), tableName, ESQLDataObjectType.dotTable));
211    }
212
213    /**
214     *
215     * @param schemaObjectName 该名称不带 catalog, schema 前缀
216     * @param dataObjectType
217     * @return
218     */
219    public TSQLSchemaObject createSchemaObject(String schemaObjectName, ESQLDataObjectType dataObjectType){
220        return createSchemaObject(schemaObjectName, dataObjectType, 0);
221    }
222
223    /**
224     *
225     * @param schemaObjectName 该名称不带 catalog, schema 前缀
226     * @param dataObjectType
227     * @param priority
228     * @return
229     */
230    protected TSQLSchemaObject createSchemaObject(String schemaObjectName, ESQLDataObjectType dataObjectType, int priority){
231        TSQLSchemaObject result = null;
232        TSQLSchemaObject schemaObject = findSchemaObject(dataObjectType, schemaObjectName);
233        if (schemaObject == null){
234            switch (dataObjectType){
235                case dotTable:
236                    result = new TSQLTable(this,schemaObjectName);
237                    break;
238                case dotOraclePackage:
239                    result = new TSQLOraclePackage(this,schemaObjectName);
240                    break;
241                case dotProcedure:
242                    result = new TSQLProcedure(this,schemaObjectName);
243                    break;
244                case dotFunction:
245                    result = new TSQLFunction(this,schemaObjectName);
246                    break;
247                case dotTrigger:
248                    result = new TSQLTrigger(this,schemaObjectName);
249                    break;
250                case dotSynonyms:
251                    result = new TSQLSynonyms(this,schemaObjectName);
252                    break;
253            }
254        }else if (dataObjectType == schemaObject.getDataObjectType()){
255            result = schemaObject;
256        }else{
257            System.out.println("object name conflict:"+getQualifiedName()+"."+schemaObjectName+",type:"+dataObjectType+" VS "+schemaObject.getQualifiedName()+", type:"+schemaObject.getDataObjectType());
258        };
259
260        if (result!=null && priority > result.getPriority()) {
261            result.setPriority(priority);
262        }
263        return result;
264    }
265
266}