libasynql  3.2.0
Asynchronous MySQL access library for PocketMine plugins.
MysqlStatementImpl.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * libasynql
5  *
6  * Copyright (C) 2018 SOFe
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 declare(strict_types=1);
22 
23 namespace poggit\libasynql\generic;
24 
27 use function array_map;
28 use function assert;
29 use function bin2hex;
30 use function implode;
31 use function is_array;
32 use function is_bool;
33 use function is_finite;
34 use function is_float;
35 use function is_int;
36 use function is_string;
37 use function rand;
38 use function random_bytes;
39 
41  public function getDialect() : string{
42  return "mysql";
43  }
44 
45  protected function formatVariable(GenericVariable $variable, $value, ?string $placeHolder, array &$outArgs) : string{
46  if($variable->isList()){
47  assert(is_array($value));
48  if(empty($value)){
49  if(!$variable->canBeEmpty()){
50  throw new InvalidArgumentException("Cannot pass an empty array for :{$variable->getName()}");
51  }
52 
53  return "('" . bin2hex(random_bytes(20)) . ")";
54  }
55 
56  $unlist = $variable->unlist();
57  return "(" . implode(",", array_map(function($value) use ($unlist, $placeHolder, &$outArgs){
58  return $this->formatVariable($unlist, $value, $placeHolder, $outArgs);
59  }, $value)) . ")";
60  }
61 
62  if($value === null){
63  if(!$variable->isNullable()){
64  throw new InvalidArgumentException("The variable :{$variable->getName()} is not nullable");
65  }
66  return "NULL";
67  }
68 
69  switch($variable->getType()){
71  assert(is_bool($value));
72  return $value ? "1" : "0";
73 
75  assert(is_int($value));
76  return (string) $value;
77 
79  assert(is_int($value) || is_float($value));
80  if(!is_finite($value)){
81  throw new InvalidArgumentException("Cannot encode $value in MySQL");
82  }
83  return (string) $value;
84 
86  assert(is_string($value));
87  if($placeHolder !== null){
88  $outArgs[] = $value;
89  return $placeHolder;
90  }
91 
92  do{
93  $varName = ":var" . rand(0, 10000000);
94  }while(isset($outArgs[$varName]));
95  $outArgs[$varName] = $value;
96  return " " . $varName . " ";
97 
99  assert(is_int($value) || is_float($value));
100  if($value === GenericVariable::TIME_NOW){
101  return "CURRENT_TIMESTAMP";
102  }
103  return "FROM_UNIXTIME($value)";
104  }
105 
106  throw new RuntimeException("Unsupported variable type");
107  }
108 }
formatVariable(GenericVariable $variable, $value, ?string $placeHolder, array &$outArgs)