JRuby - query result is converted to String instead of Date - ruby-on-rails-4

activerecord 4.2.11.1
activerecord-jdbc-adapter 1.3.25
activerecord-jdbcpostgresql-adapter 1.3.25
jdbc-postgres 9.4.1206
The following method call returns a Date when using ruby 2.3.3 but returns a String when using JRuby-9.1.17.0 with activerecord-jdbcpostgresql-adapter :
2.3.3 :017 > Table.select('now() as date').first.date
Table Load (0.7ms) SELECT now() as date FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> 2019-05-17 03:46:52 UTC
jruby-9.1.17.0 :002 > Table.select('now() as date').first.date
Table Load (2.0ms) SELECT now() as date FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> "2019-05-17 03:48:57.572526+00"
This only happens if the selected attribute does not exist in the database:
2.3.3 :012 > Table.select(:created_at).first.created_at
Table Load (0.6ms) SELECT "tables"."created_at" FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> Wed, 25 Sep 2013 14:26:17 -03 -03:00
jruby-9.1.17.0 :019 > Table.select(:created_at).first.created_at
Table Load (0.8ms) SELECT "tables"."created_at" FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> Wed, 25 Sep 2013 14:26:17 -03 -03:00
This happens with either WebRick or Tomcat 7. This error does not occur with the same version of activerecord-jdbc-adapter if using Active Record 4.0 or 4.1. Upgrading to latest jruby-9.2.7.0 did not help either.

seems like a compatibility bug in AR-JDBC, at this point likely a won't fix as AR 4.2 also isn't supported.
you should try upgrading to 5.x if this is an issue or try resolving the problem and submit a PR ...
as a last resort try setting ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time = true (altough I think its already truewhen running on AR 4.2)

as #kares noted, this look like a bug, also it is related to the configuration ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time. Its seems like if it is set to true the method call below returns a String, otherwise it returns a Time instance:
Locatario.select('now() as date').first.date
I have checked this behaviour with 3 versions of active_record:
Rails 3.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns String
JRuby: returns String
Rails 4.0/4.1
default raw_date_time? == nil
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns Time
Rails 4.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns String
So, it looks like to emulate active_record >= 4.0 the gem activerecord-jdbc-adapter should use raw_date_time = false, because it should return a Time instance with the method call in question. As a workaround I created a file with the following content under initializers:
if RUBY_PLATFORM == "java" && Rails::VERSION::STRING.starts_with?("4.2.")
ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time = false
end

Related

regexp_substr coalesce nulliff

I am trying to achieve extract the version number of an app. Some rows are missing the app version and i wanted to populate that as well.
post_mobileappid
app_version
My someword 13.0.0
13.0.0
MySomeWord 12.0
12.0
I have the following
MAX(cast(coalesce(nullif(regexp_substr(post_mobileappid, 'My Some (\d\.\d*)', 0, 1, 'e'),''),'0') as double precision)) AS app_version
I am only getting 0 in the app_version column.
Can I please ask if there are ways to extract the full version number and populate something if it is NULL.
Thanks

collect data via API from the table "apex_activity_log" in APEX ORACLE

I have one question, I want to have one page view statistics and send them through REST.
here is my request:
select count (*)
from apex_activity_log l
where flow_id = 100
and time_stamp> = sysdate - (1/24/60/60 * 2419200)
and userid is not null
and step_id = 172
;
answer : 867
This query works great in "SQL Commands". But when I use this query in Packages I get 0. Although the answer should be completely different. How do I give Packages access to apex_activity_log. That the correct answer came back to me. Thanks)
My api in Packages :
PROCEDURE post_connect_service (
p_status OUT NUMBER,
p_blob IN BLOB
) IS
v_blob blob := p_blob;
v_clob CLOB;
tv apex_json.t_values;
v_id varchar2(1000);
v_number varchar2(1000);
v_date_last date;
v_count_v_pl int;
v_count_sum_v int;
BEGIN
v_clob := iot_general.blob_to_clob(v_blob);
apex_json.parse(tv,v_clob);
v_id := apex_json.get_varchar2(p_path => 'id', p_values => tv);
select count(*) into v_count_sum_v
from apex_activity_log;
p_status := 200;
apex_json.open_object;
apex_json.write('success', true);
apex_json.write('count_views', v_count_v_pl);
apex_json.write('count_sum_views', v_count_sum_v);
apex_json.close_object;
EXCEPTION
WHEN OTHERS THEN
p_status := 500;
apex_json.open_object;
apex_json.write('success', false);
apex_json.write('message', substr(sqlerrm,1,4000));
apex_json.close_object;
END post_connect_service;
The view apex_activity_log has data for the current apex context. If it is queried outside of an apex context, it will return no rows. Easiest way is to create an apex session before querying it.
koen>SELECT COUNT(*) FROM apex_activity_log;
COUNT(*)
___________
0
koen>DECLARE
2 BEGIN
3 apex_session.create_session (
4 p_app_id => 286,
5 p_page_id => 1,
6 p_username => 'JOHN.DOE');
7 END;
8* /
PL/SQL procedure successfully completed.
koen>SELECT COUNT(*) FROM apex_activity_log;
COUNT(*)
___________
9327
koen>
first, there is a pre-configured REST API for this within ORDS.
https://docs.oracle.com/en/database/oracle/oracle-database/21/dbrst/api-oracle-apex.html
To answer your actual question:
Does your procedure run within the same workspace as application 100 is? Or are you dependent on the APEX_ADMINISTRATOR_ROLE for this to return rows?
if the latter, make sure to create your package or procedure with AUTHID CURRENT_USER to make sure that it ...
a) runs with the privieges of the invoking user
b) have roles (such as the APEX_ADMINISTRATOR_ROLE) enabled during PL/SQL execution.

How to get latest result set based on the timestamp in amazon qldb?

I have many an IonStruct as follows.
{
revenueId: "0dcb7eb6-8cec-4af1-babe-7292618b9c69",
ownerId: "u102john2021",
revenueAddedTime: 2020-06-20T19:31:31.000Z,
}
I want to write a query to select the latest records set within a given year.
for example, suppose I have a set of timestamps like this -
A - 2019-06-20T19:31:31.000Z
B - 2020-06-20T19:31:31.000Z
C - 2020-06-20T19:31:31.000Z
D - 2021-07-20T19:31:31.000Z
E - 2020-09-20T19:31:31.000Z
F - 2020-09-20T19:31:31.000Z
If the selected year is between 2020 and 2021, I want to return records which having the latest timestamp.
in this case. E and F,
I tried many ways like
"SELECT * FROM REVENUES AS r WHERE r.ownerId = ? AND r.revenueAddedTime >= ? AND r.revenueAddedTime < ?"
Can anyone help me here?
Although I have no experience in qldb syntax, it seems to have similar properties to other db syntax in the sense that you can format your timestamps using these doc:
https://docs.aws.amazon.com/qldb/latest/developerguide/ql-functions.timestamp-format.html
https://docs.aws.amazon.com/qldb/latest/developerguide/ql-functions.to_timestamp.html
Once you format the timestamp, you may be able to do the > and < query syntax.

Django - Group by return only value Sum

In Django how to perform the following SQL:
select sum(valor_aprovado) as vla
from convenios
group by ano
order by ano
Desired result:
sum_value |
-------------|
472837446.59|
163438615.51|
448397994.27|
959203234.57|
739555948.32|
I'm using the following syntax, but I just want it to return the VLA field and not the ANO
Convenios.objects.values_list('ano').order_by('ano').annotate(vla=Sum('valor_aprovado')
Result:
[(2002, Decimal('36246385105.92')), (2003, Decimal('163442260.52')), (2004, Decimal('447464292.52')), (2005, Decimal('948880015.70')), (2006, Decimal('737373593.32')), (2007, Decimal('1449818896.88')), (2008, Decimal('1812889287.82')), (2009, Decimal('2306375485.81')), (2010, Decimal('8730479758.56')), (2011, Decimal('1662088139.88')), (2012, Decimal('1886396504.43')), (2013, Decimal('535507602.69')), (2014, Decimal('4279003118.70')), (2015, Decimal('1883240765.95')), (2016, Decimal('1245291830.72')), (2017, Decimal('2161176688.18')), (2018, Decimal('1346548803.43'))]
Can you help me?
Thanks
You can try the following query
Convenios.objects.values_list('ano').order_by('ano').annotate(vla=Sum('valor_aprovado')).values('vla')

Regex for getting the date

The query
SELECT REGEXP_SUBSTR('Outstanding Trade Ticket Report_08 Apr 14.xlsx', '\_(.*)\.') AS FILE_DATE FROM DUAL
gives the OUTPUT:
_08 Apr 14.
Please advise the correct regex to be used for getting the date without the characters.
I can use RTRIM and LTRIM but want to try it using regex.
You can use:
SELECT REGEXP_SUBSTR('Outstanding Trade Ticket Report_08 Apr 14.xlsx', '\_(.*)\.',
1, 1, NULL, 1) from dual
The last argument is used to determine which matched group to return.
Link to Fiddler