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

Function mysql_sql_parse_group_order_add

sqladvisor/main.cc:859–925  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

857}
858
859void mysql_sql_parse_group_order_add(TABLE_LIST *table,
860 SQL_I_List<ORDER> order_list) {
861 INDEX_FIELD * index_head = NULL;
862 INDEX_FIELD * index_prev = NULL;
863 ORDER * order_field = order_list.first;
864 while (order_field != NULL) {
865 if (order_field->item_ptr->type() == Item::FIELD_ITEM) {
866 if (isPrimary(((Item_field *) (order_field->item_ptr))->field_name,
867 table->table_name)) {
868 if (options.verbose)
869 sql_print_information("第%d步:字段%s是主键并且在列表末尾,忽略不添加 \n",
870 STEP_CNT++,
871 ((Item_field *) (order_field->item_ptr))->field_name);
872 break; //当字段是主键时,则一定是在order by| group by 列表末尾,直接忽略。
873 }
874 Field_Description * field_index = new Field_Description(
875 (Item_field *) order_field->item_ptr);
876 INDEX_FIELD * index_field = new INDEX_FIELD(field_index);
877 if (index_head == NULL) {
878 index_head = index_field;
879 index_prev = index_head;
880 } else {
881 index_prev->next_field = index_field;
882 index_prev = index_prev->next_field;
883 }
884 }
885 order_field = order_field->next;
886 }
887 if (index_head != NULL) {
888 INDEX_FIELD *index = table->index_field_head;
889 if (index == NULL
890 || index->index_field->operator_type != Item_func::EQ_FUNC) {
891 table->index_field_head = index_head;
892 } else {
893 INDEX_FIELD *PREV = NULL;
894 while (index != NULL) {
895 if (strcasecmp(index->index_field->field_name->field_name,
896 index_head->index_field->field_name->field_name) == 0) { //如果存在跟order by条件中相同字段,则直接将order by挂在后面。
897 if (PREV == NULL) {
898 table->index_field_head = index_head;
899 } else {
900 PREV->next_field = index_head;
901 }
902 break;
903 }
904 if (index->index_field->operator_type == Item_func::EQ_FUNC) {
905 if (index->next_field == NULL) {
906 index->next_field = index_head;
907 break;
908 } else {
909 PREV = index;
910 index = index->next_field;
911 }
912 } else {
913 if (PREV == NULL) {
914 table->index_field_head = index_head;
915 } else {
916 PREV->next_field = index_head;

Callers 1

mysql_sql_parse_adminFunction · 0.85

Calls 3

isPrimaryFunction · 0.85
sql_print_informationFunction · 0.85
typeMethod · 0.45

Tested by

no test coverage detected