Permalink
Browse files

finished tests

  • Loading branch information...
1 parent facdb71 commit 0b38013311fb925b017342c747d9a8095febed03 @bmuller committed Jan 6, 2008
View
3 TODO
@@ -1,4 +1,3 @@
fix datetime support so you can have dates before 1970
do something with VALUE_MAX_SIZE
-types.h get_property
-test has_object
+add hasobject test
View
@@ -2,172 +2,58 @@
using namespace stactiverecord;
using namespace std;
Sar_Dbi * Sar_Dbi::dbi;
-/*
-class Person : public Record {
-public:
- std::string firstname;
- std::string lastname;
- int age;
- bool iscool;
- DateTime dob;
- Person() : Record("person") { init(); };
- Person(int id) : Record("person", id) { init(); }
- void init() {
- get("firstname", firstname, "Unknown");
- get("lastname", lastname, "Unkown");
- get("age", age, 0);
- get("iscool", iscool, false);
- get("dob", dob, DateTime(1,1,2000));
- };
- void save() {
- set("firstname", firstname);
- set("lastname", lastname);
- set("age", age);
- set("iscool", iscool);
- set("dob", dob);
- Record::save();
- };
- void setBuddy(Person p) {
- setOne<Person>(p);
- };
- void getBuddy(Person &p) {
- getOne<Person>(p);
- };
-};
-class Test : public Record {
+// First define a class that we'll be querying on
+class Person : public Record<Person> {
public:
- Test() : Record("test") {};
- Test(int id) : Record("test", id) {};
+ static string classname;
+ Person() : Record<Person>() {};
+ Person(int id) : Record<Person>(id) {};
};
+string Person::classname = "person";
-class TestTwo : public Record {
-public:
- TestTwo() : Record("testtwo") {};
- TestTwo(int id) : Record("testtwo", id) {};
-};
-*/
int main(int argc, char* argv[]) {
if(argc != 2) {
std::cout << "Usage: ./db_test <scheme://[user[:password]@host[:port]/]database>\n";
return 1;
}
Sar_Dbi::dbi = Sar_Dbi::makeStorage(std::string(argv[1]));
- /*
- Person p;
- p.age = 23;
- p.firstname = "bob";
- //p.iscool = false;
- p.save();
- return 0;
- Person q;
- q.setBuddy(p);
- q.save();
-
- Person qq(q.id);
- Person z;
- qq.getBuddy(z);
- std::cout << z.firstname << "\n";
- */
- /*
- Person qq(q.id);
- Person z;
- qq.getOne<Person>(z);
- std::cout << z.firstname << "\n";
- */
-
- /*
- Test t;
- t.set("name", "bob");
- //t.set("fname", "whoops");
- t.set("age", 70);
- t.save();
-
- TestTwo tt;
- //tt.set("name", "fred");
- tt.set("age", 55);
- tt.set<Test>(t);
- tt.save();
-
- // ObjGroup<Test> og = Record::find<Test>(Q("age", between(40, 56)) && Q("name", "fred"));
- //ObjGroup<Test> og = Record::find<Test>(Q("age", nbetween(40, 56)) || Q("name", "fred"));
- ObjGroup<TestTwo> og = Record::find<TestTwo>(Q("age", between(40, 56)) && Q(hasobject(t)));
-
- //ObjGroup<Test> og = Record::find<Test>(Q("name", "fred") || (Q("name", "bob") && Q("fname", "whoops")));
- for(unsigned int i=0; i < og.size(); i++)
- std::cout << og[i].id << "\n";
-
-
- Test ttt;
- ttt.set("name", "asdf");
- ttt.save();
- ObjGroup<Test> og = Record::find_by<Test>("name", "fred");
- og.get_ids().dump();
+ Person bob;
+ bob.set("name", "Bob");
+ bob.set("age", 40);
+ bob.save();
- ObjGroup<Test> og;
- og << t;
- og << tt;
-
- TestTwo f;
- f.setMany<Test>(og);
- f.save();
-
- ObjGroup<Test> secondog;
- secondog << t;
- Test x;
- x.set("name", "yourmamma");
- x.save();
- secondog << x;
-
- f.setMany<Test>(secondog);
- f.save();
-
- TestTwo ff(f.id);
- ObjGroup<Test> oogg = ff.getMany<Test>();
- string n1, n2;
- oogg[0].get("name", n1);
- oogg[1].get("name", n2);
- cout << n1 << " " << n2 << "\n";
-
- ff.del<Test>();
- ff.save();
+ Person fred;
+ fred.set("name", "Fred");
+ fred.set("age", 35);
+ fred.setOne<Person>(bob);
+ fred.save();
- Test f;
- f.set("one", "blah");
- f.save();
-
- f.set("one", 1);
- f.save();
- f.save();
- f.save();
-
- f.del();
- */
- //cout << "Saved one as integer 1\n";
+ ObjGroup<Person> people = Person::find(Q("age", between(30, 39)) || Q("name", equals("Bob")));
+ cout << "Should be Bob and Fred:\n";
+ for(unsigned int i=0; i < people.size(); i++) {
+ string name;
+ people[i].get("name", name);
+ std::cout << name << "\n";
+ }
- //Test t(f.id);
- //cout << "value should be 1: " << t.get("one") << "\n";
- /*
- t.del("one");
- t.set("one", "four");
- t.set("one", "five");
-
- f.save();
+ people = Person::find(Q(hasobject(bob)));
+ cout << "Should be Fred:\n";
+ for(unsigned int i=0; i < people.size(); i++) {
+ string name;
+ people[i].get("name", name);
+ std::cout << name << "\n";
+ }
+ people = Person::find(Q(hasobject(fred)));
+ cout << "Should be Bob:\n";
+ for(unsigned int i=0; i < people.size(); i++) {
+ string name;
+ people[i].get("name", name);
+ std::cout << name << "\n";
+ }
- Test t(f.id);
- t.del("one");
- t.set("one", 2);
- t.del("one");
- t.set("one", 3);
- t.save();
- */
- //cout << "id: " << t.id << "\n";
- //string value;
- //t.get("one", value);
- //cout << "one: " << value << "\n";
- //t.set("one", "three");
delete Sar_Dbi::dbi;
return 0;
};
View
@@ -56,6 +56,8 @@ namespace stactiverecord {
*/
Q(std::string _key, std::string _value) : key(_key), value(_value) { ct = STRING; where = equals(_value); };
+ Q(std::string _key, const char * _value) : key(_key), value(std::string(_value)) { ct = STRING; where = equals(std::string(_value)); };
+
/** Create query object
* @param _key The key being queried
* @param _ivalue The exact value to match for the given key.
View
@@ -326,6 +326,7 @@ namespace stactiverecord {
else throw Sar_NoSuchPropertyException("property \"" + key + "\" does not exist");
};
+ /** Delete property with the given key name */
void del(std::string key) {
if(!initial_update && id!=-1) update();
coltype ct = type(key);
@@ -351,11 +352,13 @@ namespace stactiverecord {
};
};
+ /** Determine if a property is set */
bool isset(std::string colname) {
if(!initial_update && id!=-1) update();
return (type(colname) == NONE);
};
+ /** Determine if an object relation is set */
template <class T> bool isset() {
ObjGroup<T> others = getMany<T>();
return others.size() > 0;
@@ -375,6 +378,7 @@ namespace stactiverecord {
return NONE;
};
+ /** Delete Klass record */
void del() {
if(!initial_update && id!=-1) update();
if(id != -1)
@@ -470,11 +474,12 @@ namespace stactiverecord {
return ObjGroup<Klass>(results);
}
+ /** Determine if a Klass with the given id exists */
static bool exists(int id) {
return Sar_Dbi::dbi->exists(Klass::classname, id);
};
- /** Delete all records of type T */
+ /** Delete all records of type Klass */
static void delete_all() {
Sar_Dbi::dbi->delete_records(Klass::classname);
};
@@ -24,6 +24,9 @@ namespace stactiverecord {
*/
enum coltype { NONE, INTEGER, STRING, RECORD, DATETIME, ALL };
enum wheretype { STARTSWITH, ENDSWITH, CONTAINS, GREATERTHAN, LESSTHAN, BETWEEN, EQUALS, OBJECTRELATION, IN, ISNULL };
+
+ // Forward declaration of class Record
+ template <class T> class Record;
};
#include "config.h"
@@ -37,12 +40,6 @@ namespace stactiverecord {
#include "storage.h"
#include "record.h"
-namespace stactiverecord {
- // Due to the fracking limitations of C++, this definition can't go in where.h
- // or a circular dependency is introduced. Gack!
- Where * hasobject(Record<CUDPropertyRegister>& r);
-};
-
#define VALUE_MAX_SIZE 255
#define VALUE_MAX_SIZE_S "255"
@@ -425,6 +425,12 @@ namespace stactiverecord {
} else {
cols << KVT("class_one_id", INTEGER);
rows = select(tablename, cols, Q("class_one", classname) && Q("class_two_id", where->ivalue) && Q("class_two", where->svalue));
+ // in the case in which the two classnames are the same, we need to try both (swap will be false, so try them swapped as well)
+ if(classname == where->svalue) {
+ cols.clear();
+ cols << KVT("class_two_id", INTEGER);
+ rows.unionize(select(tablename, cols, Q("class_two", classname) && Q("class_one_id", where->ivalue) && Q("class_one", where->svalue)));
+ }
}
}
View
@@ -193,17 +193,17 @@ namespace stactiverecord {
sv.push_back(this->at(i).id);
return sv;
};
- /*
- template <T> SarVector<T> get_property(std::string name) {
- SarVector<T> sv;
- T value;
+ /** This will return a SarVector containing the propertie values for each object
+ element in this ObjGroup **/
+ template <class K> SarVector<K> get_property(std::string name) {
+ SarVector<K> sv;
+ K value;
for(unsigned int i=0; i < this->size(); i++) {
this->at(i).get(name, value);
sv << value;
}
return sv;
};
- */
bool has_id(int id) {
return get_ids().includes(id);
};
View
@@ -83,8 +83,8 @@ namespace stactiverecord {
return new Where(0, EQUALS);
};
- Where * hasobject(Record<CUDPropertyRegister>& r) {
- return new Where(r.classname, r.id, OBJECTRELATION);
+ Where * equals(const char * value) {
+ return equals(std::string(value));
};
Where * in(std::vector<int> values) {
@@ -149,6 +149,10 @@ namespace stactiverecord {
return equals(!value);
};
+ Where * nequals(const char * value) {
+ return nequals(std::string(value));
+ };
+
Where * nin(std::vector<int> values) {
return new Where(values, IN, true);
};
View
@@ -60,15 +60,20 @@ namespace stactiverecord {
Where * lessthan(int value);
Where * greaterthan(int value);
Where * between(int value, int valuetwo);
- Where * equals(int value);
Where * lessthan(DateTime value);
Where * greaterthan(DateTime value);
Where * between(DateTime value, DateTime valuetwo);
Where * equals(DateTime value);
+ Where * equals(int value);
Where * equals(std::string value);
Where * equals(bool value);
+ Where * equals(const char * value);
Where * in(std::vector<int> values);
Where * isnull();
+ template <class T> Where * hasobject(Record<T>& r) {
+ return new Where(r.classname, r.id, OBJECTRELATION);
+ };
+
// negated values
Where * nstartswith(std::string value);
@@ -84,6 +89,7 @@ namespace stactiverecord {
Where * nequals(DateTime value);
Where * nequals(std::string value);
Where * nequals(bool value);
+ Where * nequals(const char * value);
Where * nin(std::vector<int> values);
Where * nisnull();
};

0 comments on commit 0b38013

Please sign in to comment.