Mysql ORM (Object Relation Mapping)
well,firstly you should have installed your mysql.
You can install it from official website
if you use makefile tool or gcc,just add it. Or if you are in windows by vs, you should include a dynamic library called "mysqlclient.lib" (I guess it's called that.....:no_mouth:)
in Qt, you should add "QMAKE_LIBS+= -lmysqlclient" in your pro file.
firstly,you should add
using namespace ORM_MYSQL_THIEF;
and then you should add some codes at your class, just like this:
// add this line
using namespace ORM_MYSQL_THIEF;
class MyClass
{
// this line are critical, first "serverDB" is your table name, first "id" will be your primary key if you don't want to pass another primary key.
// And id ... score are column fields that you want to add to your database
ORMAP_MYSQL(serverDB,id,name,level,score)
public:
MyClass(std::string idr,std::string namer,int levels,float score)
:id(idr),name(namer),level(levels),score(score)
{}
std::string id;
std::string name;
int level;
float score;
};
Then you can connect your mysql by just a line code:
ORMapper mapper("localhost","username","password","database",port);
This orm (:laughing: i think i can say it's called that...) just implements few operations and if you are interested in it, you can change it.
/////////// You can create your class like this.
std::vector<MyClass> listClassObject;
MyClass helper("000","yyy",0,0);
MyClass wodner1("111","thief",999,56.4);
MyClass wodner2("222","fly",88,54.2);
MyClass wodner3("333","universe",5,45.9);
MyClass wodner4("444","keybord",34,99.9);
listClassObject.push_back(wodner1);
listClassObject.push_back(wodner2);
listClassObject.push_back(wodner3);
listClassObject.push_back(wodner4);
////////// Then you can _DO_ :
/* create */
mapper.createTbl(helper); //create a table by info in wodner1
// | id | name | level | score |
mapper.createTbl(wodner1,1,"number"); // add a "auto_increment" field "number" to your table
// | id | name | level | score | number |
/* insert classObject */
mapper.insert(wodner1); // insert one
mapper.insertRange(listClassObject); // insert a vector
/* delete */
mapper.dropTbl(helper); //delete table
mapper.deleteRow(wodner1); //delete a row
/* update */
mapper.update(wodner1); // update one
mapper.updateRange(listClassObject); //update a vector
/* select and query */
mapper.select(helper,Exp("name,id")).toVector(); //select name,id
mapper.select(helper).toVector();
auto resultA=mapper.select(helper)
.query()
.where(Exp("level")<78)
.toVector();
auto resultB=mapper.query(helper) // no select, will select *
.where(Exp("level")<90) // also .where(Exp("name")=="thief")
.limit(2) // we also can have a "limit" and "offset" operation
.offset(0)
.toVector();
// when you want to create a table, select or query, you need to pass a "helper" classObject.
// you can find we can use Exp to wrap your fields names, then we can create select fields (like "name,id")
//where (like "level")for querying.
// query and select will get a two-dimensional vector whose structure you can get value like this:
for(auto h:resultB)
{
// resultB is a two-dimensional vector, h is a vector
// now h[0] is a string for "id", h[1] is a string for "name", h[2] is a int for "level", h[3] is a float for "score"
MyClass lol(h[0],h[1],h[2],h[3]);
lol.printItself();
}
/* count */
int counter=mapper.query(helper).where(Exp("level")<88).count(); // return a number for your query
😆 Some other funtions in mysql are not implemented (like null value, multiple tables operations and etc....😆).Hope that you can improve it if you like it. I also believe my comment can help you understand these codes.If you have questions you can create a issue.