~ubuntu-branches/ubuntu/trusty/postgresql-8.4/trusty

« back to all changes in this revision

Viewing changes to contrib/tablefunc/tablefunc.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $PostgreSQL: pgsql/contrib/tablefunc/tablefunc.c,v 1.59 2009/01/07 13:44:36 tgl Exp $ 
 
2
 * $PostgreSQL: pgsql/contrib/tablefunc/tablefunc.c,v 1.60 2009/06/11 14:48:52 momjian Exp $
3
3
 *
4
4
 *
5
5
 * tablefunc
93
93
        float8          stddev;                 /* stddev of the distribution */
94
94
        float8          carry_val;              /* hold second generated value */
95
95
        bool            use_carry;              /* use second generated value */
96
 
}       normal_rand_fctx;
 
96
} normal_rand_fctx;
97
97
 
98
98
#define xpfree(var_) \
99
99
        do { \
124
124
{
125
125
        char       *catname;            /* full category name */
126
126
        int                     attidx;                 /* zero based */
127
 
}       crosstab_cat_desc;
 
127
} crosstab_cat_desc;
128
128
 
129
129
#define MAX_CATNAME_LEN                 NAMEDATALEN
130
130
#define INIT_CATS                               64
163
163
{
164
164
        char            internal_catname[MAX_CATNAME_LEN];
165
165
        crosstab_cat_desc *catdesc;
166
 
}       crosstab_HashEnt;
 
166
} crosstab_HashEnt;
167
167
 
168
168
/*
169
169
 * normal_rand - return requested number of random values
440
440
        }
441
441
 
442
442
        /*
443
 
         * Check that return tupdesc is compatible with the data we got from
444
 
         * SPI, at least based on number and type of attributes
 
443
         * Check that return tupdesc is compatible with the data we got from SPI,
 
444
         * at least based on number and type of attributes
445
445
         */
446
446
        if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
447
447
                ereport(ERROR,
465
465
        MemoryContextSwitchTo(oldcontext);
466
466
 
467
467
        /*
468
 
         * Generate attribute metadata needed later to produce tuples from raw
469
 
         * C strings
 
468
         * Generate attribute metadata needed later to produce tuples from raw C
 
469
         * strings
470
470
         */
471
471
        attinmeta = TupleDescGetAttInMetadata(tupdesc);
472
472
 
488
488
                values = (char **) palloc0((1 + num_categories) * sizeof(char *));
489
489
 
490
490
                /*
491
 
                 * now loop through the sql results and assign each value in
492
 
                 * sequence to the next category
 
491
                 * now loop through the sql results and assign each value in sequence
 
492
                 * to the next category
493
493
                 */
494
494
                for (i = 0; i < num_categories; i++)
495
495
                {
507
507
                        rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
508
508
 
509
509
                        /*
510
 
                         * If this is the first pass through the values for this
511
 
                         * rowid, set the first column to rowid
 
510
                         * If this is the first pass through the values for this rowid,
 
511
                         * set the first column to rowid
512
512
                         */
513
513
                        if (i == 0)
514
514
                        {
515
515
                                xpstrdup(values[0], rowid);
516
516
 
517
517
                                /*
518
 
                                 * Check to see if the rowid is the same as that of the
519
 
                                 * last tuple sent -- if so, skip this tuple entirely
 
518
                                 * Check to see if the rowid is the same as that of the last
 
519
                                 * tuple sent -- if so, skip this tuple entirely
520
520
                                 */
521
521
                                if (!firstpass && xstreq(lastrowid, rowid))
522
522
                                {
533
533
                        if (xstreq(rowid, values[0]))
534
534
                        {
535
535
                                /*
536
 
                                 * Get the next category item value, which is always
537
 
                                 * attribute number three.
 
536
                                 * Get the next category item value, which is always attribute
 
537
                                 * number three.
538
538
                                 *
539
 
                                 * Be careful to assign the value to the array index based
540
 
                                 * on which category we are presently processing.
 
539
                                 * Be careful to assign the value to the array index based on
 
540
                                 * which category we are presently processing.
541
541
                                 */
542
542
                                values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
543
543
 
544
544
                                /*
545
545
                                 * increment the counter since we consume a row for each
546
 
                                 * category, but not for last pass because the outer loop
547
 
                                 * will do that for us
 
546
                                 * category, but not for last pass because the outer loop will
 
547
                                 * do that for us
548
548
                                 */
549
549
                                if (i < (num_categories - 1))
550
550
                                        call_cntr++;
553
553
                        else
554
554
                        {
555
555
                                /*
556
 
                                 * We'll fill in NULLs for the missing values, but we need
557
 
                                 * to decrement the counter since this sql result row
558
 
                                 * doesn't belong to the current output tuple.
 
556
                                 * We'll fill in NULLs for the missing values, but we need to
 
557
                                 * decrement the counter since this sql result row doesn't
 
558
                                 * belong to the current output tuple.
559
559
                                 */
560
560
                                call_cntr--;
561
561
                                xpfree(rowid);
690
690
                                                                                                crosstab_hash,
691
691
                                                                                                tupdesc,
692
692
                                                                                                per_query_ctx,
693
 
                                                                                                rsinfo->allowedModes & SFRM_Materialize_Random);
 
693
                                                         rsinfo->allowedModes & SFRM_Materialize_Random);
694
694
 
695
695
        /*
696
696
         * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1059
1059
                                                                  show_branch,
1060
1060
                                                                  show_serial,
1061
1061
                                                                  per_query_ctx,
1062
 
                                                                  rsinfo->allowedModes & SFRM_Materialize_Random,
 
1062
                                                          rsinfo->allowedModes & SFRM_Materialize_Random,
1063
1063
                                                                  attinmeta);
1064
1064
        rsinfo->setDesc = tupdesc;
1065
1065
 
1139
1139
                                                                  show_branch,
1140
1140
                                                                  show_serial,
1141
1141
                                                                  per_query_ctx,
1142
 
                                                                  rsinfo->allowedModes & SFRM_Materialize_Random,
 
1142
                                                          rsinfo->allowedModes & SFRM_Materialize_Random,
1143
1143
                                                                  attinmeta);
1144
1144
        rsinfo->setDesc = tupdesc;
1145
1145
 
1603
1603
 
1604
1604
        rawstr_text = cstring_to_text(rawstr);
1605
1605
        result_text = DatumGetTextP(DirectFunctionCall1(quote_literal,
1606
 
                                                                                                        PointerGetDatum(rawstr_text)));
 
1606
                                                                                          PointerGetDatum(rawstr_text)));
1607
1607
        result = text_to_cstring(result_text);
1608
1608
 
1609
1609
        return result;