0

I have an abstract class, Foo, which Bars, Barz, and a bunch of others that hold some bits of data.

I am storing the data into several different tables in SQLite. I also wrote a small class that basically contains the java class that it "creates", the name of the table in the sqlite master and a pair for each column's name and column's data type to help with quickly turning records into objects when the application is running.

I would like to keep things as abstract as possible for re-usability. How could I create the correct subclass of the object with the correct data types by just having the table information from above for a single record from "that table" without an O(n) loop over each class (it's fast, but it's also hard coded for each class and it's a lot of typing).

SQLiteTable class:

class SQLiteTable<T>
{
    ...
    SQLiteTable(String name, SQLiteColumn...columns)
    ...
}

JDBC SQLite abstract requested help:

...
public ArrayList<Foo> query(SQLiteTable table, String statement)
{
    safeString = purgeOfFilt(statement);
    statement = connection.prepareStatement(safeString);
    results = statement.executeQuery();
    ArrayList<Foo> records = new ArrayList<>();
    while(results.next())
    {
        ... // for loop to extract field information about the object I'd like to create
        records.add(/*I'm not sure how to create the correct instance of the class here 
                     (with the extracted fields--some of which are final at creation)*/));
    }
}
...
David Fisher
  • 282
  • 2
  • 13
  • You might want to look at https://stackoverflow.com/questions/452385/what-java-orm-do-you-prefer-and-why – Progman Oct 30 '19 at 18:44
  • Unhelpful. The linked question is an opinion based question. I'm unsure as to what an ORM is, the link doesn't help me with understanding it (neither do most of the github pages for the ORM's in the answers). If it's written in java and is able to do what it is I'm asking about than there should be a finite answer to this question. With everything else being done, the only step left is to ensure the correct instance of the class is instantiated/created. – David Fisher Oct 30 '19 at 18:54
  • 1
    @DavidFisher an ORM is an Object-Relational Mapping (Framework). It's what you're reinventing poorly. – Kayaman Oct 30 '19 at 19:01
  • https://stackoverflow.com/questions/494816/using-an-orm-or-plain-sql/494853#494853 ... Poorly or not, what I'm trying to do is not attached to JDBC and I could rephrase the question completely while still being after the same result. – David Fisher Oct 30 '19 at 19:17
  • 1
    @DavidFisher ..actually it's more [active record](https://en.wikipedia.org/wiki/Active_record_pattern) than ORM, but it's a bit unclear what you're asking. Why is `SQLiteTable` generic in the declaration, but you're passing a raw type to `query()`? Are you asking how to use [reflection](https://docs.oracle.com/javase/tutorial/reflect/index.html)? What was that link in your last comment supposed to mean? – Kayaman Oct 30 '19 at 19:49
  • The link was meant to point out the problems with ORM's. `ArrayList` would net me the correct class. And since that table has a list of all the attributes and their types for constructing that class (and I can provide an order to them), I can call the correct constructor without needing reflection. I'll need to test it to see which is faster but the goal was to be faster than reflection and also not have a ridiculous if else block. I'm also trying to avoid reflection in general because I would like to disable it all together after testing and debugging for security reasons. – David Fisher Oct 31 '19 at 11:37
  • @DavidFisher I'm well aware of the pros and cons of ORMs. But this seems like a traditional "I'll make my own wheel, it'll be rounder than all the previous wheels" type of thing. You should at least know the playing ground and what other solutions exist to reduce boilerplate, such as [jOOQ](https://www.jooq.org/) and others. You'll also soon realize that trying to go without reflection isn't something you just decide to do. It's used for very good reasons. Good luck with your effort, if nothing else, you'll probably learn a lot. – Kayaman Oct 31 '19 at 17:12

0 Answers0