📜 ⬆️ ⬇️

SQL query for PHP (Version 0.2)

image

Made changes to its class implementation for generating SQL queries in comparison with the previous version . However, before writing about them, I wanted to immediately clarify some of the issues that, in my opinion, were not disclosed in the first post:

  1. This is not just another QueryBuilder. Although the syntax looks similar, but in fact it works differently. QueryBuilder is usually executed in Runtime. My code first needs to be compiled and the already compiled version should be launched for execution. In particular, the code from the image will be compiled into the following PHP code:

    if (!isset($__query58)) { $__query58 = new \ML\SQL\Query( '0cfd84f430c39bb567ef7cd28bf36054', array( 'server' => '***', 'database' => '***', 'user' => '***', 'pass' => '***', 'prefix' => 'ms-', 'codepage' => 'utf8' ) ); } $__query58->sql = ' SELECT `profile`.`iduser` AS `profile.iduser`,`user`.`name` AS `user.name` FROM `ms-Users-Profile` AS `profile`LEFT OUTER JOIN `ms-Users-User` AS `user` ON `user`.`id`=`profile`.`iduser` WHERE (`profile`.`net`=\'VK\' OR `profile`.`net`=\'OK\') AND `user`.`sex`=\'M\''; $rows = $__query58->rows(); 

    When using variables as field names or values, they will be inserted into the query through calls to special functions that will eliminate SQL injection.
    ')
  2. For what it is needed: I plan to make my ORM (by itself with blackjack and whores) and PHPSQL will be an abstract layer for working with the database. It is planned to write requests in the form of:

     select(id)->from(Users\User)->where(sex == 'M'); 

    Which will then compile to PHP code with intermediate compilation in PHPSQL.

  3. No, this approach to compiling files will not slow down the work. It is enough to do this once when you first access the file, and then cache the compiled version and continue to work with it. You can also do this once when installing / updating the engine through the console. In this case, it does not matter to us how long the compilation takes, the main thing is that the final file works quickly.

  4. Yes, such an approach can really create problems in debugging. Because due to the generated code, if an error occurs in the code, the line number of the error will correspond to the new compiled file, and not the one in which you typed this code. At the moment I have not yet figured out how to get around this. If you have any ideas, voice them.

  5. Yes, I am aware that "instead of inventing my bicycle, I would rather use something cool, such as Doctrine | Eloquent | another option." But I went my own way because 1) I find it interesting 2) I like it.

  6. Why did not put it on gibhab: because this class uses functions from my other libraries, which in turn require more libraries and in general it all takes 17 mb of source code for which there is no help. In this case, some of the libraries use one / two unpretentious functions. At the moment, I see no reason to refactor in order to bring this functionality into a separate small folder for general access. There will be interest - lay out.

Well, now for the changes:


For those who are interested, the test site for compiling code is located at the same address. If you find any error, write in the comments. So far this is version 0.2, so errors are very likely.

UPD: Apparently, 90% of the commentators mastered only the title and the picture under it, and immediately began to write a comment in the style of “your bike is shit, write to xxx / yyy / zzz”. Although specifically for such paragraph 5 was written.

UPD2: Added commands INSERT, UPDATE, DELETE. Also in the SELECT command added offset () and limit (). So now you can do your ORM.

UPD2.1: Added COUNT method in SELECT. If you specify a variable in it, then the total number of records will be returned to it without taking into account the LIMIT command. The request itself will also be executed. This is for paginators done.

Source: https://habr.com/ru/post/327160/


All Articles