xref: /illumos-gate/usr/src/lib/libnisdb/db_query.cc (revision a7cee4e9766ebda975dd156d1f10a70f51c242f0)
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 <stdio.h>
33 #include <string.h>
34 
35 /* Returns db_query containing the index values as obtained from 'attrlist.' */
36 db_query::db_query(db_scheme * scheme, int size, nis_attr* attrlist)
37 {
38 	int i;
39 	num_components = size;
40 	components = new db_qcomp[size];
41 
42 	if (components == NULL) {
43 		num_components = 0;
44 		FATAL(
45 		    "db_query::db_query: cannot allocate space for components",
46 		    DB_MEMORY_LIMIT);
47 	}
48 
49 	for (i = 0; i < size; i++) {
50 		if (!scheme->find_index(attrlist[i].zattr_ndx,
51 					&(components[i].which_index))) {
52 			syslog(LOG_ERR, "db_query::db_query: bad index (%s)",
53 				attrlist[i].zattr_ndx);
54 			clear_components(i);
55 			return;
56 		}
57 		components[i].index_value = new
58 				item(attrlist[i].zattr_val.zattr_val_val,
59 					attrlist[i].zattr_val.zattr_val_len);
60 		if (components[i].index_value  == NULL) {
61 			clear_components(i);
62 			FATAL(
63 			"db_query::db_query:cannot allocate space for index",
64 			DB_MEMORY_LIMIT);
65 		}
66 	}
67 }
68 
69 /*
70  * Returns a newly db_query containing the index values as
71  * obtained from the given object.  The object itself,
72  * along with information on the scheme given, will determine
73  * which values are extracted from the object and placed into the query.
74  * Returns an empty query if 'obj' is not a valid entry.
75  * Note that space is allocated for the query and the index values
76  * (i.e. do not share pointers with strings in 'obj'.)
77 */
78 db_query::db_query(db_scheme *scheme, entry_object_p obj)
79 {
80 	num_components = scheme->numkeys();	// scheme's view of key count
81 	db_key_desc *keyinfo = scheme->keyloc();
82 
83 	int objsize = obj->en_cols.en_cols_len;	// total num columns in obj */
84 	struct entry_col * objcols = obj->en_cols.en_cols_val;
85 
86 	/* components of query to be returned */
87 	components = new db_qcomp[num_components];
88 
89 	int wherein_obj, i;
90 	if (components == NULL) {
91 		FATAL(
92 		    "db_query::db_query: cannot allocate space for components",
93 		    DB_MEMORY_LIMIT);
94 	}
95 
96 	/* fill in each component of query */
97 	for (i = 0; i < num_components; i++) {
98 		components[i].which_index = i;		// index i
99 		wherein_obj = keyinfo[i].column_number; // column in entry obj
100 		if (wherein_obj >= objsize) {
101 			syslog(LOG_ERR,
102 "db_query::column %d cannot occur in object with %d columns (start counting at 0)\n",
103 				wherein_obj, objsize);
104 			clear_components(i);		// clean up
105 			return;
106 		}
107 
108 		components[i].index_value = new
109 			item(objcols[wherein_obj].ec_value.ec_value_val,
110 				objcols[wherein_obj].ec_value.ec_value_len);
111 		if (components[i].index_value  == NULL) {
112 			clear_components(i);
113 			FATAL(
114 			"db_query::db_query:cannot allocate space for index",
115 			DB_MEMORY_LIMIT);
116 		}
117 
118 		/* do something about null keys? */
119 	}
120 }
121 
122 void
123 db_query::clear_components(int how_many)
124 {
125 	int i;
126 	if (components) {
127 		for (i = 0; i < how_many; i++)
128 			if (components[i].index_value)
129 				delete components[i].index_value;
130 		delete components;
131 		components = NULL;
132 	}
133 	num_components = 0;
134 }
135 
136 /* destructor */
137 db_query::~db_query()
138 {
139 	clear_components(num_components);
140 }
141 
142 /* Print all components of this query to stdout. */
143 void
144 db_query::print()
145 {
146 	int i;
147 	for (i = 0; i < num_components; i++) {
148 		printf("%d: ", components[i].which_index);
149 		components[i].index_value->print();
150 		putchar('\n');
151 	}
152 }
153