Skip to content

STR4NG3R/JOrm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JOrm

JOrm is a simple java Orm with capabilities to use entity like frameworks Its built from scratch no dependencies, and it is focused to be simple and can be used on any kind of project, since it only needs the connection to work, it has powerful features such as query builder, partial updates, insert, delete and mapper. This project uses QueryBuilder4J for the Sql Generation

You could look at the unit test cases to get some inspiration.

1. Installation

For default installation, see Releases section to download the .jar file and add it to the path of your project.

1.1. Installation with Maven

To install with maven

Step 1. Add the JitPack repository to your build file

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Step 2. Add the dependency

<dependency>
   <groupId>com.github.STR4NG3R</groupId>
   <artifactId>JOrm</artifactId>
   <version>Tag</version>
</dependency>

2. SELECT Statement

2.1. Basic SELECT statement

Usage:

public class Usage {
    public static void main(String[] args)
    {
      UserDao user = new Runner<UserDao>()
              .setConnection(getConnection())
              .select(
                      new Selector()
                              .select("users", "id", "name", "email", "password", "role")
                              .where("id = :id", (p) -> p.put("id", 1)),
                      UserDao.class
              ).get(0);
    }
}

2.2. SELECT with Specific Fields

Usage:

public class Usage {
    public static void main(String[] args)
    {
      String name = "Pablo", lastName = null, cp = null;
      Selector s = new Selector()
              .select("users as u",
                      "u.id id", "u.name name", "u.email email", "u.role role",
                      "u.email as email"
              )
              .join(Join.JOIN.LEFT, "userAddress as ua", "u.id = ua.userId")
              .join(Join.JOIN.INNER, "addresses as a", "a.id = ua.addressId")
              .setDialect(Constants.SqlDialect.Postgres);

      if (name != null)
        s.andWhere("u.name LIKE  CONCAT('%', :name, '%')", parameters -> parameters.put("name", name));

      if (lastName != null)
        s.andWhere("u.lastName LIKE CONCAT('%', :lastName, '%')", parameters -> parameters.put("lastName", lastName));

      if (cp != null)
        s.andWhere("a.cp = :cp", parameters -> parameters.put("cp", cp));
      
      UserDao user = new Runner<UserDao>()
              .setConnection(getConnection())
              .select(s, UserDao.class)
              .get(0); 
    }
}

2.3. SELECT with Pagination

You can paginate your database as simple like below code

Usage:

public class Usage {
    public static void main(String[] args)
    {
      String name = "Pablo", lastName = null, cp = null;
      Selector s = new Selector()
              .select("users as u",
                      "u.id id", "u.name name", "u.email email", "u.role role",
                      "u.email as email"
              )
              .join(Join.JOIN.LEFT, "userAddress as ua", "u.id = ua.userId")
              .join(Join.JOIN.INNER, "addresses as a", "a.id = ua.addressId")
              .setDialect(Constants.SqlDialect.Postgres);

      if (name != null)
        s.andWhere("u.name LIKE  CONCAT('%', :name, '%')", parameters -> parameters.put("name", name));

      if (lastName != null)
        s.andWhere("u.lastName LIKE CONCAT('%', :lastName, '%')", parameters -> parameters.put("lastName", lastName));

      if (cp != null)
        s.andWhere("a.cp = :cp", parameters -> parameters.put("cp", cp));

      Template<List<UserDao>> paginated = new Runner<UserDao>()
              .setConnection(getConnection())
              .selectPaginated(
                      1,
                      10,
                      s,
                      UserDao.class
              );
      // This generate a paginated list with the first 10 elements
    }
}

3. INNER JOIN statement

3.1. Simple Inner join

The join() method expects an enum of possible type of JOIN This method is described as: innerJoin(JOIN join,String table, String on).

Usage:

public class Usage {
    public static void main(String[] args)
    {
      List<UserDao> user = new Runner<UserDao>()
              .setConnection(getConnection())
              .select(
                      new Selector()
                              .select("users as u", "id", "name", "email")
                              .join(JOIN.INNER, "roles as r", "r.id = u.role_id")
                              .addSelect("r.name", "r.id", "r.level")
                              .join(JOIN.RIGHT, "address as a", "a.user_id = u.id")
                              .addSelect("a.street", "a.cp", "a.number")
                              .write(),
                      UserDao.class
              );
    
    }
}

4 Insert Statement

If you pass an already inserted id record it shoudl partially update

4.1 Simple Insert Example

Usage:

public class Usage {
  public static void main(String[] args)
  {

    new Runner<UserDao>()
            .setConnection(getConnection())
            .insert(
                    UserDao.class, 
                    new UserDao("Pablo", "[email protected]", "password123", "customer")
            );
  }
}

5 Update Statement

Same as insert, but you can exclude columns to be updated

6 Delete Statement

Simple delete statement

7 Entity Declaration

Such as another orms this support entity pattern design

package dao;

import org.example.Column;
import org.example.Entity;
import org.example.Id;

@Entity(name = "users")
public class UserDao {
  @Id
  Integer id;
  @Column
  String name;

  @Column
  String email;

  @Column
  String role;

  @Column
  String password;
}

8. Authors

Pablo Eduardo Martinez Solis

9. License

JOrm is licensed under the GPLv3 license.

The GPLv3 License (GPLv3)

Copyright (c) 2023 Pablo Eduardo Martinez Solis, Derick Felix

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

About

A simple and powerful java Orm

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages