I'm working on my own database management system, developped in PHP, and I've chosen the same syntax as the MySQL queries for my own queries.
I'd like to know if there was a tool to check that a MySQL query is valid, without having to connect to a real MySQL database.
Does someone have a way to do it ? I've though about using some regular expressions but I'm not sure this is the easiest (and fastest) way to do this.
There's a few good PHP SQL parsers that break down the query into structured arrays.
You could run the code through the parser and see if it breaks to determine whether it's valid syntax.
http://code.google.com/p/php-sql-parser/
and
http://pear.php.net/package/SQL_Parser
are 2 I have used in the past.
set #s := 'your sql script';
prepare stm1 from #s;
a tool like that can't ensure tables or columns exist. the tool needs database schema for checking errors. I use heidisql and If I had more money ı would buy right to have more than one database on mysql server and I would execute the "create database" or copy database code and test on the second database which doesn't have any values, or prefereably, with values.
Related
Let me explain.
I used a large SELECT sentence with multiple joins inside my PHP code. It's big because some joins depends on some values from first tables (something like if table1.column_a=1 then alias_b=table2.column_a, if table1.column_a=2 then alias_b=table3.column_b, ...) and because I send condition from my html form which can be changed dynamically (sometimes it could just have some_date>='2017/09/01', but it could turn into something more complex like some_date>='2017/09/01' and (name like J%on not sec_name='Doe') ...").
I received the instruction of make a stored procedure for this query and for more time i take analyze this i can't find some way for make this query dynamic without putting it inside of string into the stored procedure and receiving the WHERE condition as stored procedure parametter (in the stored procedure i have somthing like SET #sql = CONCAT('SELECT ... JOIN ... JOIN... WHERE ',param_condition); PREPARE stmt FROM #sql;..." )
My question is... is this the same as sending the sql from php?
Yes, preparing and executing a statement from a stored procedure is pretty much the same as sending the query from PHP. The only difference is whether the work of concatenating all the pieces is done in the PHP code running on the client, or the MySQL server.
If the contents of the query being constructed is dependent on other table data, it may be better to do it in a stored procedure, so you don't need multiple round trips between the database and PHP to get all the information.
But if it's only dependent on parameters already available in PHP, it's probably best to construct the query in PHP, if only because (IMHO) the PHP syntax is likely to be easier to understand. The performance difference should be minor -- string concatenation is simple for both PHP and MySQL. But I generally think that anything that isn't dependent on SQL data should not be done in the database server.
Lets say I have a general DB class with a query method that is used all over my source code and I want to find out table names that are used in my mysql queries.
The limitation is that I'm using MySql 5.5.36.
Also lets assume that we are talking about millions of tables that I'm using and that using the mysql information schema is not going to happen.
What I would like to know is there an easy way to get table names used?
Explain is obviously good for SELECT statments but since its MySql 5.5.36 I can't use it on replace,update,insert etc.
PDOStatement::getColumnMeta might help us with getting a table name, but it won't work with the queries that return result set.
Some kind of regexp for this might might be possible but I very much doubt that is a good solution for this, my queries are big have multiple JOINS etc. the regexp would be very complicated and probably fail fair percentage of time.
Any other ideas?
Right now I do some project to make normal MySQL syntax in PHP can work with MongoDB.
I already done in some simple syntax(SELECT, CREATE) by create my own PHP library for user to pass their MySQL syntax to my function(Ex. MongoQuery("SELECT User FROM A=1")), it will parser MySQL to Mongo Syntax and also query data from MongoDB.
But my question is, have any possible ways to make php can capture SQL syntax real-time when web is running?
For example, user not need to edit any code in their PHP file, just simple put it in web server folder and my program will handle to change MySQL operation to MongoDB operation and also query data from MongoDB return to normal SQL return value variable. Thank you :)
You can not just change those things in the fly, unless you write a PHP extension.
It is also never a good thing to use a document database like MongoDB as it were a relational database like MySQL. They have totally different features and performance characteristics. For example, MongoDB does not have joins, but instead uses embedded or nested documents; MongoDB does not do transactions either, so you will need to do things in another atomic way; MongoDB also does not do "WHERE A=B", so you need to redo your query thoughts.
In short, transparently converting SQL DQL to MongoDB queries is not possible.
For what I thought would be a common problem, after a medium amount of searching has returned nothing. I have several mysql servers with different tables on them and each is either a master or a read slave. I would like to route all SELECT and other non-table-modifying queries through a read slave and all others INSERT,UPDATE,ALTER,etc. to a master as well as make sure that the right master slave combo actually has the tables I am asking about.
TLDR: based on the tables and the query type (read or write) I want to use a different mysql connection.
In order to do this in a clean way I am making a wrapper function which will examine the query and decide which connection to use. In this function will be the gory details about which tables are on which servers and which are read slaves or masters. All that I will pass in is a string containing the sql statement. I know how I can implement this using regular expressions but I would much prefer to use a built in function if one exists to account for any variability in the SQL syntax or the possibility of statement types I don't know about.
TLDR: is there any way better than regex to get the table name(s) and the query type from a string of an sql query? What is an exhaustive list of the read type sql operations? Does anyone have a good regex already to find the parts of the sql statement for all variations of SQL queries?
I would imagine that many people have faced a similar problem in setting up their systems and at least some of them solved it in a similar way. Help on any of these directions would be greatly appreciated. Thanks for your time.
Use a SQL parser, such as:
http://pear.php.net/package/SQL_Parser
http://sourceforge.net/projects/txtsql/
http://sourceforge.net/projects/osqlp/
http://code.google.com/p/php-sql-parser/
http://www.phpclasses.org/package/5007
http://www.phpclasses.org/package/4916
The way an ORM like Doctrine or Propel would address a situation like this would be to associate each table with a connection and then use a query object to build the resulting SQL.
For example, to build a SELECT query in Doctrine, you might do something like this:
$users = UserTable::getInstance()
->createQuery() // SELECT is the default operation.
->andWhere('is_active = ?', $active)
->fetch();
If you use a class to build the query for you in a piecewise method this way, it is fairly trivial to determine the nature of the operation and the primary table for the query.
I have undertaken a small project which already evolved a current database. The application was written in php and the database was mysql.
I am rewriting the application, yet I still need to maintain the database's structure as well as data. I have received an sql dump file. When I try running it in sql server management studio I receive many errors. I wanted to know what work around is there to convert the sql script from the phpMyAdmin dump file that was created to tsql?
Any Ideas?
phpMyAdmin is a front-end for MySQL databases. Dumping databases can be done in various formats, including SQL script code, but I guess your problem is that you are using SQL Server, and T-SQL is different from MySQL.
EDIT: I see the original poster was aware of that (there was no MySQL tag on the post). My suggestion would be to re-dump the database in CSV format (for example) and to import via bulk insert, for example, for a single table,
CREATE TABLE MySQLData [...]
BULK
INSERT MySQLData
FROM 'c:\mysqldata.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
This should work fine if the database isn't too large and has only few tables.
You do have more problems than making a script run, by the way: Mapping of data types is definitely not easy.
Here is an article about migration MySQL -> SQL Server via the DTS Import/Export wizard, which may well be a good way if your database is large (and you still have access, ie, not only have the dump).
The syntax between Tsql and Mysql is not a million miles off, you could probably rewrite it through trial and error and a series of find and replaces.
A better option would probably be to install mysql and mysqlconnector, and restore the database using the dubp file.
You could then create a Linked Server on the SQL server and do a series of queries like the following:
SELECT *
INTO SQLTableName
FROM OPENQUERY
(LinkedServerName, 'SELECT * FROM MySqlTableName')
MySQL's mysqldump utility can produce somewhat compatible dumps for other systems. For instance, use --compatible=mssql. This option does not guarantee compatibility with other servers, but might prevent most errors, leaving less for you to manually alter.