Get the data type of the field attached to an element

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Get the data type of the field attached to an element

Postby ahmad » Tue Aug 31, 2004 3:00 pm

Hi,

I have written a function which forms the search string by looping through all the elements in my find form and surround the field values by % so that the user can perform LIKE searches without entering the %

Code: Select all
  frmObjFind= globals.find_commonScripts('getFrmObjFind');

  frmObjFind.controller.find();
 
   var i;
   for (i=0; i < frmObjFind.elements.length; i++) {
      objName = frmObjFind.elements[i].getName();
      // the name of the element is set exactly same as the field name

      if (frmObjFind[objName] != "" && frmObjFind[objName] != null ) {
         frmObjFind[objName] = "%" +  frmObjFind[objName] + "%";
      }
   
   totRec = frmObjFind.controller.search();
   }


This works great for the text fields. But the problem comes when search is done on an Integer field

Question:
How to get the data provider attached to an element and how to get the data type of that data provider.

If I can get this I can change my function so that it won't add any % for the Integer fields.

Since I want to generalise all the finds in the database into one common function I don't like to do any hard coding like if (field = this & that etc..)

Expecing your kind reply

Thanks
Ahmad
Programmer
Pilot simple software
Hong Kong
ahmad
 
Posts: 139
Joined: Wed Dec 24, 2003 12:01 pm
Location: Hong Kong

Postby david » Wed Sep 01, 2004 1:01 pm

This is my function for your situation. It is called from a loop to set up multiple find requests if necessary. In addition to setting up a "like" request by adding the percent symbol (%), it adds the hash symbol (#) and the not symbol (!) based on whether the user specifies this search as case insensitive and/or an omit:

Code: Select all
var return_value;

var theValue = arguments[0];
var theField = arguments[1];
var theCase = arguments[2];
var theOmit = arguments[3];

//check theValue for operators
var operator = theValue.replace(/\./g,'');
var operator = operator.replace(/\>/,'');
var operator = operator.replace(/\</,'');
var operator = operator.replace(/\=/,'');


//check to see if search string is a number
var aNumber = theValue.replace(/\.\.\./,'');
aNumber = aNumber.replace(/\>/,'');
aNumber = aNumber.replace(/\</,'');
aNumber = aNumber.replace(/\=/,'');
aNumber = utils.stringToNumber(aNumber);

if (aNumber != theValue && aNumber != operator)
{
   if (theValue == operator)
   {
      return_value = "%" + theValue + "%";
      if (!theCase)
      {
         return_value = "#" + return_value;
      }
      if (theOmit)
      {
         return_value = "!" + return_value;
      }

   }
   else
   {
      return_value = theValue;
      if (theOmit)
      {
         return_value = "!" + return_value;
      }
   }

}
else
{
   return_value = theValue;
   if (theOmit)
   {
      return_value = "!" + return_value;
   }
}

return return_value;


- David
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby IT2Be » Wed Sep 01, 2004 2:18 pm

If you want to check for a value you can also use my tools plugin where you have 3 functions: isNumber, isInteger, isTextOnly...
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany

Postby ahmad » Thu Sep 02, 2004 7:39 am

Thanks very much David & ITBE,

I can either use the method by David or use ITBE's plugin to check whether the value is integer or text.

This will work until I don't search a number value in the text field

For eg: if the Item# = A2500 and I search for 2500, my search string will be now considered as a number though it's a text and it won't be surrounded with % and hence no results.

Anyway for the time being I pass an array of field names for Integer & date fields and hence no % added for these fields (hard coding)

It would be nice if Servoy can give us the data provider attached to any element on the form and also provide a function which returns the data type of a field

Cheers
Ahmad
Programmer
Pilot simple software
Hong Kong
ahmad
 
Posts: 139
Joined: Wed Dec 24, 2003 12:01 pm
Location: Hong Kong

Postby IT2Be » Thu Sep 02, 2004 9:40 am

t would be nice if Servoy can give us the data provider attached to any element on the form and also provide a function which returns the data type of a field


As for your first remark Ahmad: an element that can have a dataprovider attached can already return the dataprovider name by using elementname.getDataProviderID.
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany

Postby jcompagner » Thu Sep 02, 2004 11:51 am

guys

Code: Select all
var jstable = databaseManager.getTable(controller.getServerName(), controller.getTableName());
var jscolumn = jstable.getColumn('message_key');
var type = jscolumn.getType();
// see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Types.html
var number =  (type == java.sql.Types.INTEGER || type == java.sql.Types.DECIMAL || type == java.sql.Types.NUMERIC)
application.output(number)
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8833
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Postby bcusick » Fri Oct 22, 2004 9:53 pm

Thanks, Johan!

Here's some code that will put it all together:

Code: Select all
for ( var i = 0 ; i < elements.length ; i++ )
{
   var name = elements[i].getName()
   var text = false
   
   var jstable = databaseManager.getTable(controller.getServerName(), controller.getTableName());
   var jscolumn = jstable.getColumn(name);
   var type = jscolumn.getType();
   // see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Types.html
   var number =  (type == java.sql.Types.INTEGER || type == java.sql.Types.DECIMAL || type == java.sql.Types.NUMERIC)
   var date =  (type == java.sql.Types.DATE || type == java.sql.Types.TIMESTAMP)
   if(!number && !date)
   {
      var text = true
   }

   application.output(name + ": " + text + ", " + number + ", " + date)
}


Hope this helps inspire!

NOTE: This will only work for elements with the NAME property filled in, and you need to name the element the SAME THING as the database column. You can do it other ways by having a mapping table, etc - but this is the most straight-forward way.

Bob
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 30 guests

cron