Skip to content

Commit

Permalink
Additional methods (#29)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Mark Horninger authored and spam-n-eggs committed Feb 13, 2019
1 parent 73c8ba3 commit d720000
Show file tree
Hide file tree
Showing 21 changed files with 846 additions and 105 deletions.
File renamed without changes.
43 changes: 36 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[![Build Status](https://travis-ci.org/spam-n-eggs/laravel-mysqlite.svg?branch=master)](https://travis-ci.org/spam-n-eggs/laravel-mysqlite)
[![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)
[![StyleCI](https://github.styleci.io/repos/167069269/shield?branch=master)](https://github.styleci.io/repos/167069269)
[![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)
[![Latest Stable Version](https://poser.pugx.org/spam-n-eggs/laravel-mysqlite/v/stable)](https://packagist.org/packages/spam-n-eggs/laravel-mysqlite)
[![Total Downloads](https://poser.pugx.org/spam-n-eggs/laravel-mysqlite/downloads)](https://packagist.org/packages/spam-n-eggs/laravel-mysqlite)
[![License](https://poser.pugx.org/spam-n-eggs/laravel-mysqlite/license)](https://packagist.org/packages/spam-n-eggs/laravel-mysqlite)
Expand Down Expand Up @@ -39,30 +40,57 @@ In order to provide clutter it is preferable to create a separate Service Provid
# Ported Functionality
## Constants
- [UTC_TIMESTAMP][utc_timestamp]
## Operators
- [DIV](https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html#operator_div)
## Methods
### Aggregate
- [bit_or (int ...)](https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_bit-or)
### Date and Time
- [convert_tz(date, fromTimezone, toTimezone)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_convert-tz)
- [date_add(date, INTERVAL <number_of> <interval_specifier>)]()
- Ported Interval Specifiers
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- YEAR
- Differences
- All DATE_ADD calls bring back the entire date time as opposed to just the short date.
- [date_format(date, format)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format)
- Un-ported Format Strings: `%U`, `%V`, `%X`
- Other Limitations: `%j` is off by 1 day.
- [hour(time)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_hour)
- [minute(time)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_minute)
- [now()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now)
- [timestampdiff($timeUnit, $startTimeStamp, $endTimeStamp)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff)
- [time_to_sec($timeExpression)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-to-sec)
- [to_days($date)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-days)
- [unix_timestamp($date = null)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp)
- [timestampdiff(timeUnit, startTimeStamp, endTimeStamp)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff)
- [time_to_sec(timeExpression)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-to-sec)
- [timediff(timeExpression1, timeExpression2)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timediff)
- [to_days(date)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-days)
- [unix_timestamp(date = null)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp)
- [utc_timestamp()][utc_timestamp]
- [weekday(date)](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_weekday)
### Flow
- [if($condition, $onTrue, $onFalse)](https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#function_if)
- [if(condition, onTrue, onFalse)](https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#function_if)
### Numeric
- [mod(number, divisor)](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_mod)
- Limitations - Support for Standard `MOD(N,M)` and `N % M` notation only. `N MOD M` is not supported.
- [rand()](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_rand)
- [sqrt($value)](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sqrt)
- [sqrt(value)](https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_sqrt)
### String
- [concat(string ...)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat)
- [concat_ws(separator, string ...)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws)
- [format(number, decimals, locale = 'en_US')](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_format)
- [lpad(string, length, pad)](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad)
### Miscellaneous
- [inet_ntoa(ipAddress)](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_inet-ntoa)
### Vectorface-Specific
#### Comparison
- [least(mixed ...)](https://github.com/Vectorface/MySQLite/blob/master/src/Vectorface/MySQLite/MySQL/Comparison.php)

# Contributing
Want to file a bug, contribute some code, improve documentation, or request a feature? Awesome Sauce! Read up on our guidelines for [contributing][contributing]
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].

# Questions
Have a question? [Log an issue][issue] with the **Question** tag. We'll get back to you in a timely fashion.
Expand All @@ -81,3 +109,4 @@ License: (MIT) https://github.com/Vectorface/MySQLite/blob/master/LICENSE
[utc_timestamp]: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_utc-timestamp
[contributing]: ./.github/contributing.md
[issue]: https://github.com/spam-n-eggs/laravel-mysqlite/issues
[codeofconduct]:./.github/CODE_OF_CONDUCT.md
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spam-n-eggs/laravel-mysqlite",
"description": "MySQLite connector for laravel that Leverages vectorface/mysqlite to add MySQL functionality to SQLite",
"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.",
"type": "library",
"require": {
"vectorface/mysqlite": "^0.1.4",
Expand All @@ -19,7 +19,12 @@
"authors": [
{
"name": "Mark J. Horninger",
"email": "thisisafakeemailaddr@gmail.com"
"email": "thisisafakeemailaddr@gmail.com",
"role": "Developer"
},
{
"name": "Contributors",
"homepage": "https://github.com/spam-n-eggs/laravel-mysqlite/graphs/contributors"
}
],
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
bootstrap="test/bootstrap.php"
colors="true"
processIsolation="false"
convertErrorsToExceptions="true"
Expand Down
19 changes: 19 additions & 0 deletions src/Mhorninger/MySQLite/MethodRewriteConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Mhorninger\MySQLite;

class MethodRewriteConstants
{
const METHOD_REPLACEMENTS = [
'/(DATE_ADD)(?=.*?, INTERVAL.*?\\))/' => 'datetime',
'/INTERVAL (?=.*?\\))/' => '\'+',
'/INTERVAL (?=.*?\\))/' => '\'+',
'/SECOND(?=\\))/' => 'seconds\'',
'/MINUTE(?=\\))/' => 'minutes\'',
'/HOUR(?=\\))/' => 'hours\'',
'/DAY(?=\\))/' => 'days\'',
'/WEEK(?=\\))/' => 'weeks\'',
'/MONTH(?=\\))/' => 'months\'',
'/YEAR(?=\\))/' => 'years\'',
];
}
142 changes: 131 additions & 11 deletions src/Mhorninger/MySQLite/MySQL/DateTimeExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,127 @@
namespace Mhorninger\MySQLite\MySQL;

use DateTime;
use Mhorninger\MySQLite\Constants;
use DateTimeZone;
use Mhorninger\MySQLite\SubstitutionConstants;

trait DateTimeExtended
{
//phpcs:disable
public static function mysql_convert_tz($date, $fromTimezone, $toTimezone)
{
//phpcs:enable
if ($date && $fromTimezone && $toTimezone) {

//SYSTEM is a reserved timezone in MySQL. date_default_timezone_get is a good workaround.
if ($fromTimezone == 'SYSTEM') {
$fromTimezone = date_default_timezone_get();
}
if ($toTimezone == 'SYSTEM') {
$toTimezone = date_default_timezone_get();
}

$fromTimezone = new DateTimeZone($fromTimezone);
$toTimezone = new DateTimeZone($toTimezone);
$converted = new DateTime($date, $fromTimezone);

$converted->setTimezone($toTimezone);

return $converted->format('Y-m-d H:i:s');
}
}

//phpcs:disable
public static function mysql_date_format($date, $format)
{
//phpcs:enable
$dictionary = [
'%a' => 'D',
'%b' => 'M',
'%c' => 'n',
'%D' => 'jS',
'%d' => 'd',
'%e' => 'j',
'%f' => 'u',
'%H' => 'H',
'%h' => 'h',
'%I' => 'h',
'%i' => 'i',
'%j' => 'z',
'%k' => 'G',
'%l' => 'g',
'%M' => 'F',
'%m' => 'm',
'%p' => 'A',
'%r' => 'h:i:s A',
'%S' => 's',
'%s' => 's',
'%T' => 'H:i:s',
'%u' => 'W',
'%v' => 'W',
'%W' => 'l',
'%w' => 'w',
'%x' => 'o',
'%Y' => 'Y',
'%y' => 'y',
'%%' => '%',
];

if ($date && $format) {
$time = new DateTime($date);
$keys = array_keys($dictionary);
foreach ($keys as $key) {
$format = str_replace($key, $dictionary[$key], $format);
}

return $time->format($format);
}
}

// phpcs:disable
public static function mysql_hour($time)
{
if ($time) {
$asTime = new DateTime($time);

return $asTime->format('G');
}
}

// phpcs:disable
public static function mysql_minute($time)
{
// phpcs:enable
if ($time) {
$asTime = new DateTime($time);

return date_format($asTime, 'i');
}
}

// phpcs:disable
public static function mysql_timediff($timeExpression1, $timeExpression2)
{
// phpcs:enable
if ($timeExpression1 && $timeExpression2) {
$dateTime1 = new DateTime($timeExpression1);
$dateTime2 = new DateTime($timeExpression2);
$dateTimeInterval = $dateTime2->diff($dateTime1);
$days = $dateTimeInterval->d;
$hours = ($days * 24) + $dateTimeInterval->h;
$hourFormatter = new \NumberFormatter(\Locale::DEFAULT_LOCALE, \NumberFormatter::PATTERN_DECIMAL, '00');
$hours = $hourFormatter->format($hours, \NumberFormatter::PATTERN_DECIMAL);

return $dateTimeInterval->format("%r$hours:%I:%S.%F");
}
}

// phpcs:disable
public static function mysql_timestampdiff($timeUnit, $startTimeStamp, $endTimeStamp)
{
//phpcs:enable
if ($startTimeStamp != null && is_numeric($startTimeStamp) && $endTimeStamp != null && is_numeric($endTimeStamp)) {
$differenceInt = $endTimeStamp - $startTimeStamp;
if ($timeUnit == Constants::SECOND || $timeUnit = Constants::FRAC_SECOND) {
if ($timeUnit == SubstitutionConstants::SECOND || $timeUnit = SubstitutionConstants::FRAC_SECOND) {
return $differenceInt;
}
$difference = new DateTime();
Expand All @@ -23,15 +133,6 @@ public static function mysql_timestampdiff($timeUnit, $startTimeStamp, $endTimeS
}
}

//phpcs:disable
public static function mysql_utc_timestamp()
{
//phpcs:enable
$now = new DateTime();

return $now->getTimestamp();
}

//phpcs:disable
public static function mysql_time_to_sec($timeExpression)
{
Expand All @@ -47,4 +148,23 @@ public static function mysql_time_to_sec($timeExpression)
return $time->getTimestamp();
}
}

//phpcs:disable
public static function mysql_utc_timestamp()
{
//phpcs:enable
$now = new DateTime();

return $now->getTimestamp();
}

// phpcs:disable
public static function mysql_weekday($date)
{
if ($date) {
$dateTime = new DateTime($date);

return $dateTime->format('N') - 1;
}
}
}
15 changes: 15 additions & 0 deletions src/Mhorninger/MySQLite/MySQL/Miscellaneous.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Mhorninger\MySQLite\MySQL;

trait Miscellaneous
{
//phpcs:disable
public static function mysql_inet_ntoa($numeric)
{
//phpcs:enable
if ($numeric && is_numeric($numeric)) {
return long2ip(sprintf('%d', $numeric));
}
}
}
15 changes: 15 additions & 0 deletions src/Mhorninger/MySQLite/MySQL/NumericExtended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Mhorninger\MySQLite\MySQL;

trait NumericExtended
{
// phpcs:disable
public static function mysql_mod($number, $divisor)
{
//phpcs:enable
if ($number && $divisor) {
return fmod($number, $divisor);
}
}
}
47 changes: 47 additions & 0 deletions src/Mhorninger/MySQLite/MySQL/StringExtended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Mhorninger\MySQLite\MySQL;

use NumberFormatter;

trait StringExtended
{
/**
* Format a number according to the nubmer of decimals provided and culture.
* @param mixed... number, decimals, culture.
*/
// phpcs:disable
public static function mysql_format()
{
//phpcs:enable
$args = func_get_args();
$length = count($args);
if ($args && 0 < $length && ($number = $args[0]) != null) {
$decimals = 1 < $length ? $args[1] : 0;
$culture = 2 < $length ? $args[2] : 'en_US';
$pattern = '#,##0';
if ($decimals > 0) {
$pattern = $pattern.'.';
$base = strlen($pattern);
$decimals = $base + $decimals;
$pattern = str_pad($pattern, $decimals, '0', STR_PAD_RIGHT);
}
$formatter = new NumberFormatter($culture, NumberFormatter::PATTERN_DECIMAL, $pattern);

return $formatter->format($number);
}
}

// phpcs:disable
public static function mysql_lpad($string, $length, $pad)
{
//phpcs:enable
if ($string && $length && $pad) {
if (strlen($string) < $length) {
return str_pad($string, $length, $pad, STR_PAD_LEFT);
}

return substr($string, 0, $length);
}
}
}
Loading

0 comments on commit d720000

Please sign in to comment.