MCPcopy Create free account
hub / github.com/Meituan-Dianping/SQLAdvisor / isPrimary

Function isPrimary

sqladvisor/main.cc:1070–1110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1068}
1069
1070bool isPrimary(const char *field_name, char *table_name) {
1071 if (options.verbose)
1072 sql_print_information("第%d步:开始验证 字段%s是不是主键。表名:%s \n", STEP_CNT++,
1073 field_name, table_name);
1074 MYSQL *conn = mysql_init(NULL);
1075 if (conn == NULL || table_name == NULL || field_name == NULL) {
1076 finish_with_error(conn);
1077 }
1078 if (mysql_real_connect(conn, options.host, options.username,
1079 options.password, options.dbname, options.port, NULL, 0) == NULL) {
1080 finish_with_error(conn);
1081 }
1082 mysql_query(conn, "set names utf8");
1083 GString *table_exist_index_sql = g_string_new(NULL);
1084 g_string_sprintf(table_exist_index_sql,
1085 "show index from %s where Key_name = 'PRIMARY' and Column_name ='%s' and Seq_in_index = 1",
1086 table_name, field_name);
1087 print_sql(table_exist_index_sql->str);
1088 if (mysql_query(conn, table_exist_index_sql->str)) {
1089 finish_with_error(conn);
1090 }
1091 MYSQL_RES *result = mysql_store_result(conn);
1092 if (result == NULL) {
1093 finish_with_error(conn);
1094 }
1095 bool is_primary = false;
1096 if (mysql_num_rows(result) > 0) {
1097 if (options.verbose)
1098 sql_print_information("第%d步:字段%s是主键。表名:%s \n", STEP_CNT++,
1099 field_name, table_name);
1100 is_primary = true;
1101 } else {
1102 if (options.verbose)
1103 sql_print_information("第%d步:字段%s不是主键。表名:%s \n", STEP_CNT++,
1104 field_name, table_name);
1105 }
1106 g_string_free(table_exist_index_sql, TRUE);
1107 mysql_free_result(result);
1108 mysql_close(conn);
1109 return is_primary;
1110}
1111
1112bool is_like_pre(char * field_ptr) {
1113 bool meet_space = false;

Callers 3

mysql_sql_parse_orderFunction · 0.85
print_indexFunction · 0.85

Calls 6

sql_print_informationFunction · 0.85
finish_with_errorFunction · 0.85
print_sqlFunction · 0.85
mysql_num_rowsFunction · 0.85
mysql_free_resultFunction · 0.85
mysql_closeFunction · 0.85

Tested by

no test coverage detected