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