Select querybuilder for cassandra - json

I'm trying to get a few columns from cassandra table with just one column as Json string using datastax queryBuilder. I tried to construct the query in folloing ways and didnt work.
Select.Where selectByKey = QueryBuilder.select().fcall("fromJson", "columnX")
.column("columnX")
.from("keyspaceName", "tableName")
.where(QueryBuilder.eq(key, QueryBuilder.bindMarker()));
Select.Where selectByKey = QueryBuilder.select().fcall("fromJson", "columnX")
.all()
.from("keyspaceName", "tableName")
.where(QueryBuilder.eq(key, QueryBuilder.bindMarker()));
Select.Where selectByKey = QueryBuilder.select().fcall("fromJson", "columnX")
.column("[json]")
.from("keyspaceName", "tableName")
.where(QueryBuilder.eq(key, QueryBuilder.bindMarker()));
Errors are like, no viable alternative at input 'columnX' (SELECT "[json]",fromJson[(]'columnX...)
All the columns are basically text except columnX which has a text in a Json format. I need some solution to get a single column as json, or whole record as json using Query Builder. Thanks

From what I understand from here, you need to use, QueryBuilder.column("columnX") instead of "columnX" as a parameter to fcall.
Hope it helps!

Related

JSON_VALUE not working in old version. is there any other way in SQL to get a value from a JSON

So the main problem I'm having is that I really need to get a value from another table that is in JSON format, obviously I thought, well just use JSON_VALUE to get the property I want... Problem arises when I created in adonis the migration, and surprise. After running the migrations I realise I can't use JSON_VALUE because of the version of adonis server. Is there another way I can get the value?
Well, it was easier than I'd expected. I could, because I wanted to search a String inside the JSON it was as easy as using LIKE. Example
user.settings was a JSON more less like:
'{"platform" : "windows"}'
UPDATE order
SET order.platform = (
SELECT
CASE
WHEN users.settings LIKE '%mac%' THEN 'mac'
WHEN users.settings LIKE '%windows%' THEN 'windows'
ELSE NULL
END
FROM users
WHERE users.id = orders.user_id
)

how to change sequelize inner join output format

I have a problem in Sequelize. I have a query than gets data through an inner join and the response data comes in the following format;
`item.Item_name`
how do i change the output to
`Item_name`
when using raw sql we use as to provide an alias to the query outputs. my question is how do i do that within sequelize. or is there any other method to fix this. or can anyone let me know the method to read the above data format within react-native.Thanks
For that , You need to define attributes at the top level of include :
attributes : [['item.Item_name','Item_name']]
And I think , you are using raw:true in your query , that is why you
are getting name like that , remove that and you will get proper
nested values.

How to query the DB to find an element inside a column that contain a json data from a multi select (Laravel)

This is what I've in my "attrib" db column data:
["last","featured","disabled"]
I try to add in my query something like
->whereRaw('FIND_IN_SET(?,attrib)', ['featured'])
but it not works...
UPDATE
I've resolved with:
$featured = Course::where('attrib', 'like', '%featured%')->get();
But I'm still looking for a better query without the use of "LIKE".
You may use whereIn() in your model
$attrib=["last","featured","disabled"];
->whereIn('attrib',[$attrib])->get();

How to use RETURNING for query.update() with sqlalchemy

I want to specify the return values for a specific update in sqlalchemy.
The documentation of the underlying update statement (sqlalchemy.sql.expression.update) says it accepts a "returning" argument and the docs for the query object state that query.update() accepts a dictionary "update_args" which will be passed as the arguments to the query statement.
Therefore my code looks like this:
session.query(
ItemClass
).update(
{ItemClass.value: value_a},
synchronize_session='fetch',
update_args={
'returning': (ItemClass.id,)
}
)
However, this does not seem to work. It just returns the regular integer.
My question is now: Am I doing something wrong or is this simply not possible with a query object and I need to manually construct statements or write raw sql?
The full solution that worked for me was to use the SQLAlchemy table object directly.
You can get that table object and the columns from your model easily by doing
table = Model.__table__
columns = table.columns
Then with this table object, I can replicate what you did in the question:
from your_settings import db
update_statement = table.update().returning(table.id)\
.where(columns.column_name=value_one)\
.values(column_name='New column name')
result = db.session.execute(update_statement)
tuple_of_results = result.fetchall()
db.session.commit()
The tuple_of_results variable would contain a tuple of the results.
Note that you would have to run db.session.commit() in order to persist the changes to the database as you it is currently running within a transaction.
You could perform an update based on the current value of a column by doing something like:
update_statement = table.update().returning(table.id)\
.where(columns.column_name=value_one)\
.values(like_count=table_columns.like_count+1)
This would increment our numeric like_count column by one.
Hope this was helpful.
Here's a snippet from the SQLAlchemy documentation:
# UPDATE..RETURNING
result = table.update().returning(table.c.col1, table.c.col2).\
where(table.c.name=='foo').values(name='bar')
print result.fetchall()

SqlAlchemy table name reflection using an efficient method

I am using the code below to extract table names on a database at a GET call in a Flask app.:
session = db.session()
qry = session.query(models.BaseTableModel)
results = session.execute(qry)
table_names = []
for row in results:
for column, value in row.items():
#this seems like a bit of a hack
if column == "tables_table_name":
table_names.append(value)
print('{0}: '.format(table_names))
Given that tables in the database may added/deleted regularly, is the code above an efficient and reliable way to get the names of tables in a database?
One obvious optimization is to use row["tables_table_name"] instead of second loop.
Assuming that BaseTableModel is a table, which contains names of all other tables, than you're using the fastest approach to get this data.