mysql frob package for composed/improvise

4 views
Skip to first unread message

michael munson

unread,
May 27, 2012, 3:06:26 AM5/27/12
to MOO-...@googlegroups.com
First attempt to make a package using Todd's composed is a MySQL frob. Using it goes something like:

;me.tmp = $mysql:new(["hostname"->"localhost","username"->"dude","password"->"blah","database"->"ourdb"])
=> ["active" -> 1, "conn" -> #-11, "options" -> ["auto-json" -> "no", "connect_time" -> 1338101382, "convert-types" -> "yes", "database" -> "ourdb", "hostname" -> "localhost", "json-mode" -> "embedded-types", "last_query_time" -> 1338101382, "port" -> "default", "row-type" -> "key-value-pair", "server_version" -> 50158, "username" -> "dude"], "prototype" -> #186]
[used 183 ticks, 0 seconds.]


;me.tmp:query("INSERT INTO ourtable (intValue,stringValue,floatValue,timeValue,MOOValue) VALUES ('%d', '%s', '%f', %t, '%j')",21,"HELLO THERE", 6.66,time(), {"arbitrary", "moo", ["values"->{"are converted into json"}]})
=> "1 rows modified"

The above eval will be converted into the SQL string:
INSERT INTO ourtable (intValue,stringValue,floatValue,timeValue,MOOValue) VALUES ('21', 'HELLO THERE', '6.66', FROM_UNIXTIME(1338101669), '[\"arbitrary\",\"moo\",{\"values\":[\"are converted into json\"]}]')

The subs in :query() mimic sprintf a little bit. Every string in %s will be escaped using mysql_escape_string(), MOO values in %j will be encoded into json. %d values will be tointed(), %f will be tofloated(), etc

Here is an example query using some arbitrary spatial data I have in my DB:

> ;me.tmp:query("SELECT inside,AsText(point),loc,z from coordinates")
=> 3
[used 61 ticks, 0 seconds.]
> ;me.tmp:next()
=> ["AsText(point)" -> "POINT(25 25)", "inside" -> 0, "loc" -> 298, "z" -> 25.0]
[used 47 ticks, 0 seconds.]
> ;me.tmp:next()
=> ["AsText(point)" -> "POINT(0 0)", "inside" -> 0, "loc" -> 298, "z" -> 0.0]
[used 47 ticks, 0 seconds.]
> ;me.tmp:next()
=> ["AsText(point)" -> "POINT(5 5)", "inside" -> 0, "loc" -> 298, "z" -> 5.0]
[used 47 ticks, 0 seconds.]
> ;me.tmp:next()
=> 0

Note: :query returns the number of rows in the result. If your query can returns multiple results, the number returned is the number of rows in the FIRST result. Just call :next() until you get 0 is a safe bet, or use the 'loop_results' or 'list_results' verb. Loop_results puts everything into one dictionary like:
> ;me.tmp:query("SELECT inside,AsText(point),loc,z from coordinates")
=> 3
[used 61 ticks, 0 seconds.]
> ;me.tmp:loop_results()
=> ["AsText(point)" -> {"POINT(25 25)", "POINT(0 0)", "POINT(5 5)"}, "inside" -> {0, 0, 0}, "loc" -> {298, 298, 298}, "z" -> {25.0, 0.0, 5.0}]
[used 292 ticks, 0 seconds.]

list_results just puts each row into a list.


I dunno if this package will even install, actually. But if it doesn't I'll try to fix it.

Obviously you need to use my fork of the stunt server which has MySQL support to utilize this.
mysqlfrob_0_0_0.json

Todd Sundsted

unread,
May 27, 2012, 5:18:45 PM5/27/12
to MOO Talk
Very nice! I just loaded this up on my build of your server and
successfully hit a mysql database I was doing development on.

I found a few things that you should change/update in the package.

1) Add a "requires" property to the package object and specify
dependencies on the "primitive" package (to get frob support) and the
"lambdacore" package (to get `$command_utils:suspend_if_needed()')
(and the "kernel" package, too). This will ensure users have both
packages installed. Assign the property a value like:

{{"kernel", "1"}, {"primitive", ">= 0.0.2"}, {"lambdacore", ">=
0.0.4"}}

2) Make the `after_install()' verb on the package object have wiz-
perms. This will ensure that it can change the `$frobs' property when
it installs. I had to make this change by hand when the verb failed.

Also, since the package depends on a build of the server with MySQL
support, you might also want to add a `before_install()' verb that
checks to make sure the necessary built-ins are present in the
server. Without the check, the package will still fail to install,
but the verb can give users a more friendly explanation of what they
need to do (e.g. download your version of the server).

I hope that helps!

Todd
>  mysqlfrob_0_0_0.json
> 23KViewDownload

michael munson

unread,
May 28, 2012, 1:24:41 AM5/28/12
to Todd Sundsted, MOO Talk
I made those changes, although I just replaced the suspend_if_needed calls with: (ticks_left() < 4000 || seconds_left() < 2) && (suspend(0));




--
You received this message because you are subscribed to the Google Groups "MOO Talk" group.
To post to this group, send email to MOO-...@googlegroups.com.
To unsubscribe from this group, send email to MOO-talk+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/MOO-talk?hl=en.


mysqlfrob_0_0_1.json

Todd Sundsted

unread,
Jun 4, 2012, 8:13:30 PM6/4/12
to MOO Talk
Michael, sorry for the long delay in responding. Do you want to put
it up on stunt.io?

Todd

On May 28, 1:24 am, michael munson <michael.d.mun...@gmail.com> wrote:
> I made those changes, although I just replaced the suspend_if_needed calls
> with: (ticks_left() < 4000 || seconds_left() < 2) && (suspend(0));
>
>  mysqlfrob_0_0_1.json
> 24KViewDownload

michael munson

unread,
Jun 4, 2012, 8:16:25 PM6/4/12
to Todd Sundsted, MOO Talk
Sure. I have a couple of others that I'd like to release too. Primarily a file access frob, a task scheduler and a simple ANSI color system (with 256 color support.)

Todd Sundsted

unread,
Jun 5, 2012, 10:09:04 PM6/5/12
to MOO Talk
MySQL Frob package is up.

Todd

On Jun 4, 8:16 pm, michael munson <michael.d.mun...@gmail.com> wrote:
> Sure. I have a couple of others that I'd like to release too. Primarily a
> file access frob, a task scheduler and a simple ANSI color system (with 256
> color support.)
>
Reply all
Reply to author
Forward
0 new messages