steinhaug / sqlbuddy
My personal SQL friend
Requires
- php: ^8.0
- ext-mbstring: *
README
Helper class for making sure SQL inserts and updates are not crashing anything.
Table of Contents
1. Description
A class that handles the data that should be inserted into the database, including some fuzzy logic. The class builds the entire SQL query and makes sure that all data is escaped correctly.
2. Version History
v1.3.6 - Updated 1 november 2024
- Updated readme
v1.3.5 - Updated 29 august 2024
- Removed deprecation notice when value passed was null
v1.3.4 - Updated 22 august 2024
- Deprecation notice, make sure NULL is not passed to the mb_detect_encoding()
v1.3.3 - Updated 16 august 2024
- Bugfix, pseudo logic fix for null values when using string:(int)n
v1.3.2 - Updated 30 april 2024
- Property safehtml set to public. v1.3.1 - Updated 6 des 2023
+ Added unshift()
v1.3.0 - Updated 28 nov 2023
* Oppdatert og klart for PHP 8.1
+ Added time ****
v1.2.0 - Updated 21 des 2021
* Oppdatert og klart for PHP 8.0
v1.1.1 - Updated 20 aug 2021
* rewrote parsing logic, now all parsing will assume: col, val, type, has_null
+ Typical values as NULL and NOW() will automatically get set without quotes, automagically.
v1.0.2 - Updated 2 mai 2021
* Better handling of NULL.
v1.0.1 - Updated 27 mai 2020
+ Any type can be forcefully cut on given length by adding suffix :n. Example: string:128 will be a string cut to 128 characters max.
v1.0.0 - Updated 14 may 2020
3. Usage
3.1 Syntax
$sql->que($k, $v, ?$t, ?$n);
$k = DB Column,
$v = Value,
$t = optional - Variable type, int string float etc.
$n = optional - (bool) has_null. If true evaluates $v as NULL when appropriate
3.2 ->que()
$sql->que( string
$columnName, string
$value, string
$valueType, boolean
$nullable )
columnName
Name of column to insert/ update
value
the value to be inserted
valueType
Optional, default string.
Possible values are str, string, text, email, float, ornull, strornull, int, tinyint, intornull, dec, decimal, date, dateornull, datetime, datetimeornull, raw, boolean, column, col.
nullable
Boolean statment for the value being considered a NULL, in which the insert or update will insert a real mysql NULL.
3.3 Specials
When using 3'rd param as true, 3 params only:
$sql->que($k, $v, true);
Will evaluate as:
$sql->que($k, $v, 'string', true);
3.4 Example
// typical usage
$sql = new sqlbuddy;
$sql->que('first','Kim Stalsberg');
$sql->que('last','Steinhaug');
$sql->que('age','44','int');
echo $sql->build('update','users','id=1');
echo $sql->build('insert','users');
// outputs:
UPDATE `users` SET `first`='Kim', `last`='Steinhaug', `age`=44 WHERE id=1;
INSERT INTO `demo` (`first`, `last`, `age`) VALUES ('Kim', 'Steinhaug', 44)
4. Information
4.1 License
This project is licensed under the terms of the MIT License. Enjoy!
4.2 Feel generous?
Buy me a beer, donate.
4.3 Author
Kim Steinhaug, steinhaug at gmail dot com.
Sosiale lenker: LinkedIn, SoundCloud, Instagram, Youtube, X, Ko-Fi, Github, Gitlab
Generative AI lenker: Udio, Suno, Huggingface
Resurser og hjelpesider: Linktr.ee/steinhaugai, Linktr.ee/stainhaug, pinterest/steinhaug, pinterest/stainhaug