0

I'm looking into using greenDAO for my Android app, but I noticed it doesn't seem to support any kind of data validation other than "not null", "unique", and foreign keys, either on the SQL level (constraints defined when creating tables) or the Java level (validation logic in setter methods). "Keep sections" don't seem like they would be helpful in this case because you can't have them within individual methods. Am I missing something, or would I really need to add yet another layer on top of the generated Java objects if I wanted to validate input data? (I'm somewhat confused how the framework could be useful without providing any place to include validation logic.)

ecraig12345
  • 2,328
  • 1
  • 19
  • 26
  • What kind of validation do you mean. Please give an example. – AlexS Jan 29 '14 at 19:46
  • @AlexS Some examples would be values that must be limited to a certain range, or one field that must always be less than another field. Basically, anything that you could do with a SQL constraint. – ecraig12345 Jan 30 '14 at 01:12
  • 1
    Greendao is meant for persisting and accessing your data in a simple way and is your DAO layer. Validation is part of business logic and should normally take place upon input or in business logic and before data is persisted. – AlexS Jan 31 '14 at 07:56

1 Answers1

0

1.

You can write a method

boolean check ();

in KEEP-SECTION of the entity which you call manually before INSERT or UPDATE.

2.

Another possibility is to extend the sourcecode of greendao generator to support checks: In property.java you could add a method to Property.Builder

public Property.Builder check (String expr) {
    property.checkConditon = expr;
}

Of course you would have to introduce the String checkCondition = ""; and use it for generating the dao in the dao-template.

Problem: With new versions of greendao your changes would be lost (but then again new version may already contain such a feature)

3.

A third possibility is to copy the generated CREATE TABLE statement, modify it to fit your needs and call your modified statement instead of the original one or to drop the original table and call your statement.

Problem: If your table changes you will have to repeat this.

AlexS
  • 5,295
  • 3
  • 38
  • 54
  • I'm not sure how this helps anything, unless it's called automatically from somewhere? – ecraig12345 Jan 30 '14 at 15:39
  • I am afraid, you will have to call it yourself at the moment. – AlexS Jan 30 '14 at 22:31
  • That's next to useless then--having to manually call some sort of validation method periodically, rather than validating data within setters (or on database updates) isn't a good way to design things. – ecraig12345 Jan 31 '14 at 04:14
  • The validation in setters is not possible with greendao. For validations using the database capabilities see my changes. – AlexS Jan 31 '14 at 07:48
  • Thanks for the expansion on the answer! I think I'll probably still end up trying something else though, so I can handle things more how I would like. – ecraig12345 Feb 02 '14 at 06:15