/* * cursor_has_set_vobjs () - * return: nonzero iff set has some vobjs, zero otherwise * seq(in): set/sequence db_value */ static bool cursor_has_set_vobjs (DB_SET * set) { int i, size; DB_VALUE element; size = db_set_size (set); for (i = 0; i < size; i++) { if (db_set_get (set, i, &element) != NO_ERROR) { return false; } if (DB_VALUE_TYPE (&element) == DB_TYPE_VOBJ) { pr_clear_value (&element); return true; } pr_clear_value (&element); } return false; }
/* * cursor_fixup_set_vobjs() - if val is a set/seq of vobjs then * turn it into a set/seq of vmops * return: NO_ERROR on all ok, ER status( or ER_FAILED) otherwise * val(in/out): a db_value */ static int cursor_fixup_set_vobjs (DB_VALUE * value_p) { DB_TYPE type; int rc, i, size; DB_VALUE element; DB_SET *set, *new_set; type = DB_VALUE_TYPE (value_p); if (!pr_is_set_type (type)) { return ER_FAILED; } set = DB_GET_SET (value_p); size = db_set_size (set); if (cursor_has_set_vobjs (set) == false) { return set_convert_oids_to_objects (set); } switch (type) { case DB_TYPE_SET: new_set = db_set_create_basic (NULL, NULL); break; case DB_TYPE_MULTISET: new_set = db_set_create_multi (NULL, NULL); break; case DB_TYPE_SEQUENCE: new_set = db_seq_create (NULL, NULL, size); break; default: return ER_FAILED; } /* fixup element vobjs into vmops and add them to new */ for (i = 0; i < size; i++) { if (db_set_get (set, i, &element) != NO_ERROR) { db_set_free (new_set); return ER_FAILED; } if (cursor_fixup_vobjs (&element) != NO_ERROR) { db_set_free (new_set); return ER_FAILED; } if (type == DB_TYPE_SEQUENCE) { rc = db_seq_put (new_set, i, &element); } else { rc = db_set_add (new_set, &element); } if (rc != NO_ERROR) { db_set_free (new_set); return ER_FAILED; } } pr_clear_value (value_p); switch (type) { case DB_TYPE_SET: DB_MAKE_SET (value_p, new_set); break; case DB_TYPE_MULTISET: DB_MAKE_MULTISET (value_p, new_set); break; case DB_TYPE_SEQUENCE: DB_MAKE_SEQUENCE (value_p, new_set); break; default: db_set_free (new_set); return ER_FAILED; } return NO_ERROR; }
static int dbmt_user_login (int argc, char *argv[], int opt_begin) { const char *outfile, *errfile, *dbname, *dbuser, *dbpasswd; FILE *outfp = NULL, *errfp = NULL; bool isdba = false; if (argc - opt_begin < 5) return -1; outfile = argv[opt_begin++]; outfile = (outfile) ? outfile : ""; errfile = argv[opt_begin++]; errfile = (errfile) ? errfile : ""; dbname = argv[opt_begin++]; dbname = (dbname) ? dbname : ""; dbuser = argv[opt_begin++]; dbuser = (dbuser) ? dbuser : ""; dbpasswd = argv[opt_begin++]; dbpasswd = (dbpasswd) ? dbpasswd : ""; outfp = fopen (outfile, "w"); errfp = fopen (errfile, "w"); if (outfp == NULL || errfp == NULL) { goto login_err; } db_login (dbuser, dbpasswd); if (db_restart (argv[0], 0, dbname) < 0) { fprintf (errfp, "%s", db_error_string (1)); goto login_err; } if (strcasecmp (dbuser, "DBA") == 0) { isdba = true; } else { DB_OBJECT *user, *obj; DB_VALUE v; DB_COLLECTION *col; int i; char *username; user = db_find_user (dbuser); if (user == NULL) { fprintf (errfp, "%s", db_error_string (1)); goto login_err; } db_get (user, "groups", &v); col = db_get_set (&v); for (i = 0; i < db_set_size (col); i++) { db_set_get (col, i, &v); obj = db_get_object (&v); db_get (obj, "name", &v); username = db_get_string (&v); if (username != NULL && strcasecmp (username, "DBA") == 0) { isdba = true; break; } } } if (isdba == true) fprintf (outfp, "isdba\n"); else fprintf (outfp, "isnotdba\n"); db_shutdown (); fclose (outfp); fclose (errfp); return 0; login_err: db_shutdown (); if (outfp != NULL) fclose (outfp); if (errfp != NULL) fclose (errfp); return -1; }
/* * set_to_string() - convert set value to string * return: formatted string * value(in): set value to convert * begin_notation(in): character to use to denote begin of set * end_notation(in): character to use to denote end of set * max_entries(in): maximum number of entries to convert. -1 for all * plain_string(in): refine string for plain output */ static char * set_to_string (DB_VALUE * value, char begin_notation, char end_notation, int max_entries, bool plain_string) { int cardinality, total_string_length, i; char **string_array; char *return_string = NULL; DB_VALUE element; int set_error; DB_SET *set; set = DB_GET_SET (value); if (set == NULL) { return (NULL); } /* pre-fetch any objects in the set, this will prevent multiple server calls during set rendering */ db_fetch_set (set, DB_FETCH_READ, 0); /* formerly we filtered out deleted elements here, now just use db_set_size to get the current size, including NULL & * deleted elements */ cardinality = db_set_size (set); if (cardinality < 0) { return (NULL); } else if (cardinality == 0) { char temp_buffer[4]; i = 0; if (begin_notation != '\0') { temp_buffer[i++] = begin_notation; } if (end_notation != '\0') { temp_buffer[i++] = end_notation; } temp_buffer[i] = '\0'; return (duplicate_string ((const char *) &(temp_buffer[0]))); } if (max_entries != -1 && max_entries < cardinality) { cardinality = max_entries; } string_array = (char **) malloc ((cardinality + 2) * sizeof (char *)); if (string_array == NULL) { return (NULL); } memset (string_array, 0, (cardinality + 2) * sizeof (char *)); total_string_length = cardinality * 2; for (i = 0; i < cardinality; i++) { set_error = db_set_get (set, i, &element); if (set_error != NO_ERROR) { goto finalize; } string_array[i] = csql_db_value_as_string (&element, NULL, plain_string); db_value_clear (&element); if (string_array[i] == NULL) { string_array[i] = duplicate_string ("NULL"); if (string_array[i] == NULL) { goto finalize; } } total_string_length += strlen (string_array[i]); } /* for (i = 0; i < cardinality... */ return_string = (char *) malloc (total_string_length + 4); if (return_string == NULL) { goto finalize; } if (begin_notation != '\0') { (void) sprintf (return_string, "%c%s", begin_notation, string_array[0]); } else { (void) strcpy (return_string, string_array[0]); } for (i = 1; i < cardinality; i++) { (void) strcat (return_string, ", "); (void) strcat (return_string, string_array[i]); } if (end_notation != '\0') { int len = strlen (return_string); return_string[len++] = end_notation; return_string[len] = '\0'; } finalize: for (i = 0; i < cardinality; i++) { if (string_array[i] == NULL) { break; } free_and_init (string_array[i]); } free_and_init (string_array); return return_string; }