1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * db_query.cc
24*0Sstevel@tonic-gate *
25*0Sstevel@tonic-gate * Copyright (c) 1988-2000 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate * All Rights Reserved.
27*0Sstevel@tonic-gate */
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #include "db_headers.h"
32*0Sstevel@tonic-gate #include "db_query.h"
33*0Sstevel@tonic-gate #include "nisdb_mt.h"
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate /* Returns db_query containing the index values as obtained from 'attrlist.' */
db_query(db_scheme * scheme,int size,nis_attr * attrlist)37*0Sstevel@tonic-gate db_query::db_query(db_scheme * scheme, int size, nis_attr* attrlist)
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate int i;
40*0Sstevel@tonic-gate num_components = size;
41*0Sstevel@tonic-gate components = new db_qcomp[size];
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate if (components == NULL) {
44*0Sstevel@tonic-gate num_components = 0;
45*0Sstevel@tonic-gate FATAL(
46*0Sstevel@tonic-gate "db_query::db_query: cannot allocate space for components",
47*0Sstevel@tonic-gate DB_MEMORY_LIMIT);
48*0Sstevel@tonic-gate }
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate for (i = 0; i < size; i++) {
51*0Sstevel@tonic-gate if (!scheme->find_index(attrlist[i].zattr_ndx,
52*0Sstevel@tonic-gate &(components[i].which_index))) {
53*0Sstevel@tonic-gate syslog(LOG_ERR, "db_query::db_query: bad index (%s)",
54*0Sstevel@tonic-gate attrlist[i].zattr_ndx);
55*0Sstevel@tonic-gate clear_components(i);
56*0Sstevel@tonic-gate return;
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate components[i].index_value = new
59*0Sstevel@tonic-gate item(attrlist[i].zattr_val.zattr_val_val,
60*0Sstevel@tonic-gate attrlist[i].zattr_val.zattr_val_len);
61*0Sstevel@tonic-gate if (components[i].index_value == NULL) {
62*0Sstevel@tonic-gate clear_components(i);
63*0Sstevel@tonic-gate FATAL(
64*0Sstevel@tonic-gate "db_query::db_query:cannot allocate space for index",
65*0Sstevel@tonic-gate DB_MEMORY_LIMIT);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate * Returns a newly db_query containing the index values as
72*0Sstevel@tonic-gate * obtained from the given object. The object itself,
73*0Sstevel@tonic-gate * along with information on the scheme given, will determine
74*0Sstevel@tonic-gate * which values are extracted from the object and placed into the query.
75*0Sstevel@tonic-gate * Returns an empty query if 'obj' is not a valid entry.
76*0Sstevel@tonic-gate * Note that space is allocated for the query and the index values
77*0Sstevel@tonic-gate * (i.e. do not share pointers with strings in 'obj'.)
78*0Sstevel@tonic-gate */
db_query(db_scheme * scheme,entry_object_p obj)79*0Sstevel@tonic-gate db_query::db_query(db_scheme *scheme, entry_object_p obj)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate num_components = scheme->numkeys(); // scheme's view of key count
82*0Sstevel@tonic-gate db_key_desc *keyinfo = scheme->keyloc();
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate int objsize = obj->en_cols.en_cols_len; // total num columns in obj */
85*0Sstevel@tonic-gate struct entry_col * objcols = obj->en_cols.en_cols_val;
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate /* components of query to be returned */
88*0Sstevel@tonic-gate components = new db_qcomp[num_components];
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate int wherein_obj, i;
91*0Sstevel@tonic-gate if (components == NULL) {
92*0Sstevel@tonic-gate FATAL(
93*0Sstevel@tonic-gate "db_query::db_query: cannot allocate space for components",
94*0Sstevel@tonic-gate DB_MEMORY_LIMIT);
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate /* fill in each component of query */
98*0Sstevel@tonic-gate for (i = 0; i < num_components; i++) {
99*0Sstevel@tonic-gate components[i].which_index = i; // index i
100*0Sstevel@tonic-gate wherein_obj = keyinfo[i].column_number; // column in entry obj
101*0Sstevel@tonic-gate if (wherein_obj >= objsize) {
102*0Sstevel@tonic-gate syslog(LOG_ERR,
103*0Sstevel@tonic-gate "db_query::column %d cannot occur in object with %d columns (start counting at 0)\n",
104*0Sstevel@tonic-gate wherein_obj, objsize);
105*0Sstevel@tonic-gate clear_components(i); // clean up
106*0Sstevel@tonic-gate return;
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate components[i].index_value = new
110*0Sstevel@tonic-gate item(objcols[wherein_obj].ec_value.ec_value_val,
111*0Sstevel@tonic-gate objcols[wherein_obj].ec_value.ec_value_len);
112*0Sstevel@tonic-gate if (components[i].index_value == NULL) {
113*0Sstevel@tonic-gate clear_components(i);
114*0Sstevel@tonic-gate FATAL(
115*0Sstevel@tonic-gate "db_query::db_query:cannot allocate space for index",
116*0Sstevel@tonic-gate DB_MEMORY_LIMIT);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate /* do something about null keys? */
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate void
clear_components(int how_many)124*0Sstevel@tonic-gate db_query::clear_components(int how_many)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate int i;
127*0Sstevel@tonic-gate if (components) {
128*0Sstevel@tonic-gate for (i = 0; i < how_many; i++)
129*0Sstevel@tonic-gate if (components[i].index_value)
130*0Sstevel@tonic-gate delete components[i].index_value;
131*0Sstevel@tonic-gate delete components;
132*0Sstevel@tonic-gate components = NULL;
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate num_components = 0;
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate /* destructor */
~db_query()138*0Sstevel@tonic-gate db_query::~db_query()
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate clear_components(num_components);
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate /* Print all components of this query to stdout. */
144*0Sstevel@tonic-gate void
print()145*0Sstevel@tonic-gate db_query::print()
146*0Sstevel@tonic-gate {
147*0Sstevel@tonic-gate int i;
148*0Sstevel@tonic-gate for (i = 0; i < num_components; i++) {
149*0Sstevel@tonic-gate printf("%d: ", components[i].which_index);
150*0Sstevel@tonic-gate components[i].index_value->print();
151*0Sstevel@tonic-gate putchar('\n');
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate }
154