Skip to content

Commit d720000

Browse files
Mark Horningerspam-n-eggs
authored andcommitted
Additional methods (#29)
* Fixed Code of Conduct. * Refactoring tests to support more. * #22 Added support for: date_format, LPAD, mod, minute, format, CONVERT_TZ, TIMEDIFF, UNIX_TIMESTAMP, weekday, inet_ntoa, and date_add with select intervals * #22 Added DIV Operator * #22 Added a Test for an edge case from @DrLongGhost * #16 Cleaned up composer.json
1 parent 73c8ba3 commit d720000

21 files changed

+846
-105
lines changed
File renamed without changes.

README.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![Build Status](https://travis-ci.org/spam-n-eggs/laravel-mysqlite.svg?branch=master)](https://travis-ci.org/spam-n-eggs/laravel-mysqlite)
33
[![Coverage Status](https://coveralls.io/repos/github/spam-n-eggs/laravel-mysqlite/badge.svg?branch=master)](https://coveralls.io/github/spam-n-eggs/laravel-mysqlite?branch=master)
44
[![StyleCI](https://github.styleci.io/repos/167069269/shield?branch=master)](https://github.styleci.io/repos/167069269)
5+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/spam-n-eggs/laravel-mysqlite/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/spam-n-eggs/laravel-mysqlite/?branch=master)
56
[![Latest Stable Version](https://poser.pugx.org/spam-n-eggs/laravel-mysqlite/v/stable)](https://packagist.org/packages/spam-n-eggs/laravel-mysqlite)
67
[![Total Downloads](https://poser.pugx.org/spam-n-eggs/laravel-mysqlite/downloads)](https://packagist.org/packages/spam-n-eggs/laravel-mysqlite)
78
[![License](https://poser.pugx.org/spam-n-eggs/laravel-mysqlite/license)](https://packagist.org/packages/spam-n-eggs/laravel-mysqlite)
@@ -39,30 +40,57 @@ In order to provide clutter it is preferable to create a separate Service Provid
3940
# Ported Functionality
4041
## Constants
4142
- [UTC_TIMESTAMP][utc_timestamp]
43+
## Operators
44+
- [DIV](https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html#operator_div)
4245
## Methods
4346
### Aggregate
4447
- [bit_or (int ...)](https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_bit-or)
4548
### Date and Time
49+
- [convert_tz(date, fromTimezone, toTimezone)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_convert-tz)
50+
- [date_add(date, INTERVAL <number_of> <interval_specifier>)]()
51+
- Ported Interval Specifiers
52+
- SECOND
53+
- MINUTE
54+
- HOUR
55+
- DAY
56+
- WEEK
57+
- MONTH
58+
- YEAR
59+
- Differences
60+
- All DATE_ADD calls bring back the entire date time as opposed to just the short date.
61+
- [date_format(date, format)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format)
62+
- Un-ported Format Strings: `%U`, `%V`, `%X`
63+
- Other Limitations: `%j` is off by 1 day.
64+
- [hour(time)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_hour)
65+
- [minute(time)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_minute)
4666
- [now()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now)
47-
- [timestampdiff($timeUnit, $startTimeStamp, $endTimeStamp)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff)
48-
- [time_to_sec($timeExpression)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-to-sec)
49-
- [to_days($date)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-days)
50-
- [unix_timestamp($date = null)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp)
67+
- [timestampdiff(timeUnit, startTimeStamp, endTimeStamp)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff)
68+
- [time_to_sec(timeExpression)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-to-sec)
69+
- [timediff(timeExpression1, timeExpression2)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timediff)
70+
- [to_days(date)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-days)
71+
- [unix_timestamp(date = null)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp)
5172
- [utc_timestamp()][utc_timestamp]
73+
- [weekday(date)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_weekday)
5274
### Flow
53-
- [if($condition, $onTrue, $onFalse)](https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#function_if)
75+
- [if(condition, onTrue, onFalse)](https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#function_if)
5476
### Numeric
77+
- [mod(number, divisor)](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_mod)
78+
- Limitations - Support for Standard `MOD(N,M)` and `N % M` notation only. `N MOD M` is not supported.
5579
- [rand()](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_rand)
56-
- [sqrt($value)](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sqrt)
80+
- [sqrt(value)](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sqrt)
5781
### String
5882
- [concat(string ...)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat)
5983
- [concat_ws(separator, string ...)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws)
84+
- [format(number, decimals, locale = 'en_US')](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_format)
85+
- [lpad(string, length, pad)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad)
86+
### Miscellaneous
87+
- [inet_ntoa(ipAddress)](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_inet-ntoa)
6088
### Vectorface-Specific
6189
#### Comparison
6290
- [least(mixed ...)](https://github.com/Vectorface/MySQLite/blob/master/src/Vectorface/MySQLite/MySQL/Comparison.php)
6391

6492
# Contributing
65-
Want to file a bug, contribute some code, improve documentation, or request a feature? Awesome Sauce! Read up on our guidelines for [contributing][contributing]
93+
Want to file a bug, contribute some code, improve documentation, or request a feature? Awesome Sauce! Read up on our guidelines for [contributing][contributing]. All contributions must follow our [Code of Conduct][codeofconduct].
6694

6795
# Questions
6896
Have a question? [Log an issue][issue] with the **Question** tag. We'll get back to you in a timely fashion.
@@ -81,3 +109,4 @@ License: (MIT) https://github.com/Vectorface/MySQLite/blob/master/LICENSE
81109
[utc_timestamp]: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_utc-timestamp
82110
[contributing]: ./.github/contributing.md
83111
[issue]: https://github.com/spam-n-eggs/laravel-mysqlite/issues
112+
[codeofconduct]:./.github/CODE_OF_CONDUCT.md

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spam-n-eggs/laravel-mysqlite",
3-
"description": "MySQLite connector for laravel that Leverages vectorface/mysqlite to add MySQL functionality to SQLite",
3+
"description": "MySQLite is a Laravel connector for SQLite that emulates MySQL functionality. The currently ported functionalities can be viewed in README.md on the github site.",
44
"type": "library",
55
"require": {
66
"vectorface/mysqlite": "^0.1.4",
@@ -19,7 +19,12 @@
1919
"authors": [
2020
{
2121
"name": "Mark J. Horninger",
22-
"email": "[email protected]"
22+
"email": "[email protected]",
23+
"role": "Developer"
24+
},
25+
{
26+
"name": "Contributors",
27+
"homepage": "https://github.com/spam-n-eggs/laravel-mysqlite/graphs/contributors"
2328
}
2429
],
2530
"autoload": {

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
4+
bootstrap="test/bootstrap.php"
55
colors="true"
66
processIsolation="false"
77
convertErrorsToExceptions="true"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Mhorninger\MySQLite;
4+
5+
class MethodRewriteConstants
6+
{
7+
const METHOD_REPLACEMENTS = [
8+
'/(DATE_ADD)(?=.*?, INTERVAL.*?\\))/' => 'datetime',
9+
'/INTERVAL (?=.*?\\))/' => '\'+',
10+
'/INTERVAL (?=.*?\\))/' => '\'+',
11+
'/SECOND(?=\\))/' => 'seconds\'',
12+
'/MINUTE(?=\\))/' => 'minutes\'',
13+
'/HOUR(?=\\))/' => 'hours\'',
14+
'/DAY(?=\\))/' => 'days\'',
15+
'/WEEK(?=\\))/' => 'weeks\'',
16+
'/MONTH(?=\\))/' => 'months\'',
17+
'/YEAR(?=\\))/' => 'years\'',
18+
];
19+
}

src/Mhorninger/MySQLite/MySQL/DateTimeExtended.php

Lines changed: 131 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,127 @@
33
namespace Mhorninger\MySQLite\MySQL;
44

55
use DateTime;
6-
use Mhorninger\MySQLite\Constants;
6+
use DateTimeZone;
7+
use Mhorninger\MySQLite\SubstitutionConstants;
78

89
trait DateTimeExtended
910
{
11+
//phpcs:disable
12+
public static function mysql_convert_tz($date, $fromTimezone, $toTimezone)
13+
{
14+
//phpcs:enable
15+
if ($date && $fromTimezone && $toTimezone) {
16+
17+
//SYSTEM is a reserved timezone in MySQL. date_default_timezone_get is a good workaround.
18+
if ($fromTimezone == 'SYSTEM') {
19+
$fromTimezone = date_default_timezone_get();
20+
}
21+
if ($toTimezone == 'SYSTEM') {
22+
$toTimezone = date_default_timezone_get();
23+
}
24+
25+
$fromTimezone = new DateTimeZone($fromTimezone);
26+
$toTimezone = new DateTimeZone($toTimezone);
27+
$converted = new DateTime($date, $fromTimezone);
28+
29+
$converted->setTimezone($toTimezone);
30+
31+
return $converted->format('Y-m-d H:i:s');
32+
}
33+
}
34+
35+
//phpcs:disable
36+
public static function mysql_date_format($date, $format)
37+
{
38+
//phpcs:enable
39+
$dictionary = [
40+
'%a' => 'D',
41+
'%b' => 'M',
42+
'%c' => 'n',
43+
'%D' => 'jS',
44+
'%d' => 'd',
45+
'%e' => 'j',
46+
'%f' => 'u',
47+
'%H' => 'H',
48+
'%h' => 'h',
49+
'%I' => 'h',
50+
'%i' => 'i',
51+
'%j' => 'z',
52+
'%k' => 'G',
53+
'%l' => 'g',
54+
'%M' => 'F',
55+
'%m' => 'm',
56+
'%p' => 'A',
57+
'%r' => 'h:i:s A',
58+
'%S' => 's',
59+
'%s' => 's',
60+
'%T' => 'H:i:s',
61+
'%u' => 'W',
62+
'%v' => 'W',
63+
'%W' => 'l',
64+
'%w' => 'w',
65+
'%x' => 'o',
66+
'%Y' => 'Y',
67+
'%y' => 'y',
68+
'%%' => '%',
69+
];
70+
71+
if ($date && $format) {
72+
$time = new DateTime($date);
73+
$keys = array_keys($dictionary);
74+
foreach ($keys as $key) {
75+
$format = str_replace($key, $dictionary[$key], $format);
76+
}
77+
78+
return $time->format($format);
79+
}
80+
}
81+
82+
// phpcs:disable
83+
public static function mysql_hour($time)
84+
{
85+
if ($time) {
86+
$asTime = new DateTime($time);
87+
88+
return $asTime->format('G');
89+
}
90+
}
91+
92+
// phpcs:disable
93+
public static function mysql_minute($time)
94+
{
95+
// phpcs:enable
96+
if ($time) {
97+
$asTime = new DateTime($time);
98+
99+
return date_format($asTime, 'i');
100+
}
101+
}
102+
103+
// phpcs:disable
104+
public static function mysql_timediff($timeExpression1, $timeExpression2)
105+
{
106+
// phpcs:enable
107+
if ($timeExpression1 && $timeExpression2) {
108+
$dateTime1 = new DateTime($timeExpression1);
109+
$dateTime2 = new DateTime($timeExpression2);
110+
$dateTimeInterval = $dateTime2->diff($dateTime1);
111+
$days = $dateTimeInterval->d;
112+
$hours = ($days * 24) + $dateTimeInterval->h;
113+
$hourFormatter = new \NumberFormatter(\Locale::DEFAULT_LOCALE, \NumberFormatter::PATTERN_DECIMAL, '00');
114+
$hours = $hourFormatter->format($hours, \NumberFormatter::PATTERN_DECIMAL);
115+
116+
return $dateTimeInterval->format("%r$hours:%I:%S.%F");
117+
}
118+
}
119+
10120
// phpcs:disable
11121
public static function mysql_timestampdiff($timeUnit, $startTimeStamp, $endTimeStamp)
12122
{
13123
//phpcs:enable
14124
if ($startTimeStamp != null && is_numeric($startTimeStamp) && $endTimeStamp != null && is_numeric($endTimeStamp)) {
15125
$differenceInt = $endTimeStamp - $startTimeStamp;
16-
if ($timeUnit == Constants::SECOND || $timeUnit = Constants::FRAC_SECOND) {
126+
if ($timeUnit == SubstitutionConstants::SECOND || $timeUnit = SubstitutionConstants::FRAC_SECOND) {
17127
return $differenceInt;
18128
}
19129
$difference = new DateTime();
@@ -23,15 +133,6 @@ public static function mysql_timestampdiff($timeUnit, $startTimeStamp, $endTimeS
23133
}
24134
}
25135

26-
//phpcs:disable
27-
public static function mysql_utc_timestamp()
28-
{
29-
//phpcs:enable
30-
$now = new DateTime();
31-
32-
return $now->getTimestamp();
33-
}
34-
35136
//phpcs:disable
36137
public static function mysql_time_to_sec($timeExpression)
37138
{
@@ -47,4 +148,23 @@ public static function mysql_time_to_sec($timeExpression)
47148
return $time->getTimestamp();
48149
}
49150
}
151+
152+
//phpcs:disable
153+
public static function mysql_utc_timestamp()
154+
{
155+
//phpcs:enable
156+
$now = new DateTime();
157+
158+
return $now->getTimestamp();
159+
}
160+
161+
// phpcs:disable
162+
public static function mysql_weekday($date)
163+
{
164+
if ($date) {
165+
$dateTime = new DateTime($date);
166+
167+
return $dateTime->format('N') - 1;
168+
}
169+
}
50170
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Mhorninger\MySQLite\MySQL;
4+
5+
trait Miscellaneous
6+
{
7+
//phpcs:disable
8+
public static function mysql_inet_ntoa($numeric)
9+
{
10+
//phpcs:enable
11+
if ($numeric && is_numeric($numeric)) {
12+
return long2ip(sprintf('%d', $numeric));
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Mhorninger\MySQLite\MySQL;
4+
5+
trait NumericExtended
6+
{
7+
// phpcs:disable
8+
public static function mysql_mod($number, $divisor)
9+
{
10+
//phpcs:enable
11+
if ($number && $divisor) {
12+
return fmod($number, $divisor);
13+
}
14+
}
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Mhorninger\MySQLite\MySQL;
4+
5+
use NumberFormatter;
6+
7+
trait StringExtended
8+
{
9+
/**
10+
* Format a number according to the nubmer of decimals provided and culture.
11+
* @param mixed... number, decimals, culture.
12+
*/
13+
// phpcs:disable
14+
public static function mysql_format()
15+
{
16+
//phpcs:enable
17+
$args = func_get_args();
18+
$length = count($args);
19+
if ($args && 0 < $length && ($number = $args[0]) != null) {
20+
$decimals = 1 < $length ? $args[1] : 0;
21+
$culture = 2 < $length ? $args[2] : 'en_US';
22+
$pattern = '#,##0';
23+
if ($decimals > 0) {
24+
$pattern = $pattern.'.';
25+
$base = strlen($pattern);
26+
$decimals = $base + $decimals;
27+
$pattern = str_pad($pattern, $decimals, '0', STR_PAD_RIGHT);
28+
}
29+
$formatter = new NumberFormatter($culture, NumberFormatter::PATTERN_DECIMAL, $pattern);
30+
31+
return $formatter->format($number);
32+
}
33+
}
34+
35+
// phpcs:disable
36+
public static function mysql_lpad($string, $length, $pad)
37+
{
38+
//phpcs:enable
39+
if ($string && $length && $pad) {
40+
if (strlen($string) < $length) {
41+
return str_pad($string, $length, $pad, STR_PAD_LEFT);
42+
}
43+
44+
return substr($string, 0, $length);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)