Rails 5 - Link associated tables in query that is joined through another table with includes method - model-associations

I am trying to add an associated table to a query that is joined through another table. For example below, I would like to include facility (though I know it is not valid syntax).
Actual belongs to Encounter and Encounter belongs to Facility. (The Actual table has an encounter_id field, and the Encounters table has a facility_id field.)
Sudo Code:
respond_with(Actual.where(:encounter_id => params[:encounter_id]),
:include => [:encounter, :encounter.facility])

You can do something like:
respond_with(Actual.where(encounter_id: params[:encounter_id]).includes(encounter: :facility))

Related

Print table name on which query is executed

Looking at the following lines of code:
query = "DROP TABLE IF EXISTS my_table"
cur.execute(query)
conn.commit()
# print(table_name)
I'm running the query against multiple tables with various query and I want to return the name of the table and the action executed each time. Is there a way to get some kind of meta data from cur.execute or conn.commit on the action running?
In the example above I'd like to print the table name (my_table) and the action (DROP TABLE). however I want this to be dynamic. If I'm creating a table I want to the name of the table newly created and the action (CREATE TABLE).
Thanks.
Quick and Dirty
tables = ['table_1', 'table_2', 'table_3']
action = 'DROP TABLE'
for table in tables:
cur.execute(f'{str(action)} IF EXISTS {str(table)}')
print(f'ACTION: {action}')
print(f'TABLE: {table}')
conn.commit()
HOWEVER, please do not ever do something like this in anything other than a tiny app that will never leave your computer, and especially not with anything that will accept input from a user.
Bad things will happen.
Dynamically interfacing with databases using OOP is a solved problem, and its not worth reinventing the wheel. Have you considered using an ORM like SQLAlchemy?

Get underlying non-aliased table from aliased table in jOOQ

I have an aliased table
Author a = AUTHOR.as("a");
and I'm trying to access the underlying table AUTHOR from a but cant see how to.
I've tried a.alias.wrapped but that doesn't work for me
The underlying table is not exposed via the Aliased table and so you can't get it.
Instead if trying to get check if the Aliased table is a certain type (which was the original reason I wanted the underlying table) you can do
if (a is Author) {
// Do Something
}

Cassandra List <frozen> data type

I want to display the list of fields in the Cassandra List data type.
If i use the command for a table: "DESC Tablename" It shows all the table structure.
Similarly i would like to know the command which will display list of fields under this List column
I am able to see the list of fields under the List data type.
In Dbeaver if i navigate to Keyspace-->Tables-->User Types-->Double click on the List field name.
Update:
I just realized that you want to see the data inside a list column which is frozen.
This isn't supported in DevCenter and cqlsh as of now.
Reason: They are serialized as single entity.
Hopefully, future versions will support it.
---------- This part is wrong.
When you see a frozen keyword, it means that a UDT (User defined Type) is used.
So to display the inner fields, you can use DESCRIBE TYPE type command.
Hope this helps!

can postgres return the names of tables participating in a query?

I would like to create an express middleware function that examines a sql string (could be either select/insert/update/delete) and extracts the names of the participating tables in the query.
for specific tables I would then have the middleware function add conditions based on application's permission definitions.
I see for select statements I could use views and extract the information from information_schema.view_table_usage. can I extract this info for arbitrary sql ?
basically you can only check return data. BUT: let us assume the following query ...
SELECT function();
function() is a blackbox ... it can touch anything anywhere. so, you can inspect the return data and maybe identify a table but you can certainly not see, what has been used inside the query to actually get to the result.

Dynamically updating the jdbc:inbound-channel-adapter query statement

We are using a jdbc:inbound-channel-adapter with a poller to select from a table. The requirement is to select rows with a timestamp within a specific range. That range must be updated each time the database is polled (so rows are not read twice). Originally I envisioned having a separate table to store a datetime, and use the adapter's update attribute to update this table. This value would then have been used in the SELECT statement defined in the query attribute, as part of the WHERE clause against the main table.
We have now been told that while we will have read access to the main table, we will not be given permission to add anything to the existing schema, or have write permissions. This rules out using a separate table in which to continually update the timestamp used by the main SQL query.
The option of defining in the query attribute some SpEL which would return a dynamically generated SELECT statement will also not work. This is because the SpEL will only be evaluated once when Spring loads.
Is there another way to dynamically update the SQL in the query attribute?
Otherwise, what is the correct strategy for this use case, where the SQL which is used by the adapter must change each time the database is polled?
Thanks
You can add a parameter source to the adapter and use queries like:
"SELECT * from FOO where KEY=:key"
See attribute select-sql-parameter-source.
You can write your own parameter source, but also see ExpressionEvaluatingSqlParameterSourceFactory.

Resources