void session::query_schema(const string &dbName, const string &tableName, std::vector<column_definition> &columns) { if (!is_open()) return; // TODO: use binding for table parameter string pk_sql = string("SELECT tc.table_schema, tc.table_name, kc.column_name FROM information_schema.table_constraints tc ") + "JOIN information_schema.key_column_usage kc ON kc.table_name = tc.table_name AND kc.table_schema = tc.table_schema " + "WHERE tc.constraint_type = 'PRIMARY KEY' AND tc.table_name = '" + tableName + "' AND tc.table_schema = '" + dbName + "' ORDER BY tc.table_schema, tc.table_name, " "kc.position_in_unique_constraint;"; string col_sql = "SELECT column_name, data_type, extra, column_default FROM information_schema.columns WHERE table_name = '" + tableName + "' AND table_schema = '" + dbName + "';"; auto rs = query(col_sql); auto primary_keys = query(pk_sql); while (rs->next()) { auto row = rs->current_row(); column_definition def; // column name def.name = row["column_name"].to_value().to_string(); if (def.name.empty()) { continue; } def.pk = false; def.autoincrement = false; primary_keys->reset(); while (primary_keys->next()) { auto pk = primary_keys->current_row(); if (pk["column_name"].to_value() == def.name) { def.pk = true; def.autoincrement = row["extra"].to_value() == "auto_increment"; } } // find type def.type = row["data_type"].to_value().to_string(); def.default_value = row["column_default"].to_value().to_string(); columns.push_back(def); } }
void session::query_schema(const string &dbName, const string &tableName, std::vector<column_definition> &columns) { if (tableName.empty()) { throw database_exception("no table name to query schema"); } auto rs = query("pragma table_info(" + tableName + ")"); while (rs->next()) { auto row = rs->current_row(); column_definition def; // column name def.name = row["name"].to_value().to_string(); // primary key check def.pk = row["pk"].to_value().to_bool(); // find type def.type = row["type"].to_value().to_string(); def.autoincrement = def.pk; def.default_value = row["dflt_value"].to_value().to_string(); columns.push_back(def); } }
/* printf-style print function which calls printw, but only if the cursor is * not on the last line. */ static void print(const char *fmt, ...) { va_list args; if(current_row() < lines()-1) { va_start(args, fmt); vw_printw(stdscr, fmt, args); va_end(args); } }
static void top(void) { xenstat_domain **domains; unsigned int i, num_domains = 0; /* Now get the node information */ if (prev_node != NULL) xenstat_free_node(prev_node); prev_node = cur_node; cur_node = xenstat_get_node(xhandle, XENSTAT_ALL); if (cur_node == NULL) fail("Failed to retrieve statistics from libxenstat\n"); /* dump summary top information */ if (!batch) do_summary(); /* Count the number of domains for which to report data */ num_domains = xenstat_node_num_domains(cur_node); domains = calloc(num_domains, sizeof(xenstat_domain *)); if(domains == NULL) fail("Failed to allocate memory\n"); for (i=0; i < num_domains; i++) domains[i] = xenstat_node_domain_by_index(cur_node, i); /* Sort */ qsort(domains, num_domains, sizeof(xenstat_domain *), (int(*)(const void *, const void *))compare_domains); if(first_domain_index >= num_domains) first_domain_index = num_domains-1; for (i = first_domain_index; i < num_domains; i++) { if(!batch && current_row() == lines()-1) break; if (i == first_domain_index || repeat_header) do_header(); do_domain(domains[i]); if (show_vcpus) do_vcpu(domains[i]); if (show_networks) do_network(domains[i]); if (show_vbds) do_vbd(domains[i]); if (show_tmem) do_tmem(domains[i]); } if (!batch) do_bottom_line(); free(domains); }
int SCP_instance::exclude_a_row(int row_id) { row_covered[row_id] = true; row_excluded[row_id] = true; set<int> ¤t_row((*rows)[row_id]); for (set<int>::iterator iterator1 = current_row.begin(); iterator1 != current_row.end(); iterator1++) { ((*columns)[*iterator1]).erase(row_id); } ((*rows)[row_id]).clear(); return 0; };
/* printf-style print function which calls printw, but only if the cursor is * not on the last line. */ static void print(const char *fmt, ...) { va_list args; if (!batch) { if((current_row() < lines()-1)) { va_start(args, fmt); vwprintw(stdscr, (curses_str_t)fmt, args); va_end(args); } } else { va_start(args, fmt); vprintf(fmt, args); va_end(args); } }
int SCP_instance::include_a_column(int column_id) { column_chosen[column_id] = true; /*Make a copy of the column*/ set<int> copy_of_the_column = (*columns)[column_id]; /* (*iterator1) is the row_id */ for (set<int>::iterator iterator1 = copy_of_the_column.begin(); iterator1 != copy_of_the_column.end(); iterator1++) { set<int> ¤t_row((*rows)[*iterator1]); /* (*iterator2) is the column id */ for (set<int>::iterator iterator2 = current_row.begin(); iterator2 != current_row.end(); iterator2++) { ((*columns)[*iterator2]).erase(*iterator1); } ((*rows)[*iterator1]).clear(); row_covered[*iterator1] = true; }; ((*columns)[column_id]).clear(); return 0; };
void SCP_instance::consistency_checking() { /*check that the numbers within the rows and the columns are all legitimate*/ for (int ii = 0; ii < number_of_rows; ii++) { set<int> ¤t_row((*rows)[ii]); for (set<int>::iterator iterator1= current_row.begin(); iterator1 != current_row.end(); iterator1 ++) { if (((*iterator1) >= number_of_columns) or ((*iterator1) < 0)) { cout << "EXCEPTION: within SCP_instance::consistency_checking the number is invalid" << endl; } } } for (int ii = 0; ii < number_of_columns; ii++) { set<int> ¤t_column((*columns)[ii]); for (set<int>::iterator iterator1= current_column.begin(); iterator1 != current_column.end(); iterator1 ++) { if (((*iterator1) >= number_of_rows) or ((*iterator1) < 0)) { cout << "EXCEPTION: within SCP_instance::consistency_checking the number is invalid" << endl; } } } /*check that none of the rows or columns are empty*/ for (int ii = 0 ; ii < number_of_rows; ii++) { if (((*rows)[ii]).size() == 0) { cout << "EXCEPTION within SCP_instance::consistency_checking, an empty row" << endl; } } for (int ii = 0 ; ii < number_of_columns; ii++) { if (((*columns)[ii]).size() == 0 ) { cout << "EXCEPTION within SCP_instance::consistency_checking, an empty column" << endl; } } /*check that the rows and the columns are consistent with one another, and none of them are empty*/ for (int ii = 0 ; ii < number_of_rows; ii ++) { set<int> & current_row((*rows)[ii]); for (set<int>::iterator iterator1 = current_row.begin(); iterator1 != current_row.end(); iterator1++) { if (((*columns)[*iterator1]).find(ii) == ((*columns)[*iterator1]).end()) { cout << "ERROR, rows is not consistent with columns" << endl; } } } for (int ii = 0 ; ii < number_of_columns; ii ++) { set<int> & current_column((*columns)[ii]); for (set<int>::iterator iterator1 = current_column.begin(); iterator1 != current_column.end(); iterator1++) { if (((*rows)[*iterator1]).find(ii) == ((*rows)[*iterator1]).end()) { cout << "ERROR, rows is not consistent with columns" << endl; } } } };