Getting and Filtering Records in DataMgr

In working with a subcontractor, I realized that I have done a poor job so far in providing a simple example of getting and filtering records with DataMgr. DataMgr makes this task very easy, but it does require a little explanation.

DataMgr essentially provides a ColdFusion API for many basic database interactions. Although it sounds like it, this isn't really meant to be a replacement for SQL. I love SQL and wouldn't want to replace it. In some situations, however, it can become tedious to write the same SQL queries repeatedly. Moreover, DataMgr provides a syntax that works like much of the rest of CFML.

Like SQL, the DataMgr API is declarative  meaning that you describe what you want instead of how you want to achieve it. This is often the first leap in using DataMgr. The initial desire is to get DataMgr to perform a certain SQL function rather than return a specific result.

In order to retrieve records from a table, use DataMgr's getRecords() method.

Here is an example table:

employees

employee_id firstname lastname birthdate
1 Sam Smith 1/2/1967
2 Bob Baker 2/1/1976
3 Sandra Smith 6/7/1921
4 Bill Baker 7/6/1912

To get records from this table, use the following syntax:

<cfset qEmployees = DataMgr.getRecords(tablename="employees")>

The qEmployees variable will now hold a recordset with every row and column from the employees table.

In order to only get employees with a last name of "Smith", first create a structure to indicate that filter:

<cfset sEmployees = StructNew()>
<cfset sEmployees.lastname = "Smith">

Then include the filter:

<cfset qEmployees = DataMgr.getRecords(tablename="employees",data=sEmployees)>

In order to have the recordset only return the employee_id and firstname, use the following code:

<cfset sEmployees = StructNew()>
<cfset sEmployees.lastname = "Smith">
<cfset fields = "employee_id,firstname">
<cfset qEmployees = DataMgr.getRecords(tablename="employees",data=sEmployees,fieldlist=fields)>

That's it!

If you have any questions about filtering data with DataMgr, let me know.

DataMgr is open source and free for any use.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Is there a method for using comparison operators? For example, "where lastname like "sm%"
# Posted By Randy | 1/5/08 1:53 AM
Randy,

Not really - the data argument takes a struct that tests for equality.

That being said, version 2.2 will have two feature that could help you out in that (one that could help you now):

I plan to add (and have working in a local dev copy) a "filters" feature which will allow you to define the operator used for a comparison.

Another feature for 2.2 is an argument in getRecords called "advsql". This will be a structure with keys for the different parts of a SQL statement (SELECT,FROM,WHERE,ORDER BY) that would allow to insert your own SQL into the SQL created by getRecords().

If you really need to use a LIKE operator with DataMgr now, you might try to use this feature as though it exists. It could be that the feature is in 2.1, but left as undocumented.

Good luck!
# Posted By Steve Bryant | 1/5/08 7:21 AM
Steve,

Thanks for responding so quickly! I am very impressed with DataMgr. I have been seriously evaluating various ORM tools for ColdFusion, but it seems like everything has some major drawback that is a deal-killer for me. DataMgr is amazingly simple, yet helpful. So, props to you for coming up with this great design. I think it is going to work for me. But the like feature is a critical one.

Do you know when the 2.2 is going to be available (approximately)? Could I be a beta tester for you?

Also. I noticed in your documentation on 2.1 that it mentioned the "advsql". Would it be possible for you to send a quick syntax example of using that in the getRecords() function?

Thanks again, and great work!
# Posted By Randy | 1/7/08 10:01 AM
Randy,

Thanks for the kind words.

I expect to have a blog entry covering advsql later this week. I also expect to announce a public Alpha release on DataMgr 2.2 soon (hopefully measured in days, not weeks). Given my goals, it may still be some time until it is feature-complete though.

Let me know if you have any more questions or run into any trouble.
# Posted By Steve Bryant | 1/7/08 11:02 AM
Steve,

Just after I sent it I looked into the datamgr.cfc file to see your comments there. Anyway, I was able to make it work using the following code. I assume this is the way that it was intended. Once again, nice design and great code comments.

<cfset advsql = structnew()>
<cfset advsql.where = "email like 'randy%'">
<cfset qUser = Application.appsecurity.datamgr.getrecords(tablename="users", advsql=advsql, fieldlist="username,userid,email")>


Thanks!
# Posted By Randy | 1/7/08 11:07 AM
You got it!
# Posted By Steve Bryant | 1/7/08 5:24 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.