30
30
static void AppendAlterDatabaseOwnerStmt (StringInfo buf , AlterOwnerStmt * stmt );
31
31
static void AppendAlterDatabaseSetStmt (StringInfo buf , AlterDatabaseSetStmt * stmt );
32
32
static void AppendAlterDatabaseStmt (StringInfo buf , AlterDatabaseStmt * stmt );
33
- static void AppendDefElemConnLimit (StringInfo buf , DefElem * def );
34
33
static void AppendCreateDatabaseStmt (StringInfo buf , CreatedbStmt * stmt );
35
34
static void AppendDropDatabaseStmt (StringInfo buf , DropdbStmt * stmt );
36
35
static void AppendGrantOnDatabaseStmt (StringInfo buf , GrantStmt * stmt );
36
+ static void AppendBasicAlterDatabaseOptions (StringInfo buf , AlterDatabaseStmt * stmt );
37
+ static void AppendGrantDatabases (StringInfo buf , GrantStmt * stmt );
38
+ static void AppendAlterDatabaseSetTablespace (StringInfo buf , DefElem * def , char * dbname );
37
39
38
- const DefElemOptionFormat create_database_option_formats [] = {
40
+ const DefElemOptionFormat createDatabaseOptionFormats [] = {
39
41
{ "owner" , " OWNER %s" , OPTION_FORMAT_STRING },
40
42
{ "template" , " TEMPLATE %s" , OPTION_FORMAT_STRING },
41
43
{ "encoding" , " ENCODING %s" , OPTION_FORMAT_LITERAL_CSTR },
@@ -53,6 +55,14 @@ const DefElemOptionFormat create_database_option_formats[] = {
53
55
{ "is_template" , " IS_TEMPLATE %s" , OPTION_FORMAT_BOOLEAN }
54
56
};
55
57
58
+
59
+ const DefElemOptionFormat alterDatabaseOptionFormats [] = {
60
+ { "is_template" , " IS_TEMPLATE %s" , OPTION_FORMAT_BOOLEAN },
61
+ { "allow_connections" , " ALLOW_CONNECTIONS %s" , OPTION_FORMAT_BOOLEAN },
62
+ { "connection_limit" , " CONNECTION LIMIT %d" , OPTION_FORMAT_INTEGER },
63
+ };
64
+
65
+
56
66
char *
57
67
DeparseAlterDatabaseOwnerStmt (Node * node )
58
68
{
@@ -111,52 +121,67 @@ AppendGrantOnDatabaseStmt(StringInfo buf, GrantStmt *stmt)
111
121
}
112
122
113
123
114
- static void
115
- AppendDefElemConnLimit (StringInfo buf , DefElem * def )
116
- {
117
- appendStringInfo (buf , " CONNECTION LIMIT %ld" , (long int ) defGetNumeric (def ));
118
- }
119
-
120
-
121
124
static void
122
125
AppendAlterDatabaseStmt (StringInfo buf , AlterDatabaseStmt * stmt )
123
126
{
124
- appendStringInfo (buf , "ALTER DATABASE %s " , quote_identifier (stmt -> dbname ));
127
+ if (list_length (stmt -> options ) == 0 )
128
+ {
129
+ elog (ERROR , "got unexpected number of options for ALTER DATABASE" );
130
+ }
125
131
126
132
if (stmt -> options )
127
133
{
128
- ListCell * cell = NULL ;
129
- appendStringInfo (buf , "WITH " );
130
- foreach (cell , stmt -> options )
134
+ DefElem * firstOption = linitial (stmt -> options );
135
+ if (strcmp (firstOption -> defname , "tablespace" ) == 0 )
131
136
{
132
- DefElem * def = castNode (DefElem , lfirst (cell ));
133
- if (strcmp (def -> defname , "is_template" ) == 0 )
134
- {
135
- appendStringInfo (buf , "IS_TEMPLATE %s" ,
136
- quote_literal_cstr (strVal (def -> arg )));
137
- }
138
- else if (strcmp (def -> defname , "connection_limit" ) == 0 )
139
- {
140
- AppendDefElemConnLimit (buf , def );
141
- }
142
- else if (strcmp (def -> defname , "allow_connections" ) == 0 )
143
- {
144
- ereport (ERROR ,
145
- errmsg ("ALLOW_CONNECTIONS is not supported" ));
146
- }
147
- else
148
- {
149
- ereport (ERROR ,
150
- errmsg ("unrecognized ALTER DATABASE option: %s" ,
151
- def -> defname ));
152
- }
137
+ AppendAlterDatabaseSetTablespace (buf , firstOption , stmt -> dbname );
138
+
139
+ /* SET tablespace cannot be combined with other options */
140
+ return ;
153
141
}
142
+
143
+
144
+ appendStringInfo (buf , "ALTER DATABASE %s WITH" ,
145
+ quote_identifier (stmt -> dbname ));
146
+
147
+ AppendBasicAlterDatabaseOptions (buf , stmt );
154
148
}
155
149
156
150
appendStringInfo (buf , ";" );
157
151
}
158
152
159
153
154
+ static void
155
+ AppendAlterDatabaseSetTablespace (StringInfo buf , DefElem * def , char * dbname )
156
+ {
157
+ appendStringInfo (buf ,
158
+ "ALTER DATABASE %s SET TABLESPACE %s" ,
159
+ quote_identifier (dbname ), quote_identifier (defGetString (def )));
160
+ }
161
+
162
+
163
+ /*
164
+ * AppendBasicAlterDatabaseOptions appends basic ALTER DATABASE options to a string buffer.
165
+ * Basic options are those that can be appended to the ALTER DATABASE statement
166
+ * after the "WITH" keyword.(i.e. ALLOW_CONNECTIONS, CONNECTION LIMIT, IS_TEMPLATE)
167
+ * For example, the tablespace option is not a basic option since it is defined via SET keyword.
168
+ *
169
+ * This function takes a string buffer and an AlterDatabaseStmt as input.
170
+ * It appends the basic options to the string buffer.
171
+ *
172
+ */
173
+ static void
174
+ AppendBasicAlterDatabaseOptions (StringInfo buf , AlterDatabaseStmt * stmt )
175
+ {
176
+ DefElem * def = NULL ;
177
+ foreach_ptr (def , stmt -> options )
178
+ {
179
+ DefElemOptionToStatement (buf , def , alterDatabaseOptionFormats , lengthof (
180
+ alterDatabaseOptionFormats ));
181
+ }
182
+ }
183
+
184
+
160
185
char *
161
186
DeparseGrantOnDatabaseStmt (Node * node )
162
187
{
@@ -216,6 +241,22 @@ AppendAlterDatabaseSetStmt(StringInfo buf, AlterDatabaseSetStmt *stmt)
216
241
}
217
242
218
243
244
+ char *
245
+ DeparseAlterDatabaseRenameStmt (Node * node )
246
+ {
247
+ RenameStmt * stmt = (RenameStmt * ) node ;
248
+
249
+ StringInfoData str ;
250
+ initStringInfo (& str );
251
+
252
+ appendStringInfo (& str , "ALTER DATABASE %s RENAME TO %s" ,
253
+ quote_identifier (stmt -> subname ),
254
+ quote_identifier (stmt -> newname ));
255
+
256
+ return str .data ;
257
+ }
258
+
259
+
219
260
char *
220
261
DeparseAlterDatabaseSetStmt (Node * node )
221
262
{
@@ -246,8 +287,8 @@ AppendCreateDatabaseStmt(StringInfo buf, CreatedbStmt *stmt)
246
287
DefElem * option = NULL ;
247
288
foreach_ptr (option , stmt -> options )
248
289
{
249
- DefElemOptionToStatement (buf , option , create_database_option_formats ,
250
- lengthof (create_database_option_formats ));
290
+ DefElemOptionToStatement (buf , option , createDatabaseOptionFormats ,
291
+ lengthof (createDatabaseOptionFormats ));
251
292
}
252
293
}
253
294
0 commit comments