There is an example request:

SELECT * FROM table JOIN table2 ON (column1 = arg1) WHERE column2 = arg2; 

Is it possible to make it so that when we create a connection with the base (session), then a global alias is transmitted for the current session? When the mysql server encounters a certain combination of characters, for example, column1 = arg1 , this alias would tell you to replace it with another condition, for example, SELECT id FROM table 124 WHERE name = lastname or any other. In general, by the type of how we set the parameter, for example, SET sql_mode = ANSI_QUOTES; and during the session we work with the specified sql mode .

Is there an easy way to solve this problem?

I will explain why this is necessary. There is an engine in which there is a mass of requests. One of the conditions in the queries is the combination of goods by default. Something like: column1 = arg1 . I am writing a module that will watch the user's session, and set up for it a combination of all products, by default, based on geo-location.
You can of course everywhere in the engine, find the entries column1 = arg1 and replace them with SELECT id FROM table 124 WHERE name = lastname , but this seems to me a bad decision. The task of any normal module is to work without interfering with the engine files. Therefore, there was such an idea.
I would be grateful if someone advises a different way to solve the problem.

  • one
    Try user variables that are with @ . make such a variable and in all sql queries put a condition of the type (@arg is null or column1=@arg) immediately after connecting to the base and establishing (@arg is null or column1=@arg) do simply set @arg=xxx - Mike
  • In general, this is just the task of the engine, to be able to put some global settings into requests. - Mike
  • (@arg is null or column1 = @ arg) this will not work. I have just the task of not touching anything in the engine, and even more so in all requests to put a replacement. The task is just that - zemlia-zemlia
  • one
    Well, so you do it once and forget. and then just set the @arg variable. otherwise it will not work out anyway. there is no replacement. In SQL queries, you initially need to write something to make a universal mechanism - Mike
  • one
    Even as an option, views are made that look like ordinary tables for SQL queries but in fact impose the limitations we need. though in mysql, it seems that apart from variables or other tables there is no place to get information what restrictions to impose - Mike

1 answer 1

Reply from comments from Mike

There is no simple replacement, in SQL queries, you initially need to write something to make a universal mechanism.

The best thing to offer is user variables that are with @ . Create such a variable and in all SQL queries put a condition of the type (@arg is null or column1=@arg) right after connecting to the base and establishing geolocation just set @arg=xxx .

Another option is to create a VIEW view, which for SQL queries seems like a regular table, but in fact imposes the necessary restrictions. The truth is that in MySQL, apart from variables or other tables, there is no place to get information what restrictions should be imposed.