robotoworks / mechanoid

Eclipse plugin providing a set of DSL's for the rapid development of Android apps
58 stars 26 forks source link

current_timestamp #194

Closed hannesa2 closed 10 years ago

hannesa2 commented 11 years ago

I define my time fields like

    create table tab1 (
        _id integer primary key autoincrement,
        created integer default current_timestamp )

but when I read the automatic generated "created" value, I get only '2013' instead of 1378239788

Any idea what's wrong here ?

fluxtah commented 11 years ago

it looks to me like current_timestamp is actually an integer, if you use sqlite studio to:

select current_timestamp

The result is a timestamp like 2013-10-10 13:00:00 or something ilke that, so it looks like there is a cast happening that casts current_timestamp to an integer since your column type is an integer therefore truncating the timestamp.

From the sqlite documentation (http://www.sqlite.org/lang_createtable.html):

There is a load of date time functions in the sqlite docs, one of which might do what you want:

SELECT strftime('%s','now');

Check out the whole list here http://www.sqlite.org/lang_datefunc.html

Just to clarify try this:

create table tab1 (
        _id integer primary key autoincrement,
        created integer default strftime('%s','now') )
hannesa2 commented 11 years ago

I get

no viable alternative at input 'strftime'

maybe 'strftime' should be added to grammar ?

fluxtah commented 11 years ago

Sorry please try this

        create table tab1 (
            _id integer primary key autoincrement,
            created integer default (strftime('%s','now') )
        );
hannesa2 commented 11 years ago

Sorry

    create table ftab1 (
        _id integer primary key autoincrement,
        created integer default (strftime('%s','now')),...

causes

android.database.sqlite.SQLiteException: 
default value of column [created] is not constant (code 1): 

during create table

fluxtah commented 11 years ago

ah wow thanks its possible sqlite does not support functions :(

hannesa2 commented 11 years ago

I tested it with SQLiteStudio, there it works. I've no idea what's wrong in android

fluxtah commented 11 years ago

I tried this:

    migration {
        create table t1 (
            id integer primary key,
            time integer default (strftime('%s', 'now')),
            txt text);
    }

and then this:

        T1.newBuilder().setTxt("a").insert();
        T1.newBuilder().setTxt("b").insert();
        T1.newBuilder().setTxt("c").insert();
        T1.newBuilder().setTxt("d").insert();

I now see the data in the database after pulling it off the device, try that see if it works.

hannesa2 commented 11 years ago

interesting how do you create your table. during runtime, I see this with your table

android.database.sqlite.SQLiteException: default value of column [time] is not constant (code   1): , while compiling: create table t1 ( id integer primary key, time integer default ( strftime (   "%s" , "now" ) ), txt text )

but I don't want to annoy you with this, I can handle it with manual set of values

fluxtah commented 11 years ago

Not sure what version of Android are you running this on?

Sent from Samsung Mobile

-------- Original message -------- From: hannesa2 notifications@github.com Date:
To: robotoworks/mechanoid mechanoid@noreply.github.com Cc: Ian Warwick fluxtah@hotmail.com Subject: Re: [mechanoid] current_timestamp (#194)

interesting how do you create your table. during runtime, I see this with your table

android.database.sqlite.SQLiteException: default value of column [time] is not constant (code 1): , while compiling: create table t1 ( id integer primary key, time integer default ( strftime ( "%s" , "now" ) ), txt text ) — Reply to this email directly or view it on GitHub.

hannesa2 commented 11 years ago

Android 4.1.2 do you need a testapp ?

fluxtah commented 11 years ago

Yes please I cannot replicate this, I was thinking it could be due to sqlite versions but I think this has always been supported.

hannesa2 commented 11 years ago

here its is: https://github.com/hannesa2/mechanoid.issue.194

I found 2 issues

hannesa2 commented 10 years ago

My testapp https://github.com/hannesa2/mechanoid.issue.194 with your recent version 0.2.2.201309200743 still gets this error

default value of column [time] is not constant
Unable to create application com.example.mech194.TestApp: android.database.sqlite.SQLiteException: 
    default value of column [time] is not constant (code 1): , while compiling: create table fp ( _id integer primary key autoincrement, time integer default ( strftime ( "%s" , "now" ) ), rId integer, s_id integer )

and T1Record is not generated

fluxtah commented 10 years ago

@hannesa2 can you try the following and paste me your mechdb source and generated code, I want to check to see if our code generates differently:

mechdb

    migration {
        create table qux (
            time integer default ( strftime ( '%s' , 'now' ) )
        );
    }

generated

        db.execSQL(
            "create table qux ( " +
            "time integer default ( strftime ( \"%s\" , \"now\" ) ) " +
            ") "
        );  
fluxtah commented 10 years ago

@hannesa2 its ok I found the issue now it seems the code generator generates incorrect quotes, it should preserve single quotes but it replaces them with double, but not all the time so its completely inconsistent, this is what it should generate, but it does not:

        db.execSQL(
            "create table t1 ( " +
            "id integer primary key, " +
            "time integer default ( strftime ( '%s' , 'now' ) ), " +
            "txt text " +
            ") "
        );  

Instead it generates this which seems to fail with the sql error as you found:

        db.execSQL(
            "create table t1 ( " +
            "id integer primary key, " +
            "time integer default ( strftime ( \"%s\" , \"now\" ) ), " +
            "txt text " +
            ") "
        );  

I will try look into this to see why its happening.

fluxtah commented 10 years ago

Should be fixed, please try latest version

hannesa2 commented 10 years ago

Thank you very much !

'Timestamp' now working perfect.

Just for your information: The secondary issue "T1Record is not generated" is still alive I updated the testapp, but for me the primary issue about 'timestamp' is solved the other I just found by accident

fluxtah commented 10 years ago

ah cheers will look into that

fluxtah commented 10 years ago

Ok I see now why T1Record is not generated because the table definition does not have an _id :) just change the id to _id and it will work.

hannesa2 commented 10 years ago

thank you for the hint !