amine605 / DopsieORM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dopsie

Lightweight ORM for Java / SQL developers.

Getting started

First, you should download the latest version from here.

Then, you can add the jar file to you project for example if you're using Netbeans, you should go to the Project Tree, Right click on Libraries, and choose the 'Add Jar/Folder' option, then select the path to the downloaded jar file.

Before start using the ORM you should provide credentials:

public class TestingDopsieORM {

    public static void main(String[] args) {
        System.setProperty("host", "localhost");
        System.setProperty("port", "3306");
        System.setProperty("database", "esprit");
        System.setProperty("user", "root");
        System.setProperty("password", "root");
        
        // You can use Models here.
       
    }
    
}

Usage

Creating a Model

Create a Model inside Modelsby extending the Model class from Models package:

package Models;

import Core.ORM.*;

/**
* User Model
*/
public class User extends Model {

}

Retrieve Data

After you will be able to retrieve data as Model from the database:

To retrieve a User by its id :

User user = Model.find(User.class, 1);

To retrieve all users:

ArrayList<User> allUsersList = Model.fetch(User.class).all().execute();

You are able to apply filters on the data using the where method:

ArrayList<User> allUsersList = Model.fetch(User.class)
					.all()
					.where("last_name", "=", "john")
					.execute();

You are also able to order the data using the orderBy method:

ArrayList<User> allUsersList = Model.fetch(User.class)
					.all()
					.where("last_name", "=", "john")
					.orderBy("last_name", "DESC")
					.execute();

You can get an attribute using getAttr method:

String lastName = user.getAttr("last_name");

Update Data

You can set an attribute in an existing Model object using:

user.setAttr("last_name", "john");

After creating new model object or updating a retrieved object you can push updates to the database using save:

user.save();

Delete Data

If you want to delete a Model Object you have to trigger the delete method:

user.delete();

Defining Relationships

hasMany

public class User extends Model {
    public ArrayList<Post> posts() throws ModelException{
        return this.hasMany(Post.class);
    }
}

More details

hasOne

public class User extends Model {
    public Address address() throws ModelException{
        return this.hasOne(Address.class);
    }
}

More details

belongsTo

public class Post extends Model {
    public User author() throws ModelException{
        return this.hasOne(User.class);
    }
}

More details

belongsToMany

public class User extends Model {
    public ArrayList<Role> roles() throws ModelException{
        return this.belongsToMany(Role.class);
    }
}

More details

manyToMany

public class Order extends Model {
    public ArrayList<Product> products() throws ModelException{
        return this.manyToMany(Product.class, ProductOrder.class);
    }
}

More details

Go Further

Custom table name

public class User extends Model {
    @Override
    public String getTableName() {
        return "person";
    }
}

Custom Primary Key column name

public class User extends Model {
    @Override
    public String getPrimaryKeyName() {
        return "user_id";
    }
}

Executing proper SQL

Queries

You are able to execute SQL queries by calling sqlQuery and it will return a ResultSet:

Model.sqlQuery("SELECT * FROM users");

Or make it into a collection of Model objects by:

Model.sqlQuery("SELECT * FROM users", User.class);

Updates

For Updates you should provide the SQL statment along to arguments:

ArrayList args = new ArrayList(Arrays.asList("Mike", 1));
sqlUpdate("UPDATE users SET last_name = ? WHERE id = ?", args)

About


Languages

Language:Java 99.2%Language:Makefile 0.8%