Sequelize vs SQL query - sql

in real projects or in industry usually use sequelize or sql query ? I'm still confused, is it better to use orm like sequelize or write sql queries?

Related

How to convert sql query to django orm query?

We are using django and sql, so we have written query in sql , here my request is how can I convert sql query to django query.Please let me know
You can use ModelName.objects.raw() to run SQL queries which will return model Instances.
Refer: https://docs.djangoproject.com/en/2.2/topics/db/sql/
or to execute directly from Database import connections from django.db.
Refer:https://docs.djangoproject.com/en/2.2/topics/db/sql/#executing-custom-sql-directly
Note: Django ORM is secure than normal SQL. If using raw SQL queries read docs on how to make raw SQL queries secure.

SQL queries Is there any website where I get to build tables and test SQL queries?

Is there any website where I get to build tables and test SQL queries?
I have 3 tables and would like to execute some SQL statement but I wanna make sure the queries is correct. I want to see the output.
Thank you for your help ?
You can use this website for testing SQL queries
http://sqlfiddle.com/
you can choose from MySQL, Oracle, Postgre, and MSSQL

is it possible to apply a query in hibernate which uses two databases?

I have two different databases (on same server) and i want to join tables across databases. I am using Hibernate, is it possible to create a query in hibernate which can join two tables in those databases?
Hibernate will create an SQL query for your HQL or Criteria query and this SQL will be sent through jdbc to the database. This means that hibernate does not support for what you are trying to do.
However, you can achieve the same result in some cases. Some databases give you the option to create an alias for a table that resides in a different database. So you will be able to write an SQL query that joins the two tables and execute it on the database.
We are doing that with DB2.
If you can do that, it depends on your database.
I guess, that it would impossible if you have two different databases (example DB2 and MySQL) but if both databases are of the same vendor, then maybe it's achievable.
You should try to find more info in you database server's documentation.

Generate SQL queries with Ruby

I want an easy way to generate SQL queries in Ruby. I know all about ActiveRecord, Sequel and DataMapper. I'm not looking for an ORM but just an easier way to generate SQL statement strings.
I'm using RBHive to run Hive queries and would like an easy way to generate the Hive query statements.
Surely this exists (AR, etc) and I've looked at Arel, which seems promising. But can't figure out how to strip the SQL statement generation pieces off of the popular ORM libraries. Everything requires a connection to a database server.
Currently I just use raw SQL strings, but I want to get away from that as the queries are becoming more and more complex and error-prone.
Any ideas?
You said
Everything requires a connection to a database server.
I think, at least with Sequel you can use a dummy Database:
require 'sequel'
DB = Sequel::Database.new() #-> <Sequel::Database: >
puts DB[:test].sql #-> SELECT * FROM "TEST"
You're right, you're looking for Arel, which is used to build an AST for SQL statements and does not require a database connection.
The syntax is pretty different from what you're used to seeing, though. So be aware.
Read the README here to see if it makes sense to you:
https://github.com/rails/arel

Hibernate complex query

I am trying to execute a query against a MySQL database.
The query is fairly complex it has 5 inner joins, including 1 join to itself and
it returns 3 pieces of information from 2 different tables.
We are using hibernate and till now I have used it for simple queries only.
I have written the sql query and tested it too. I am wondering how to implement this using
hibernate, can I execute plain sql statements with hibernate? If so what do I need, a separate hbm.xml?
If I use hibernate and execute the plain sql query can I still utilize caching later on?
Yes, you can execute plain SQL queries with Hibernate.
No, you don't need a separate hbm.xml mapping file (unless you WANT to separate sql queries from the rest, in which case you can do so). You can map your named SQL query the same way you do with named HQL queries.
Whether you will be able to "utilize caching" depends on what exactly you understand by "caching" and how you're going to map your SQL query; it's impossible to answer without knowing more details.
All that said, you may not need to resort to SQL query; HQL is quite powerful and it may very well be possible (assuming appropriate mappings exist) to write your query as HQL. Can you post relevant mappings / schemas and your SQL query?
I strongly recommend criteria queries over HQL queries. They are much closer to your program code without sacrificing any expression power. They DO however depend on relations to be explicitly mapped, otherwise they get quite complicated.
To speed up development, set property hibernate.show_sql=true, and play with the system in the debugger, using the "reload modified class" and "drop stack frame" features of the IDE+jvm until the SQL emitted looks like the one you've posted.