简体   繁体   中英

guidance on java class design

I am in the process of building a data tool that allows users to build ETL code. Before getting to the meat and bones of this, I need to build an 'agent' that will deal with all the database related work the tool will need to perform. Specifically, this would involve -

  • Selecting, inserting, updating, and deleting
  • multiple records
  • from/to/in different tables with different layouts

My thought is to build a class with the following 4 methods -

ArrayList[][] selectFromRepository  (String dbTable, String[] columnNames)
ArrayList[][] selectFromRepository  (String dbTable, String[] columnNames, Map<String, Object> predicate) 
void          insertToRepository    (String dbTable, Map<String, Object>[] payload)
void          updateInRepository    (String dbTable, Map<String, Object>[] payload, Map<String,Object>[] predicate)
void          deleteFromRepository  (String dbTable, Map<String, Object>[] payload)

selectFromRepository will respond with a 2D arraylist of heterogenous elements based on the types of the elements in "columnNames" retrieved from the argument "dbTable". The overloaded version allows specifying a where clause predicate through a "predicate" Map. I was thinking I could inspect the type of the Map object element passed to the method and build the where clause correctly (for eg

"where " + key + " = '" + predicate.get(k) + "'" if predicate.get(k) instanceOf String 

and so on ..

Other methods will work similarly with update methods predicate working similar to that of the overloaded select.

I am looking for your guidance on best practices, disadvantages of my idea (and therefore advantages of you doing it your way :) ). Also, would it make sense to build a generic type called DB element that accepts all known DB data types that I expect to work with as a type input, and then use that instead of the "Object" type in map/arraylist arguments?

Thanks in advance!

Well this is quite the question.

First of all i would not implement this myself. there are a number of free libraries out there (Spring, Hibernate) that do this for you, and much better in a form of an ORM ( http://en.wikipedia.org/wiki/Object-relational_mapping ).

If you do want to do this yourself this is how I would do this:

I would follow the ADO and create a class for each table I have. For example I would make the class Employee for my employee table.

Then I would create a query builder such as this:

QueryBuilder
    .selectFrom(Employee.class)
    .equalTo(propery, value)
    .graterThenOrEqualTo(property, value);

my properties would look like so:

interface Property<From, To>{
    To get(From from);
}

so for example:

class EmployeeToId<Employee, Integer>{
    public Integer get(Employee emp){
        return emp.id;
    }
}

So in my previos example:

QueryBuilder
    .selectFrom(Employee.class)
    .equalTo(new EmployeeToId(), 15)
    .build().fetch(); // This returns an Optional<Collection<Employee>>;

My query builder would have a number methods such as:

class QueryBuilder{
   public <Entity> SelectBuilder<Entity> selectFrom(Class<Entity> clazz);
}

This is just the jist of it. You can make it as complex as you like. But to make it good, that will take alot of time, just ask hibernate or jooq.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM