Compare commits

...

15 Commits
dev ... master

Author SHA1 Message Date
Misterzym 62351242d8 Мои изменения в распространяемую библиотеку 7 years ago
colshrapnel e1cbace64e
remove explicit version from composer.json 7 years ago
colshrapnel 4f76f45271
Merge pull request #48 from Habetdin/patch-1 7 years ago
Habetdin 4977ba1e86
Fix small typo 7 years ago
colshrapnel 5fcb267fbf Methods made protected instead of private 8 years ago
colshrapnel ccbf8c3332 Properties made protected instead of private 8 years ago
colshrapnel 5eba26f429 Merge pull request #40 from colshrapnel/binary-test 8 years ago
colshrapnel bdb8cf6ba5 Delete .gitattributes 8 years ago
colshrapnel 1d851923c8 Create README.md 8 years ago
colshrapnel 0afa8ddfc2 Delete README.md 8 years ago
colshrapnel c1cb308188 Create .gitattributes 8 years ago
colshrapnel 4a2ff95fe4 Merge pull request #39 from PeterMortensen/master 8 years ago
Peter Mortensen 26c7cdfeba Update README.md 8 years ago
Peter Mortensen 0059986eec Update README.md 8 years ago
colshrapnel 9360eabc1c Merge pull request #32 from colshrapnel/dev 10 years ago
  1. 39
      README.md
  2. 1
      composer.json
  3. 1168
      safemysql.class.php

39
README.md

@ -1,28 +1,28 @@
SafeMySQL SafeMySQL
========= =========
SafeMySQL is a PHP class for safe and convenient handling of Mysql queries. SafeMySQL is a PHP class for safe and convenient handling of MySQL queries.
- safe because <b>every</b> dynamic query part goes into query via <b>placeholder</b> - Safe because <b>every</b> dynamic query part goes into the query via <b>placeholder</b>
- convenient because it makes application code short and meaningful, without useless repetitions, making it Extra <abbr title="Don't Repeat Yourself">DRY</abbr> - Convenient because it makes application code short and meaningful, without useless repetitions, making it ''extra'' <abbr title="Don't Repeat Yourself">DRY</abbr>
This class is distinguished by three main features This class is distinguished by three main features
- unlike standard libraries, it is using **type-hinted placeholders**, for the **everything** that may be put into query - Unlike standard libraries, it is using **type-hinted placeholders**, for the **everything** that may be put into the query
- unlike standard libraries, it require no repetitive binding, fetching and such, - Unlike standard libraries, it requires no repetitive binding, fetching and such,
thanks to set of helper methods to get the desired result right out of the query thanks to set of helper methods to get the desired result right out of the query
- unlike standard libraries, it can parse placeholders not in the whole query only, but in the arbitary query part, - Unlike standard libraries, it can parse placeholders not in the whole query only, but in the arbitary query part,
thanks to indispensabe **parse()** method, making complex queries as easy and safe as regular ones. thanks to the indispensabe **parse()** method, making complex queries as easy and safe as regular ones.
Yet it is very easy to use. You need to learn only few things: Yet, it is very easy to use. You need to learn only a few things:
1. You have to **always** pass whatever dynamical data into query via *placeholder* 1. You have to **always** pass whatever dynamical data into the query via *placeholder*
2. Each placeholder have to be marked with data type. At the moment there are 6 types: 2. Each placeholder have to be marked with data type. At the moment there are six types:
* ?s ("string") - strings (also ```DATE```, ```FLOAT``` and ```DECIMAL```) * ?s ("string") - strings (also ```DATE```, ```FLOAT``` and ```DECIMAL```)
* ?i ("integer") - the name says it all * ?i ("integer") - the name says it all
* ?n ("name") - identifiers (table and field names) * ?n ("name") - identifiers (table and field names)
* ?a ("array") - complex placeholder for ```IN()``` operator (substituted with string of 'a','b','c' format, without parentesis) * ?a ("array") - complex placeholder for ```IN()``` operator (substituted with string of 'a','b','c' format, without parentesis)
* ?u ("update") - complex placeholder for ```SET``` operator (substituted with string of `field`='value',`field`='value' format) * ?u ("update") - complex placeholder for ```SET``` operator (substituted with string of `field`='value',`field`='value' format)
* ?p ("parsed") - special type placeholder, for inserting already parsed statements without any processing, to avoid double parsing. * ?p ("parsed") - special type placeholder, for inserting already parsed statements without any processing, to avoid double parsing.
3. To get data right out of the query there are helper methods for the most used : 3. To get data right out of the query there are helper methods for the most used:
* query($query,$param1,$param2, ...) - returns mysqli resource. * query($query,$param1,$param2, ...) - returns mysqli resource.
* getOne($query,$param1,$param2, ...) - returns scalar value * getOne($query,$param1,$param2, ...) - returns scalar value
* getRow($query,$param1,$param2, ...) - returns 1-dimensional array, a row * getRow($query,$param1,$param2, ...) - returns 1-dimensional array, a row
@ -30,7 +30,7 @@ Yet it is very easy to use. You need to learn only few things:
* getAll($query,$param1,$param2, ...) - returns 2-dimensional array, an array of rows * getAll($query,$param1,$param2, ...) - returns 2-dimensional array, an array of rows
* getInd($key,$query,$par1,$par2, ...) - returns an indexed 2-dimensional array, an array of rows * getInd($key,$query,$par1,$par2, ...) - returns an indexed 2-dimensional array, an array of rows
* getIndCol($key,$query,$par1,$par2, ...) - returns 1-dimensional array, an indexed column, consists of key => value pairs * getIndCol($key,$query,$par1,$par2, ...) - returns 1-dimensional array, an indexed column, consists of key => value pairs
4. For the whatever complex case always use **parse()** method. And insert already parsed parts via **?p** placeholder 4. For the whatever complex case always use the **parse()** method. And insert
The rest is as usual - just create a regular SQL (with placeholders) and get a result: The rest is as usual - just create a regular SQL (with placeholders) and get a result:
@ -39,16 +39,16 @@ The rest is as usual - just create a regular SQL (with placeholders) and get a r
* ```$data = $db->getAll("SELECT * FROM ?n WHERE mod=?s LIMIT ?i",$table,$mod,$limit);``` * ```$data = $db->getAll("SELECT * FROM ?n WHERE mod=?s LIMIT ?i",$table,$mod,$limit);```
The main feature of this class is a <i>type-hinted placeholders</i>. The main feature of this class is a <i>type-hinted placeholders</i>.
And it's really great step further from just ordinal placeholders used in prepared statements. And it's a really great step further from just ordinal placeholders used in prepared statements.
Simply because <b>dynamical parts of the query aren't limited to just scalar data!</b> Simply because <b>dynamical parts of the query aren't limited to just scalar data!</b>
In the real life we have to add identifiers, arrays for ```IN``` operator, arrays for ```INSERT``` and ```UPDATE``` queries. In the real life we have to add identifiers, arrays for ```IN``` operator, and arrays for ```INSERT``` and ```UPDATE``` queries.
So - we need <b>many</b> different types of data formatting. Thus, we need the way to tell the driver how to format this particular data. So - we need <b>many</b> different types of data formatting. Thus, we need the way to tell the driver how to format this particular data.
Conventional prepared statements use toilsome and repeating bind_* functions. Conventional prepared statements use toilsome and repeating bind_* functions.
But there is a way more sleek and useful way - to set the type along with placeholder itself. It is not something new - well-known ```printf()``` function uses exactly the same mechanism. So, I hesitated not to borrow such a brilliant idea. But there is a way more sleek and useful way - to set the type along with placeholder itself. It is not something new - well-known ```printf()``` function uses exactly the same mechanism. So, I hesitated not to borrow such a brilliant idea.
To implement such a feature, no doubt one have to have their own query parser. No problem, it's not a big deal. But the benefits are innumerable. To implement such a feature, no doubt one have to have their own query parser. No problem, it's not a big deal. But the benefits are innumerable.
Look at all the questions on Stackoverflow where developers trying in vain to bind a field name. Look at all the questions on Stack Overflow where developers are trying in vain to bind a field name.
Voila - with identifier placeholder it is as easy as adding a field value: Voila - with the identifier placeholder it is as easy as adding a field value:
```php ```php
$field = $_POST['field']; $field = $_POST['field'];
@ -62,14 +62,13 @@ Nothing could be easier!
Of course we will have placeholders for the common types - strings and numbers. Of course we will have placeholders for the common types - strings and numbers.
But as we started inventing new placeholders - let's make some more! But as we started inventing new placeholders - let's make some more!
Another trouble in creating prepared queries - arrays going to IN operator. Everyone is trying to do it their own way but the type-hinted placeholder makes it as simple as adding a string: Another trouble in creating prepared queries - arrays going to the IN operator. Everyone is trying to do it their own way, but the type-hinted placeholder makes it as simple as adding a string:
```php ```php
$array = array(1,2,3); $array = array(1,2,3);
$data = $db->query("SELECT * FROM table WHERE id IN (?a)",$array); $data = $db->query("SELECT * FROM table WHERE id IN (?a)",$array);
``` ```
Same goes for such toilsome queries like ```INSERT``` and ```UPDATE```. The same goes for such toilsome queries like ```INSERT``` and ```UPDATE```.
And, of course, we have a set of helper functions to turn type-hinted placeholders into real brilliant, making almost every call to database as simple as 1 or 2 lines of code for all the regular real life tasks.
And, of course, we have a set of helper functions to turn type-hinted placeholders into real brilliant, making almost every call to the database as simple as one or two lines of code for all the regular real life tasks.

1
composer.json

@ -2,7 +2,6 @@
"name": "colshrapnel/safemysql", "name": "colshrapnel/safemysql",
"description": "A real safe and convenient way to handle MySQL queries.", "description": "A real safe and convenient way to handle MySQL queries.",
"type": "library", "type": "library",
"version": "1.0.0",
"keywords": [ "keywords": [
"db", "db",
"mysql" "mysql"

1168
safemysql.class.php

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save