Yii CSqlDataProvider->getData as Float? - yii

I am trying to retreive a SQL command to use as an input parameter to another component that will convert the values from that query to a JSON enconded array. The problem is that column1 is an string and column2 is an FLOAT field.
Query Example:
SELECT col1, col2
FROM tableA
Consider that col1 is a VARCHAR and col2 is a FLOAT field.
How can I run CSqlDataProvider->getData() and have an array as:
array(
'col1' => 'value1_as_string',
'col2' => value2_as_float_or_number
);
I can manage to run the query, but CSqlDataProvider->getData returns an array of arrays and all values are than converted to STRING as default.
How can I get an array mixed with strings and numbers with CSqlDataProvider->getData()?

If you want to have this value as Float you should convert it by using
(float)$value or floatval($value)
but maybe you should write your own
YourSQLProvider extends CSqlDataProvider {
protected function fetchData(){
$data = parent::fetchData();
//...parse $data ...
return $parsedData;
}
}

Related

Query for retrieve matching json Objects as a list

Assume i have a table called MyTable and this table have a JSON type column called myjson and this column have next value as a json array hold multiple objects, for example like next:
[
{
"budgetType": "CF",
"financeNumber": 1236547,
"budget": 1000000
},
{
"budgetType": "ENVELOPE",
"financeNumber": 1236888,
"budget": 2000000
}
]
So how i can search if the record has any JSON objects inside its JSON array with financeNumber=1236547
Something like this:
SELECT
t.*
FROM
"MyTable",
LATERAL json_to_recordset(myjson) AS t ("budgetType" varchar,
"financeNumber" int,
budget varchar)
WHERE
"financeNumber" = 1236547;
Obviously not tested on your data, but it should provide a starting point.
with a as(
SELECT json_array_elements(myjson)->'financeNumber' as col FROM mytable)
select exists(select from a where col::text = '1236547'::text );
https://www.postgresql.org/docs/current/functions-json.html
json_array_elements return setof json, so you need cast.
Check if a row exists: Fastest check if row exists in PostgreSQL

Construct nested JSON value in sql/json (Oracle Database)

How do I construct a JSON value with a nested JSON value as a serialized string? I tried this:
SQL> select json { 'y' : json_serialize(json('{"hello":"world"}')) } x
from dual;
X
--------------------------------------------------------------------
{"y":{"hello":"world"}}
But the result I want is:
{"y":"{\"hello\":\"world\"}"}
I'm using Oracle Database 20c.
The JSON object constructor recognizes the output of json_serialize as serialized JSON and converts it to a JSON value when constructing the outer object. Use to_clob() instead:
select json { 'y' : to_clob(json('{"hello":"world"}')) } x
from dual;

Returning a tuple column type from slick plain SQL query

In slick 3 with postgres, I'm trying to use a plain sql query with a tuple column return type. My query is something like this:
sql"""
select (column1, column2) as tup from table group by tup;
""".as[((Int, String))]
But at compile time I get the following error:
could not find implicit value for parameter rconv: slick.jdbc.GetResult[((Int, String), String)]
How can I return a tuple column type with a plain sql query?
GetResult[T] is a wrapper for function PositionedResult => T and expects an implicit val with PositionedResult methods such as nextInt, nextString to extract positional typed fields. The following implicit val should address your need:
implicit val getTableResult = GetResult(r => (r.nextInt, r.nextString))
More details can be found in this Slick doc.

Perl - From Database to data structure

I'm querying a table in a database with SQL like this:
Select col1, col2 from table_name
For reference, col2 will be an integer value, and col1 will be the name of an element. E.G.
FOO, 3
BAR, 10
I want a data structure where the values can be addressed like vars->{valueofcol1} should return the value of col2.
So
$vars->FOO
would return 3
Basically I don't know how to return the SQL results into a data structure I can address like this.
You need to fetch reach row and build that hashref yourself.
my $vars; # declare the variable for the hash ref outside the loop
my $sth = $dbh->prepare(q{select col1, col2 from table_name});
$sth->execute;
while ( my $res = $sth->fetchrow_hashref ) { # fetch row by row
$vars->{ $res->{col1} } = $res->{col2}; # build up data structure
}
print $vars->{FOO};
__END__
3
You may want to read up on DBI, especially how to fetch stuff.

How can I use a Sequel SQL function in an insert statement?

I would like to use a SQL function when inserting a new value into a dataset. However, I keep getting TypeError: can't convert Sequel::SQL::Function into String errors. The Sequel docs explain how to select using functions, but not insert.
Ideally I would like to avoid DB.run since I am inserting dynamically and raw SQL is messy and inflexible.
This is what I'm trying to do:
INSERT INTO dataset (col1, col2, col3) VALUES ('something', 3, func(value))
This is what I have in Sequel:
x = 'something'
y = 3
func_value = Sequel.function(:func, value)
DB[:dataset].insert (:col1 => x, :col2 => y, :col3 => func_value)
Is this possible? If so, what am I missing?
Figured it out. Create a Sequel::function object, and then make it the value in your hash.
irb(main):028:0> tbl
=> #<Sequel::MySQL::Dataset: "SELECT * FROM `foo`.`bar`">
irb(main):029:0> uhex = Sequel.function(:unhex,7)
=> #<Sequel::SQL::Function #args=>[7], #f=>:unhex>
irb(main):030:0> tbl.insert_sql( {:field_name => uhex })
=> "INSERT INTO `foo`.`bar` (`field_name`) VALUES (unhex(7))"
Looks like there a few different ways syntactically of calling SQL functions in Sequel:
http://sequel.rubyforge.org/rdoc/files/doc/dataset_filtering_rdoc.html