Ubiquity  2.0.0
php rapid development framework
SqlUtils.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\db;
4 
7 
13 class SqlUtils {
14 
15  private static function getParameters($keyAndValues) {
16  $ret=array ();
17  foreach ( $keyAndValues as $key => $value ) {
18  $ret[]=":" . $key;
19  }
20  return $ret;
21  }
22 
23  private static function getQuotedKeys($keyAndValues, $quote="`") {
24  $ret=array ();
25  foreach ( $keyAndValues as $key => $value ) {
26  $ret[]=$quote . $key . $quote;
27  }
28  return $ret;
29  }
30 
31  public static function getWhere($keyAndValues, $quote="`") {
32  $ret=array ();
33  foreach ( $keyAndValues as $key => $value ) {
34  $ret[]=$quote . $key . $quote . "= :" . $key;
35  }
36  return implode(" AND ", $ret);
37  }
38 
39  public static function getMultiWhere($values, $field, $quote="`") {
40  $ret=array ();
41  foreach ( $values as $value ) {
42  $ret[]=$quote . $field . $quote . "='" . $value . "'";
43  }
44  return implode(" OR ", $ret);
45  }
46 
47  public static function getInsertFields($keyAndValues) {
48  return implode(",", self::getQuotedKeys($keyAndValues));
49  }
50 
51  public static function getInsertFieldsValues($keyAndValues) {
52  return implode(",", self::getParameters($keyAndValues));
53  }
54 
55  public static function getUpdateFieldsKeyAndValues($keyAndValues, $quote="`") {
56  $ret=array ();
57  foreach ( $keyAndValues as $key => $value ) {
58  $ret[]=$quote . $key . $quote . "= :" . $key;
59  }
60  return implode(",", $ret);
61  }
62 
63  public static function checkWhere($condition){
64  $c=\strtolower($condition);
65  if ($condition != '' && \strstr($c, " join ")===false){
66  $condition=" WHERE " . $condition;
67  }
68  return $condition;
69  }
70 
71  public static function getCondition($keyValues,$classname=NULL,$separator=" AND ") {
72  $retArray=array ();
73  if (is_array($keyValues)) {
74  if(!UArray::isAssociative($keyValues)){
75  if(isset($classname)){
76  $keys=OrmUtils::getKeyFields($classname);
77  $keyValues=\array_combine($keys, $keyValues);
78  }
79  }
80  foreach ( $keyValues as $key => $value ) {
81  $retArray[]="`" . $key . "` = '" . $value . "'";
82  }
83  $condition=implode($separator, $retArray);
84  } else
85  $condition=$keyValues;
86  return $condition;
87  }
88 
89  public static function getFieldList($fields){
90  if(!\is_array($fields)){
91  return $fields;
92  }
93  $result=[];
94  foreach ($fields as $field) {
95  $result[]= "`{$field}`";
96  }
97  return \implode(",", $result);
98  }
99 }
Utilitaires SQL.
Definition: SqlUtils.php:13
static checkWhere($condition)
Definition: SqlUtils.php:63
static getFieldList($fields)
Definition: SqlUtils.php:89
static getQuotedKeys($keyAndValues, $quote="`")
Definition: SqlUtils.php:23
static getInsertFields($keyAndValues)
Definition: SqlUtils.php:47
static getCondition($keyValues, $classname=NULL, $separator=" AND ")
Definition: SqlUtils.php:71
static getWhere($keyAndValues, $quote="`")
Definition: SqlUtils.php:31
static isAssociative($array)
Definition: UArray.php:7
static getUpdateFieldsKeyAndValues($keyAndValues, $quote="`")
Definition: SqlUtils.php:55
static getInsertFieldsValues($keyAndValues)
Definition: SqlUtils.php:51
static getKeyFields($instance)
Definition: OrmUtils.php:71
static getParameters($keyAndValues)
Definition: SqlUtils.php:15
static getMultiWhere($values, $field, $quote="`")
Definition: SqlUtils.php:39