/* ----------------------------------------------------------------------------- */ static bool has_col( const VTable * tab, const char * colname ) { bool res = false; struct KNamelist * columns; rc_t rc = VTableListReadableColumns( tab, &columns ); if ( rc == 0 ) { uint32_t count; rc = KNamelistCount( columns, &count ); if ( rc == 0 && count > 0 ) { uint32_t idx; size_t colname_size = string_size( colname ); for ( idx = 0; idx < count && rc == 0 && !res; ++idx ) { const char * a_name; rc = KNamelistGet ( columns, idx, &a_name ); if ( rc == 0 ) { int cmp; size_t a_name_size = string_size( a_name ); uint32_t max_chars = ( uint32_t )colname_size; if ( a_name_size > max_chars ) max_chars = ( uint32_t )a_name_size; cmp = strcase_cmp ( colname, colname_size, a_name, a_name_size, max_chars ); res = ( cmp == 0 ); } } } KNamelistRelease( columns ); } return res; }
/* * calls VTableListReadableColumns to get a list of all column-names of the table * creates a temporary read-cursor to test and get the column-type * walks the KNamelist with the available column-names * tries to add every one of them to the temp. cursor * if the column can be added to the cursor, it is appended * to the column-definition list */ rc_t col_defs_extract_from_table( col_defs* defs, const VTable *table ) { KNamelist *names; rc_t rc; if ( defs == NULL ) return RC( rcExe, rcNoTarg, rcResolving, rcSelf, rcNull ); if ( table == NULL ) return RC( rcExe, rcNoTarg, rcResolving, rcParam, rcNull ); rc = VTableListReadableColumns( table, &names ); DISP_RC( rc, "col_defs_extract_from_table:VTableListReadableColumns() failed" ); if ( rc == 0 ) { const VCursor *cursor; rc = VTableCreateCursorRead( table, &cursor ); DISP_RC( rc, "col_defs_extract_from_table:VTableCreateCursorRead() failed" ); if ( rc == 0 ) { uint32_t count; rc = KNamelistCount( names, &count ); DISP_RC( rc, "col_defs_extract_from_table:KNamelistCount() failed" ); if ( rc == 0 ) { uint32_t idx; for ( idx = 0; idx < count && rc == 0; ++idx ) { const char *name; rc = KNamelistGet( names, idx, &name ); DISP_RC( rc, "col_defs_extract_from_table:KNamelistGet() failed" ); if ( rc == 0 ) { uint32_t temp_idx; rc_t rc1 = VCursorAddColumn( cursor, &temp_idx, "%s", name ); DISP_RC( rc1, "col_defs_extract_from_table:VCursorAddColumn() failed" ); if ( rc1 == 0 ) { rc = col_defs_append_col( defs, name ); DISP_RC( rc, "col_defs_extract_from_table:col_defs_append_col() failed" ); } } } } rc = VCursorRelease( cursor ); DISP_RC( rc, "col_defs_extract_from_table:VCursorRelease() failed" ); } rc = KNamelistRelease( names ); DISP_RC( rc, "col_defs_extract_from_table:KNamelistRelease() failed" ); } return rc; }
/* sort by cid */ return VCtxIdCmp ( & a -> cid, & b -> cid ); } #if 0 /* more for later */ static rc_t create_cursor_all_readable_columns(const VTable *self, unsigned *ncol, uint32_t **idx, const VCursor **curs) { KNamelist *list; rc_t rc = VTableListReadableColumns(self, &list); if (rc == 0) { rc = VTableCreateCursorReadInternal(self, curs); if (rc == 0) { uint32_t n; rc = KNamelistCount(list, ncol); if (rc == 0) { n = *ncol; *idx = malloc(n * sizeof(**idx)); if (idx) { unsigned i; for (i = 0; i != (unsigned)n; ++i) { const char *name; rc = KNamelistGet(list, i, &name); if (rc) break; rc = VCursorAddColumn(*curs, &(*idx)[i], name); if (rc) break; } if (rc) free(*idx); } else rc = RC(rcVDB, rcTable, rcValidating, rcMemory, rcExhausted); } if (rc) VCursorRelease(*curs); } KNamelistRelease(list); } if (rc) { *idx = NULL; *curs = NULL; *ncol = 0; } return rc; }
static bool does_table_have_column( VTable const * tbl, char const column[] ) { KNamelist * column_names; bool res = false; rc_t rc = VTableListReadableColumns ( tbl, &column_names ); if ( rc == 0 ) { uint32_t count; rc = KNamelistCount ( column_names, &count ); if ( rc == 0 && count > 0 ) { uint32_t idx; size_t col_name_size; const char * col_name = string_chr ( column, string_size( column ), ')' ); if ( col_name == NULL ) col_name = column; else col_name++; col_name_size = string_size( col_name ); for ( idx = 0; idx < count && rc == 0 && !res; ++idx ) { const char * name; rc = KNamelistGet ( column_names, idx, &name ); if ( rc == 0 && name != NULL ) { int cmp = string_cmp( col_name, col_name_size, name, string_size( name ), 0xFFFF ); if ( cmp == 0 ) res = true; } } } KNamelistRelease ( column_names ); } return res; }
/* v2.0 interface */ LIB_EXPORT rc_t CC VTableListCol ( const VTable *cself, KNamelist **names ) { return VTableListReadableColumns ( cself, names ); }