001package gudusoft.gsqlparser.dlineage.dataflow.model;
002
003import gudusoft.gsqlparser.dlineage.routine.RoutineAliasKey;
004import gudusoft.gsqlparser.EDbVendor;
005import gudusoft.gsqlparser.ESqlStatementType;
006import gudusoft.gsqlparser.ETableSource;
007import gudusoft.gsqlparser.TCustomSqlStatement;
008import gudusoft.gsqlparser.TSourceToken;
009import gudusoft.gsqlparser.dlineage.util.DlineageUtil;
010import gudusoft.gsqlparser.dlineage.util.Pair3;
011import gudusoft.gsqlparser.nodes.TFunctionCall;
012import gudusoft.gsqlparser.nodes.TObjectName;
013import gudusoft.gsqlparser.nodes.TParseTreeNode;
014import gudusoft.gsqlparser.sqlenv.TSQLCatalog;
015import gudusoft.gsqlparser.sqlenv.ESQLDataObjectType;
016import gudusoft.gsqlparser.sqlenv.TSQLEnv;
017import gudusoft.gsqlparser.sqlenv.TSQLSchema;
018import gudusoft.gsqlparser.sqlenv.TSQLSchemaObject;
019import gudusoft.gsqlparser.sqlenv.TSQLTable;
020import gudusoft.gsqlparser.stmt.TStoredProcedureSqlStatement;
021import gudusoft.gsqlparser.stmt.teradata.TTeradataCreateProcedure;
022import gudusoft.gsqlparser.util.Logger;
023import gudusoft.gsqlparser.util.LoggerFactory;
024import gudusoft.gsqlparser.util.SQLUtil;
025
026import java.io.ByteArrayInputStream;
027import java.io.IOException;
028import java.util.ArrayList;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032import java.util.Properties;
033
034public class Procedure {
035    private static final Logger logger = LoggerFactory.getLogger(Procedure.class);
036    private long id;
037    private String server;
038    private String database;
039    private String schema;
040    private String name;
041    private String fullName;
042    private Pair3<Long, Long, String> startPosition;
043    private Pair3<Long, Long, String> endPosition;
044    private List<Argument> arguments = new ArrayList<Argument>();
045    private ESqlStatementType type;
046    private TParseTreeNode procedureObject;
047    private OraclePackage parentPackage;
048
049    public Procedure(TStoredProcedureSqlStatement procedure) {
050        if (procedure == null) {
051            throw new IllegalArgumentException("Procedure arguments can't be null.");
052        } else {
053            this.id = ++ModelBindingManager.get().TABLE_COLUMN_ID;
054            describe(procedure);
055        }
056    }
057
058    /**
059     * Makes the model that a call created by the routine's name ({@link #Procedure(TObjectName)}), before any
060     * definition of the routine was analyzed, the model of this definition: from now on its name, position, schema,
061     * type and statement are the definition's, exactly as if the definition had been analyzed first. It keeps its id,
062     * so what already refers to it, such as the call, refers to the definition. The analyzer calls this while it
063     * analyzes the definition, whose state the description reads.
064     */
065    public void defineBy(TStoredProcedureSqlStatement definition) {
066        if (definition == null) {
067            throw new IllegalArgumentException("Procedure arguments can't be null.");
068        }
069        this.server = null;
070        this.database = null;
071        this.schema = null;
072        this.aliasKey = null;
073        describe(definition);
074    }
075
076    private void describe(TStoredProcedureSqlStatement procedure) {
077        this.procedureObject = procedure;
078        TObjectName procedureName = getProcedureName();
079        TSourceToken startToken = procedure.getStartToken();
080        TSourceToken endToken = procedure.getEndToken();
081        this.startPosition = new Pair3<Long, Long, String>(startToken.lineNo, startToken.columnNo, ModelBindingManager.getGlobalHash());
082        this.endPosition = new Pair3<Long, Long, String>(endToken.lineNo,
083                endToken.columnNo + (long) SQLUtil.endTrim(endToken.getAstext()).length(), ModelBindingManager.getGlobalHash());
084        this.fullName = procedureName.toString();
085        this.name = procedureName.toString();
086
087        EDbVendor vendor = ModelBindingManager.getGlobalOption().getVendor();
088        boolean supportCatalog = TSQLEnv.supportCatalog(vendor);
089        boolean supportSchema = TSQLEnv.supportSchema(vendor);
090
091        fillSchemaInfo();
092
093        if (SQLUtil.isEmpty(this.database)) {
094                        this.database = DlineageUtil.getTableDatabase(procedureName.toString());
095                        if (SQLUtil.isEmpty(this.database) && ModelBindingManager.getGlobalOption().isShowImplicitSchema()
096                                        && !SQLUtil.isEmpty(procedureName.getImplictDatabaseString())) {
097                                this.database = procedureName.getImplictDatabaseString();
098                        }
099                }
100
101                if (SQLUtil.isEmpty(this.schema)) {
102                        this.schema = DlineageUtil.getTableSchema(procedureName.toString());
103                        if (SQLUtil.isEmpty(this.schema) && ModelBindingManager.getGlobalOption().isShowImplicitSchema()
104                                        && !SQLUtil.isEmpty(procedureName.getImplictSchemaString())) {
105                                this.schema = procedureName.getImplictSchemaString();
106                        }
107                }
108
109                if (!SQLUtil.isEmpty(this.database) && SQLUtil.isEmpty(this.schema)) {
110                        if (!SQLUtil.isEmpty(procedureName.getImplictSchemaString())) {
111                                this.schema = procedureName.getImplictSchemaString();
112                        }
113                }
114
115                if (!supportCatalog) {
116                        this.database = null;
117                } else if (this.database == null && !TSQLEnv.DEFAULT_DB_NAME.equals(getDefaultDatabase())) {
118                        this.database = getDefaultDatabase();
119                }
120
121                if (!supportSchema) {
122                        this.schema = null;
123                } else if (this.schema == null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equals(getDefaultSchema())) {
124                        this.schema = getDefaultSchema();
125                }
126
127        updateProcedureName(supportCatalog, supportSchema);
128
129        this.type = procedure.sqlstatementtype;
130
131        if (this.server == null && !TSQLEnv.DEFAULT_SERVER_NAME.equals(getDefaultServer())) {
132            this.server = getDefaultServer();
133        }
134    }
135
136    public Procedure(TObjectName procedureName) {
137        if (procedureName == null) {
138            throw new IllegalArgumentException("Procedure arguments can't be null.");
139        } else {
140            this.id = ++ModelBindingManager.get().TABLE_COLUMN_ID;
141            this.procedureObject = procedureName;
142            TSourceToken startToken = procedureName.getStartToken();
143            TSourceToken endToken = procedureName.getEndToken();
144            this.startPosition = new Pair3<Long, Long, String>(startToken.lineNo, startToken.columnNo, ModelBindingManager.getGlobalHash());
145            this.endPosition = new Pair3<Long, Long, String>(endToken.lineNo,
146                    endToken.columnNo + (long) SQLUtil.endTrim(endToken.getAstext()).length(), ModelBindingManager.getGlobalHash());
147            this.fullName = procedureName.toString();
148            this.name = procedureName.toString();
149
150            EDbVendor vendor = ModelBindingManager.getGlobalOption().getVendor();
151            boolean supportCatalog = TSQLEnv.supportCatalog(vendor);
152            boolean supportSchema = TSQLEnv.supportSchema(vendor);
153
154            fillSchemaInfo();
155
156            if (SQLUtil.isEmpty(this.database)) {
157                        this.database = DlineageUtil.getTableDatabase(procedureName.toString());
158                        if (SQLUtil.isEmpty(this.database) && ModelBindingManager.getGlobalOption().isShowImplicitSchema()
159                                        && !SQLUtil.isEmpty(procedureName.getImplictDatabaseString())) {
160                                this.database = procedureName.getImplictDatabaseString();
161                        }
162                }
163
164                if (SQLUtil.isEmpty(this.schema)) {
165                        this.schema = DlineageUtil.getTableSchema(procedureName.toString());
166                        if (SQLUtil.isEmpty(this.schema) && ModelBindingManager.getGlobalOption().isShowImplicitSchema()
167                                        && !SQLUtil.isEmpty(procedureName.getImplictSchemaString())) {
168                                this.schema = procedureName.getImplictSchemaString();
169                        }
170                }
171
172                if (!SQLUtil.isEmpty(this.database) && SQLUtil.isEmpty(this.schema)) {
173                        if (!SQLUtil.isEmpty(procedureName.getImplictSchemaString())) {
174                                this.schema = procedureName.getImplictSchemaString();
175                        }
176                }
177
178                if (!supportCatalog) {
179                        this.database = null;
180                } else if (this.database == null && !TSQLEnv.DEFAULT_DB_NAME.equals(getDefaultDatabase())) {
181                        this.database = getDefaultDatabase();
182                }
183
184                if (!supportSchema) {
185                        this.schema = null;
186                } else if (this.schema == null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equals(getDefaultSchema())) {
187                        this.schema = getDefaultSchema();
188                }
189
190            updateProcedureName(supportCatalog, supportSchema);
191
192            if (this.server == null && !TSQLEnv.DEFAULT_SERVER_NAME.equals(getDefaultServer())) {
193                this.server = getDefaultServer();
194            }
195        }
196    }
197    
198    public Procedure(TFunctionCall function) {
199        if (function == null) {
200            throw new IllegalArgumentException("Procedure arguments can't be null.");
201        } else {
202            this.id = ++ModelBindingManager.get().TABLE_COLUMN_ID;
203            this.procedureObject = function;
204            TObjectName procedureName = getProcedureName();
205            TSourceToken startToken = procedureName.getStartToken();
206            TSourceToken endToken = procedureName.getEndToken();
207            this.startPosition = new Pair3<Long, Long, String>(startToken.lineNo, startToken.columnNo, ModelBindingManager.getGlobalHash());
208            this.endPosition = new Pair3<Long, Long, String>(endToken.lineNo,
209                    endToken.columnNo + (long) SQLUtil.endTrim(endToken.getAstext()).length(), ModelBindingManager.getGlobalHash());
210            this.fullName = procedureName.toString();
211            this.name = procedureName.toString();
212
213            EDbVendor vendor = ModelBindingManager.getGlobalOption().getVendor();
214            boolean supportCatalog = TSQLEnv.supportCatalog(vendor);
215            boolean supportSchema = TSQLEnv.supportSchema(vendor);
216
217            fillSchemaInfo();
218
219            if (SQLUtil.isEmpty(this.database)) {
220                        this.database = DlineageUtil.getTableDatabase(procedureName.toString());
221                        if (SQLUtil.isEmpty(this.database) && ModelBindingManager.getGlobalOption().isShowImplicitSchema()
222                                        && !SQLUtil.isEmpty(procedureName.getImplictDatabaseString())) {
223                                this.database = procedureName.getImplictDatabaseString();
224                        }
225                }
226
227                if (SQLUtil.isEmpty(this.schema)) {
228                        this.schema = DlineageUtil.getTableSchema(procedureName.toString());
229                        if (SQLUtil.isEmpty(this.schema) && ModelBindingManager.getGlobalOption().isShowImplicitSchema()
230                                        && !SQLUtil.isEmpty(procedureName.getImplictSchemaString())) {
231                                this.schema = procedureName.getImplictSchemaString();
232                        }
233                }
234
235                if (!SQLUtil.isEmpty(this.database) && SQLUtil.isEmpty(this.schema)) {
236                        if (!SQLUtil.isEmpty(procedureName.getImplictSchemaString())) {
237                                this.schema = procedureName.getImplictSchemaString();
238                        }
239                }
240
241                if (!supportCatalog) {
242                        this.database = null;
243                } else if (this.database == null && !TSQLEnv.DEFAULT_DB_NAME.equals(getDefaultDatabase())) {
244                        this.database = getDefaultDatabase();
245                }
246
247                if (!supportSchema) {
248                        this.schema = null;
249                } else if (this.schema == null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equals(getDefaultSchema())) {
250                        this.schema = getDefaultSchema();
251                }
252
253            updateProcedureName(supportCatalog, supportSchema);
254
255            if (this.server == null && !TSQLEnv.DEFAULT_SERVER_NAME.equals(getDefaultServer())) {
256                this.server = getDefaultServer();
257            }
258        }
259    }
260    
261    private void fillSchemaInfo() {
262                TCustomSqlStatement stmt = DlineageUtil.getTopStmt(ModelBindingManager.getGlobalStmtStack().peek());
263                String sqlComment = null;
264                try {
265                        sqlComment = stmt.getCommentBeforeNode();
266                } catch (Exception e) {
267                }
268                if (!SQLUtil.isEmpty(sqlComment) && (sqlComment.indexOf("db") != -1 || sqlComment.indexOf("schema") != -1)) {
269                        Properties properties = new Properties();
270                        try {
271                                properties.load(
272                                                new ByteArrayInputStream(sqlComment.replace("--", "").trim().replace(",", "\n").getBytes()));
273                                if (SQLUtil.isEmpty(this.server) && properties.containsKey("db-instance")) {
274                                        this.server = properties.getProperty("db-instance");
275                                }
276                                if (SQLUtil.isEmpty(this.database) && properties.containsKey("db")) {
277                                        this.database = properties.getProperty("db");
278                                        if(this.database.indexOf(".")!=-1) {
279                                                this.database = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotCatalog, this.database);
280                                        }
281                                }
282                                if (SQLUtil.isEmpty(this.schema) && properties.containsKey("schema")) {
283                                        this.schema = properties.getProperty("schema");
284                                        if(this.schema.indexOf(".")!=-1) {
285                                                this.schema = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotSchema, this.schema);
286                                        }
287                                }
288                        } catch (IOException e) {
289                logger.error("load sql comment properties failed.", e);
290                        }
291                }
292                
293                if (SQLUtil.isEmpty(this.database) || this.database.equals(ModelBindingManager.getGlobalDatabase())) {
294                        TSQLEnv sqlEnv = ModelBindingManager.getGlobalSQLEnv();
295                        if (sqlEnv == null) {
296                                return;
297                        }
298                        int occurrence = 0;
299                        int maxPriority = -1;
300                        String tableDatabase = null;
301                        String tableSchema = null;
302                        Map<Integer, List<String>> databaseSchemasMap = new HashMap<Integer, List<String>>();
303                        for (TSQLCatalog catalog : sqlEnv.getCatalogList()) {
304                                if (SQLUtil.isEmpty(schema) || this.schema.equals(ModelBindingManager.getGlobalSchema())) {
305                                        for (TSQLSchema schema : catalog.getSchemaList()) {
306                                                TSQLSchemaObject tsqlTable = schema.findSchemaObject(DlineageUtil.getSimpleTableName(this.name));
307                                                if (tsqlTable != null) {
308                                                        occurrence += 1;
309                                                        if (tsqlTable.getPriority() > maxPriority
310                                                                        || (maxPriority > 0 && tsqlTable.getPriority() == maxPriority)) {
311                                                                tableDatabase = catalog.getName();
312                                                                if(tableDatabase.indexOf(".")!=-1) {
313                                                                        tableDatabase = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotCatalog, tableDatabase);
314                                                                }
315                                                                tableSchema = schema.getName();
316                                                                if(tableSchema.indexOf(".")!=-1) {
317                                                                        tableSchema = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotSchema, tableSchema);
318                                                                }
319                                                                maxPriority = tsqlTable.getPriority();
320                                                                if(!databaseSchemasMap.containsKey(maxPriority)) {
321                                                                        databaseSchemasMap.put(maxPriority, new ArrayList<String>());
322                                                                }
323                                                                databaseSchemasMap.get(maxPriority).add(tableDatabase + "___" + tableSchema);
324                                                        }
325                                                }
326                                        }
327                                } else {
328                                        TSQLSchema schema = catalog.getSchema(this.schema, false);
329                                        if (schema != null) {
330                                                TSQLSchemaObject tsqlTable = schema.findSchemaObject(DlineageUtil.getSimpleTableName(this.name));
331                                                if (tsqlTable != null) {
332                                                        occurrence += 1;
333                                                        if (tsqlTable.getPriority() > maxPriority
334                                                                        || (maxPriority > 0 && tsqlTable.getPriority() == maxPriority)) {
335                                                                tableDatabase = catalog.getName();
336                                                                if(tableDatabase.indexOf(".")!=-1) {
337                                                                        tableDatabase = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotCatalog, tableDatabase);
338                                                                }
339                                                                tableSchema = schema.getName();
340                                                                if(tableSchema.indexOf(".")!=-1) {
341                                                                        tableSchema = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotSchema, tableSchema);
342                                                                }
343                                                                maxPriority = tsqlTable.getPriority();
344                                                                if(!databaseSchemasMap.containsKey(maxPriority)) {
345                                                                        databaseSchemasMap.put(maxPriority, new ArrayList<String>());
346                                                                }
347                                                                databaseSchemasMap.get(maxPriority).add(tableDatabase + "___" + tableSchema);
348                                                        }
349                                                }
350                                        }
351                                }
352                        }
353                        if (occurrence == 1 || (maxPriority > 0 && databaseSchemasMap.get(maxPriority).size() == 1)) {
354                                if (this.database == null && tableDatabase != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(tableDatabase)) {
355                                        this.database = tableDatabase;
356                                }
357                                if (this.schema == null && tableSchema != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(tableSchema)) {
358                                        this.schema = tableSchema;
359                                }
360                        }
361                } else if (SQLUtil.isEmpty(this.schema) || this.schema.equals(ModelBindingManager.getGlobalSchema())) {
362                        TSQLEnv sqlEnv = ModelBindingManager.getGlobalSQLEnv();
363                        if (sqlEnv == null) {
364                                return;
365                        }
366                        int occurrence = 0;
367                        int maxPriority = -1;
368                        String tableDatabase = null;
369                        String tableSchema = null;
370                        Map<Integer, List<String>> databaseSchemasMap = new HashMap<Integer, List<String>>();
371                        TSQLCatalog catalog = sqlEnv.searchCatalog(this.database);
372                        if (catalog != null) {
373                                for (TSQLSchema schema : catalog.getSchemaList()) {
374                                        TSQLSchemaObject tsqlTable = schema.findSchemaObject(DlineageUtil.getSimpleTableName(this.name));
375                                        if (tsqlTable != null) {
376                                                occurrence += 1;
377                                                if (tsqlTable.getPriority() > maxPriority
378                                                                || (maxPriority > 0 && tsqlTable.getPriority() == maxPriority)) {
379                                                        tableDatabase = catalog.getName();
380                                                        if (tableDatabase.indexOf(".") != -1) {
381                                                                tableDatabase = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotCatalog, tableDatabase);
382                                                        }
383                                                        tableSchema = schema.getName();
384                                                        if (tableSchema.indexOf(".") != -1) {
385                                                                tableSchema = SQLUtil.quoteDottedName(ModelBindingManager.getGlobalOption().getVendor(), ESQLDataObjectType.dotSchema, tableSchema);
386                                                        }
387                                                        maxPriority = tsqlTable.getPriority();
388                                                        if(!databaseSchemasMap.containsKey(maxPriority)) {
389                                                                databaseSchemasMap.put(maxPriority, new ArrayList<String>());
390                                                        }
391                                                        databaseSchemasMap.get(maxPriority).add(tableDatabase + "___" + tableSchema);
392                                                }
393                                        }
394                                }
395                        }
396
397                        if (occurrence == 1 || (maxPriority > 0 && databaseSchemasMap.get(maxPriority).size() == 1)) {
398                                if (this.database == null && tableDatabase != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(tableDatabase)) {
399                                        this.database = tableDatabase;
400                                }
401                                if (this.schema == null && tableSchema != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(tableSchema)) {
402                                        this.schema = tableSchema;
403                                }
404                        }
405
406                        if (SQLUtil.isEmpty(this.schema) && !SQLUtil.isEmpty(this.database)) {
407                                this.schema = getDefaultSchema();
408                        }
409                }
410        }
411
412        protected void updateProcedureName(boolean supportCatalog, boolean supportSchema) {
413                List<String> segments = SQLUtil.parseNames(this.name);
414                this.name = segments.get(segments.size() - 1);
415                // The typed key of the display name composed below, level for level (RoutineAliasKey.Display).
416                RoutineAliasKey.Display display;
417                try {
418                        display = new RoutineAliasKey.Display(getProcedureName(), segments);
419                } catch (RuntimeException failure) {
420                        display = null;
421                }
422                if (supportCatalog && supportSchema) {
423                        StringBuilder builder = new StringBuilder();
424                        if (segments.size() > 2) {
425                                builder.append(segments.get(segments.size() - 3)).append(".");
426                                if (display != null) display.segment(segments.size() - 3);
427                        } else {
428                                if (!SQLUtil.isEmpty(this.database) && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(this.database)) {
429                                        builder.append(this.database).append(".");
430                                        if (display != null) display.field(this.database);
431                                }
432                        }
433                        if (segments.size() > 1) {
434                                builder.append(segments.get(segments.size() - 2)).append(".");
435                                if (display != null) display.segment(segments.size() - 2);
436                        } else {
437                                if (builder.length() > 0) {
438                                        if (this.schema == null) {
439                                                if (ModelBindingManager.getGlobalVendor() == EDbVendor.dbvmssql) {
440                                                        this.schema = "dbo";
441                                                } else {
442                                                        this.schema = getDefaultSchema();
443                                                }
444                                        }
445                                        else if (TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(this.schema)
446                                                        && ModelBindingManager.getGlobalVendor() == EDbVendor.dbvmssql) {
447                                                this.schema = "dbo";
448                                        }
449                                        builder.append(this.schema).append(".");
450                                        if (display != null) display.field(this.schema);
451                                } else {
452                                        if (!SQLUtil.isEmpty(this.schema) && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(this.schema)) {
453                                                builder.append(this.schema).append(".");
454                                                if (display != null) display.field(this.schema);
455                                        }
456                                }
457                        }
458                        builder.append(this.name);
459                        this.name = builder.toString();
460                } else if (supportCatalog) {
461                        if (segments.size() > 1) {
462                                this.name = segments.get(segments.size() - 2) + "." + this.name;
463                                if (display != null) display.segment(segments.size() - 2);
464                        }
465                        else {
466                                if (!SQLUtil.isEmpty(this.database) && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(this.database)) {
467                                        this.name = this.database + "." + this.name;
468                                        if (display != null) display.field(this.database);
469                                }
470                        }
471                } else if (supportSchema) {
472                        if (segments.size() > 1) {
473                                this.name = segments.get(segments.size() - 2) + "." + this.name;
474                                if (display != null) display.segment(segments.size() - 2);
475                        } else {
476                                if (!SQLUtil.isEmpty(this.schema) && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(this.schema)) {
477                                        this.name = this.schema + "." + this.name;
478                                        if (display != null) display.field(this.schema);
479                                }
480                        }
481                }
482                if (display != null) {
483                        try {
484                                aliasKey = display.finish();
485                        } catch (RuntimeException failure) {
486                                aliasKey = null;
487                        }
488                }
489        }
490
491    protected String getDefaultServer() {
492        String defaultServer = null;
493        if (ModelBindingManager.getGlobalSQLEnv() != null) {
494            defaultServer = ModelBindingManager.getGlobalSQLEnv().getDefaultServerName();
495        }
496        if (!SQLUtil.isEmpty(defaultServer))
497            return defaultServer;
498        return TSQLEnv.DEFAULT_SERVER_NAME;
499    }
500
501    protected String getDefaultSchema() {
502        return ModelBindingManager.getEffectiveDefaultSchema();
503    }
504
505    protected String getDefaultDatabase() {
506        return ModelBindingManager.getEffectiveDefaultDatabase();
507    }
508
509    private TObjectName getProcedureName() {
510        if (procedureObject instanceof TTeradataCreateProcedure) {
511            return ((TTeradataCreateProcedure) procedureObject).getProcedureName();
512        }
513        if (procedureObject instanceof TStoredProcedureSqlStatement) {
514            return ((TStoredProcedureSqlStatement) procedureObject).getStoredProcedureName();
515        }
516        if (procedureObject instanceof TFunctionCall) {
517            return ((TFunctionCall) procedureObject).getFunctionName();
518        }
519        return null;
520    }
521
522    public long getId() {
523        return this.id;
524    }
525
526        /**
527         * The typed key of {@link #getName()}, the display name the routine index is keyed by (see
528         * {@code RoutineAliasKey.Display}); null when it cannot be built.
529         */
530        private RoutineAliasKey aliasKey;
531
532        public RoutineAliasKey getAliasKey() {
533                return aliasKey;
534        }
535
536    public String getName() {
537        return this.name;
538    }
539
540    public void setName(String name) {
541        this.name = name;
542    }
543
544    public Pair3<Long, Long, String> getStartPosition() {
545        return this.startPosition;
546    }
547
548    public Pair3<Long, Long, String> getEndPosition() {
549        return this.endPosition;
550    }
551
552    public String getFullName() {
553        return this.fullName;
554    }
555
556    public void setFullName(String fullName) {
557        this.fullName = fullName;
558    }
559
560    // Declaration-time scope key this routine's variables were registered
561    // under (DlineageUtil.getProcedureParentName at declaration). Carried
562    // separately from fullName/name, which are display forms: call-site
563    // argument binding must look variables up by this exact key.
564    private String variableScopeKey;
565
566    public String getVariableScopeKey() {
567        return this.variableScopeKey;
568    }
569
570    public void setVariableScopeKey(String variableScopeKey) {
571        this.variableScopeKey = variableScopeKey;
572    }
573
574    // ---- B1 (routine-summary-scc-design §2.1/§3): additive identity
575    // metadata. LEGACY lookups never consult these — the identity-first
576    // paths of SHADOW/ENHANCED (slice B3) are the consumers. The
577    // identityScopeKey discriminates same-named overloads that the legacy
578    // variableScopeKey deliberately still collides (codex-C2: LEGACY may
579    // retain legacy behavior; goldens stay byte-identical).
580    private gudusoft.gsqlparser.dlineage.dynamicsql.RoutineIdentity routineIdentity;
581    private String identityScopeKey;
582
583    public gudusoft.gsqlparser.dlineage.dynamicsql.RoutineIdentity getRoutineIdentity() {
584        return this.routineIdentity;
585    }
586
587    public void setRoutineIdentity(
588            gudusoft.gsqlparser.dlineage.dynamicsql.RoutineIdentity routineIdentity) {
589        this.routineIdentity = routineIdentity;
590    }
591
592    public String getIdentityScopeKey() {
593        return this.identityScopeKey;
594    }
595
596    public void setIdentityScopeKey(String identityScopeKey) {
597        this.identityScopeKey = identityScopeKey;
598    }
599
600    public List<Argument> getArguments() {
601        return this.arguments;
602    }
603
604    public void setArguments(List<Argument> arguments) {
605        this.arguments = arguments;
606    }
607
608    public void addArgument(Argument argument) {
609        if (argument != null && !this.arguments.contains(argument)) {
610            this.arguments.add(argument);
611        }
612
613    }
614
615    public ESqlStatementType getType() {
616        return this.type;
617    }
618
619    public void setType(ESqlStatementType type) {
620        this.type = type;
621    }
622
623    public TParseTreeNode getProcedureObject() {
624        return this.procedureObject;
625    }
626
627    public void setProcedureObject(TParseTreeNode procedureObject) {
628        this.procedureObject = procedureObject;
629    }
630
631    public void setId(int id) {
632        this.id = id;
633    }
634
635    public void setStartPosition(Pair3<Long, Long, String> startPosition) {
636        this.startPosition = startPosition;
637    }
638
639    public void setEndPosition(Pair3<Long, Long, String> endPosition) {
640        this.endPosition = endPosition;
641    }
642
643    public String getDatabase() {
644        return database;
645    }
646
647    public String getSchema() {
648        return schema;
649    }
650
651    public OraclePackage getParentPackage() {
652        return parentPackage;
653    }
654
655    public void setParentPackage(OraclePackage parentPackage) {
656        this.parentPackage = parentPackage;
657    }
658
659    public String getServer() {
660        return server;
661    }
662}