Re: [web2py] Re: Modules: how to access db

2012-02-10 Thread Sathvik Ponangi
Thanks for that!

I've never tested it in a multi-threaded environment.

On Sat, Feb 11, 2012 at 4:41 AM, nick name wrote:

> This solution will lead to a race condition. Do not use it!
>
> If you have multiple threads, they might update your commons at the same
> time and you'll get request from one session, and session from another, and
> db from a third.
>
> "current" is a thread-local thing, guaranteed not to be touched by any
> other part - use "current", not "common" (or whatever your own module is
> called)
>



-- 
Sathvik Ponangi


Re: [web2py] Re: Modules: how to access db

2012-02-10 Thread Michele Comitini
other ways to do it without using current.


in the module (ex. tools.py):

 def regte_verwysing(regte, tabel, veld, db):
 lys = db(db[tabel].article == regte).select(db[tabel][veld])
 verwysings = [x[veld] for x in lys]
 return verwysings

in the model (ex. db.py):

import tools.py
def regte_verwysing(regte, tabel, veld):
  return tools.regte_verwysing(regte, tabel, veld, db)


If you like closures.

in the module (ex. tools.py):

def regte_verwysing(db):
 def f(regte, tabel, veld):
 lys = db(db[tabel].article == regte).select(db[tabel][veld])
 verwysings = [x[veld] for x in lys]
 return verwysings
  return f

in the model (ex. db.py):

import tools.py
regte_verwysing = tools.regte_verwysing(db) #<- returns a callable
with args (regte, tabel, veld)

you can then call regte_verwysing(regte, tabel, veld) in your code

mic

2012/2/10 Johann Spies :
> On 9 February 2012 18:14, Bruno Rocha  wrote:
>>
>> I dont know exactly how it works, but I had problems serializing db in to
>> current, so Massimo said to always pass it explicitly to classes ad
>> functions, I remember that SQLITE worked, but not Postgres.
>
>
> Anthony's advice to me earlier in this thread is working with PostgreSQL:
>
> In db.py:
>
>
> from gluon import current
> current.db = db
>
> and in the module:
>
> from gluon import *
> def regte_verwysing(regte, tabel, veld):
>     db = current.db
>     lys = db(db[tabel].article == regte).select(db[tabel][veld])
>     verwysings = [x[veld] for x in lys]
>     return verwysings
>
> Regards
> Johann
> --
> Because experiencing your loyal love is better than life itself,
> my lips will praise you.  (Psalm 63:3)
>


Re: [web2py] Re: Modules: how to access db

2012-02-10 Thread Johann Spies
On 9 February 2012 18:14, Bruno Rocha  wrote:

> I dont know exactly how it works, but I had problems serializing db in to
> current, so Massimo said to always pass it explicitly to classes ad
> functions, I remember that SQLITE worked, but not Postgres.


Anthony's advice to me earlier in this thread is working with PostgreSQL:

In db.py:

from gluon import current
current.db = db

and in the module:

from gluon import *
def regte_verwysing(regte, tabel, veld):
db = current.db
lys = db(db[tabel].article == regte).select(db[tabel][veld])
verwysings = [x[veld] for x in lys]
return verwysings

Regards
Johann
-- 
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)


Re: [web2py] Re: Modules: how to access db

2012-02-09 Thread Bruno Rocha
I dont know exactly how it works, but I had problems serializing db in to
current, so Massimo said to always pass it explicitly to classes ad
functions, I remember that SQLITE worked, but not Postgres.


-- 

Bruno Rocha
[http://rochacbruno.com.br]


Re: [web2py] Re: Modules: how to access db

2012-02-09 Thread Bruce Wade
Now that is a lot nicer then passing in the db to every module class. Does
this only work with trunk? Or the latest stable release?

On Thu, Feb 9, 2012 at 7:30 AM, omicron  wrote:

> With the last version, you can do this :
>
> from gluon import current
>
> dba = current.globalenv['db']
>
>


-- 
-- 
Regards,
Bruce Wade
http://ca.linkedin.com/in/brucelwade
http://www.wadecybertech.com
http://www.warplydesigned.com
http://www.fitnessfriendsfinder.com


Re: [web2py] Re: Modules: how to access db

2012-02-09 Thread Bruno Rocha
On Thu, Feb 9, 2012 at 11:45 AM, Anthony  wrote:

> from gluon import current
> current.db = db
>

I dont think that adding db to current will work properly, it can work in
SQLITE, but for sure will raise problems with another databases.

I realized that db needs to be instantiated inside a module or passed
explicitly as an arg, it is not possible to serialize it in to current.  It
works in the first request, but not for the subsequents.

Another advice is that you never have to assign variables to current
outside functions and methods

Example.

*Module.py*

from gluon import current
> request = current.request
>
> def myfunction(db):
> if request.args... # HERE BE THE DRAGONS!
>


To work you need to do

*Module.py*

from gluon import current

def myfunction(db):
request = current.request
if request.args   # IT WORKS



It happens because if you assign outside the function, it will be evaluated
only at the first request, and for the subsequents, the request object will
always be the same.








-- 

Bruno Rocha
[http://rochacbruno.com.br]


Re: [web2py] Re: Modules: how to access db

2012-02-09 Thread Johann Spies
On 9 February 2012 15:45, Anthony  wrote:

> I do not understand this properly.
>>
>> After explicitly importing 'current' in the controller, I made some
>> progress.  But why is the class definition necessary and how does it
>> initialise db when db is not available in the module?
>>
>
> You don't necessarily need a class. The point is just that you need to
> pass in the db instance, either via an __init__ method of a class, or
> simply as an argument to your function. In your case:
>
> def number_of_records(db, table):
>
> And when you call that function, pass in your db object as the first
> argument. Another option is to add the db object to "current" -- for
> example, in the db.py model file:
>
> from gluon import current
> current.db = db
>
> Then when you import current in your module, you can refer to current.db.
>
>
Thanks Anthony.  That clears up a few things for me.

Regards
Johann

-- 
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)


Re: [web2py] Re: Modules: how to access db

2012-02-09 Thread Anthony

>
> I do not understand this properly. 
>
> After explicitly importing 'current' in the controller, I made some 
> progress.  But why is the class definition necessary and how does it 
> initialise db when db is not available in the module?
>

You don't necessarily need a class. The point is just that you need to pass 
in the db instance, either via an __init__ method of a class, or simply as 
an argument to your function. In your case:

def number_of_records(db, table):

And when you call that function, pass in your db object as the first 
argument. Another option is to add the db object to "current" -- for 
example, in the db.py model file:

from gluon import current
current.db = db

Then when you import current in your module, you can refer to current.db.

Anthony



Re: [web2py] Re: Modules: how to access db

2012-02-09 Thread Johann Spies
Thanks Ross.

On 9 February 2012 14:39, Ross Peoples  wrote:

> from gluon import *
>
> This should give you the "current" object which has these:
>
> current.request
> current.response
> current.cache
>
> You must initialize your modules with a db instance though. So I usually
> do this:
>
> class MyModule(object):
> def __init__(self, db, ...)
>

I do not understand this properly.

After explicitly importing 'current' in the controller, I made some
progress.  But why is the class definition necessary and how does it
initialise db when db is not available in the module?

Regards

Johann
-- 
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)