Laravel, what's the best practice to define Raw queries? - mysql

I'm crafting a Laravel App that will perform data synchronization from DB to DB. Using MySQL.
I have set up a set of SQL raw statements that will perform some data extraction across tables and using MySQL functions.
I don't have then a 1:1 relationship in order to relate Model to table (Common Eloquent approach)
What's the suggested approach to have things set up in a clean manner?
Store SQL statements as strings in a config file?
Issue virtual model Classes having the raw query inside as class constant?
Any best practice approach in Laravel flavour?

Related

How to create stored procedures on sequelize

I started to learn sequelize with nodejs and I already maked a simple crud and start a new table on my database, but I do not have no clue about stored procedures with sequelize.
I did not find anything on the documentation...
It is possible to creat stored procedures with sequelize?
if so, how?
PS: I am using mysql 5.7.23
You just create them and use them using regular queries. There's no specific wrapper for them since Sequelize is an abstraction layer around different databases, each of which has its own method for defining and executing stored procedures.
In Sequelize it's pretty easy to run raw queries and define these:
sequelize.query("DEFINE ...");

Why did you sql query overcome hibernate query?

I want to know difference between Mysql Query and Hibernate Query. Anybody know give your Suggestion
There is a world of difference between the two. I will try my best to explain it to you.
For writing a MySQL query, you need to think in terms of tables, whereas for a Hibernate query, you need to think in terms of objects.
If you have MySQL queries embedded in your Java code, when you try to switch your database to a different one (say Oracle for example), your queries may not work anymore. This is because different db vendors have different syntax's that they need you to use to accomplish the same goal.
However, in the case of a Hibernate query, you will need to just change the appropriate property in the hibernate configuration file. Since you write queries in terms of objects, Hibernate will automatically generate the appropriate SQL it requires to work with the underlying db.
Also, one another major difference between the two (or between Hibernate and a specific db query language) is the way joins are done. We can use the dot operator in Hibernate Query Language (HQL) to access the properties of a component object without needing to explicitly specify a JOIN clause as we would in a specific query language.
Besides this, there are tons of differences between the two and in no way can all of them be summarized here.

How do I convert RDBMS DDL to Hive DDL script

We've a large and disparate data sources including oracle,db2,mysql. We also need to append few audit columns at the end.
I came across the following Java class org.apache.sqoop.hive.HiveTypes. I am planning to create a simple interpreter that accepts RDBMS DDL and spits out Hive DDL script. Any pointers on how I can achieve this?
Hive QL is more or less similar to normal RDBMS DDL. But there are certain things that it lacks and thats why it does not fully follow ANSI SQL. There is no automated process to convert it.
But you have to try running the SQL queries on Hive and wherever it violates you have to change the query according to hive.
For instance Hive takes only equality condition as join condition which is not the case in RDBMS.
For creating an interpreter yourself you first have to list down the common differences between RDBMS query construct and Hive QL construct. Whenever you encounter a RDBMS construct which according to your list will violate in hive the query gets rebuild as per hive. This replacement logic has to be coded.

Using Yii Querybuilder vs writing actual query

What are the pros and cons of using Yii QueryBuilder (or any other types of query builders) to construct actual queries instead of just writing out the queries?
From guide:
The Query Builder is best used when you need to assemble a SQL statement procedurally, or based on some conditional logic in your application. The main benefits of using the Query Builder include:
It allows building complex SQL statements programmatically.
It automatically quotes table names and column names to prevent conflict with SQL reserved words and special characters.
It also quotes parameter values and uses parameter binding when possible, which helps reduce risk of SQL injection attacks.
It offers certain degree of DB abstraction, which simplifies migration to different DB platforms.

Dynamic Linq - query a schema that is only known at run time?

I know with dynamic linq you can construct expressions dynamically in the same way that you might build and execute a dynamic SQL statement - e.g. a dynamic where clause or a dynamic select list. Is it possible to do this in cases where the schema is not known at compile time?
In a database I'm working with users can define their own entities which causes new tables/columns to be created in the back-end database. At run time I'll know the table & column names I need to work with but I won't know the schema at compile time hence I can't build a DBML to work with up front.
Is there any facility for the dynamic discovery of the schema at run time or is this a case where I need to stick with building dynamic SQL statements?
As far as we understand, you don't know neither schema name nor the full structure of your schema for sure.
In this case it seems that the strongly-typed ExecuteQuery method overload will be an option.
Just write the SQL queries and add the necessary parameters (like table and column names) either using string concatenation or as parameters.