chiark / gitweb /
WIP per-island-pair trades
authorIan Jackson <ian@liberator.(none)>
Sat, 26 Sep 2009 18:25:50 +0000 (19:25 +0100)
committerIan Jackson <ian@liberator.(none)>
Sat, 26 Sep 2009 18:25:50 +0000 (19:25 +0100)
yarrg/rsvalue.c

index 6778523..a4e70f6 100644 (file)
@@ -2,51 +2,38 @@
 
 #include "rscommon.h"
 
+static void ipair_gettrades(int si, int di) {
+  char *stmt= masprintf
+    ("SELECT\n"
+     " sell.price              src_price,\n"
+     " sum(sell.qty)           src_qty,\n"
+     " buy.price               dst_price,\n"
+     " sum(buy.qty)            dst_qty,\n"
+     " commods.commodid                commodid,\n"
+     " commods.unitmass                unitmass,\n"
+     " commods.unitvolume      unitvolume\n"
+     " FROM commods\n"
+     " JOIN sell ON commods.commodid = sell.commodid\n"
+     " JOIN buy  ON commods.commodid = buy.commodid\n"
+     " WHERE buy.price > sell.price\n"
+     " AND sell.islandid=%d\n"
+     " AND buy.islandid=%d\n"
+     " GROUP BY commods.commodid, sell.price, buy.price\n",
+     si, di);
+  
+  printf("SQL\n[\n%s\n]\n", stmt);
+
+  free(stmt);
+}
+
 void value_route(int nislands, const int *islands) {
-  char stmt[1024+80*nislands+40*nislands*nislands], *sp;
   int s,d;
   
-  sp= stmt;
-  sp += sprintf(sp,
-               "SELECT\n"
-               " sell.islandid         src_id,\n"
-               " sell.price            src_price,\n"
-               " sum(sell.qty)         src_qty,\n"
-               " buy.islandid          dst_id,\n"
-               " buy.price             dst_price,\n"
-               " sum(buy.qty)          dst_qty,\n"
-               " commods.commodid      commodid,\n"
-               " commods.unitmass      unitmass,\n"
-               " commods.unitvolume    unitvolume,\n"
-               " dist                  dist,\n"
-               " buy.price-sell.price  unitprofit\n"
-               " FROM commods\n"
-               " JOIN sell ON commods.commodid = sell.commodid\n"
-               " JOIN buy  ON commods.commodid = buy.commodid\n"
-               " JOIN dists ON aiid = sell.islandid AND biid = buy.islandid\n"
-               " WHERE buy.price > sell.price\n"
-               "       AND (");
   for (s=0; s<nislands; s++) {
-    sp += sprintf(sp, "%s(sell.islandid=%d AND (",
-                 !s ? "" : "\n   OR ",
-                 islands[s]);
     for (d=s; d<nislands; d++) {
-      sp += sprintf(sp, "%sbuy.islandid=%d",
-                   d==s ? "" : " OR ",
-                   islands[d]);
+      ipair_gettrades(islands[s], islands[d]);
     }
-    sp += sprintf(sp, "))");
   }
-  sp += sprintf(sp,
-               ")\n"
-               " GROUP BY commods.commodid,\n"
-               "       sell.islandid, sell.price,\n"
-               "       buy.islandid, buy.price\n"
-               );
-
-  assert(sp < stmt + sizeof(stmt) - 1);
-
-  printf("SQL\n[\n%s\n]\n", stmt);
 
   //char *tail;
   //struct sqlite_vm *sth;