laravel mysql函数,在Laravel 5.5中创建MySQL函数

I have MySQL 5.6 and I need to use MySQL 5.7 function ST_Distance_Sphere (for geolocalization purposes).

I found this hack and it looks correct to me (?).

Now I'm trying to use it in Laravel 5.5.

I put it in app\Providers\AppServiceProvider.php in boot() function.

I tried with DB::statement, DB::raw, DB::unprepared.

I tried removing DELIMITER and $$.

$sql = '

DELIMITER $$

DROP FUNCTION IF EXISTS `ST_Distance_Sphere`$$

CREATE FUNCTION `ST_Distance_Sphere` (point1 POINT, point2 POINT)

RETURNS FLOAT

no sql deterministic

BEGIN

declare R INTEGER DEFAULT 6371000;

declare `φ1` float;

declare `φ2` float;

declare `Δφ` float;

declare `Δλ` float;

declare a float;

declare c float;

set `φ1` = radians(y(point1));

set `φ2` = radians(y(point2));

set `Δφ` = radians(y(point2) - y(point1));

set `Δλ` = radians(x(point2) - x(point1));

set a = sin(`Δφ` / 2) * sin(`Δφ` / 2) + cos(`φ1`) * cos(`φ2`) * sin(`Δλ` / 2) * sin(`Δλ` / 2);

set c = 2 * atan2(sqrt(a), sqrt(1-a));

return R * c;

END$$

DELIMITER ;

';

DB::statement($sql);

I have a syntax error everytime. For example:

``Illuminate \ Database \ QueryException (42000)SQLSTATE[42000]: Syntax error or access violation: 1064 Erreur de syntaxe près de 'DELIMITER $$ DROP FUNCTION IF EXISTS `ST_Distance_Sphere`$$ ' à la ligne 1 (SQL: DELIMITER $$ DROP FUNCTION IF EXISTS `ST_Distance_Sphere`$$ CREATE FUNCTION `ST_Distance_Sphere` (point1 POINT, point2 POINT) RETURNS FLOAT no sql deterministic BEGIN declare R INTEGER DEFAULT 6371000; declare `φ1` float; declare `φ2` float; declare `Δφ` float; declare `Δλ` float; declare a float; declare c float; set `φ1` = radians(y(point1)); set `φ2` = radians(y(point2)); set `Δφ` = radians(y(point2) - y(point1)); set `Δλ` = radians(x(point2) - x(point1)); set a = sin(`Δφ` / 2) * sin(`Δφ` / 2) + cos(`φ1`) * cos(`φ2`) * sin(`Δλ` / 2) * sin(`Δλ` / 2); set c = 2 * atan2(sqrt(a), sqrt(1-a)); return R * c; END$$ DELIMITER ; )``

解决方案

I answer my own question

It seems I didn't have access to drop a MySQL function...

So I replaced DROP FUNCTION IF EXISTS `ST_Distance_Sphere` by a MySQL version check:

$mysql_version_check = DB::select(DB::raw('SHOW VARIABLES LIKE "version";'));

$mysql_version = $mysql_version_check[0]->Value;

if (substr($mysql_version,2, 1) < '7' AND substr($mysql_version,4, 1) < '6') {

$sql = '

CREATE FUNCTION `ST_Distance_Sphere` (point1 POINT, point2 POINT)

RETURNS FLOAT

no sql deterministic

BEGIN

declare R INTEGER DEFAULT 6371000;

declare `φ1` float;

declare `φ2` float;

declare `Δφ` float;

declare `Δλ` float;

declare a float;

declare c float;

set `φ1` = radians(y(point1));

set `φ2` = radians(y(point2));

set `Δφ` = radians(y(point2) - y(point1));

set `Δλ` = radians(x(point2) - x(point1));

set a = sin(`Δφ` / 2) * sin(`Δφ` / 2) + cos(`φ1`) * cos(`φ2`) * sin(`Δλ` / 2) * sin(`Δλ` / 2);

set c = 2 * atan2(sqrt(a), sqrt(1-a));

return R * c;

END;

';

DB::unprepared($sql);

}

It's a bit ugly, but it appears to work...

EDIT

Actually, it will work only the 1st time it is executed. It appears this function could save the mysql function to the mysql database. You will have an error the next times like "Mysql function already exists". You just have to comment the block above.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值