Ubiquity  2.0.3
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 getSearchWhere($fields, $value,$jokerBefore="%",$jokerAfter="%") {
50  $ret=array ();
51  foreach ( $fields as $field ) {
52  $ret[]=self::$quote . $field . self::$quote . " LIKE '".$jokerBefore . $value . $jokerAfter."'";
53  }
54  return implode(" OR ", $ret);
55  }
56 
57  public static function getInsertFields($keyAndValues) {
58  return implode(",", self::getQuotedKeys($keyAndValues));
59  }
60 
61  public static function getInsertFieldsValues($keyAndValues) {
62  return implode(",", self::getParameters($keyAndValues));
63  }
64 
65  public static function getUpdateFieldsKeyAndValues($keyAndValues) {
66  $ret=array ();
67  foreach ( $keyAndValues as $key => $value ) {
68  $ret[]=self::$quote . $key . self::$quote . "= :" . $key;
69  }
70  return implode(",", $ret);
71  }
72 
73  public static function checkWhere($condition){
74  $c=\strtolower($condition);
75  if ($condition != '' && \strstr($c, " join ")===false){
76  $condition=" WHERE " . $condition;
77  }
78  return $condition;
79  }
80 
81  public static function getCondition($keyValues,$classname=NULL,$separator=" AND ") {
82  $retArray=array ();
83  if (is_array($keyValues)) {
84  if(!UArray::isAssociative($keyValues)){
85  if(isset($classname)){
86  $keys=OrmUtils::getKeyFields($classname);
87  $keyValues=\array_combine($keys, $keyValues);
88  }
89  }
90  foreach ( $keyValues as $key => $value ) {
91  $retArray[]=self::$quote . $key . self::$quote . " = '" . $value . "'";
92  }
93  $condition=implode($separator, $retArray);
94  } else
95  $condition=$keyValues;
96  return $condition;
97  }
98 
99  public static function getFieldList($fields,$tableName=false){
100  if(!\is_array($fields)){
101  return $fields;
102  }
103  $result=[];
104  $prefix="";
105  if($tableName)
106  $prefix=self::$quote.$tableName.self::$quote.".";
107  foreach ($fields as $field) {
108  $result[]= $prefix.self::$quote.$field.self::$quote;
109  }
110  return \implode(",", $result);
111  }
112 }
Utilitaires SQL.
Definition: SqlUtils.php:13
static checkWhere($condition)
Definition: SqlUtils.php:73
static getUpdateFieldsKeyAndValues($keyAndValues)
Definition: SqlUtils.php:65
static getWhere($keyAndValues)
Definition: SqlUtils.php:33
static getMultiWhere($values, $field)
Definition: SqlUtils.php:41
static getSearchWhere($fields, $value, $jokerBefore="%", $jokerAfter="%")
Definition: SqlUtils.php:49
static getQuotedKeys($keyAndValues)
Definition: SqlUtils.php:25
static getInsertFields($keyAndValues)
Definition: SqlUtils.php:57
static getCondition($keyValues, $classname=NULL, $separator=" AND ")
Definition: SqlUtils.php:81
static isAssociative($array)
Tests if array is associative.
Definition: UArray.php:17
static getFieldList($fields, $tableName=false)
Definition: SqlUtils.php:99
static getInsertFieldsValues($keyAndValues)
Definition: SqlUtils.php:61
static getKeyFields($instance)
Definition: OrmUtils.php:72
static getParameters($keyAndValues)
Definition: SqlUtils.php:17