Skip to content

Commit

Permalink
Better way to do db.get by ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
zedshaw committed Aug 30, 2013
1 parent de0b390 commit 2665b93
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,22 @@ Databasing
FuqIt took the public domain web.py database module and uses that. The best docs for it is currently from
the Web.py folks over at <http://webpy.org/docs/0.3/tutorial#databasing>.

Keep in mind that it's not too usable right now as you can really only get at the database by using the
raw API and doing it in modules. Later versions will make that easier.
You use a database by doing two things:

1. Edit the config.py file that fuqit makes for you. Inside there is an initial configuration that makes a db variable configured from the fuqit.data.database call.
2. Change the data.database call to use the database you want. It's currently setup to use a SQLite3 database.

That's it. Once you have that configure, the web variable in templates and modules will have a web.db variable for you to do database stuff with.

Web.py Database Modifications
-----------------------------

There are a few minor modifications to the default web.py that you need to know:

1. It's renamed data so that it can be more than just for databases and so it doesn't conflict with the db variable in config.py.
2. I added a web.db.get() function that's a reduced version of web.db.select() that is used to just get one record.

As I evolve web.py's DB I'll just call it the fuqit.data API and document it differently.

But That's Magic!
=================
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/app/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
def run(web):
post_id = int(web.sub_path[1:])

web.post = web.db.get('post', where='id = $id', vars={'id': post_id})
web.post = web.db.get('post', by_id=post_id)

if not web.post:
return web.app.render_error(404, "Not Found")
Expand Down
13 changes: 8 additions & 5 deletions fuqit/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,14 @@ def iterwrapper():
self.ctx.commit()
return out

def get(self, tables, vars=None, what='*', where=None, order=None, group=None,
offset=None, _test=False):
results = self.select(tables, vars=vars, what=what,
where=where, order=order, group=group,
limit=1, offset=offset, _test=_test)
def get(self, tables, by_id=None, vars=None, what='*', where=None, _test=False):
if by_id:
assert not (vars and where), "If by_id you can't give vars and where."
results = self.select(tables, vars={'id': by_id}, what=what,
where='id = $id', limit=1, _test=_test)
else:
results = self.select(tables, vars=vars, what=what, where=where, limit=1, _test=_test)

if not results:
return None
else:
Expand Down

0 comments on commit 2665b93

Please sign in to comment.