Changeset 14591 for trunk/FACT++


Ignore:
Timestamp:
11/08/12 17:09:50 (12 years ago)
Author:
tbretz
Message:
Some improvements to exeption texts; stored the issued query for reference in the DB object; added function to calculate the distance between two points on the local sphere
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r14579 r14591  
    397397Handle<Value> InterpreterV8::FuncDbQuery(const Arguments &args)
    398398{
    399     if (args.Length()!=1)
    400         return ThrowException(String::New("Number of arguments must be exactly 1."));
    401 
    402     if (!args[0]->IsString())
    403         return ThrowException(String::New("Both arguments must be a string."));
     399    if (args.Length()==0)
     400        return ThrowException(String::New("Arguments expected."));
    404401
    405402    HandleScope handle_scope;
     
    409406        return Undefined();
    410407
    411     const String::Utf8Value query(args[0]);
     408    string query;
     409    for (int i=0; i<args.Length(); i++)
     410        query += string(" ") + *String::Utf8Value(args[i]);
     411    query.erase(0, 1);
    412412
    413413#ifdef HAVE_SQL
     
    416416        Database *db = reinterpret_cast<Database*>(ptr);
    417417
    418         const mysqlpp::StoreQueryResult res = db->query(*query).store();
     418        const mysqlpp::StoreQueryResult res = db->query(query).store();
    419419
    420420        Handle<Array> ret = Array::New();
    421         ret->Set(String::New("table"), String::New(res.table()), ReadOnly);
     421        ret->Set(String::New("table"), String::New(res.table()),   ReadOnly);
     422        ret->Set(String::New("query"), String::New(query.c_str()), ReadOnly);
    422423
    423424        Handle<Array> cols = Array::New();
     
    434435                const Handle<Value> name = String::New((*list)[i].c_str());
    435436                if (irow==0)
    436                     cols->Set(Integer::NewFromUnsigned(i), name, ReadOnly);
     437                    cols->Set(i, name);
    437438
    438439                if ((*it)[i].is_null())
     
    528529            }
    529530
    530             ret->Set(Integer::NewFromUnsigned(irow++), row, ReadOnly);
     531            ret->Set(irow++, row);
    531532        }
    532533
     
    546547{
    547548    if (args.Length()!=1)
    548         return ThrowException(String::New("Number of arguments must be exactly 1."));
     549        return ThrowException(String::New("Number of arguments must be 1."));
    549550
    550551    if (!args[0]->IsString())
    551         return ThrowException(String::New("Argument 1 must be a string."));
    552 
    553     HandleScope handle_scope;
    554 
    555     const String::Utf8Value database(args[0]);
    556     const String::Utf8Value query   (args[1]);
     552        return ThrowException(String::New("Argument 1 not a string."));
     553
     554    HandleScope handle_scope;
    557555
    558556#ifdef HAVE_SQL
    559557    try
    560558    {
    561         Database *db = new Database(*database);
     559        Database *db = new Database(*String::Utf8Value(args[0]));
    562560        fDatabases.push_back(db);
    563561
     
    10841082}
    10851083
     1084Handle<Value> InterpreterV8::LocalDist(const Arguments &args)
     1085{
     1086    HandleScope handle_scope;
     1087
     1088    if (args.Length()!=2)
     1089        return ThrowException(String::New("dist must not be called with two arguments."));
     1090
     1091    if (!args[0]->IsObject() || !args[1]->IsObject())
     1092        return ThrowException(String::New("at least one argument not an object."));
     1093
     1094    Handle<Object> obj[2] =
     1095    {
     1096        Handle<Object>::Cast(args[0]),
     1097        Handle<Object>::Cast(args[1])
     1098    };
     1099
     1100    const Handle<String> s_zd = String::New("zd");
     1101    const Handle<String> s_az = String::New("az");
     1102
     1103    const double zd0 = obj[0]->Get(s_zd)->NumberValue() * M_PI/180;
     1104    const double az0 = obj[0]->Get(s_az)->NumberValue() * M_PI/180;
     1105    const double zd1 = obj[1]->Get(s_zd)->NumberValue() * M_PI/180;
     1106    const double az1 = obj[1]->Get(s_az)->NumberValue() * M_PI/180;
     1107
     1108    if (!finite(zd0) || !finite(zd1) || !finite(az0) || !finite(az1))
     1109        return ThrowException(String::New("some values not valid or not finite."));
     1110
     1111    /*
     1112    const double x0 = sin(zd0) * cos(az0);   // az0 -= az0
     1113    const double y0 = sin(zd0) * sin(az0);   // az0 -= az0
     1114    const double z0 = cos(zd0);
     1115
     1116    const double x1 = sin(zd1) * cos(az1);   // az1 -= az0
     1117    const double y1 = sin(zd1) * sin(az1);   // az1 -= az0
     1118    const double z1 = cos(zd1);
     1119
     1120    const double res = acos(x0*x1 + y0*y1 + z0*z1) * 180/M_PI;
     1121    */
     1122
     1123    // cos(az1-az0) = cos(az1)*cos(az0) + sin(az1)*sin(az0)
     1124
     1125    const double x = sin(zd0) * sin(zd1) * cos(az1-az0);
     1126    const double y = cos(zd0) * cos(zd1);
     1127
     1128    const double res = acos(x + y) * 180/M_PI;
     1129
     1130    return handle_scope.Close(Number::New(res));
     1131}
     1132
    10861133Handle<Value> InterpreterV8::MoonDisk(const Arguments &args)
    10871134{
     
    10941141    const Time utc = args.Length()==0 ? Time() : Time(v/1000, v%1000);
    10951142
    1096     handle_scope.Close(Number::New(ln_get_lunar_disk(utc.JD())));
     1143    return handle_scope.Close(Number::New(ln_get_lunar_disk(utc.JD())));
    10971144}
    10981145
     
    14381485
    14391486    Handle<FunctionTemplate> loc = FunctionTemplate::New(ConstructorLocal);
     1487    loc->Set(String::New("dist"), FunctionTemplate::New(LocalDist), ReadOnly);
    14401488    global->Set(String::New("Local"), loc, ReadOnly);
    14411489
  • trunk/FACT++/src/InterpreterV8.h

    r14578 r14591  
    7272    static v8::Handle<v8::Value> ConstructLocal(double zd, double az, v8::Handle<v8::Value> time=v8::Handle<v8::Value>());
    7373    static v8::Handle<v8::Value> ConstructSky(double ra, double dec, v8::Handle<v8::Value> time=v8::Handle<v8::Value>(), bool ismoon=false);
     74    static v8::Handle<v8::Value> LocalDist(const v8::Arguments &args);
    7475    static v8::Handle<v8::Value> MoonDisk(const v8::Arguments &args);
    7576    static v8::Handle<v8::Value> LocalToSky(const v8::Arguments &args);
Note: See TracChangeset for help on using the changeset viewer.