Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Server] ORM
#1
ORM build for Gothic 2 Online

works with: MySql Module(V.0.3)

Test version! 
(Be aware that it's not been tested in real environment)


Link to file

!Report Error here

Script contains 4 main elements
- Active Record with control ur model creation save/modify
- Connector with is simples of all. Allows to create connection with database.
- Builder starting from phrase Query() with allows user to create most of operation simply by using functions builded in.
- Migration module allows user to create sql queries only one time.

Active Record

Build in base of query builder use some of features to controll proces of creating new model and update him. Allows also to easy find and use models base on table. Contains also some function to control rules for fields in table. (Validation). With this model you can easily create e.g. Player class or House.

Connector

Contain simple function to build query like queryGetAll, queryGet and some basics stuff to operate on Database. Build on Class you can directly use 2 databases in same time. 
If u dont wanna use Builder/Active Record u should use basiclly just this class to controll your connection and quer'ies.

Code:
local db = Connector(host, user, pass, db, port);
db.queryGetAll @return {array}
db.queryGet @return {array}
db.getDatabaseSchema @return {array}
db.query @return {bool}
db.check @return {bool}

example
DB <- Connector("localhost", "root", "", "gothic", 3306);
DB.query("CREATE TABLE `test` (person int(11));");


Builder

Builder allows user to create query command simply by use builded functions. (Fast way to create query). Remember to have on mind that is only alpha version. I need to build more functions.

Code:
local query = Query();
query.getSQL @return {string};
query.execute @return {bool};
query.all @return {array};
query.one @return {array};
query.where @return {obj};
query.from @return {obj};
query.update @return {obj};
query.orderBy @return {obj};
query.createTable @return {obj};
query.insertInto @return {obj};
query.deleteFrom @return {obj};
query.primaryKey @return {obj};
query.dropTable @return {obj};

Query().select().from("player").where(["id = 1", "name = 'Quarchodron'"]).one();
Query().deleteFrom("player").where(["name = 'Jacek'"]).execute();
Query().insertInto("player", ["name", "password"], ["'Jacek'", "'Warnenczyk'"]).execute();
Query().update("player", ["name", "password"], ["'Jacek'","'Warnpozmianie'"]).where(["name = 'Jacek'"]).execute();


Migration 

Simple class contain controll of creating new table / inserts or just changes in database. Changes is saved in table 'migrations' with allows to not build same changes twice.

Code:
local mig = Migration();
mig.run();
mig.down();

// Installation
class TestMigration extends Migration
{
    // need to use this to create row in table 'migrations'
    static nameMigration = "PlayerMigration";

    // Create run up function base on builder
    function runUp() {
        Query().createTable("character",[
            ["id int NOT NULL AUTO_INCREMENT PRIMARY KEY"],
            ["name varchar(255) NOT NULL"],
            ["password varchar(255) NOT NULL"],
            ["liczba int"],
            ["data DATE"],
        ]).execute();
    }

    // Create run down function
    function runDown() {
        Query().dropTable("character").execute();
    }
}
//Use run up
TestMigration().run();

//Use run down
TestMigration().down();
[Image: Quarchodr_sheqnnr.png]
Reply
#2
Is it good idea to use strings as a part of the query?
This approach doesn't enforce building the 'correct' queries to the database.
If someone will make a typo in a string, then finding the cause of the problems can be really difficult.
Reply
#3
Actually active record use name of fields in building query. So IT worka another way. Problem is in building query other way than from model. Using query() u have to be aware that if u make mistake u will get error. Every modern orm Using that in that way. You get more possible ways to build a query. Ofc. You csn do some damage. But in future of this orm probably user will not use query builder seperate from active record at all. To remove SQL injection u have to Just use builded function grom module.
[Image: Quarchodr_sheqnnr.png]
Reply
#4
[email protected] Version 0.2

- Add rules/validation for orm
- New types in Builder
- New module migration
- Fixes in orm
[Image: Quarchodr_sheqnnr.png]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [0.1.3] [server] ItemGroundSpawner HammelGammel 1 2,351 05.02.2020, 18:40
Last Post: demor140202
  [0.1.4.*] [client/server] Simple anty-cheat Profesores 8 5,879 20.03.2018, 19:16
Last Post: Patrix
  [0.1.3] [client/server] BotCreator HammelGammel 22 15,200 08.02.2018, 22:02
Last Post: HammelGammel
  [0.1.3] [client/server] Utility Scripts HammelGammel 1 3,337 27.01.2018, 14:03
Last Post: HammelGammel
  [G2O v.0.1.2][Server-Side] NPC Manager Quarchodron 16 10,494 21.01.2018, 19:59
Last Post: Quarchodron

Forum Jump:


Users browsing this thread: 1 Guest(s)