Petr Jakeš | 6 Aug 2008 13:05
Picon

timestamp - date select

Hi,
in my tables I am using timestamp field.
I would like to select all records from the table that have the same date (not the same timestamp!!), say all records with the date 31.12.2007. What is the best way to do it in the SQLObject?

Thanks for your suggestions.

Petr Jakes
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Oleg Broytmann | 6 Aug 2008 13:20
X-Face
Picon
Favicon

Re: timestamp - date select

On Wed, Aug 06, 2008 at 01:05:02PM +0200, Petr Jake?? wrote:
> in my tables I am using timestamp field.
> I would like to select all records from the table that have the same date
> (not the same timestamp!!), say all records with the date 31.12.2007. What
> is the best way to do it in the SQLObject?

   Using 'date()' function in SQL (not in SQLObject):

from sqlobject import *
from sqlobject.sqlbuilder import *

__connection__ = "sqlite:/:memory:?debug=1"

from datetime import datetime

class Test(SQLObject):
   ts = DateTimeCol()

Test.createTable()

Test(ts=datetime.strptime('2008-08-01 21:44:33', '%Y-%m-%d %H:%M:%S'))

print list(Test.select(func.date(Test.q.ts) == '2008-08-01'))

 2/QueryIns:  INSERT INTO test (ts) VALUES ('2008-08-01 21:44:33')
 2/QueryR  :  INSERT INTO test (ts) VALUES ('2008-08-01 21:44:33')
 3/QueryOne:  SELECT ts FROM test WHERE ((test.id) = (1))
 3/QueryR  :  SELECT ts FROM test WHERE ((test.id) = (1))
 4/Select  :  SELECT test.id, test.ts FROM test WHERE ((date(test.ts)) = ('2008-08-01'))
 4/QueryR  :  SELECT test.id, test.ts FROM test WHERE ((date(test.ts)) = ('2008-08-01'))
[<Test 1 ts='datetime.datetime...)'>]

   'func' is SQLBuilder's magic that passes its attribute to SQL unchanged.
Note 'date(test.ts)'! You should check that your backend actually
implements the function (for 'date' most do).

Oleg.
--

-- 
     Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Petr Jakeš | 6 Aug 2008 19:10
Picon

Re: timestamp - date select

from sqlobject import *
from sqlobject.sqlbuilder import *

__connection__ = "sqlite:/:memory:?debug=1"

from datetime import datetime

class Test(SQLObject):
  ts = DateTimeCol()

Test.createTable()

Test(ts=datetime.strptime('2008-08-01 21:44:33', '%Y-%m-%d %H:%M:%S'))

print list(Test.select(func.date(Test.q.ts) == '2008-08-01'))

 2/QueryIns:  INSERT INTO test (ts) VALUES ('2008-08-01 21:44:33')
 2/QueryR  :  INSERT INTO test (ts) VALUES ('2008-08-01 21:44:33')
 3/QueryOne:  SELECT ts FROM test WHERE ((test.id) = (1))
 3/QueryR  :  SELECT ts FROM test WHERE ((test.id) = (1))
 4/Select  :  SELECT test.id, test.ts FROM test WHERE ((date(test.ts)) = ('2008-08-01'))
 4/QueryR  :  SELECT test.id, test.ts FROM test WHERE ((date(test.ts)) = ('2008-08-01'))
[<Test 1 ts='datetime.datetime...)'>]

  'func' is SQLBuilder's magic that passes its attribute to SQL unchanged.
Note 'date(test.ts)'! You should check that your backend actually
implements the function (for 'date' most do).

I am on Firebird right now. AFIK Firebird does not support 'date'.
The syntax which works is:

cast(test.ts as date) = '2008-08-01'

But Python does not accept following construction:

print list(Test.select(func.cast(Test.q.ts as date) == '2008-08-01'))





-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Matthew Wilson | 6 Aug 2008 19:23
Gravatar

How can I add extra columns to my select results?

This is the SQL I want to do:

    select employee.*, 1 + 1 as x
    from employee
    where employee.id = 44;

I know how to do this:

    User.select(User.q.id == 44)

But how can I get the extra column?

In my real-world example, the extra column is more complex.

Matt

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Oleg Broytmann | 6 Aug 2008 19:29
X-Face
Picon
Favicon

Re: timestamp - date select

On Wed, Aug 06, 2008 at 07:10:12PM +0200, Petr Jake?? wrote:
> I am on Firebird right now. AFIK Firebird does not support 'date'.
> The syntax which works is:
> 
> cast(test.ts as date) = '2008-08-01'
> 
> But Python does not accept following construction:
> 
> print list(Test.select(func.cast(Test.q.ts as date) == '2008-08-01'))

   Of course no. Not SQLObject fault, though. ;) You can overcome this by
using strings:

print list(Test.select("cast(ts as date) == '2008-08-01'"))

   or by implementing your own 'func'-like object (name it 'cast') so you
can pass it two parameters; something like this:

print list(Test.select(cast(Test.q.ts, 'date') == '2008-08-01'))

   See sqlobject/sqlbuilder.py on how 'func' is implemented.

Oleg.
--

-- 
     Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Oleg Broytmann | 6 Aug 2008 19:34
X-Face
Picon
Favicon

Re: How can I add extra columns to my select results?

On Wed, Aug 06, 2008 at 05:23:34PM +0000, Matthew Wilson wrote:
> This is the SQL I want to do:
> 
>     select employee.*, 1 + 1 as x
>     from employee
>     where employee.id = 44;
> 
> I know how to do this:
> 
>     User.select(User.q.id == 44)
> 
> But how can I get the extra column?

   You have to declared the column in the User class:

class MyCol(Col):
   ...implementation...

class User(SQLObject):
   extra = MyCol()
   ...other columns...

   Or you can use sqlbuilder.Select() - it allows to select arbitrary list
of columns.

Oleg.
--

-- 
     Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Luis Javier Peris | 12 Aug 2008 18:18
Picon

Inheritance and MultipleJoin

I'd like to know if there is a restriction using inheritance about multiplejoin function because I'm getting an error and I don't know why. I've the following code:

class Question(InheritableSQLObject):
    filename = StringCol(default=None, length=255, unique=True)
    title = StringCol(default=None)
    default_mark = FloatCol(default=0.0)
    wording = RelatedJoin('Content')
    term_questions = RelatedJoin('Term', joinColumn='term', otherColumn='question',
                                 intermediateTable='Term_questions')
    answers = MultipleJoin('Answer')
    questionref = MultipleJoin('QuestionRef')


class Answer(SQLObject):
    value = StringCol(default=None)
    calification = FloatCol(default=None)
    ide = StringCol(default=None)
    question = ForeignKey('Question')


class Ord(Question):
    _inheritable = False
    shuffle = BoolCol()
    orderanswers = MultipleJoin('OrderChoice')


class OrderChoice(SQLObject):
    value = StringCol(default=None)
    ord = IntCol(default=None)
    ide = StringCol(default=None)
    question = ForeignKey('Ord')

and when I've a Ord instance (called 'q') and I do 'q.orderanswers' I get the following error:
 
sqlobject.dberrors.OperationalError: Unknown column 'ord_id' in 'where clause'

Any idea?

Thanks!!
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Oleg Broytmann | 12 Aug 2008 18:38
X-Face
Picon
Favicon

Re: MultipleJoin

On Tue, Aug 12, 2008 at 06:18:39PM +0200, Luis Javier Peris wrote:
> I'd like to know if there is a restriction using inheritance about
> multiplejoin function because I'm getting an error and I don't know why.
> I've the following code:
> 
> class Question(InheritableSQLObject):
>     filename = StringCol(default=None, length=255, unique=True)
>     title = StringCol(default=None)
>     default_mark = FloatCol(default=0.0)
>     wording = RelatedJoin('Content')
>     term_questions = RelatedJoin('Term', joinColumn='term',
> otherColumn='question',
>                                  intermediateTable='Term_questions')
>     answers = MultipleJoin('Answer')
>     questionref = MultipleJoin('QuestionRef')
> 
> 
> class Answer(SQLObject):
>     value = StringCol(default=None)
>     calification = FloatCol(default=None)
>     ide = StringCol(default=None)
>     question = ForeignKey('Question')
> 
> 
> class Ord(Question):
>     _inheritable = False
>     shuffle = BoolCol()
>     orderanswers = MultipleJoin('OrderChoice')
> 
> 
> class OrderChoice(SQLObject):
>     value = StringCol(default=None)
>     ord = IntCol(default=None)
>     ide = StringCol(default=None)
>     question = ForeignKey('Ord')
> 
> and when I've a Ord instance (called 'q') and I do 'q.orderanswers' I get
> the following error:
> 
> sqlobject.dberrors.OperationalError: Unknown column 'ord_id' in 'where
> clause'

   It has nothing with inheritance. I simplified the program (you have to
do this yourself) to:

class Ord(SQLObject):
    orderanswers = MultipleJoin('OrderChoice')

class OrderChoice(SQLObject):
    question = ForeignKey('Ord')

Ord.createTable()
OrderChoice.createTable()

ord = Ord()
print ord.orderanswers

   and got the same error. Well, with this simple program and SQL it
generates it easy to see where is the problem:

 1/QueryR  :  CREATE TABLE ord (
    id INTEGER PRIMARY KEY
)
 2/QueryR  :  CREATE TABLE order_choice (
    id INTEGER PRIMARY KEY,
    question_id INT CONSTRAINT question_id_exists REFERENCES ord(id) 
)
 3/QueryR  :  INSERT INTO ord VALUES (NULL)
 4/QueryR  :  SELECT NULL FROM ord WHERE ((ord.id) = (1))
 5/QueryR  :  SELECT id FROM order_choice WHERE ord_id = (1)

Oleg.
--

-- 
     Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Luis Javier Peris | 12 Aug 2008 20:08
Picon

Re: MultipleJoin

Ok, it's solved using joinColumn.

Thanks.

Greetings.

2008/8/12 Oleg Broytmann <phd <at> phd.pp.ru>
On Tue, Aug 12, 2008 at 06:18:39PM +0200, Luis Javier Peris wrote:
> I'd like to know if there is a restriction using inheritance about
> multiplejoin function because I'm getting an error and I don't know why.
> I've the following code:
>
> class Question(InheritableSQLObject):
>     filename = StringCol(default=None, length=255, unique=True)
>     title = StringCol(default=None)
>     default_mark = FloatCol(default=0.0)
>     wording = RelatedJoin('Content')
>     term_questions = RelatedJoin('Term', joinColumn='term',
> otherColumn='question',
>                                  intermediateTable='Term_questions')
>     answers = MultipleJoin('Answer')
>     questionref = MultipleJoin('QuestionRef')
>
>
> class Answer(SQLObject):
>     value = StringCol(default=None)
>     calification = FloatCol(default=None)
>     ide = StringCol(default=None)
>     question = ForeignKey('Question')
>
>
> class Ord(Question):
>     _inheritable = False
>     shuffle = BoolCol()
>     orderanswers = MultipleJoin('OrderChoice')
>
>
> class OrderChoice(SQLObject):
>     value = StringCol(default=None)
>     ord = IntCol(default=None)
>     ide = StringCol(default=None)
>     question = ForeignKey('Ord')
>
> and when I've a Ord instance (called 'q') and I do 'q.orderanswers' I get
> the following error:
>
> sqlobject.dberrors.OperationalError: Unknown column 'ord_id' in 'where
> clause'

  It has nothing with inheritance. I simplified the program (you have to
do this yourself) to:

class Ord(SQLObject):
   orderanswers = MultipleJoin('OrderChoice')

class OrderChoice(SQLObject):
   question = ForeignKey('Ord')

Ord.createTable()
OrderChoice.createTable()

ord = Ord()
print ord.orderanswers

  and got the same error. Well, with this simple program and SQL it
generates it easy to see where is the problem:

 1/QueryR  :  CREATE TABLE ord (
   id INTEGER PRIMARY KEY
)
 2/QueryR  :  CREATE TABLE order_choice (
   id INTEGER PRIMARY KEY,
   question_id INT CONSTRAINT question_id_exists REFERENCES ord(id)
)
 3/QueryR  :  INSERT INTO ord VALUES (NULL)
 4/QueryR  :  SELECT NULL FROM ord WHERE ((ord.id) = (1))
 5/QueryR  :  SELECT id FROM order_choice WHERE ord_id = (1)

Oleg.
--
    Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
          Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Sam's Lists | 17 Aug 2008 12:46
Picon

Having a modified column filled in automagically...

Hi...

I'd like to be able to create a timestamp column called "mtime" in some of my columns.  This would hold a timestamp of when the row was last modified.

But I'd like the timestamp to be updated automatically whenever sqlobject updates anything else in the row.

How can I do this?  I'd like a solution that is easy to add to any table....maybe so easy that the table only has to have a column called mtime and then everything happens without anything extra on my part.

Thanks

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Gmane