Skip to content

Problem with Propertyname "order" #31

Closed
sebastiandeutsch opened this Issue Apr 17, 2012 · 10 comments

4 participants

@sebastiandeutsch

When you add

entity.addIntProperty("order");

it will generate some code without any errors. Unfortunately the SQL generated by the querybuilder will throw an Exception since ORDER is a reserved keyword. Maybe greenDAO should throw an Exception in the generator when a reserved word is used.

@greenrobot
Owner

This is weird, because I have unit tests for special property/column names. Also for QueryBuilder:
https://github.com/greenrobot/greenDAO/blob/master/DaoTest/src/de/greenrobot/daotest/query/QuerySpecialNamesTest.java

However, I did not test "order" yet...

@greenrobot
Owner

I just added an "order" property to those unit tests. It seems your use case is different and the current unit test do not cover it. Could you supply more info on your case, e.g. your query, the generated SQL, and the exception? Thanks.

@greenrobot
Owner

I added another unit test (5aa643c), but still cannot reproduce this error. Please let me know your query leading to the error. Without being able to reproduce, I'd have to close the issue otherwise.

@sebastiandeutsch

Hi, I've put up an Repository to reproduce the "bug" https://github.com/sebastiandeutsch/OrderBugTestAndGenerator - I understand if you don't fix it, since it's not a real bug, it's more the lameness of SQLite that you cannot escape fieldnames. But maybe you should enlist all reserved words in the docu, or in the FAQ.

@greenrobot
Owner

Thanks for getting back. I thought you were referring to greenDAO's QueryBuilder class. If it's about using SimpleCursorAdapter, you should try escaping your column names with a single quote, e.g. 'order' instead of order.

That's what QueryBuilder is doing and the unit tests seem to run correctly.

@greenrobot
Owner

I guess the question is whether to have column names wrapped inside single quotes somewhere in Properties.

@gauthierm

Using a field named "order" as an order field in a to-many relationship breaks because it is not quoted. See https://github.com/greenrobot/greenDAO/blob/master/DaoGenerator/src/de/greenrobot/daogenerator/PropertyOrderList.java#L67

Anywhere columns are put into generated SQL, they should be escaped using a method.

@gauthierm

I will make a PR to do that if you will merge it.

@greenrobot
Owner

fixed PropertyOrderList in master, no other issues reported by this ticket

@greenrobot greenrobot closed this Jun 28, 2015
@greenrobot greenrobot added the Bug label Jun 28, 2015
@greenrobot greenrobot added this to the Next Major milestone Jun 28, 2015
@andrewindayang

I think it is not greendao bug. you can modify that from the JSON. Please check my answer:

http://stackoverflow.com/questions/31120433/using-retrofit-and-greendao-with-nested-json-objects/36789972#36789972

<----SOLUTION------>
//add this to daogenerator and generate the daogenerator
beacon.addStringProperty("order_food"); //Daogenerator

//add this to network manager
//add this into your network manager
FieldNamingStrategy strategy = new FieldNamingStrategy() {
@Override
public String translateName(Field field) {
if(field.getName().equalsIgnoreCase("order_food")) {
return "order";
} else {
return field.getName();
}
}
};

//ADD THIS INTO YOUR GSON INSIDE YOUR NETWORK MANAGER
//add this in your Gson

.setFieldNamingStrategy(strategy)

hopefully it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.