Added program to output data from sql db
authorltracy
Tue Apr 07 16:52:07 2009 -0700 (10 months ago)
changeset 431524321b91c50b
parent 4314 edd3003429c7
child 4316 7f236cc1d990
Added program to output data from sql db
scratch/read-dat-sql.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/scratch/read-dat-sql.cc	Tue Apr 07 16:52:07 2009 -0700
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008 University of Washington
     1.6 + *
     1.7 + * This program is free software: you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation, either version 3 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.19 + *
    1.20 + * Author: Leonard Tracy <lentracy@u.washington.edu>
    1.21 + *
    1.22 + *
    1.23 + */
    1.24 +
    1.25 +#include "ns3/core-module.h"
    1.26 +#include "ns3/common-module.h"
    1.27 +#include "ns3/uan-module.h"
    1.28 +#include "ns3/node-module.h"
    1.29 +#include "ns3/mobility-module.h"
    1.30 +#include "ns3/contrib-module.h"
    1.31 +#include "ns3/helper-module.h"
    1.32 +#include <fstream>
    1.33 +#include <sstream>
    1.34 +#include <string>
    1.35 +#include <sqlite3.h>
    1.36 +
    1.37 +using namespace ns3;
    1.38 +
    1.39 +NS_LOG_COMPONENT_DEFINE("ReadDatSql");
    1.40 +
    1.41 +
    1.42 +std::ofstream mlfile;
    1.43 +
    1.44 +static int
    1.45 +dataCallback(void *vd, int32_t argc, char **argv, char **cname)
    1.46 +{
    1.47 +  NS_ASSERT(mlfile.is_open());
    1.48 +
    1.49 +  for(int32_t i=0;i<argc;i++)
    1.50 +    {
    1.51 +      std::cout << "Column: " << cname[i] << " is " << argv[i] << std::endl;
    1.52 +      mlfile << argv[i] << " ";
    1.53 +    }
    1.54 +  mlfile << std::endl;
    1.55 +  return 0;
    1.56 +}
    1.57 +static int
    1.58 +tapIdCallback(void *vti, int32_t argc, char **argv, char **cname)
    1.59 +{
    1.60 +
    1.61 +
    1.62 +  NS_ASSERT(argc == 1);
    1.63 +
    1.64 +  uint32_t *ti = (uint32_t *) vti;
    1.65 +  *ti = std::atoi(argv[0]);
    1.66 +  return 0;
    1.67 +}
    1.68 +int
    1.69 +main(int argc, char **argv)
    1.70 +{
    1.71 +  LogComponentEnable("ReadDatSql", LOG_LEVEL_ALL);
    1.72 +
    1.73 +  std::string mlfilename = "sqlbhout.txt";
    1.74 +  std::string cfgfile = "sqltst.cfg";
    1.75 +  std::string dbfile = "sqldbtest.db";
    1.76 +
    1.77 +  uint32_t srcDepth = 10;
    1.78 +  uint32_t recDepth = 10;
    1.79 +  uint32_t range = 100;
    1.80 +  uint32_t freq = 10000;
    1.81 +
    1.82 +  CommandLine cmd;
    1.83 +  cmd.AddValue("MlFile", "File to write PDP to", mlfilename);
    1.84 +  cmd.AddValue("CfgFile", "CFG file to read", cfgfile);
    1.85 +  cmd.AddValue("SrcDepth", "Source depth", srcDepth);
    1.86 +  cmd.AddValue("RecDepth", "Receiver depth", recDepth);
    1.87 +  cmd.AddValue("Range", "Range", range);
    1.88 +  cmd.AddValue("Freq", "Frequency", freq);
    1.89 +  cmd.AddValue("DbFile", "Db filename", dbfile);
    1.90 +  cmd.Parse(argc, argv);
    1.91 +
    1.92 +  mlfile.open(mlfilename.c_str());
    1.93 +
    1.94 +  BhRespManager &man = BhRespManager::GetInstance();
    1.95 +  man.Configure(cfgfile);
    1.96 +
    1.97 +  BhIndex bi = man.MakeIndex(freq, srcDepth, recDepth, range);
    1.98 +
    1.99 +  std::ostringstream sql;
   1.100 +  sqlite3 *sqldb;
   1.101 +  sqlite3_open(dbfile.c_str(), &sqldb);
   1.102 +
   1.103 +  sql << "SELECT tapid FROM indextable WHERE "
   1.104 +      << "srcdepth = " << bi.m_SrcDepthM << " AND "
   1.105 +      << "recdepth = " << bi.m_RecDepthM << " AND "
   1.106 +      << "range = " << bi.m_RangeM << " AND "
   1.107 +      << "freq = " << bi.m_FreqHz << ";";
   1.108 +
   1.109 +  uint32_t tapid;
   1.110 +  int32_t rc;
   1.111 +
   1.112 +  char *err;
   1.113 +  rc = sqlite3_exec(sqldb, sql.str().c_str(), &tapIdCallback, &tapid, &err);
   1.114 +  if(rc)
   1.115 +    {
   1.116 +      NS_FATAL_ERROR("Error: " << err);
   1.117 +    }
   1.118 +
   1.119 +  sql.str("");
   1.120 +
   1.121 +  sql << "SELECT delay, realamp, imagamp FROM taptable WHERE "
   1.122 +      << "tapid = " << tapid << ";";
   1.123 +
   1.124 +  rc = sqlite3_exec(sqldb, sql.str().c_str(), &dataCallback, 0, &err);
   1.125 +  if(rc)
   1.126 +    {
   1.127 +      NS_FATAL_ERROR("Error: " << err);
   1.128 +    }
   1.129 +
   1.130 +
   1.131 +}
   1.132 +