简体   繁体   中英

GAE Python Datastore - query on nested class table

I have a Java class that contains some nested classes, as follow:

public class OuterClass{

    @PrimaryKey
    private String id;

    @Persistent
    private String name;

    // --- blah blah

    static class MyNestedClass{
        //--- properties declaration
    }
}

This class works fine, basically within just one structure (OuterClass) I can store n nested structures, and each structure works as a datastore table.

Inspecting my ds with datastore viewer I see that I have now a table called OuterClass$MyNestedClass , I can successfully run GQL queries on this table like this:

//--- note the wrapping quotation marks on the table name
SELECT * FROM "OuterClass$MyNestedClass" where something = '100'

Till now everything is OK. Now I need to create a google app engine python method that empties that nested class table. I already did this with other tables/classes, but never with nested classes.

This is my python code:

import cgi
import datetime
import urllib
import webapp2

from google.appengine.ext import db

class OuterClass$MyNestedClass(db.Model):
    name = db.StringProperty()
    surname= db.StringProperty()
    age = db.StringProperty()

class emptyMyNestedClassHandler(webapp2.RequestHandler):
    def get(self):
        nestedclass= db.GqlQuery("SELECT __key__ FROM \"OuterClass$MyNestedClass\"")
        count = 0

        for p in nestedclass:
            count += 1
        db.delete(nestedclass)

        self.response.out.write("Deleted SMyNestedClass Entities: " + str(count))

app = webapp2.WSGIApplication([
    ('/emptyMyNestedClass', emptyMyNestedClassHandler)
], debug=True)

and this is the error I'm getting

class OuterClass$MyNestedClass(db.Model):
                ^
SyntaxError: invalid syntax

I tried to change the class name from OuterClass$MyNestedClass to MyNestedClass , but I get this error:

KindError: No implementation for kind 'OuterClass$MyNestedClass'

Which name do I have to assign to my python class in order to make it work? How can I handle the $ issue?

hope I've been clear enough, thank you


Solution

Following Matt's suggestion , my JAVA class now looks like this:

public class OuterClass{

    @PrimaryKey
    private String id;

    @Persistent
    private String name;

    // --- blah blah
    @PersistenceCapable( table="NestedClass")
    @Embedded
    static class NestedClass{
        //--- properties declaration
    }
}

And this is the Python code:

import cgi
import datetime
import urllib
import webapp2

from google.appengine.ext import db

class NestedClass(db.Model):
    name = db.StringProperty()
    surname = db.StringProperty()
    age = db.StringProperty()

class emptyNestedClassHandler(webapp2.RequestHandler):
    def get(self):
        nestedclass= db.GqlQuery("SELECT __key__ FROM NestedClass")
        count = 0

        for p in nestedclass:
            count += 1
        db.delete(nestedclass)

        self.response.out.write("Deleted NestedClassEntities: " + str(count))

app = webapp2.WSGIApplication([
    ('/emptyNestedClass', emptyNestedClassHandler)
], debug=True)

Note that I had to declare class NestedClass .

Thanks!

Take a look at this resource for JDO annotations .

you may want to annotate your class like this:

public class OuterClass{

    @PrimaryKey
    private String id;

    @Persistent
    private String name;

    // --- blah blah
    @PersistenceCapable( table="NestedClass")
    @Embedded
    static class NestedClass{
        //--- properties declaration
    }
}

With which from GQL you should be able to do this:

SELECT * FROM "OuterClass" where NestedClass.something = '100'

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