GAEã§ç°¡åãªå¤å¯¾å¤çãªã¢ã
db.ReferenceProperty(db.Mode)ã使ã£ã¦ããã¼ãã«ãä½ãã
(http://ondras.zarovi.cz/sql/demo/)
ãããªæãã«ããã¼ãã«ãè¨è¨ãããMySQLç¨ã®ERå³ãªã®ã§ããã£ã¼ã«ãã®ååãéããã©ãªãã¨ãªãããããäºã¯éããã¨æããã£ã¦ããããã¯ã«ã¬ã
å·¦ã®Tableã¨ãããã¼ãã«å
ã®id_Coreã¨id_TagããReferencePropertyã§å®ç¾©ãããFKã¨ãªãã
ã¢ãã«ã¯
class Table(db.Model): ref_core = db.ReferenceProperty(Core) ref_tag = db.ReferenceProperty(Tag) create_at = db.DateTimeProperty(auto_now_add = True)
ã¨è¨ã風ã«æ¸ãã¦ã¿ãã
ããããäº
- æç« ï¼ä»¥ä¸Coreï¼ã¨ä¸ç·ã«ã¡ã¿ãã¼ã¿ï¼ä»¥ä¸Tag)ãä¿åãã
- Coreã«å¯¾ãã¦ãè¤æ°ã®Tagãä»ããããããã«ããã
- åãå 容ã®Tagã¯ä½¿ãåããã
- TagãããCoreãæ¤ç´¢ããã
Tagããclassã¨ãããã®ã¯ãã¡ã¿ãã¼ã¿ã«å¯¾ãã¦ä½¿ç¨é »åº¦ãªã©ã®ã¡ã¿ã¡ã¿ãã¼ã¿ãä»å ãããããã¾ããä»å¾Tagã¨ã³ã¸ã³ã追å ããéã«Tagæ å ±ã ãã®DBããã¦ãããããçº
å®è£ ãã¦ã¿ã
æ°ã¥ãã¦ãã¾ãã°ããªãã§ãããªç°¡åãªãã¨ãæ°ã¥ããªãã£ãã®ã ããã¨æãã1æéã»ã©ã§åºæ¥ã¦ãã¾ã£ããï¼ã¦ã¼ã¶ã¼æ å ±ãããã©ãããã£ãã®ã§å®è£ ããã¯å¤ããï¼
# main.py # encoding:utf-8 #! /usr/bin/env python import wsgiref.handlers from google.appengine.ext import db from google.appengine.ext import webapp from google.appengine.ext.webapp \ import template class Core(db.Model): body = db.StringProperty(required = True) create_at = db.DateTimeProperty(auto_now_add = True) class Tag(db.Model): body = db.StringProperty() create_at = db.DateTimeProperty(auto_now_add = True) class Table(db.Model): ref_core = db.ReferenceProperty(Core) ref_tag = db.ReferenceProperty(Tag) create_at = db.DateTimeProperty(auto_now_add = True) def tagSplit(str): """æååãã³ã³ãåºåãã«é åã§è¿ã""" return str.split(',') class index(webapp.RequestHandler): def get(self): tables = db.GqlQuery( 'SELECT * from Table ORDER BY create_at DESC') cores = db.GqlQuery( 'SELECT * from Core ORDER BY create_at DESC') tags = db.GqlQuery( 'SELECT * from Tag ORDER BY create_at DESC') tableObj = Table() data_arr, num = [], 0 # ãã¹ã¦ã®coreãã¼ã¿ã§å¦çãåã for c in cores: tag_list = [] # tableãã¼ã¿å ã§coreæ¯ã«tagãåãåºãtagListãä½æãã for i in tableObj.all().filter('ref_core = ', c): if i.ref_core.key()==c.key(): tag_list.append(i.ref_tag) # coreãKeyã«ãã¦ãTagListãappend data_arr.append({ 'core' : c, 'tag' : tag_list }) values = { 'tables' : tables, 'cores' : cores, 'tags' : tags, 'core_cnt' : cores.count(), 'tag_cnt' : tags.count(), 'table_cnt' : tables.count(), 'core_list' : data_arr, } self.response.out.write( template.render('main.html', values)) def post(self): # ã³ã¢ã®ã¿ä¿åãã core = Core(body = self.request.get('core')) core.put() tt = Table() # è¤æ°ã¿ã°å¯¾å¿ for i in tagSplit(self.request.get('tag')): tag = Tag(body = i) tag.put() table = Table( ref_tag = tag, ref_core = core ) # tagã¨tableãã¿ã°æ¯ã«coreã¨çµã³ã¤ãã¦ä¿åãã table.put() self.redirect('/') def main(): app = webapp.WSGIApplication([ (r'.*', index)], debug=True) wsgiref.handlers.CGIHandler().run(app) if __name__ == '__main__': main()
<H1>mrs test</H1> <hr> <form action="" method="post" accept-charset='utf-8'> <p>Core: <input type="text" name="core" value="" id=""></p> <p>Tag: <input type="text" name="tag" value="" id=""></p> <p><input type="submit" value="save"></p> </form> {% for i in core_list.values %} {{i.core.body}}<br> {% for ii in i.tag %} [{{ii.body}}] {% endfor %}<br> {{i.core.create_at}} <p> {% endfor %} <hr> coreæ°ï¼{{core_cnt}}<br> tagæ°:{{tag_cnt}}<br> tableæ°:{{table_cnt}}<br>
è¦è¦ããã¦ç³ã訳ãªãã
ãã¼ãã«ãå ã«ãã¦ãcoreã¨Tagãåå¾ããã¨Coreã®æ°ãTableåå¢ãã¦ãã¾ãã®ã§ãHashã®Keyã«Coreã®Keyã使ç¨ãã¦Coreã®æ°åã ãã®ãã¼ã¿ãçæããã
Datastoreã®ããã¥ã¡ã³ãã«ã¯ãéåç §ã®ãµã³ãã«ãè¼ã£ã¦ããããã¾ãåããªãã£ããç¾å¨ã¯ãCoreã®æ°ã ãTableã«ç·å½ããã§æ¢ãã«ãã£ã¦ããã®ã§ãã®ããæ¹ã¯ã¾ããã¨æããCoreãSetåã§ä½ã£ã¦ãTagãAddãã¦ããã°ãCoreã¨Tableãä¸åãã¤ã®èµ°æ»ã«ãªããã¨æã£ããã©ããæ¹ãããããããªãã£ãã®ã§ãæ³¥èããã¨ããã¦ãã¾ã£ãã
ä»å¾ã®èª²é¡
- éåç §ã®ããæ¹ã身ã«ã¤ãã
- Setåã使ãããªãã¦å¤å¯¾å¤ã®é¢é£ãã·ã³ãã«ã«æãåºã
- Tagããã使ç¨ããã¦ããCoreãéåç §ãç¨ãã¦filterãã
- Tagãæ¯åä½ããã¦ããã®ã§ãget_or_insertã使ãããã«ãã
追è¨
ãã£ã«ã¿ã¼æç¡ã§ã¹ãã¼ãè¨ã£ã¦ã¿ãã
éçºã³ã³ã½ã¼ã«ä¸ã§è¨ã£ãã®ã§æ£ãããªãããç¥ããªã
import datetime import wsgiref.handlers from google.appengine.ext import db from google.appengine.ext import webapp from google.appengine.ext.webapp \ import template class Core(db.Model): body = db.StringProperty(required = True) create_at = db.DateTimeProperty(auto_now_add = True) class Tag(db.Model): body = db.StringProperty() create_at = db.DateTimeProperty(auto_now_add = True) class Table(db.Model): ref_core = db.ReferenceProperty(Core) ref_tag = db.ReferenceProperty(Tag) create_at = db.DateTimeProperty(auto_now_add = True) tim1 = datetime.datetime.now() for h in Core.all(): print h.body for i in Table.all().filter('ref_core =', h): print "["+i.ref_tag.body+"]" print '' tim2 = datetime.datetime.now() tim2d = tim2-tim1 for h in Core.all(): print h.body for i in Table.all(): if i.ref_core.key() == h.key(): print "["+i.ref_tag.body+"]" print '' tim3 = datetime.datetime.now() tim3d = tim3 - tim2 print '---' print tim2d print tim3d print Core.all().count() print Table.all().count()
çµæã¯
--- 0:00:00.401738 0:00:00.781612 17 14
ã¨è¨ãäºã§ã14åç¨åº¦ããç¡ãã«ããããããåè¿ãéãã®ã§ããã£ã±ç·å½ããã¯è¯ããªãããã
ä¸ã®ã½ã¼ã¹ãããã£ã«ã¿ã¼ããã«å¤ãã¾ããã