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