I cannot get TimeUUIDType with phpcassa - uuid

Noob here.
I have a super column family sorted by timeuuidtype which has a number of entries. I'm trying to perform a simple get function with phpcassa that wont work. I'm trying to return a specific value from a UTF8 sorted column within a TimeUUID sorted SC. The exact code works with a similar SC Family sorted by BytesType.
Here is the info on the scf I'm trying to get from which i previously entered via -cli.
ColumnFamily: testSCF (Super)
Columns sorted by: org.apache.cassandra.db.marshal.TimeUUIDType/org.apache.cassandra.db.marshal.UTF8Type
RowKey: TestKey
=> (super_column=48dd0330-5bd6-11e0-adc5-343960c1b6b8,
(column=test, value=74657374, timestamp=1301603831288000))
=> (super_column=141a69b0-5c6e-11e0-bcce-343960c1b6b8,
(column=new test, value=6e657774657374, timestamp=1301669004440000))
And here is the phpcassa script I'm using to retrieve the data.
<?php
require_once('.../connection.php');
require_once('.../columnfamily.php');
$conn = new Connection('siteRoot');
$scf = 'testSCF';
$key = 'testKey';
$super = '141a69b0-5c6e-11e0-bcce-343960c1b6b8';
$col = 'new test';
$entry = new ColumnFamily($conn, $scf);
$q = ($entry->get($key, $columns=array($super)));
echo $q[$super][$col];
?>
Also if I don't specify the SC like so.
$q = ($entry->get($key));
print_r($q);
It returns:
Array ( [HÝ0[Öà­Å49`Á¶¸] => Array ( [test] => test ) [i°\nà¼Î49`Á¶¸] => Array ( [new test] => newtest ) )
I know part of the issue might have been brought up in How do I insert a row with a TimeUUIDType column in Cassandra?
But it didn't really help me as I presumably have accepted timeuuidtypes.
Thanks for any help guys.

Suppose I didn't try hard enough to begin with. The answer in fact was everything to do with the link.
Appears that the -cli accepted what jbellis in the link describes as 32 byte representation of the timeUUID (141a69b0-5c6e-11e0-bcce-343960c1b6b8) when I inserted it. This confused me.
It works fine when you 'get()' with the "raw" 16 byte form (HÝ0[Öà­Å49`Á¶¸).
Cheers.

Related

What causes DBD::ODBC::st fetchrow_array failed: st_fetch/SQLFetch and how to avoid it?

EDIT1:
I don't actually get an empty array as stated below. Instead, I get an empty response body because of the following exception:
DBD::ODBC::st fetchrow_array failed: st_fetch/SQLFetch (long truncated DBI attribute LongTruncOk not set and/or LongReadLen too small) (SQL-HY000) [state was HY000 now 01004]
Which I can see there are posts about. I will look at those to see if I can fix this on my on. Will edit if not successful.
First let me start by saying, I do not know Perl well at all. This could be a careless error — I hope it is. I am building a hash from an array that is returned from SQL or from JavaScript on the front-end and one of the keys in the hash, "short-desc" needs to have the value which in the code below will be coming from a SQL database.
BFHHOTH 15x24S/S +2 UP-HNDWHL-UNSPOKED-GALV 15"x24" flush escape hatch w/hinge, internal handwheel, T-handle on top steel cover and ring
However with the code (removed unnecessary cases from switch):
#!perl
use Switch;
use DBI;
use JSON;
use CGI qw /param/;
use CGI::Carp qw(fatalsToBrowser);
use IO::Compress::Gzip qw(gzip $GzipError);
use URI::Encode;
use URI::Escape;
my $gzip_ok;
my $accept_encoding = $ENV{HTTP_ACCEPT_ENCODING};
if ( $accept_encoding && $accept_encoding =~ /\bgzip\b/ ) {
# $gzip_ok = 1;
}
print "Content-Type: application/json\n";
if ($gzip_ok) {
print "Content-Encoding: gzip\n";
}
print "\n";
my $action = param('ACTION');
my %jsonData;
my #jsonArray;
my $azDSN = DBI->connect('dbi:ODBC:Driver={SQL Server Native Client 10.0};Server=myServer;Database=myDB;Uid=me;Pwd={myPass};Encrypt=yes;Connection Timeout=30;');
switch ($action) {
case "GETINFO" {
my $paramID = param('ID');
getInfo($paramID);
my $json_text = JSON->new->pretty->utf8->encode( \#jsonArray );
if ($gzip_ok) {
my $zipText;
gzip \$json_text, \$zipText,
or die "gzip failed: $GzipError\n";
print $zipText;
}
else {
print $json_text;
}
}
}
sub getInfo {
my $myID = $_[0];
my $statement = <<"SQL";
SELECT
trefQuoteItemsID,
quote_position,
description,
comments
FROM
myDB.dbo.myTable where tID = $myID;
SQL
my $sti = $azDSN->prepare($statement) or die $statement;
$sti->execute() or die $DBI::errstr;
while ( my #row = $sti->fetchrow_array ) {
my %tempData;
%tempData = (
"tref" => $row[0],
"position" => $row[1],
"short_desc" => $row[2],
"comments" => $row[3]
);
$jsonArray[$count] = {%tempData};
$count++;
}
}
An empty array is returned to me on the front-end.
Oddly, if the string is:
BFHHOTH 15x24S/S +2 UP-HNDWHL-UNSPOKED-
the array contains the correct object.
But empty again if the string is:
BFHHOTH 15x24S/S +2 UP-HNDWHL-UNSPOKED-G
Have also tried with strings:
qwertyuiopasdfghjklzxcvbnm1234567890qwe #length is 39
which lets the hash gets built and:
qwertyuiopasdfghjklzxcvbnm1234567890qwer #length is 40
which will return an empty array so hash doesn't get built.
Are there any Perl gurus who have any suggestions?
From what I can tell, it's related to "long object" columns.
DBD::ODBC says:
You can retrieve a lob in chunks like this:
$sth->bind_col($column, undef, {TreatAsLOB=>1});
while(my $retrieved = $sth->odbc_lob_read($column, \my $data, $length)) {
print "retrieved=$retrieved lob_data=$data\n";
}
NOTE: to retrieve a lob like this you must first bind the lob column specifying BindAsLOB or DBD::ODBC will 1) bind the column as normal and it will be subject to LongReadLen and b) fail odbc_lob_read.
NOTE: Some database engines and ODBC drivers do not allow you to retrieve columns out of order (e.g., MS SQL Server unless you are using cursors). In those cases you must ensure the lob retrieved is the last (or only) column in your select list.
NOTE: You can retrieve only part of a lob but you will probably have to call finish on the statement handle before you do anything else with that statement. When only retrieving part of a large lob you could see a small delay when you call finish as some protocols used by ODBC drivers send the lob down the socket synchronously and there is no way to stop it (this means the ODBC driver needs to read all the lob from the socket even though you never retrieved it all yourself).
NOTE: If your select contains multiple lobs you cannot read part of the first lob, the second lob then return to the first lob. You must read all lobs in order and completely or read part of a lob and then do no further calls to odbc_lob_read.
There's no mention if you can retrieve a lob any other way, so I don't know if you have to do it this way, but at least you know one way. But I believe you can work around the problem by increasing the connection's LongReadLen attribute.
You should be able to set the attribute as follows:
my $dbh = DBI->connect($dsn, $user, $passwd, {
...,
LongReadLen => ...,
});
You should also be able to set the attribute as follows:
$dbh->{LongReadLen} = ...;
Hopefully, someone can give you a better answer.

How can I update mulitple records at once in Ruby on Rails?

I am developing a Ruby on Rails app. In my controller I need to update the table attributes multiple times. I've put this logic in the controller.
def index
if request.post?
#user_new = Bookmark.new(params[:user_new])
tags = #user_new.tags.split(",")
i=0
while i < tags.length
#user_new.update_attributes(:title => #user_new.title, :url => #user_new.url, :tags => i)
i=i+1
end
#check = "hello"
end
end
This iterates over the while loop until the tags array size is reached. And multiple times updating is done with different values inside the table.
This should yield updation of all the records. In a case if array size is 3, there should be 3 records inserted. But it is not happenning. Can anyone tell me how to insert mulitple records using array as the differentiation factor in each row?
Would something like this work:
#user_new = Bookmark.new(params[:user_new])
if #user_new.save!
#user_new.tags.split(",").each do |tag|
tag.update_attributes(:title => #user_new.title, :url => #user_new.url)
end
else
< do something else >>
end
Or you could use .create instead.
Follow the other post from #Thilo to index the tags.
Also, if you run this with ! on update_attributes! do you see any errors.
You might have some validation errors. Try that and check the console / log.

array deccleration

I have written a code in php to connect and insert into a MSSQL database. i don't know much in php because am new to this. i used odbc to connect database.user can enter his details through the form. after submitting the form the details are getting stored into a database.
while inserting rows into a database am not trying to insert duplicate values . for this i have given if conditions.these conditions are able to notice the user cname and name exist in the database if the same name exist. but the else part after these conditions not working i.e rows are not getting inserted. i put everything inside the while loop. how can i correct it?
this is my code written in php
$connect = odbc_connect('ServerDB','sa', 'pwd');//connects database
$query2="select count(*) from company";//this is needer for loop through
$result2=odbc_exec($connect,$query2);
while(odbc_fetch_row($result2));
{
$count=odbc_result($result2,1);
echo "</br>","$count";
}
$query1="select * from company";
$result1 = odbc_exec($connect, $query1);
# fetch the data from the database
$index=0;
while(odbc_fetch_row($result1))
{
$compar[$count] = odbc_result($result1, 1);
$namearray[$count] = odbc_result($result1, 2);
if($compar[$count]==$_POST['cname'])
{
echo "<script> alert(\"cname Exists\") </script>";
}
else if($namearray[$count]==$_POST['name'])
{
echo "<script> alert(\"Name Exists\") </script>";
}
else {
$query=("INSERT INTO company(cname,name) VALUES ('$_POST[cname]','$_POST[name]') ");
$result = odbc_exec($connect, $query);
echo "<script> alert(\"Row Inserted\") </script>";
} }
You generally don't allocate memory in PHP. The PHP interpreter handles it for you. Just go ahead and assign elements to your array and all the memory allocation is taken care of for you.
$index = 0;
Then you do $index++ until the last index is reached.
So index further on stays e.g. 12
So anywhere you can only access the index [12]
In PHP arrays are implemented as what would be a HashMap in Java. It will be sized automatically depending on what you put into it, and there is no way you can give an initial capacity.

cakephp check multiple tables

I am getting a list of Rate information from the database with conditions. However I wish to add another condition of minimum Stay if the date range has a date in it and rateID form another table called minimum stay. The Rate table already has a min stay but on certain dates I want to over ride this if the date falls within the date range I pass.
I am new to cakephp so I am unsure how to check the minStay table for dates in the date range. Then get the largest minStay and the add it to the condition.
Default minStay in Rate table is 1
Here is the data in the minStay table:
date:31-10-2011 rateID:21 minStay:2
date:1-11-2011 rateID:21 minStay:3
Results: If date range is 31-10-2011 to 2-11-2011 / 2 nights then no results. If 3 nights or more then result.
I hope I have explaind that correctly.
Variable containing the date range is $todays
$conditions = array(
'Rate.enabled'=>1,'Rate.is_corporate'=>$is_corporate, 'Rate.minimum_stay <='=>$days,
'Rate.valid_from < '=>$date_start,'Rate.valid_to >'=>$date_end,
'OR'=>array('Rate.maximum_stay'=>0,'Rate.maximum_stay >='=>$days)
);
$order = 'Rate.list_no';
$this->Rate->contain('Room.id','Room.title','Room.max_adults','Room.max_children');
$rates = $this->Rate->find('all',array('conditions'=>$conditions,'order'=>$order));
I think i understood what you want to do, if not please comment.
To do that you need to do somthing like this (assuming you have something like has many MinStay)
$conditions = array(
'Rate.enabled'=>1,'Rate.is_corporate'=>$is_corporate, 'Rate.minimum_stay <='=>$days,
'Rate.valid_from < '=>$date_start,'Rate.valid_to >'=>$date_end,
'OR'=>array('Rate.maximum_stay'=>0,'Rate.maximum_stay >='=>$days),
'AND' => array('OR'=> array('minStay.date BETWEEN ? AND ?' => array($date_start,$date_end)),
'minStay.minimum_stay <' =>$days )
);
$order = 'Rate.list_no';
$fields = array ('Room.id','Room.title','Room.max_adults','Room.max_children');
$rates = $this->Rate->find('all',array('conditions'=>$conditions,'order'=>$order, 'fields'=> $fields));
I think that will do it, hope it works for you

With cakePHP, can I increment the value of a column without reading it's value first?

I'd like to increment the value of a column in cakePHP.
Is there a way to have cakePHP write this?
UPDATE `gigs` SET `visits` = visits+1 WHERE `gigs`.`id` = 1
I tried this:
function addVisit($id){
$this->id = $id;
$this->saveField('visits', 'visits+1');
}
but cakePHP adds quotes around visits+1.
UPDATE `gigs` SET `visits` = 'visits+1' WHERE `gigs`.`id` = 1
I tried double quotes, results the same.
$this->updateAll(array(), array('Model.field + 1'))
or
$this->updateAll(null, array('Model.field + 1'))
one of them should work, saveField does not allow sql fragments, updateAll does

Resources