0

I'm trying to create a hierarchy of SQL query objects. My base class will be named Query. It will have methods like Query.setTable("tableName").

Then I plan on having a SelectiveQuery object extend the Query object. This object will have methods like SelectiveQuery.where("aColumn","=","123").

Then I plan on having UpdateQuery, SelectQuery, DeleteQuery, and CountQuery all extend SelectiveQuery. My dilemma arises when I try to figure out how to create the InsertQuery class. I want it to extend Query but not extend SelectiveQuery.

The kicker is that InsertQuery and UpdateQuery will share some of the same methods, such as setValue("aColumn","aValue"). I'd prefer to only maintain the methods UpdateQuery and InsertQuery have in common in one location. How do I tackle this? If I were programming in PHP. I think I'd use Traits to do this.

enter image description here

pinkpanther
  • 4,770
  • 2
  • 38
  • 62
David
  • 10,418
  • 17
  • 72
  • 122
  • This is a design question here. You should change your diagram: make a base `ReadQuery` and `WriteQuery` inheriting `Query`, and add a `Criterion` API which you would use only on queries which need it. No need for traits ;) – fge Jun 15 '13 at 17:50
  • so the `Criterion` API would be added to Query? – David Jun 15 '13 at 17:52
  • No, it would be another API; you'd add the criterion to the query before you build it. Something like `Query.newSelectQuery().onTable(table).usingCriterion(criterion).build()` – fge Jun 15 '13 at 18:00
  • Does this answer your question? [java traits or mixins pattern?](https://stackoverflow.com/questions/263121/java-traits-or-mixins-pattern) – Top-Master Jun 01 '22 at 16:04

3 Answers3

1

If InsertQuery and UpdateQuery have behaviour in common, can't you just create an abstract class that contains the common stuff and have InsertQuery and UpdateQuery extend it? The abstract class would extend SelectiveQuery so InsertQuery and UpdateQuery would inherit all of the other behaviour too.

However... Perhaps you could take a look at an ORM framework like Hibernate. No need to reinvent the wheel etc.

rcgeorge23
  • 3,594
  • 4
  • 29
  • 54
  • i don't want InsertQuery to extend SelectiveQuery. It doesn't make sense to have methods like InsertQuery.where("aColumn","=","123") – David Jun 15 '13 at 17:40
  • Ok, what about creating an interface that describes the methods that are common to both. Insert and Update would implement this interface. You can then create a service that contains implementations of these methods and Insert and Update can delegate to the service. – rcgeorge23 Jun 15 '13 at 17:44
  • @David if methods don't make sense for a certain type of query, just make them do nothing! And BTW, you should have a look at the builder pattern – fge Jun 15 '13 at 17:46
  • 1
    @fge Methods doing nothing is indicator of a poor design. – kan Jun 15 '13 at 17:57
  • @kan disagree, in some case it is _very_ useful – fge Jun 15 '13 at 17:58
  • @fge How is a method doing nothing could be useful?? – kan Jun 15 '13 at 17:59
  • @kan very simple: you have a DDL interface, you need certain DDL commands to be executed _before_ other queries are issued and some DDL commands _after_ other queries are issued. If you don't have empty methods, how do you make `.before()` "do nothing" if, for instance, you remove a column? Similarly, how do you make `.after()` do nothing if you add a column? – fge Jun 15 '13 at 18:03
1

You could replace inheritance by mixin, i.e. introduce another class such as Columns, InsertQuery and SelectiveQuery will have the class as a member, so the syntax will be insertQuery.columns().setValue("aColumn", "aValue"). Also, have a look here: querydsl.

kan
  • 28,279
  • 7
  • 71
  • 101
  • can be like this : ColumnsQuery extends Query, and UpdateQuery, InsertQuery extends ColumnsQuery – jospratik Jun 15 '13 at 17:53
  • @jospratik Not of course, the select, count and delete will have it then, which doesn't make sense. – kan Jun 15 '13 at 17:55
0

Source;

Is the object-purist stirring in you today?

Think you could do with a little composite oriented programming?

Then you, sir, are looking for Apache Polygene (formerly named Qi4J, then it renamed to Zest and/or Apache-Zest) ;)

Update 2022; It's discontinued currently, but useful anyway.

Top-Master
  • 7,611
  • 5
  • 39
  • 71