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