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