Proper Syntax for Writing Multiple Fields with odbc_func? - mysql

This is a follow-up to my previous post. Trying to write to the CDR table on hangup. Asterisk 13 seems to prevent using the CDR() in the h extension (though I could in *11).
SO, I will just create a nice ODBC_FUNC function and use that to write my data.
Except I can't make it work.
I want to write multiple fields:
exten => h,n,Set(ODBC_WRITE_TEST(${E})=First\,Second,Third,Fourth)
or
exten => h,n,Set(ODBC_WRITE_TEST(${E})=${cause_name},${cause_name}, ${TrNumber},${uID})
Neither of these work. They both write a blank record to the table so we know what ODBC is working. But the field content does not get passed.
func_odbc.conf:
[WRITE_TEST]
dsn=asterisk
; writesql=INSERT INTO LogAndDebug (debugMessage, debugData, logMessage) VALUES ('test','123','done')
writesql=INSERT INTO LogAndDebug (debugMessage, debugData, logMessage) VALUES ('${SQL_ESC(${ARG1})}','${SQL_ESC(${VAR2})}','${SQL_ESC(${VAR1})}')
The commented line above does write data as expected. The uncommend line writes an empty record.
I have tried it without the SQL_ESC. The fields are varchars...so quoted text seems correct. It looks as if the ARGx and VARx are not being passed.
I have looked for documentation on this in Asterisk docs, and surprise -- NADA

As of asterisk 1.8 func_odbc is like that:
In func_odbc.conf:
[WRITE_TEST]
dsn=asterisk
writesql=INSERT INTO LogAndDebug (debugMessage, debugData, logMessage) VALUES ('${ARG1}','${VAR2}','${VAR3}')
In dialplan:
exten => s,n,NoOp(WRITE_TEST.${ODBC_WRITE_TEST('this','is','test')})

Related

My UPDATE query in mysql perl is not updating. What am I doing wrong?

I am wanting to take data from mysql, display it, have users edit this data and then save it to the database. I have all currently working except the saving to the database part. I have been lead to believe that the UPDATE query in mysql is how you get this to work. I put an UPDATE query in place and had no luck. Has anyone here experienced this issue before? I have read several posts here and on perl monks about this issue and can't seem to find an answer that solves my problem. I will put some of my code below. Thanks!
my $dbh=DBI->connect("dbi:mysql:survey_one", "user", "password", { PrintError =>0, RaiseError => 1, AutoCommit => 1}) or die $DBI::errstr;
my $edit_sql = q{UPDATE new_survey SET question = ? WHERE title= ?};
my $sthe = $dbh->prepare($edit_sql);
$sthe->execute($questionedit, $marathon);
$sthe->finish();
I'd like to note that if I were to SET the question column to a string like 'does this work?' I would have success. It's when I try to use user input $questionedit, which is defined as
$questionedit = param('editquestion'); This is where users can edit the question field.
Thanks!
The following are the four possible outcomes:
Nothing happens because the code isn't executed.
An exception is thrown because an error occurred (and RaiseError => 1 was used). The exception will end up being printed to STDERR unless caught.
->execute returns the string 0E0 (which is true, but numifies to zero) because no rows were updated because the WHERE clause didn't match any rows.
->execute returns a positive number indicating the number of rows modified.
Determine which case is applicable, and you'll know how to move forward.

Asterisk phrase variables within variables?

I have a odd situation where I would like to phrase a variable inside an SQL string. Basically ODBC will return a query with a string, in that string there will be an Asterisk variable and I need that phrased and passed back to SQL. For example (pointless code but showing the example)-
exten => s,n,Set(QUERY=${ODBC_GET_QUERY(${EXTEN})})
The SQL query in func_odbc.conf is SELECT query FROM tablea WHERE number = ${ARG1}
Now QUERY will look like to = ${DIALED}, ${DIALED} being a asterisk variable (I will make it 17005551212 for example) I need that phrased so I end up with -
exten => s,n,Set(ALLOWED=${ODBC_GET_ALLOWED(${QUERY})})
The SQL query in func_odbc.conf would be SELECT allowed FROM tableb WHERE ${ARG1} so the SQL query would resolve to SELECT allowed WHERE to = 17005551212.
Before I dive into this and re-invent the wheel, is it possible or even allowed? I have actually not tried it yet. I know in a Set() statement it will phrase a variable inline, but is there a way to phrase variable that is in a variable when its returned via ODBC? Thanks!
Please read carefully source code.
Func odbc use prepair call. So it will not work for your example just becuase prepair do not allow do that.
In general you can substitute variables. Example 1 WILL work ok.
Workaround - use mysql EXEC.

Function odbc cannot be read - Asterisk

i am working on getting numbers from soft phone and then inserting in mysql db.
all drivers and stuffs is ok. i configure them. i can select /poll all my datas. but i cant insert my new data. my func_odbc.conf likes that;
[ADDX];
dsn=asterisk
writesql = INSERT INTO aktarma (musterino,aktartel) values (${VAL1},${VAL2})
and my extensions.conf is ;
exten=>_X.,n(sqlekle),SET(a=${ODBC_ADDX(${digit},${aktartel})})
i checked my variables ${digit} and ${aktartel} is right it gives error as ;
[Jan 30 05:43:21] ERROR[4601]: pbx.c:3380 ast_func_read: Function ODBC_ADDX cannot be read
-- Executing [XXXXXXXXX#phones:30] Set("SIP/out-0000001a", "a=") in new stack
So what is wrong friends. i cannot find a way to solve this
Thanks a lot.
It's a year late but I'm going to answer this to help anyone coming from search land like I was.
In your func_odbc.conf you are trying to write VAL1 and VAL2 but in fact you want to use ARG1 and ARG2; in the dialplan that follows, VAL1 and VAL2 are not set (they would come after the =)
[ADDX]
dsn=asterisk
writesql = INSERT INTO aktarma (musterino,aktartel) VALUES (${ARG1},${ARG2})
One more note, it's always good practice to escape and quote your SQL input; you never know when some bastard is going to press "A" on his DTMF keypad just to keep you on your toes! In your query use '${SQL_ESC(${ARG1})}':
INSERT INTO aktarma (musterino, aktartel) VALUES ('${SQL_ESC(${ARG1})}', '${SQL_ESC(${ARG2})}')
Moving on to your dialplan: you were trying to read a value from the function which is only a write function. Even if you aren't reading a value, you still need an = in your Set command to avoid errors, but it should be at the end. Also the function doesn't need to be wrapped in ${} for writing.
exten=>_X.,n(sqlekle),Set(ODBC_ADDX(${digit},${aktartel})=)
Regarding ARG vs VAL, here is an example that uses both:
[ADDX]
dsn=asterisk
writesql = INSERT INTO aktarma SET ${ARG1}='${VAL1}', ${ARG2}='${VAL2}'
We use both VAL and ARG; then put this in the dialplan:
exten=>_X.,n(sqlekle),Set(ODBC_ADDX(musterino,aktartel)=${digit},${aktartel})
So ARGx is passed as an argument to the function, while VALx is on the right-hand side of the Set call.
There is little documentation available on how this works; hopefully this helps someone.
You have use write-only functions in left part of assigment.
[PRESENCE]
dsn=mydb
writesql=UPDATE `locationtable` SET `location`=${SQL_ESC(${VAL1})}` WHERE `username`='${SQL_ESC(${ARG1})}'
extensions.conf:
exten => 1234,1,NoOp(Set and read location)
exten => 1234,n,Set(ODBC_PRESENCE(${EXTEN})=office)

Ruby On Rails map 2 database columns to 2D JSON array

I am trying to grab two columns of data out of a database, using Ruby on Rails ActiveRecord calls and put them into a 2D JSON array for passing to the client.
I have it working for one column. Now I need to get it working for 2 columns.
This is what I have so far for the database call:
select("TOTAL").map{|x| x.TOTAL.ceil}
This is what I have for the controller:
#results = JSON.dump({ :totals => PerformanceResults.find_totals })
This gives me something like this:
{"totals" [145,132,863,693,372,74,838,91,18,172,84,90,373,161,160,173,1910,210,513,14,79,21,84,41,2630,0,93,150,2971]}
To get two columns, this is how I'm starting out, but it's not going well:
Database call:
select("TOTAL, time_stamp ").map{|x| x.attributes.slice(:x.TOTAL.ceil, x.time_stamp)}
Its telling me "undefined method `TOTAL' for :x:Symbol", which I understand, but since I'm new to Ruby on Rails and also JSON, I thought I'd ask for some help in doing this...
My goal is to get this passed to the client: {"totals" [['timestamp', data], ['timestamp', data], etc.... ]}
I have solved this on my own using the following for anyone looking for this solution in the future.
select("TOTAL, time_stamp ").map{|x| [x.TOTAL.ceil, x.time_stamp]}
In rails console, to fetch multiple columns, you could also use the following method. Suppose you have a User table and you wish to print the id's and email's of the users, You have to do it as shown below:
User.all.map{|user| "#{user.id},#{user.email}"}
This is an alternative to what was already explained above.

MySQL replace colum names instead of individual "as"

Is there any easier way of writing sql to replace all columns names with "schedule_" to make it easier to work with in PHP.
SELECT * FROM schedules
Array
(
[schedule_id] => 9
[schedule_datetime_from] => 2011-12-22 18:28:00
[schedule_datetime_until] => 2011-12-22 22:28:00
[schedule_capacity] => 89
[schedule_amount] => 9.99
[content_id] => 77
)
At the moment I end up doing:
$stmnt1 = "SELECT s.schedule_id as id, s.schedule_datetime_from as datetime_from, s.schedule_datetime_until as datetime_until, etc FROM schedules s";
There is no other way to do this through SQL -- and really, why would you want to? It's not like you are typing in the query by hand each time.
What you could do is write PHP code that changes the array keys after each row has been read, but that's orders of magnitude worse as a solution. Just go with what you already have.
I agree with Jon, but if you REALLY have to get this done I would recommend using the MySQL command line interface. However, you must also consider indices, UNIQUE constraints, and Foreign Keys. And, if you have any code which is expecting the non-prefixed versions of the columns you will end up breaking it, so be sure your code is updated as well.
Read up on ALTER TABLE CHANGE via MySQL docs, the line you are looking for is: "You can rename a column using a CHANGE old_col_name new_col_name column_definition clause"