6
6
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
9
- * $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.294 2004/01/10 23:28:45 neilc Exp $
9
+ * $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.295 2004/01/11 04:58:17 neilc Exp $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -54,8 +54,11 @@ typedef struct
54
54
const char * stmtType ; /* "CREATE SCHEMA" or "ALTER SCHEMA" */
55
55
char * schemaname ; /* name of schema */
56
56
char * authid ; /* owner of schema */
57
+ List * sequences ; /* CREATE SEQUENCE items */
57
58
List * tables ; /* CREATE TABLE items */
58
59
List * views ; /* CREATE VIEW items */
60
+ List * indexes ; /* CREATE INDEX items */
61
+ List * triggers ; /* CREATE TRIGGER items */
59
62
List * grants ; /* GRANT items */
60
63
List * fwconstraints ; /* Forward referencing FOREIGN KEY
61
64
* constraints */
@@ -3152,13 +3155,28 @@ transformColumnType(ParseState *pstate, ColumnDef *column)
3152
3155
ReleaseSysCache (ctype );
3153
3156
}
3154
3157
3158
+ static void
3159
+ setSchemaName (char * context_schema , char * * stmt_schema_name )
3160
+ {
3161
+ if (* stmt_schema_name == NULL )
3162
+ * stmt_schema_name = context_schema ;
3163
+ else if (strcmp (context_schema , * stmt_schema_name ) != 0 )
3164
+ ereport (ERROR ,
3165
+ (errcode (ERRCODE_INVALID_SCHEMA_DEFINITION ),
3166
+ errmsg ("CREATE specifies a schema (%s) "
3167
+ "different from the one being created (%s)" ,
3168
+ * stmt_schema_name , context_schema )));
3169
+ }
3170
+
3155
3171
/*
3156
3172
* analyzeCreateSchemaStmt -
3157
3173
* analyzes the "create schema" statement
3158
3174
*
3159
3175
* Split the schema element list into individual commands and place
3160
- * them in the result list in an order such that there are no
3161
- * forward references (e.g. GRANT to a table created later in the list).
3176
+ * them in the result list in an order such that there are no forward
3177
+ * references (e.g. GRANT to a table created later in the list). Note
3178
+ * that the logic we use for determining forward references is
3179
+ * presently quite incomplete.
3162
3180
*
3163
3181
* SQL92 also allows constraints to make forward references, so thumb through
3164
3182
* the table columns and move forward references to a posterior alter-table
@@ -3168,7 +3186,7 @@ transformColumnType(ParseState *pstate, ColumnDef *column)
3168
3186
* but we can't analyze the later commands until we've executed the earlier
3169
3187
* ones, because of possible inter-object references.
3170
3188
*
3171
- * Note: Called from commands/command .c
3189
+ * Note: Called from commands/schemacmds .c
3172
3190
*/
3173
3191
List *
3174
3192
analyzeCreateSchemaStmt (CreateSchemaStmt * stmt )
@@ -3180,9 +3198,12 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
3180
3198
cxt .stmtType = "CREATE SCHEMA" ;
3181
3199
cxt .schemaname = stmt -> schemaname ;
3182
3200
cxt .authid = stmt -> authid ;
3201
+ cxt .sequences = NIL ;
3183
3202
cxt .tables = NIL ;
3184
3203
cxt .views = NIL ;
3204
+ cxt .indexes = NIL ;
3185
3205
cxt .grants = NIL ;
3206
+ cxt .triggers = NIL ;
3186
3207
cxt .fwconstraints = NIL ;
3187
3208
cxt .alters = NIL ;
3188
3209
cxt .blist = NIL ;
@@ -3198,23 +3219,24 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
3198
3219
3199
3220
switch (nodeTag (element ))
3200
3221
{
3222
+ case T_CreateSeqStmt :
3223
+ {
3224
+ CreateSeqStmt * elp = (CreateSeqStmt * ) element ;
3225
+
3226
+ setSchemaName (cxt .schemaname , & elp -> sequence -> schemaname );
3227
+ cxt .sequences = lappend (cxt .sequences , element );
3228
+ }
3229
+ break ;
3230
+
3201
3231
case T_CreateStmt :
3202
3232
{
3203
3233
CreateStmt * elp = (CreateStmt * ) element ;
3204
3234
3205
- if (elp -> relation -> schemaname == NULL )
3206
- elp -> relation -> schemaname = cxt .schemaname ;
3207
- else if (strcmp (cxt .schemaname , elp -> relation -> schemaname ) != 0 )
3208
- ereport (ERROR ,
3209
- (errcode (ERRCODE_INVALID_SCHEMA_DEFINITION ),
3210
- errmsg ("CREATE specifies a schema (%s)"
3211
- " different from the one being created (%s)" ,
3212
- elp -> relation -> schemaname , cxt .schemaname )));
3235
+ setSchemaName (cxt .schemaname , & elp -> relation -> schemaname );
3213
3236
3214
3237
/*
3215
3238
* XXX todo: deal with constraints
3216
3239
*/
3217
-
3218
3240
cxt .tables = lappend (cxt .tables , element );
3219
3241
}
3220
3242
break ;
@@ -3223,23 +3245,33 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
3223
3245
{
3224
3246
ViewStmt * elp = (ViewStmt * ) element ;
3225
3247
3226
- if (elp -> view -> schemaname == NULL )
3227
- elp -> view -> schemaname = cxt .schemaname ;
3228
- else if (strcmp (cxt .schemaname , elp -> view -> schemaname ) != 0 )
3229
- ereport (ERROR ,
3230
- (errcode (ERRCODE_INVALID_SCHEMA_DEFINITION ),
3231
- errmsg ("CREATE specifies a schema (%s)"
3232
- " different from the one being created (%s)" ,
3233
- elp -> view -> schemaname , cxt .schemaname )));
3248
+ setSchemaName (cxt .schemaname , & elp -> view -> schemaname );
3234
3249
3235
3250
/*
3236
3251
* XXX todo: deal with references between views
3237
3252
*/
3238
-
3239
3253
cxt .views = lappend (cxt .views , element );
3240
3254
}
3241
3255
break ;
3242
3256
3257
+ case T_IndexStmt :
3258
+ {
3259
+ IndexStmt * elp = (IndexStmt * ) element ;
3260
+
3261
+ setSchemaName (cxt .schemaname , & elp -> relation -> schemaname );
3262
+ cxt .indexes = lappend (cxt .indexes , element );
3263
+ }
3264
+ break ;
3265
+
3266
+ case T_CreateTrigStmt :
3267
+ {
3268
+ CreateTrigStmt * elp = (CreateTrigStmt * ) element ;
3269
+
3270
+ setSchemaName (cxt .schemaname , & elp -> relation -> schemaname );
3271
+ cxt .triggers = lappend (cxt .triggers , element );
3272
+ }
3273
+ break ;
3274
+
3243
3275
case T_GrantStmt :
3244
3276
cxt .grants = lappend (cxt .grants , element );
3245
3277
break ;
@@ -3251,8 +3283,11 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
3251
3283
}
3252
3284
3253
3285
result = NIL ;
3286
+ result = nconc (result , cxt .sequences );
3254
3287
result = nconc (result , cxt .tables );
3255
3288
result = nconc (result , cxt .views );
3289
+ result = nconc (result , cxt .indexes );
3290
+ result = nconc (result , cxt .triggers );
3256
3291
result = nconc (result , cxt .grants );
3257
3292
3258
3293
return result ;
0 commit comments