TOPIC: A rule for the same field in multiple tables
#46
Mauro

A rule for the same field in multiple tables 4 Years, 6 Months ago
In a rule definition, I can access to the fields of a record with the '.' notation, e.g.:

Rule_fieldname
function()
{
.myrecordfield = ...

...

}

In a similar way, Can I access to the tablename of the record in the rule definition context?
Can the rule functions accept some parameters? In this case, how can I pass some arguments to the rule?

My problem is that I would like to define a rule for a counter num field (integer progressive numbers from 1 to inf), but I wouldn't like to create a different rule for each table in wich I have to create this counter. If there were a way to do this, I could create a unique rule for a field 'counter' and then I could use this rule for each table that had to use a counter field.
 
 
#48
Re:A rule for the same field in multiple tables 4 Years, 6 Months ago
Unfortunately, there is no way to get the table name. (Records from queries may have come from multiple tables in the case of a join.)

And there is no way to pass arguments to a rule.

The only way to pass information to a rule is to put the information in the record. For example, you could add:

extend table = "mytable"

Another option is to use the same field name (and therefore the same rule) in multiple tables.

There should probably be a built-in way to get an automatically incrementing field.

Currently in Suneido it is often simpler to use a time stamp instead of a counter. This also has the advantage or recording when the record was created, which can be useful.

Post edited by: andrew, at: 2006/02/15 20:38
 
 
andrew
 
#49
Re:A rule for the same field in multiple tables 4 Years, 6 Months ago
Hi, thank you for your suggestions. Actually I would have preferred a counter that started from 1, but for now I will use this solution:

Rule_mycounterfieldname
function()
{
t=Timestamp()
return Number(t.FormatEn("ddMMyyhhmmss") $ t.Millisecond())
}

In other parts of my application I will use use this field as a number, then I have to convert it from date to number.
If I don't use the Number(...) function, the number in the database is stored as a binary number or as a string?
 
 
Mauro
 
#50
Re:A rule for the same field in multiple tables 4 Years, 6 Months ago
Without Number() it would be stored as a date in a packed binary representation, not as a string. The only things that are stored as strings are strings.
 
 
andrew
 
#51
Re:A rule for the same field in multiple tables 4 Years, 6 Months ago
andrew wrote:
Without Number() it would be stored as a date in a packed binary representation, not as a string. The only things that are stored as strings are strings.:blink: But the FormatEn() function returns a string, not a date... Moreover Number() is not working with date types. It's for this reason that I have used the FormatEn() function.
 
 
Mauro
 
#52
Re:A rule for the same field in multiple tables 4 Years, 6 Months ago
Sorry, I still don't understand.

All we use is:

Code:

function ()
    {
    return Timestamp()
    }

 
 
andrew
 
#53
Re:A rule for the same field in multiple tables 4 Years, 6 Months ago
You are right, but it's only that I don't like to see a value in date format (with that #xxx.yyy notation) for a database field that represents, for example, a unique code of a product. :P
Maybe I could use a Field_xxx function to display the field in my preferred way, while it's internally stored as a date, right?
 
 
Mauro
 
#54
Re:A rule for the same field in multiple tables 4 Years, 6 Months ago
Yes, you can use a Field_ definition to specify a format. We don't show these keys to the user, so it does not matter too much.
 
 
andrew