Jump to content

Hello,

 

I finished the design concept for a SQL Library I am writing for a section of a bigger library I am in the process of writing. I am stuck on one problem specifically.

 

I'm going to use annotations to specify which field corresponds to a column parameter in a table on a database. Here is the current pseudo code I have for this.
 

package net.zoramagic.library.manager;

import lombok.Getter;
import net.zoramagic.library.manager.sql.annotations.PrimaryKey;
import net.zoramagic.library.manager.sql.annotations.Table;
import net.zoramagic.library.manager.sql.annotations.TableParameter;

import java.util.UUID;

@Table(database = "testDatabase", table = "testTable")
public class ConceptTableClass
{
    @PrimaryKey
    @TableParameter(columnName = "uuid")
    private @Getter UUID playerUUID;

    @TableParameter(columnName = "kills")
    private @Getter int kills;

    @TableParameter(columnName = "deaths")
    private @Getter int deaths;

    @TableParameter(columnName = "balance")
    private @Getter double balance;
}

 

Now the current design I have for this, it allows me to use the following methods with the class that has the annotations seen above, excluding the '@Getter', like so.

SQLManager manager = new SQLManager();
SQLConnection connection = new SQLConnection();
ConceptTableClass conceptTableClass = new ConceptTableClass():
UUID uuid = UUID.randomUUID():

manager.addConnection(connection);

//Will automatically create the table if it doesn't exist. 
connection.register(conceptTableClass);

//Get the result set and parse it to a map.
Map map = connection.getResultSetFrom(conceptTableClass).where("uuid", uuid).asMap(MapOrder.ASCENDING).execute();

//Get the result set and pass it to the respective field with the corresponding `@TableParameter` parameter matching the column name of the of the result set.
ConceptTableClass concept = connection.getResultSetFrom(conceptTableClass).where("uuid", uuid).asClass(ConceptTableClass.class).execute();

//Objects passed in the object array are inserted in the the same order, descending from the first value.
connection.insert(Object.. objects).intoColumns(String... columnNames).execute();

//Like above, the columnNames are read desending from the first value, so are the objects that will replace the current ones in there.
connection.update(String... columnNames).insert(Object... objects).where(String columnName, Object object).execute();

 

Obviously, there are much more methods than just the ones I have displayed in this example. But I am wondering if there is a better way to table the create and access of tables in a database? I had the idea of replacing the methods requiring the parameter to be a class containing the '@Table' && '@TableParameter' annotations with two strings, one with the database name and the other with the table name but I am not sure this would be the best idea in the book. Is there a better alternative for managing (accessing, handling etc) tables?

Thanks,

Duke.

Link to comment
https://linustechtips.com/topic/770025-java-sql-library-problem/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×