CUNIT_TEST(DBSuite, _string_literal) { EagleDbInstance *db = EagleInstanceTest(10); EagleBoolean success; EagleLoggerEvent *error = NULL; success = EagleDbInstance_execute(db, "CREATE TABLE mytable2 (col1 int, col2 text);", &error); if (EagleFalse == success) { CUNIT_FAIL("%s", error->message); } CUNIT_ASSERT_TRUE(success); success = EagleDbInstance_execute(db, "INSERT INTO mytable2 (col1, col2) VALUES (123, 'some '' cool \' text');", &error); if (EagleFalse == success) { CUNIT_FAIL("%s", error->message); } CUNIT_ASSERT_TRUE(success); // SELECT data back EagleDbParser *p = EagleDbParser_ParseWithString("SELECT col1, col2, 'some string' FROM mytable2;"); CUNIT_ASSERT_FALSE(EagleDbParser_hasError(p)); EaglePlan *plan = EagleDbSqlSelect_parse((EagleDbSqlSelect*) p->yyparse_ast, db); //printf("%s\n", EaglePlan_toString(plan)); // catch compilation error CUNIT_ASSERT_FALSE(EaglePlan_isError(plan)); // execute EagleInstance *eagle = EagleInstance_New(1); EagleInstance_addPlan(eagle, plan); EagleInstance_run(eagle); // validate data EaglePage *page1 = EaglePageProvider_nextPage(plan->result[0]); EaglePage *page2 = EaglePageProvider_nextPage(plan->result[1]); EaglePage *page3 = EaglePageProvider_nextPage(plan->result[2]); CUNIT_ASSERT_EQUAL_INT(page1->type, EagleDataTypeInteger); CUNIT_ASSERT_EQUAL_INT(page2->type, EagleDataTypeVarchar); CUNIT_ASSERT_EQUAL_INT(page3->type, EagleDataTypeVarchar); CUNIT_ASSERT_EQUAL_INT(1, page1->count); CUNIT_ASSERT_EQUAL_INT(1, page2->count); CUNIT_ASSERT_EQUAL_INT(10, page3->count); CUNIT_ASSERT_EQUAL_INT(((int*) page1->data)[0], 123); CUNIT_ASSERT_EQUAL_STRING(((char**) page2->data)[0], "some ' cool ' text"); CUNIT_ASSERT_EQUAL_STRING(((char**) page3->data)[0], "some string"); EaglePage_Delete(page1); EaglePage_Delete(page2); EaglePage_Delete(page3); EagleInstance_Delete(eagle); EaglePlan_Delete(plan); EagleDbSqlSelect_DeleteRecursive((EagleDbSqlSelect*) p->yyparse_ast); EagleDbParser_Delete(p); EagleInstanceTest_Cleanup(db); }
CUNIT_TEST(DBSuite, EagleDbInformationSchema_tables) { int cores = 1; EagleDbInstance *db = EagleDbInstance_New(10, cores); /* parse sql */ EagleDbParser *p = EagleDbParser_ParseWithString("select table_schema, table_name from information_schema_tables;"); if (EagleTrue == EagleDbParser_hasError(p)) { CUNIT_FAIL("%s", EagleDbParser_lastError(p)); } CUNIT_ASSERT_FALSE(EagleDbParser_hasError(p)); EaglePlan *plan = EagleDbSqlSelect_parse((EagleDbSqlSelect*) p->yyparse_ast, db); if (EagleTrue == EaglePlan_isError(plan)) { CUNIT_FAIL("%s", plan->errorMessage); } CUNIT_ASSERT_FALSE(EaglePlan_isError(plan)); /* execute */ EagleInstance *eagle = EagleInstance_New(cores); EagleInstance_addPlan(eagle, plan); EagleInstance_run(eagle); /* check results */ EaglePage *page1 = EaglePageProvider_nextPage(plan->result[0]); EaglePage *page2 = EaglePageProvider_nextPage(plan->result[1]); CUNIT_ASSERT_NOT_NULL(page1); CUNIT_ASSERT_NOT_NULL(page2); EaglePage_Delete(page1); EaglePage_Delete(page2); EagleDbSqlExpression_DeleteRecursive((EagleDbSqlExpression*) p->yyparse_ast); EaglePlan_Delete(plan); EagleInstance_Delete(eagle); EagleDbParser_Delete(p); EagleDbInstance_Delete(db); }
EagleDbParser* _BenchSuite_distance(EagleDbInstance *db, int rows) { /* SLOW INSERT for(int i = 0; i < rows; ++i) { char sql[1024]; sprintf(sql, "INSERT INTO point (id, x, y) VALUES (%d, %g, %g);", i + 1, frand(1000.0), frand(1000.0)); //printf("%s\n", sql); success = EagleDbInstance_execute(db, sql, &error); if(EagleFalse == success) { CUNIT_FAIL("%s", error->message); } } */ EagleDbTableData *td = EagleDbInstance_getTable(db, "point"); EagleDbTuple *tuple = EagleDbTuple_New(td->table); for(int i = 0; i < rows; ++i) { EagleDbTuple_setInt(tuple, 0, i + 1); EagleDbTuple_setFloat(tuple, 1, frand(1000.0)); EagleDbTuple_setFloat(tuple, 2, frand(1000.0)); EagleDbTableData_insert(td, tuple); // this is a bit naughty, to save time on recreating the tuple for every row we are reusing it free(tuple->data[0]); free(tuple->data[1]); free(tuple->data[2]); tuple->data[0] = NULL; tuple->data[1] = NULL; tuple->data[2] = NULL; } EagleDbTuple_Delete(tuple); // do a distance search EagleDbParser *p = EagleDbParser_ParseWithString("SELECT id, x, y FROM point WHERE sqrt((500.0 - x) * (500.0 - x) + (500.0 - y) * (500.0 - y)) < 1.0"); if(EagleTrue == EagleDbParser_hasError(p)) { CUNIT_FAIL("%s", EagleDbParser_lastError(p)); } return p; }