| | 246 | -- Note this is in the public schema!! |
| | 247 | CREATE OR REPLACE FUNCTION _global_record(tablename text, creator text) RETURNS record AS $$ |
| | 248 | DECLARE |
| | 249 | result record; |
| | 250 | record_stmt text; |
| | 251 | BEGIN |
| | 252 | EXECUTE 'SELECT NULL::text AS __name__, NULL::text AS __create__, ' |
| | 253 | || 'NULL::text AS __record__, NULL::text AS __iter__, ' |
| | 254 | || 'NULL::text AS __attributes__, 0::int AS __len__, ' |
| | 255 | || '* FROM ' || tablename || ' LIMIT 1' INTO result; |
| | 256 | |
| | 257 | -- We add these values outside the EXECUTE in case the TEMP table has no rows. |
| | 258 | result.__name__ := tablename; |
| | 259 | result.__create__ := creator; |
| | 260 | result.__record__ := 'SELECT * FROM _global_record(''' || tablename || ''', ''' || creator || ''')'; |
| | 261 | result.__iter__ := 'SELECT * FROM ' || tablename; |
| | 262 | result.__attributes__ := 'SELECT attname FROM test.attributes(''' || tablename || ''')'; |
| | 263 | EXECUTE 'SELECT COUNT(*) FROM ' || tablename INTO result.__len__; |
| | 264 | |
| | 265 | RETURN result; |
| | 266 | END; |
| | 267 | $$ LANGUAGE plpgsql; |
| | 268 | |
| | 269 | |
| | 270 | -- Note this is in the public schema!! |
| 253 | | -- * tablename (text): The complete name of the TEMP table. This allows you |
| 254 | | -- to pass my_record_var.tablename to functions that take a 'call text' |
| 255 | | -- argument, such as assert_column, assert_values, and assert_empty |
| 256 | | -- (since no procedural languages support passing records as args). |
| | 278 | -- * __name__ (text): The complete name of the TEMP table. This allows you |
| | 279 | -- to pass g.__name__ to functions that take a 'call text' argument, |
| | 280 | -- such as assert_column, assert_values, and assert_empty (since no |
| | 281 | -- procedural languages support passing records as args). |
| | 282 | -- * __create__ (text): The SQL statement used to construct the table. |
| | 283 | -- * __record__ (text): The SQL statement used to construct the returned record. |
| | 284 | -- Use this to copy the returned record (perhaps in another function). |
| | 285 | -- Example: EXECUTE g.__record__ INTO g2; |
| | 286 | -- * __iter__ (text): An SQL string to SELECT * FROM the TEMP table. |
| | 287 | -- Example: FOR record IN EXECUTE g.__iter__ |
| | 288 | -- * __attributes__ (text): The SQL string for TEMP table column names. |
| | 289 | -- Example: FOR colname IN EXECUTE g.__attributes__ |
| | 290 | -- * __len__ (int): The number of rows in the TEMP table. This value |
| | 291 | -- is NOT updated if you change the table after its creation. |
| 262 | | EXECUTE 'CREATE TEMP TABLE ' || tablename || ' AS ' || test.statement(call); |
| 263 | | EXECUTE 'SELECT ''' || tablename || '''::text AS tablename, * FROM ' || tablename || ' LIMIT 1' INTO result; |
| 264 | | IF result.tablename IS NULL THEN |
| 265 | | -- Our temp table has no rows, so our tablename wasn't selected either. |
| 266 | | result.tablename := tablename; |
| 267 | | END IF; |
| | 298 | creator := test.statement(call); |
| | 299 | EXECUTE 'CREATE TEMP TABLE ' || tablename || ' AS ' || creator; |
| | 300 | result := _global_record(tablename, creator); |