6

I have a table with values under a column named:str_condition

values in this column can be : variables.bit_male / application.bit_male / isdefined('session.int_user_id')

The value can be complex as it can be.

I need to use the value of the values in the column.

Currently, what I am doing is

<cfif evaluate(query.str_condition)  eq true>
.....code....
</cfif>

Now, I need to omit the evaluate.

Mike G
  • 4,232
  • 9
  • 40
  • 66

4 Answers4

5

TBH, I'd stick with evaluate() for this: you're leveraging one of the few situations it makes sense. Provided what you have in the DB field is just an expression (no tags), then evaluate() will work fine.

As others have suggested... storing expressions in the DB is not ideal, but I can see how sometimes it might be the best approach, However do reconsider it though, in case you can come up with a different approach entirely (this is situation-specific, so we can't really give you guidance on that).

The only other real option for you would be to write the code from the DB to file then include it, but that would be a worse solution than just using evaluate(), I think.

A lot of people get hung up on the dogma that is evaluate() is bad, without really stopping to think about why that's the case... it's unnecessary for most situations people use it, but it's completely fine in situations in which it is needed (such as yours).

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • Actually, I have a lot of pages which look like reports. These pages have lot of column headers. I am storing the variable names in database to control the number of column headers. For eg: Column 1 need to show according to a value in application (like application.showColumn1). So I store column names in db with a nother field storing the condition. – renjithmohanan May 21 '14 at 06:16
2

This is an edited answer, since I originally misread the question.

In many cases, array notation is your freind

<cfif queryname['fieldname'][rownumber] is true>
code for true

Note that the queryname is not quoted but the fieldname is. If you don't quote the fieldname, ColdFusion will assume it's a variable.

Also pertinent is that if you are storing things in a database, such as code, that you want to select and then execute, you have to select those things, write them to another .cfm file, and then cfinclude that file. That's somewhat inefficient.

In your case, you are storing variable names in your database. If using evaluate is giving you the correct results, anything you change would likely be a change for the worse.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
  • 2
    This is incorrect - it seems you (and the upvoters) haven't read the _"values in this column can be..."_ part; you're answering a different question to what has been asked. – Peter Boughton May 09 '14 at 13:40
0

How many unique combinations exist in the database? And do new values show up without developer interaction?

If it's a reasonable number of possible values that don't change then use a switch statement and write the line of code that handles each possible value.

<cfswitch expression="#query.str_condition#"> <cfcase value="variables.bit_male"> <cfset passed = variables.bit_male> </cfcase> <cfcase value="application.bit_male"> <cfset passed = application.bit_male> </cfcase> <cfcase value="isdefined('session.int_user_id')"> <cfset passed = isdefined('session.int_user_id')> </cfcase> <cfdefaultcase> <cfset passed = false> </cfdefaultcase> </cfswitch> <cfif passed> .....code.... </cfif>

You don't have to hand write all of them, you can use a sql query to generate the repetitive part of the coldfusion code.

SELECT DISTINCT '<cfcase value="' + replace(table.str_condition,'"','""') + '"><cfset passed = ' + table.str_condition + '></cfcase>' as cfml FROM table ORDER BY len(str_condition), str_condition

efreed
  • 991
  • 11
  • 18
0

If I am reading this correctly you are not just storing variable names in the database but actual snippets of code such as [ isDefined(session.it_user_id) ].

If this is what you are doing then you need to stop and rethink what you are trying to achieve. Storing code in your database and using evaluate to execute it is an incredibly bad idea.

It sounds to me like you are trying to create a generic code block that you can copy paste in multiple places and just set your conditional logic in the db.

The short answer is not to find a way around using evaluate but to stop storing code in your database full stop.

  • Actually, I have a lot of pages which look like reports. These pages have lot of column headers. I am storing the variable names in database to control the number of column headers. For eg: Column 1 need to show according to a value in application (like application.showColumn1). So I store column names in db with another field storing the condition. To change the method is not feasible right now as I have been doing this for a long time and there are lot of pages like that. – renjithmohanan May 21 '14 at 06:19