Prototypes / QuerySim in DataMgr

I got a call yesterday from a friend of mine who is just starting to use DataMgr. He asked me how to use the QuerySim feature of DataMgr which made me realize that I had never written a good explanation of how to use it.

First of all, it is important to understand that DataMgr can use Database Introspection or Database Definition. If you don't know when you are using, then you are probably using introspection.

DataMgr's simulated database (how it imitates QuerySim) requires database definition (after all, it doesn't have a real database to introspect). This means that you must pass in to DataMgr's loadXml() method an XML string to tell DataMgr the structure of your database.

If you have already designed your database structure in a database you won't have to hand-type the XML to define that structure. Just ask DataMgr for the XML.

To do that, first create an instance of DataMgr to point to your existing database (in this case a datasource of "testdb" pointing to a SQL Server database).

<cfset DataMgrDB = CreateObject("component","DataMgr").init("testdb","MSSQL")>

Now you can ask DataMgr what XML it would use to define this database structure:

<cfset DB_XML = DataMgrDB.getXml()>

Save that XML to a file for later use.

<cffile action="WRITE" file="#ExpandPath('db.xml')#" output="#DB_XML#">

This XML can now be passed in to the instance of DataMgr that is using the simulated database.

<cfset Application.DataMgr = CreateObject("component","DataMgr").init("","Sim")>

<cffile action="READ" file="#ExpandPath('db.xml')#" variable="DatabaseXML">

<cfset Application.DataMgr.loadXml(DatabaseXML,true,true)>

Now the Application.DataMgr instance (which is currently using the simulated databas, but which will use the real database when you want the application to start working) knows the structure of your database.

The preceding steps only have to be done once for your application. Now that you have done that, it is time to created a simulated query against a table.

If you have a table named "products", this code would produce a simulated query against that table with all of the fields (and correct data types) from the table.

<cfset qProducts = Application.DataMgr.getRecords("products")>

By default, the queries will have 50 rows of simulated data. If you want a different number, edit the "table" element for that table in your XML and add a "simrows" attribute with the number of rows of simulated data you want for that table.

Hopefully this provide a good introduction to simulated queries with DataMgr. If you have any questions, just let me know.

I also have an Events demonstration that show DataMgr at work. It randomly cycles between different databases (every hour or two). If you can catch it running on the simulated database, it makes a good example of the results of the simulated database (see the "Database" line near the top of the page).

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Hey Steve,

Funny, I just wrote a blog entry about this exact thing except using the sim DB as production db. Just idea right now, but I think it has some merit.

http://steve.coldfusionjournal.com/prototypes_quer...
# Posted By Joshua | 5/28/07 8:08 PM
Joshua,

I would love to read you entry. Unfortunately, you accidently linked back to this entry. Mind letting me know the URL for yours?
# Posted By Steve Bryant | 5/28/07 8:55 PM
I found Joshua's entry.

Pretty neat stuff!:
http://www.usefulconcept.com/index.cfm/2007/5/28/a...
# Posted By Steve Bryant | 5/29/07 5:05 PM
Hi Steve,

Just downloaded DataMgr today to help with some prototyping that I'm doing. It's great that it can randomly generate query results for you but is there anyway I can customise those results? An example being that I was to return the default option to show in a drop-down where the values need to be strings.

Also how are you getting on with the Oracle release? If I get on well with DataMgr I might look to roll it out as part of this application :-) - We're running Oracle 10g here.

Cheers,
James
# Posted By James Buckingham | 5/27/08 2:50 PM
James,

Great question - worthy of its own blog entry. In the meantime, I will point you to this one:
http://www.bryantwebconsulting.com/blog/index.cfm/...

The same syntax that you use to seed data in a table when you create it, can also be used to define data in the simulated database.

Does that answer your question or did I misunderstand your issue?

As to Oracle. Soon...
# Posted By Steve Bryant | 5/27/08 4:00 PM
I'll give it a shot today, thanks for the prompt reply Steve ;-)

Just to clarify what I mean. I have a drop-down with 3 values:
Approved, Awaiting Approval, Declined

So I want those three values in the dropdown, i can hard code them just now if I have to as it's only a prototype, but I do want to randomly select a default value.

My concern as well is that the client hasn't seen any of this yet so to give them Latin would probably complicate things and distract them from looking at the overall idea.

That's great about Oracle. Look forward to giving it a test run :-).

Cheers,
James
# Posted By James Buckingham | 5/28/08 12:50 AM
Oh I forgot to mention that I don't actually have a database at the moment so I'm purely running this from the db.xml I setup.
# Posted By James Buckingham | 5/28/08 1:28 AM
Just another quickie to say that I followed your blog entry along with Joshua's and I've now got a simulated database with custom data. That was really easy as well :-D

Thanks once again for your help Steve.

Cheers,
James
# Posted By James Buckingham | 5/28/08 4:47 AM
James,

I am glad that it worked for you. :-)

As to Oracle...
http://www.bryantwebconsulting.com/blog/index.cfm/...

Feel free to try it out and let me know if you have any trouble.
# Posted By Steve Bryant | 5/28/08 6:43 AM
Just got that blog entry and it's installed now :-). Will let you know how it goes.

Cheers again,
James
# Posted By James Buckingham | 5/28/08 6:45 AM
Hi Steve,

Me again!!

Is it possible to insert new data records into the XML within DataMgr? I noticed in the DataMgr_Sim.cfc that there is method for this but when I pass a struct of data into it the XML doesn't change, although I don't get an error and a returned PK.

Thanks again for all your help so far in trying to understand this.
Jame
# Posted By James Buckingham | 7/17/08 2:57 AM
James,

It sure is.

http://www.bryantwebconsulting.com/blog/index.cfm/...

Note, however, that DataMgr assumes by default that you only want it to do this when it creates a table. If you want it to add the rows any time they don't already exists, add the permanentRows="true" attribute. Read the blog entry for more details.

Let me know if you have any trouble.
# Posted By Steve Bryant | 7/17/08 5:26 AM
Thanks Steve,

I might not have explained that so well (or I'm not understanding more likely). I've got my app setup to use the Sim version of DataMgr. So everything is XML based at the moment, so no DB.

I've got some test data hard coded into the XML which simply looks like this:

   <data table="OP_PaymentForms" permanentRows="true">
      <row>
         <field name="OPPF_ID" value="1" />
         <field name="OPPF_Name" value="Test Form 1" />
         <field name="OPPF_Created" value="2008-07-17 15:33:00.0" />
         <field name="OPPF_Author" value="James Buckingham" />
         <field name="OPPf_IsLive" value="Y" />
      </row>
   </data>

I'm wondering if there is anyway of adding another row node into that XML by using the insertRecord() method within dataMgr_sim.cfc? So I would pass in a struct for the field nodes and DataMgr would add a new <row>.

What I'm thinking is I could start building up my CFCs and forms now while I wait for DBA resource. When it does become available Iv'e got everything in place and I just need to test that the methods work ok.

Does that make sense?

Cheers again,
James
# Posted By James Buckingham | 7/17/08 5:50 AM
James,

I think I follow you now. Sorry I was a little slow on the uptake.

The simple answer there is that the Sim database really isn't designed to allow any data to be changed. It is really just for prototyping, not making the prototype functional in any way.

I do have a DataMgr_WDDX.cfc in my task list, but it has been there for a while, so who know when I will actually get around to finishing it.

Sorry I don't have a better answer on this one.
# Posted By Steve Bryant | 7/17/08 7:03 AM
No worries Steve. I was going to go the whole hog and ask if I could do CRUD with just XML but to be honest I don't feel comfortable with that. I can see too many security risks with that approach.

As you can probably guess I'm just trying to "paint a picture" of how this all comes together. It's all looking good though :-)

Cheers again,
James
# Posted By James Buckingham | 7/17/08 7:26 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.