Index: trunk/ippTools/src/magictool.c
===================================================================
--- trunk/ippTools/src/magictool.c	(revision 18526)
+++ trunk/ippTools/src/magictool.c	(revision 18561)
@@ -104,20 +104,266 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false);
+    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
+    PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
+    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
+
     // create warped skycells temp table
-{
-    char *query =
-        "CREATE TEMPORARY TABLE warpComplete\n"
-        " (warp_id BIGINT, skycell_id VARCHAR(64), tess_id VARCHAR(64),\n"
-        " PRIMARY KEY(warp_id, skycell_id, tess_id)) ENGINE=MEMORY\n";
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-}
+    {
+	psString query = pxDataGet("magictool_create_tmp_warpcomplete.sql");
+	if (!query) {
+	    psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+	    return false;
+	}
+
+	if (!p_psDBRunQuery(config->dbh, query)) {
+	    psError(PS_ERR_UNKNOWN, false, "database error");
+	    return false;
+	}
+    }
 
     // find warped skycells
-{
-    psString query = pxDataGet("magictool_find_complete_warpruns.sql");
+    {
+	psString query = pxDataGet("magictool_find_complete_warpruns.sql");
+	if (!query) {
+	    psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+	    return false;
+	}
+
+	if (!p_psDBRunQuery(config->dbh, query)) {
+	    psError(PS_ERR_UNKNOWN, false, "database error");
+	    psFree(query);
+	    return false;
+	}
+	psFree(query);
+    }
+
+    // find the diff_id's of the warped skycells
+    {
+	psString query = pxDataGet("magictool_find_complete_diffed_exposures.sql");
+	if (!query) {
+	    psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+	    return false;
+	}
+
+	if (!p_psDBRunQuery(config->dbh, query)) {
+	    psError(PS_ERR_UNKNOWN, false, "database error");
+	    psFree(query);
+	    return false;
+	}
+	psFree(query);
+    }
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psErrorCode err = psErrorCodeLast();
+        switch (err) {
+	  case PS_ERR_DB_CLIENT:
+	    psError(PXTOOLS_ERR_SYS, false, "database error");
+	  case PS_ERR_DB_SERVER:
+	    psError(PXTOOLS_ERR_PROG, false, "database error");
+	  default:
+	    psError(PXTOOLS_ERR_PROG, false, "unknown error");
+        }
+
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("magictool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    // pre-allocate enough hash buckets for 1 unique warp_id per row
+    psHash *groups = psHashAlloc(psArrayLength(output));
+    // iterate over result set, and group by warp_id
+    for (long i = 0; i < psArrayLength(output); i++) {
+        // lookup the key we're grouping by
+        bool status = false;
+        psS64 warp_id = psMetadataLookupS64(&status, output->data[i], "warp_id");
+        if (!status) {
+            psAbort("failed to lookup value for warp_id");
+        }
+
+        psString key = psDBIntToString(warp_id);
+        // look to see if there is a bin for this key
+        psArray *groupBin = psHashLookup(groups, key);
+        if (!groupBin) {
+            // if not, create one
+            groupBin = psArrayAlloc(0);
+            psHashAdd(groups, key, groupBin);
+        }
+        psFree(key);
+
+        // add this row to the bin
+        psArrayAdd(groupBin, 0, output->data[i]);
+        psFree(groupBin);
+    }
+    psFree(output);
+
+    // create a magic run
+    psArray *grouped =  psHashToArray(groups);
+    psFree(groups);
+
+    // iterate over array of grouped warp_ids
+    for (long i = 0; i < psArrayLength(grouped); i++) {
+        psArray *group = grouped->data[i];
+
+        // create a new magicRun for this group
+        magicRunRow *run = magicRunRowAlloc(
+	    0,				// ID
+	    "reg",			// state
+	    workdir,			// workdir
+	    "dirty",			// workdir_state
+	    label,			// label
+	    dvodb,			// dvodb
+	    registered,			// registered
+	    0				// fault
+	    );
+        if (!run) {
+            psAbort("failed to alloc magicRun object");
+        }
+        if (!magicRunInsertObject(config->dbh, run)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(run);
+            psFree(grouped);
+            return false;
+        }
+
+        // get the assigned warp_id
+        psS64 magic_id = psDBLastInsertID(config->dbh);
+
+        // insert all rows in this bin into the new magicRun
+        for (long j = 0; j < psArrayLength(group); j++) {
+            psMetadata *row = group->data[j];
+
+            bool status = false;
+            psS64 diff_id = psMetadataLookupS64(&status, row, "diff_id");
+            if (!status) {
+                psAbort("failed to lookup value for diff_id");
+            }
+            psString node = psMetadataLookupStr(&status, row, "skycell_id");
+            if (!status) {
+                psAbort("failed to lookup value for diff_id");
+            }
+
+            if (!magicInputSkyfileInsert(config->dbh,
+					 magic_id,
+					 diff_id,
+					 node)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+                psFree(grouped);
+                return false;
+            }
+        }
+
+        // set magicRun.state to 'run'
+        return setmagicRunState(config, magic_id, "run");
+    }
+    psFree(grouped);
+
+    return true;
+}
+
+static psS64 definerunMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // required
+    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
+
+    // optional
+    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
+    PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
+    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    magicRunRow *run = magicRunRowAlloc(
+            0,          // ID
+            "reg",      // state
+            workdir,
+            "dirty",    // workdir_state
+            label,
+            dvodb,
+            registered,
+            0
+    );
+
+    if (!run) {
+        psError(PS_ERR_UNKNOWN, false, "failed to alloc magicRun object");
+        return false;
+    }
+    if (!magicRunInsertObject(config->dbh, run)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(run);
+        return false;
+    }
+
+    // get the assigned warp_id
+    psS64 magic_id = psDBLastInsertID(config->dbh);
+    run->magic_id = magic_id;
+
+    if (!magicRunPrintObject(stdout, run, !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print object");
+            psFree(run);
+            return false;
+    }
+
+    psFree(run);
+
+    return magic_id;
+}
+
+
+static bool updaterunMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // required
+    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
+
+    if (state) {
+        // set detRun.state to state
+        return setmagicRunState(config, (psS64)atoll(magic_id), state);
+    }
+
+    return true;
+}
+
+
+static bool addinputskyfileMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // required
+    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
+    PXOPT_LOOKUP_STR(node, config->args, "-node", true, false);
+
+    magicInputSkyfileInsert(
+            config->dbh,
+            (psS64)atoll(magic_id),
+            (psS64)atoll(diff_id),
+            node
+    );
+
+    return true;
+}
+
+
+static bool inputskyfileMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-diff_id", "diff_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-node", "node", "==");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    // find all rawImfiles matching the default query
+    psString query = pxDataGet("magictool_inputskyfile.sql");
     if (!query) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
@@ -125,4 +371,18 @@
     }
 
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "magicInputSkyfile");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    // treat limit == 0 as "no limit"
+    if (limit) {
+        psString limitString = psDBGenerateLimitSQL(limit);
+        psStringAppend(&query, " %s", limitString);
+        psFree(limitString);
+    }
+
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -131,21 +391,5 @@
     }
     psFree(query);
-}
-
-    // find the diff_id's of the warped skycells
-{
-    psString query = pxDataGet("magictool_find_complete_diffed_exposures.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-}
+
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
@@ -168,193 +412,30 @@
     }
 
-    // pre-allocate enough hash buckets for 1 unique warp_id per row
-    psHash *groups = psHashAlloc(psArrayLength(output));
-    // iterate over result set, and group by warp_id
-    for (long i = 0; i < psArrayLength(output); i++) {
-        // lookup the key we're grouping by
-        bool status = false;
-        psS64 warp_id = psMetadataLookupS64(&status, output->data[i], "warp_id");
-        if (!status) {
-            psAbort("failed to lookup value for warp_id");
-        }
-
-        psString key = psDBIntToString(warp_id);
-        // look to see if there is a bin for this key
-        psArray *groupBin = psHashLookup(groups, key);
-        if (!groupBin) {
-            // if not, create one
-            groupBin = psArrayAlloc(0);
-            psHashAdd(groups, key, groupBin);
-        }
-        psFree(key);
-
-        // add this row to the bin
-        psArrayAdd(groupBin, 0, output->data[i]);
-        psFree(groupBin);
-    }
+    if (psArrayLength(output)) {
+        // negative simple so the default is true
+        if (!ippdbPrintMetadatas(stdout, output, "magicInputSkyfile", !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+            psFree(output);
+            return false;
+        }
+    }
+
     psFree(output);
 
-    // create a magic run
-    psArray *grouped =  psHashToArray(groups);
-    psFree(groups);
-
-    // iterate over array of grouped warp_ids
-    for (long i = 0; i < psArrayLength(grouped); i++) {
-        psArray *group = grouped->data[i];
-
-        // create a new magicRun for this group
-        // XXX This need to have options passed through to it
-
-        magicRunRow *run = magicRunRowAlloc(
-                0,          // ID
-                "reg",      // state
-                NULL,       // workdir
-                "dirty",    // workdir_state
-                NULL,       // label
-                NULL,       // dvodb
-                NULL,       // registered
-                0           // fault
-        );
-        if (!run) {
-            psAbort("failed to alloc magicRun object");
-        }
-        if (!magicRunInsertObject(config->dbh, run)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-            psFree(run);
-            psFree(grouped);
-            return false;
-        }
-
-        // get the assigned warp_id
-        psS64 magic_id = psDBLastInsertID(config->dbh);
-
-        // insert all rows in this bin into the new magicRun
-        for (long j = 0; j < psArrayLength(group); j++) {
-            psMetadata *row = group->data[j];
-
-            bool status = false;
-            psS64 diff_id = psMetadataLookupS64(&status, row, "diff_id");
-            if (!status) {
-                psAbort("failed to lookup value for diff_id");
-            }
-            psString node = psMetadataLookupStr(&status, row, "skycell_id");
-            if (!status) {
-                psAbort("failed to lookup value for diff_id");
-            }
-
-            if (!magicInputSkyfileInsert(config->dbh,
-                magic_id,
-                diff_id,
-                node)) {
-                psError(PS_ERR_UNKNOWN, false, "database error");
-                psFree(grouped);
-                return false;
-            }
-        }
-
-        // set magicRun.state to 'run'
-        return setmagicRunState(config, magic_id, "run");
-    }
-    psFree(grouped);
-
-    return true;
-}
-
-static psS64 definerunMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
-
-    // optional
-    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
-    PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
-
-    magicRunRow *run = magicRunRowAlloc(
-            0,          // ID
-            "reg",      // state
-            workdir,
-            "dirty",    // workdir_state
-            label,
-            dvodb,
-            registered,
-            0
-    );
-
-    if (!run) {
-        psError(PS_ERR_UNKNOWN, false, "failed to alloc magicRun object");
-        return false;
-    }
-    if (!magicRunInsertObject(config->dbh, run)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(run);
-        return false;
-    }
-
-    // get the assigned warp_id
-    psS64 magic_id = psDBLastInsertID(config->dbh);
-    run->magic_id = magic_id;
-
-    if (!magicRunPrintObject(stdout, run, !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print object");
-            psFree(run);
-            return false;
-    }
-
-    psFree(run);
-
-    return magic_id;
-}
-
-
-static bool updaterunMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
-    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
-
-    if (state) {
-        // set detRun.state to state
-        return setmagicRunState(config, (psS64)atoll(magic_id), state);
-    }
-
-    return true;
-}
-
-
-static bool addinputskyfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
-    PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
-    PXOPT_LOOKUP_STR(node, config->args, "-node", true, false);
-
-    magicInputSkyfileInsert(
-            config->dbh,
-            (psS64)atoll(magic_id),
-            (psS64)atoll(diff_id),
-            node
-    );
-
-    return true;
-}
-
-
-static bool inputskyfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
+    return true;
+}
+
+static bool totreeMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-magic_id", "magicRun.magic_id", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
-    // find all rawImfiles matching the default query
-    psString query = pxDataGet("magictool_inputskyfile.sql");
+    // look for "inputs" that need to processed
+    psString query = pxDataGet("magictool_totree.sql");
     if (!query) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
@@ -362,9 +443,10 @@
     }
 
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "magicInputSkyfile");
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -404,5 +486,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "magicInputSkyfile", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "totree", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -416,18 +498,43 @@
 }
 
-
-static bool totreeMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
+static bool inputtreeMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // required
+    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_STR(dep_file, config->args, "-dep_file", true, false);
+
+    if (!parseAndInsertNodeDeps(config, (psS64)atoll(magic_id), dep_file)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to parse file");
+        return false;
+    }
+
+    return true;
+}
+
+static bool inputsMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-node", "node", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
-    // look for "inputs" that need to processed
-    psString query = pxDataGet("magictool_totree.sql");
+    psString query = pxDataGet("magictool_inputs.sql");
     if (!query) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -467,5 +574,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "totree", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "magicNode", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -478,22 +585,4 @@
     return true;
 }
-
-
-static bool inputtreeMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
-    PXOPT_LOOKUP_STR(dep_file, config->args, "-dep_file", true, false);
-
-    if (!parseAndInsertNodeDeps(config, (psS64)atoll(magic_id), dep_file)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to parse file");
-        return false;
-    }
-
-    return true;
-}
-
 
 bool findBaseNodes(void *arg, pxNode *node)
@@ -573,5 +662,7 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", false, false);
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
+
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
@@ -583,12 +674,11 @@
     }
 
-    if (magic_id) {
-        psMetadata *where = psMetadataAlloc();
-        psMetadataAddS64(where, PS_LIST_TAIL, "magic_id", 0, "==", (psS64)atoll(magic_id));
-        psString whereClause = psDBGenerateWhereSQL(where, NULL);
-        psFree(where);
+    psString whereClause = NULL;
+    if (psListLength(where->list)) {
+        whereClause = psDBGenerateWhereSQL(where, NULL);
         psStringAppend(&query, " %s", whereClause);
-        psFree(whereClause);
-    }
+	psFree(whereClause);
+    }
+    psFree(where);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -615,6 +705,6 @@
     if (!psArrayLength(output)) {
         psTrace("magictool", PS_LOG_INFO, "no rows found");
-//        psFree(output);
-//        return true;
+	// psFree(output);
+        // return true;
     }
 
@@ -626,12 +716,6 @@
     }
 
-
-    if (magic_id) {
-        psMetadata *where = psMetadataAlloc();
-        psMetadataAddS64(where, PS_LIST_TAIL, "magic_id", 0, "==", (psS64)atoll(magic_id));
-        psString whereClause = psDBGenerateWhereSQL(where, NULL);
-        psFree(where);
+    if (whereClause) {
         psStringAppend(&query, " %s", whereClause);
-        psFree(whereClause);
     }
 
@@ -686,5 +770,5 @@
     pxNode *root = psHashLookup(forest, "root");
     psFree(forest);
-//    pxTreePrint(stdout, root);
+    // pxTreePrint(stdout, root);
 
     // crawl through the tree and looking for nodes with children that are all
@@ -703,4 +787,5 @@
 
     psFree(output);
+    psFree(whereClause);
 
     return true;
@@ -735,5 +820,5 @@
 }
 
-static bool inputsMode(pxConfig *config)
+static bool tomaskMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -742,14 +827,9 @@
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
-    psString query = pxDataGet("magictool_inputs.sql");
+    // look for "inputs" that need to processed
+    psString query = pxDataGet("magictool_tomask.sql");
     if (!query) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
-    }
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
     }
 
@@ -790,5 +870,5 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "magicNode", !simple)) {
+        if (!ippdbPrintMetadatas(stdout, output, "toprocess", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
             psFree(output);
@@ -803,17 +883,81 @@
 
 
-static bool tomaskMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
+static bool addmaskMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // required
+    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
+
+    // optional
+    PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
+    PXOPT_LOOKUP_S32(streaks, config->args, "-streaks", false, false);
+
+    // default values
+    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
+
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    if (!magicMaskInsert(config->dbh,
+                         (psS64)atoll(magic_id),
+                         uri,
+                         streaks,
+                         code
+        )) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    // Set the magicRun state
+    psString query = pxDataGet("magictool_addmask.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    // manually add constraint
+    psStringAppend(&query, " AND magic_id = %s", magic_id);
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    return true;
+}
+
+
+static bool maskMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-magic_id", "magicRun.magic_id", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
-    // look for "inputs" that need to processed
-    psString query = pxDataGet("magictool_tomask.sql");
+    psString query = pxDataGet("magictool_mask.sql");
     if (!query) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -853,129 +997,4 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "toprocess", !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(output);
-            return false;
-        }
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-
-static bool addmaskMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
-
-    // optional
-    PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
-    PXOPT_LOOKUP_S32(streaks, config->args, "-streaks", false, false);
-
-    // default values
-    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
-
-    if (!psDBTransaction(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    if (!magicMaskInsert(config->dbh,
-                         (psS64)atoll(magic_id),
-                         uri,
-                         streaks,
-                         code
-        )) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    // Set the magicRun state
-    psString query = pxDataGet("magictool_addmask.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    // Add "magic_id = value"
-    psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
-    psStringAppend(&query, " AND %s", whereClause);
-    psFree(whereClause);
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    if (!psDBCommit(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    return true;
-}
-
-
-static bool maskMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
-    psString query = pxDataGet("magictool_mask.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psErrorCode err = psErrorCodeLast();
-        switch (err) {
-            case PS_ERR_DB_CLIENT:
-                psError(PXTOOLS_ERR_SYS, false, "database error");
-            case PS_ERR_DB_SERVER:
-                psError(PXTOOLS_ERR_PROG, false, "database error");
-            default:
-                psError(PXTOOLS_ERR_PROG, false, "unknown error");
-        }
-
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psTrace("magictool", PS_LOG_INFO, "no rows found");
-        psFree(output);
-        return true;
-    }
-
-    if (psArrayLength(output)) {
-        // negative simple so the default is true
         if (!ippdbPrintMetadatas(stdout, output, "magicNode", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
