Insert a Coldfusion struct into a database - mysql

If I wanted to save a contact form submission to the database, how can I insert the form scope in as the submission? It's been some time since I used Coldfusion.
The contact forms vary depending on what part of the site it was submitted from, so it needs to scale and handle a form with 5 fields or one with 10 fields. I just want to store the data in a blob table.

Most space efficient way and least complicated to turn back into original shape is using serializeJSON. After that, you can use something like key:value|key:value, or XML representation of your struct.

Cfwddx is also an alternative.

I don't know that there is a way to store a native structure into a database, but have you thought about using JSON to represent your object as key-pair values and then parsing it into a native structure after retrieving it from the database?
There are tags/functions out there that will help you with the encoding and decoding into JSON:
cfJSON Tag
CF8 Serialize JSON
CF8 Deserialize JSON

If you can't normalize the form fields into proper table(s), you can try storing them:
in XML (SQL Server supports XML pretty well), or
in JSON (in a plain varchar field), or
ObjectLoad() & ObjectSave() (CF9 only) to store as blob.
IIRC there are ways to get object load/save functionality in pre-CF9 by tapping into Java. http://www.riaforge.org/ or http://cflib.org/ might have it.

Related

Trying to use wildcard character in JSON

I am very unfamiliar with JSON and have been assigned a task to retrieve data from an application using SoapUI with JSON. I am able to access the data, but was wondering if there is a way to use a wildcard character to download all records, instead of being limited by attributes.
My code:
{"__type":"GetPatientsRequest:http:\/\/services.varian.com\/AriaWebConnect\/Link",
"Attributes":null,
"FirstName":{"Value":""},
"IsMultipleNamesRequired":{"Value":null},
"LastName":{"Value":"JACKSON"},
"MatchingCriteria":{"Value":null},
"PatientId1":{"Value":""}
Any help will be greatly appreciated

How to write a dictionary value when calling a function in Power Query/M Language?

I have a function that queries data from an external api (making a power connector). I need one of my inputs for the api to be json text (could be nested json). However from the Power BI UI side, I can only pass in things like numbers, text, lists... I could write it as a JSON encoded string like this
MyLibrary.GetData("{""key"":{""key2"":""value""}}")
But this looks messy. I would like to write it natively like this for example (then convert it to json text in M language).
MyLibrary.GetData({"key":{"key2":"value"}})
How can I do this?
Figured it out. There is a record type like this [key=Value]. Value can be any type.

JSON definition - data structure or data format?

What is more correct? Say that JSON is a data structure or a data format?
It's almost certainly ambiguous and depends on your interpration of the words.
The way I see it:
A datastructure, in conventional Comp Sci / Programming i.e. array, queue, binary tree usually has a specific purpose. Json can be pretty much be used for anything, hence why it's a data-format. But both definitions make sense
In my opinion both is correct.
JSON is also a data format (.json) and also a data strucuture which you can use for instance in Java etc.
But more correct is data strucutre.
JSON (canonically pronounced /ˈdʒeɪsən/ jay-sən;[1] sometimes
JavaScript Object Notation) is an open-standard format that uses
human-readable text to transmit data objects consisting of
attribute–value pairs. It is the most common data format used for
asynchronous browser/server communication (AJAJ), largely replacing
XML which is used by AJAX.
Source: Wikipedia

Working with Form Arrays in ColdFusion?

I have no idea how to handle this in ColdFusion 9, I have a form being submitted (POST) with element checkboxes, called items[].
When I do a <cfdump var="#form#" /> no-problem, I get all the items shown with the proper names like items[] eg:
struct
ITEMS[] 13,14
FIELDNAMES ITEMS[]
however doing a <cfdump var="#form.items[]#" /> results in an error. How do I access the CF9 field values? Somehow loop through it?
I cannot seem to do anything with the array to get the id's out of it? Thoughts? I'm kind of stumped and ColdFusion isn't the easiest language to find examples / references on the net. ;)
Is there a correct way to deal with this? I need to get the ID's out of there so I can reference what lines were checked in the form, so I can follow up with an action.
Thanks!
There's no Form Array’s in ColdFusion. Having '[]' at the end doesn't make it an array. You can access the checkbox values from form scope like this:
FORM["ITEMS[]"]
Dot notation doesn't work 'cause of the '[]'. See: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fb2.html
Values from checkboxes are just comma separated values, which is a List in ColdFusion
To loop through it, use cfloop list=:
<cfoutput>
<cfloop index="i" list="#FORM['ITEMS[]']#">
#i#
</cfloop>
</cfoutput>
To convert a list to array, use ListToArray(). There are list functions like listGetAt(), but if you're doing lots of random access, it'd be smarter to convert the list into an array first.
Thoughts, I'm kindof stumped and
coldfusion isn't the easiest language
to find examples / references on the
net ;)
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/index.html
http://learncf.com/tutorials
http://www.easycfm.com/
http://www.carehart.org/ugtv/
I can highly recommend Brian Kotek's "Form Utils" for cases such as this: http://www.briankotek.com/blog/index.cfm/2007/9/4/Implicit-Creation-of-Arrays-and-Structures-from-Form-Fields
I use this in every app I build, because working with arrays and structs on the form submission side is much more preferable to working with lists, imo.
See also the second answer here. It describes how to retrieve values from a field with multiple instances on a form as an array. I have to say though, I've been working in CFML for many years, and I've yet to do this myself, or see it done in any app I've worked on. I think that's just because avoiding commas is very much simpler, but if you can't or don't want to work around it that way, it is possible.
Also, note that in an ajax world, if you json encode the entire body of a post request, rather than individual form fields, it can be any arbitrary data structure, retrievable easily on the server. The snippet below shows how to get to it from ColdFusion. I'm not certain about other languages, but it's almost certainly possible.
To send a post like that using jQuery, JSON.stringify your data before passing it to jQuery, as noted here and here.
If you're building your own ajax request, the punchline would be:
xhr.send(JSON.stringify(data));
To access that data on the server side, this ColdFusion example looks first for that kind json-encoded post body, then a post with json data in the form field 'input', then in a url field with that same name. In all cases, the resulting data gets deserialized and assigned to the local var 'input', which you can then put in request scope, 'rc', or whatever your code expects.
if (Find('application/json', cgi.content_type))
{
input = ToString(GetHttpRequestData().content);
if (IsJSON(input))
input = DeserializeJSON(input);
}
else if (StructKeyExists(form, 'input') and IsJSON(form.input))
input = DeserializeJSON(form.input);
else if (StructKeyExists(url, 'input') and IsJSON(url.input))
input = DeserializeJSON(url.input);
With you list that are Id's it works fine, but if you have an array with comma's in then you're stuck.
In that case you can use the Java method getParameterValues.
<cfdump var="#getPageContext().getRequest().getParameterValues('ITEMS')#">
This will give you an standard CF array which you can use.
For ColdFusion 10+, if you use the sameformfieldsasarray setting in your Application.cfc like this:
component {
this.name = "testingzone2c";
this.sameformfieldsasarray=true;
}
You will get an actual array of form fields with the same name.
ColdFusion 10 Missing Feature - Form Fields and Arrays
I suggest that you remove the "[]" from the name since it disallows dot notation as mentioned in another answer.. When more than one form element contains the same name attribute, the browser will concatenate all of the values into a comma delimited string when submitting the form. Fortunately, ColdFusion has many functions which treat a delimited string as a list. You can use <cfloop> along with these functions to consume the list.

rails mysql BLOB type - to_xml returns binary, would like to force to string

I am running RoR, Ruby 1.8.6/Rails 2.3.2.
I am persisting a String within a model object to MySQL to a Blob field. For a number of reasons, I have to use MySQL Blob data type instead of a Text data type, yet I want Rails to treat the field as a regular String (instead of a binary String - which is what I think it is doing?).
How can I force this? Everything works well in the display of the pages, the main issue is XML that is being produced.
A specific example is a "description" field of type Blob in MySQL displays perfectly in the HTML ERB, but when using the to_xml method on that model object the resulting XML is:
<description type="binary" encoding="base64">
VGhpcyB0d28tc2NyZWVuIGZvcm0gYWxsb3dzIGJ1c2luZXNzIGN1c3RvbWVy
cyB0byByYXRlIHRoZWlyIGxldmVsIG9mIHNhdGlzZmFjdGlvbiBhbmQgcmVx
dWVzdCB0byBzcGVhayB3aXRoIG1hbmFnZW1lbnQuIEl0IGlzIGVzcGVjaWFs
bHkgZGVzaWduZWQgZm9yIHRoZSBDb21taXNzaW9uIG9uIFZvY2F0aW9uYWwg
UmVoYWJpbGl0YXRpb24u
</description>
I just want it to appear be formatted as a normal string field like:
<description>test description</description>
What are the best ways to force this? I've been searching and reading through docs, but haven't been finding any helpful suggestions.
I appreciate any and all help,
thank you
I ended up monkey patching the XmlSerializer to treat binary data as text. I will never have to send binary data down in pure binary form via XML, so this was a valid option - not perfect, but in doing research couldn't find any way to override the default column settings.