1 /***************************************************************
2  Copyright (C) 2010-2011 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 
17  ***************************************************************/
18 /**
19  * \file child.c
20  */
21 #include "buckets.h"
22 
23 extern int debug;
24 
25 /**
26  * \brief Given a container uploadtree_pk and bucketdef, determine
27  * if any child is in this bucket.
28  *
29  * \param pgConn      postgresql connection
30  * \param bucketDef   Bucket
31  * \param puploadtree Upload tree element to parse
32  *
33  * \return  1 if child is in this bucket,
34  *          0 not in bucket,
35  *         -1 error
36  */
childInBucket(PGconn * pgConn,pbucketdef_t bucketDef,puploadtree_t puploadtree)37 FUNCTION int childInBucket(PGconn *pgConn, pbucketdef_t bucketDef, puploadtree_t puploadtree)
38 {
39   char *fcnName = "childInBucket";
40   char  sql[1024];
41   int   lft, rgt, upload_pk, rv;
42   PGresult *result;
43 
44   lft = puploadtree->lft;
45   rgt = puploadtree->rgt;
46   upload_pk = puploadtree->upload_fk;
47 
48   /* Are any children in this bucket?
49      First check bucket_container.
50      If none found, then look in bucket_file.
51   */
52   snprintf(sql, sizeof(sql),
53            "select uploadtree_pk from %s \
54               inner join bucket_container \
55                 on uploadtree_fk=uploadtree_pk and bucket_fk=%d \
56                    and agent_fk=%d and nomosagent_fk=%d \
57             where upload_fk=%d and %s.lft BETWEEN %d and %d limit 1",
58            bucketDef->uploadtree_tablename,
59            bucketDef->bucket_pk, bucketDef->bucket_agent_pk,
60            bucketDef->nomos_agent_pk, upload_pk,
61            bucketDef->uploadtree_tablename,
62            lft, rgt);
63 //  if (debug) printf("===%s:%d:\n%s\n===\n", __FILE__, __LINE__, sql);
64   result = PQexec(pgConn, sql);
65   if (fo_checkPQresult(pgConn, result, sql, fcnName, __LINE__)) return -1;
66   rv = PQntuples(result);
67   PQclear(result);
68   if (rv) return 1;
69 
70   /* none found so look in bucket_file for any child in this bucket */
71   snprintf(sql, sizeof(sql),
72            "select uploadtree_pk from %s \
73               inner join bucket_file \
74                 on %s.pfile_fk=bucket_file.pfile_fk and bucket_fk=%d \
75                    and agent_fk=%d and nomosagent_fk=%d \
76             where upload_fk=%d and %s.lft BETWEEN %d and %d limit 1",
77            bucketDef->uploadtree_tablename,
78            bucketDef->uploadtree_tablename,
79            bucketDef->bucket_pk, bucketDef->bucket_agent_pk,
80            bucketDef->nomos_agent_pk, upload_pk,
81            bucketDef->uploadtree_tablename,
82            lft, rgt);
83 //  if (debug) printf("===%s:%d:\n%s\n===\n", __FILE__, __LINE__, sql);
84   result = PQexec(pgConn, sql);
85   if (fo_checkPQresult(pgConn, result, sql, fcnName, __LINE__)) return -1;
86   rv = PQntuples(result);
87   PQclear(result);
88   if (rv) return 1;
89 
90   return 0;
91 }
92