Changeset 25419 for trunk/ippTools/src/magicdstool.c
- Timestamp:
- Sep 16, 2009, 9:47:16 AM (17 years ago)
- File:
-
- 1 edited
-
trunk/ippTools/src/magicdstool.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippTools/src/magicdstool.c
r25015 r25419 42 42 static bool torestoreMode(pxConfig *config); 43 43 static bool torevertMode(pxConfig *config); 44 static bool completedrevertMode(pxConfig *config); 44 45 45 46 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state); … … 75 76 MODECASE(MAGICDSTOOL_MODE_TORESTORE, torestoreMode); 76 77 MODECASE(MAGICDSTOOL_MODE_TOREVERT, torevertMode); 78 MODECASE(MAGICDSTOOL_MODE_COMPLETEDREVERT, completedrevertMode); 77 79 default: 78 80 psAbort("invalid option (this should not happen)"); … … 396 398 PS_ASSERT_PTR_NON_NULL(config, false); 397 399 400 PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false); 401 398 402 psMetadata *where = psMetadataAlloc(); 399 403 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "=="); 400 404 PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "=="); 401 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 402 PXOPT_COPY_STR(config->args, where, "-stage", "stage", "=="); 405 pxAddLabelSearchArgs (config, where, "-label", "magicDSRun.label", "=="); 403 406 404 407 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 405 408 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 406 409 407 // look for "inputs" that need to processed 408 psString query = pxDataGet("magicdstool_todestreak.sql"); 410 psString sql_file = NULL; 411 psStringAppend(&sql_file, "magicdstool_todestreak_%s.sql", stage); 412 413 psString query = pxDataGet(sql_file); 409 414 if (!query) { 410 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 411 return false; 412 } 415 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file); 416 psFree(sql_file); 417 return false; 418 } 419 psFree(sql_file); 413 420 414 421 if (psListLength(where->list)) { 415 422 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 416 psStringAppend(&query, " WHERE%s", whereClause);423 psStringAppend(&query, " AND %s", whereClause); 417 424 psFree(whereClause); 418 425 } … … 817 824 return true; 818 825 } 819 820 static bool getskycellsMode(pxConfig *config) 821 { 822 // required 823 PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id", true, false); 826 static bool completedrevertMode(pxConfig *config) 827 { 828 PS_ASSERT_PTR_NON_NULL(config, false); 824 829 825 830 psMetadata *where = psMetadataAlloc(); 826 PXOPT_COPY_STR(config->args, where, "-class_id", "warpSkyCellMap.class_id", "=="); 827 PXOPT_COPY_STR(config->args, where, "-skycell_id", "warpSkyCellMap.skycell_id", "=="); 828 829 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 830 831 psString query = pxDataGet("magicdstool_getskycells.sql"); 832 if (!query) { 833 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 834 return false; 835 } 836 831 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magicDSRun.magic_ds_id", "=="); 832 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 833 834 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 835 836 psString query = pxDataGet("magicdstool_completedrevert.sql"); 837 // treat limit == 0 as "no limit" 838 if (limit) { 839 psString limitString = psDBGenerateLimitSQL(limit); 840 psStringAppend(&query, " %s", limitString); 841 psFree(limitString); 842 } 843 844 psString whereString = NULL; 837 845 if (psListLength(where->list)) { 838 846 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 839 psStringAppend(& query, "AND %s", whereClause);847 psStringAppend(&whereString, "\nAND %s", whereClause); 840 848 psFree(whereClause); 841 849 } 842 850 psFree(where); 843 851 844 if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) { 845 psError(PS_ERR_UNKNOWN, false, "database error"); 846 psFree(query); 847 return false; 848 } 852 if (!p_psDBRunQueryF(config->dbh, query, whereString ? whereString : "")) { 853 psFree(whereString); 854 psError(PS_ERR_UNKNOWN, false, "failed to revert"); 855 return false; 856 } 857 psFree(whereString); 849 858 psFree(query); 850 851 859 psArray *output = p_psDBFetchResult(config->dbh); 852 860 if (!output) { … … 868 876 return true; 869 877 } 870 871 if (psArrayLength(output)) { 872 // negative simple so the default is true 873 if (!ippdbPrintMetadatas(stdout, output, "magicDiffSkyfile", !simple)) { 874 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 878 for (int i=0; i<psArrayLength(output); i++) { 879 psMetadata *row = output->data[i]; 880 psS64 magic_ds_id = psMetadataLookupS64(NULL, row, "magic_ds_id"); 881 psString old_state = psMetadataLookupStr(NULL, row, "state"); 882 psString new_state; 883 if (!strcmp(old_state, "goto_censored")) { 884 new_state = "censored"; 885 } else if (!strcmp(old_state, "goto_restored")) { 886 new_state = "restored"; 887 } else { 888 psError(PXTOOLS_ERR_PROG, true, "unexpected state found: %s", old_state); 875 889 psFree(output); 876 890 return false; 877 891 } 878 } 879 892 char *query2 = "UPDATE magicDSRun SET state = '%s' WHERE magic_ds_id = %" PRId64; 893 if (!p_psDBRunQueryF(config->dbh, query2, magic_ds_id, new_state)) { 894 psError(PS_ERR_UNKNOWN, false, "failed to set run magicDSRun.state to %s", new_state); 895 return false; 896 } 897 } 880 898 psFree(output); 881 899 … … 883 901 } 884 902 885 static bool setmagicDSRunState(pxConfig *config, psS64 magic_ds_id, const char *state) 886 { 887 PS_ASSERT_PTR_NON_NULL(state, false); 888 889 // check that state is a valid string value 890 if (!( 891 (strncmp(state, "new", 4) == 0) 892 || (strncmp(state, "full", 5) == 0) 893 ) 894 ) { 895 psError(PS_ERR_UNKNOWN, false, 896 "invalid magicDSRun state: %s", state); 897 return false; 898 } 899 900 char *query = "UPDATE magicDSRun SET state = '%s' WHERE magic_ds_id = %" PRId64; 901 if (!p_psDBRunQueryF(config->dbh, query, state, magic_ds_id)) { 902 psError(PS_ERR_UNKNOWN, false, 903 "failed to change state for magic_id %" PRId64, magic_ds_id); 904 return false; 905 } 906 907 return true; 908 } 909 910 static bool toremoveMode(pxConfig *config) 911 { 912 PS_ASSERT_PTR_NON_NULL(config, false); 903 static bool getskycellsMode(pxConfig *config) 904 { 905 // required 906 PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id", true, false); 913 907 914 908 psMetadata *where = psMetadataAlloc(); 915 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "=="); 916 PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "=="); 917 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 918 919 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 909 PXOPT_COPY_STR(config->args, where, "-class_id", "warpSkyCellMap.class_id", "=="); 910 PXOPT_COPY_STR(config->args, where, "-skycell_id", "warpSkyCellMap.skycell_id", "=="); 911 920 912 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 921 913 922 // look for "inputs" that need to processed 923 psString query = pxDataGet("magicdstool_toremove.sql"); 914 psString query = pxDataGet("magicdstool_getskycells.sql"); 924 915 if (!query) { 925 916 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); … … 929 920 if (psListLength(where->list)) { 930 921 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 931 psStringAppend(&query, " WHERE%s", whereClause);922 psStringAppend(&query, " AND %s", whereClause); 932 923 psFree(whereClause); 933 924 } 934 925 psFree(where); 935 926 936 // treat limit == 0 as "no limit" 937 if (limit) { 938 psString limitString = psDBGenerateLimitSQL(limit); 939 psStringAppend(&query, " %s", limitString); 940 psFree(limitString); 941 } 942 943 if (!p_psDBRunQuery(config->dbh, query)) { 927 if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) { 944 928 psError(PS_ERR_UNKNOWN, false, "database error"); 945 929 psFree(query); … … 970 954 if (psArrayLength(output)) { 971 955 // negative simple so the default is true 972 if (!ippdbPrintMetadatas(stdout, output, " toremove", !simple)) {956 if (!ippdbPrintMetadatas(stdout, output, "magicDiffSkyfile", !simple)) { 973 957 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 974 958 psFree(output); … … 981 965 return true; 982 966 } 983 static bool torestoreMode(pxConfig *config) 967 968 static bool setmagicDSRunState(pxConfig *config, psS64 magic_ds_id, const char *state) 969 { 970 PS_ASSERT_PTR_NON_NULL(state, false); 971 972 // check that state is a valid string value 973 if (!( 974 (strncmp(state, "new", 4) == 0) 975 || (strncmp(state, "full", 5) == 0) 976 ) 977 ) { 978 psError(PS_ERR_UNKNOWN, false, 979 "invalid magicDSRun state: %s", state); 980 return false; 981 } 982 983 char *query = "UPDATE magicDSRun SET state = '%s' WHERE magic_ds_id = %" PRId64; 984 if (!p_psDBRunQueryF(config->dbh, query, state, magic_ds_id)) { 985 psError(PS_ERR_UNKNOWN, false, 986 "failed to change state for magic_id %" PRId64, magic_ds_id); 987 return false; 988 } 989 990 return true; 991 } 992 993 static bool toremoveMode(pxConfig *config) 984 994 { 985 995 PS_ASSERT_PTR_NON_NULL(config, false); … … 994 1004 995 1005 // look for "inputs" that need to processed 996 psString query = pxDataGet("magicdstool_tore store.sql");1006 psString query = pxDataGet("magicdstool_toremove.sql"); 997 1007 if (!query) { 998 1008 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); … … 1043 1053 if (psArrayLength(output)) { 1044 1054 // negative simple so the default is true 1045 if (!ippdbPrintMetadatas(stdout, output, "tore store", !simple)) {1055 if (!ippdbPrintMetadatas(stdout, output, "toremove", !simple)) { 1046 1056 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 1047 1057 psFree(output); … … 1054 1064 return true; 1055 1065 } 1056 1057 1058 static bool torevertMode(pxConfig *config) 1066 static bool torestoreMode(pxConfig *config) 1059 1067 { 1060 1068 PS_ASSERT_PTR_NON_NULL(config, false); 1061 1069 1062 1070 psMetadata *where = psMetadataAlloc(); 1063 PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);1064 1065 1071 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "=="); 1066 1072 PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "=="); 1067 pxAddLabelSearchArgs (config, where, "-label", " magicDSRun.label", "==");1073 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 1068 1074 1069 1075 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 1070 1076 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 1071 1077 1072 psString sql_file = NULL; 1073 psStringAppend(&sql_file, "magicdstool_torevert_%s.sql", stage); 1074 1075 psString query = pxDataGet(sql_file); 1078 // look for "inputs" that need to processed 1079 psString query = pxDataGet("magicdstool_torestore.sql"); 1076 1080 if (!query) { 1077 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file); 1078 psFree(sql_file); 1079 return false; 1080 } 1081 psFree(sql_file); 1081 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 1082 return false; 1083 } 1082 1084 1083 1085 if (psListLength(where->list)) { 1084 1086 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 1085 psStringAppend(&query, " AND%s", whereClause);1087 psStringAppend(&query, " WHERE %s", whereClause); 1086 1088 psFree(whereClause); 1087 1089 } … … 1124 1126 if (psArrayLength(output)) { 1125 1127 // negative simple so the default is true 1128 if (!ippdbPrintMetadatas(stdout, output, "torestore", !simple)) { 1129 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 1130 psFree(output); 1131 return false; 1132 } 1133 } 1134 1135 psFree(output); 1136 1137 return true; 1138 } 1139 1140 1141 static bool torevertMode(pxConfig *config) 1142 { 1143 PS_ASSERT_PTR_NON_NULL(config, false); 1144 1145 psMetadata *where = psMetadataAlloc(); 1146 PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false); 1147 1148 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "=="); 1149 PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "=="); 1150 pxAddLabelSearchArgs (config, where, "-label", "magicDSRun.label", "=="); 1151 1152 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 1153 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 1154 1155 psString sql_file = NULL; 1156 psStringAppend(&sql_file, "magicdstool_torevert_%s.sql", stage); 1157 1158 psString query = pxDataGet(sql_file); 1159 if (!query) { 1160 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file); 1161 psFree(sql_file); 1162 return false; 1163 } 1164 psFree(sql_file); 1165 1166 if (psListLength(where->list)) { 1167 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 1168 psStringAppend(&query, " AND %s", whereClause); 1169 psFree(whereClause); 1170 } 1171 psFree(where); 1172 1173 // treat limit == 0 as "no limit" 1174 if (limit) { 1175 psString limitString = psDBGenerateLimitSQL(limit); 1176 psStringAppend(&query, " %s", limitString); 1177 psFree(limitString); 1178 } 1179 1180 if (!p_psDBRunQuery(config->dbh, query)) { 1181 psError(PS_ERR_UNKNOWN, false, "database error"); 1182 psFree(query); 1183 return false; 1184 } 1185 psFree(query); 1186 1187 psArray *output = p_psDBFetchResult(config->dbh); 1188 if (!output) { 1189 psErrorCode err = psErrorCodeLast(); 1190 switch (err) { 1191 case PS_ERR_DB_CLIENT: 1192 psError(PXTOOLS_ERR_SYS, false, "database error"); 1193 case PS_ERR_DB_SERVER: 1194 psError(PXTOOLS_ERR_PROG, false, "database error"); 1195 default: 1196 psError(PXTOOLS_ERR_PROG, false, "unknown error"); 1197 } 1198 1199 return false; 1200 } 1201 if (!psArrayLength(output)) { 1202 psTrace("magicdstool", PS_LOG_INFO, "no rows found"); 1203 psFree(output); 1204 return true; 1205 } 1206 1207 if (psArrayLength(output)) { 1208 // negative simple so the default is true 1126 1209 if (!ippdbPrintMetadatas(stdout, output, "torevert", !simple)) { 1127 1210 psError(PS_ERR_UNKNOWN, false, "failed to print array");
Note:
See TracChangeset
for help on using the changeset viewer.
