libasynql  3.2.0
Asynchronous MySQL access library for PocketMine plugins.
SqliteStatementImpl.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 
26 use SQLite3;
27 use function array_map;
28 use function assert;
29 use function implode;
30 use function is_array;
31 use function is_bool;
32 use function is_float;
33 use function is_int;
34 use function is_string;
35 
37  public function getDialect() : string{
38  return "sqlite";
39  }
40 
41  protected function formatVariable(GenericVariable $variable, $value, ?string $placeHolder, array &$outArgs) : string{
42  if($variable->isList()){
43  assert(is_array($value));
44 
45  // IN () works with SQLite3.
46  $unlist = $variable->unlist();
47  return "(" . implode(",", array_map(function($value) use ($placeHolder, $unlist, &$outArgs){
48  return $this->formatVariable($unlist, $value, $placeHolder, $outArgs);
49  }, $value)) . ")";
50  }
51 
52  switch($variable->getType()){
54  assert(is_bool($value));
55  return $value ? "1" : "0";
56 
58  assert(is_int($value));
59  return (string) $value;
60 
62  assert(is_int($value) || is_float($value));
63  return (string) $value;
64 
66  assert(is_string($value));
67  return "'" . SQLite3::escapeString($value) . "'";
68  }
69 
70  throw new RuntimeException("Unsupported variable type");
71  }
72 }
formatVariable(GenericVariable $variable, $value, ?string $placeHolder, array &$outArgs)