Repository navigation
Expand file tree
/
Copy pathSQLQuery.php
More file actions
138 lines (109 loc) · 3.45 KB
/
Copy pathSQLQuery.php
File metadata and controls
138 lines (109 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* This class lets you define your SQL queries in separate logic files
* and then comfortably import them in right place in your code.
*
* Keeps code clean and maintainable.
*
* @author jozef_cipa
*
*/
class SQLQuery{
/**
* @var array Array of files in directory
*/
private $sqlFiles;
/**
* @var string Directory path
*/
private static $dir = './sql';
/**
* @var string Name of part from sql file
*/
private $sqlPart;
private function __construct($sqlPart)
{
$this->sqlFiles = glob(self::$dir . "/*.sql");
$this->sqlPart = $sqlPart;
}
/**
* Change default sql directory path
* @param string $dir New path
*/
public static function setSqlDir($dir){
self::$dir = $dir;
}
/**
* Return new object and set sql part
* @param string $sqlPart Name of part from sql file
* @return SQLQuery Return new object
*/
public static function import($sqlPart = '*'){
return new self($sqlPart);
}
/**
* Return whole sql string or its part from given filename
* @param string $filename Name of sql file
* @return Return query string
*/
public function from($filename)
{
$filename .= '.sql';
if (!$this->fileExists($filename))
throw new Exception("File $filename doesn't exist in directory " . self::$dir);
if ($this->sqlPart == '*')
return $this->getSQL($filename);
else
return $this->getSQLPart($this->sqlPart, $filename);
}
/**
* Check if given filename exists in sql directory
* @param string $filename Name of sql file
* @return boolean
*/
private function fileExists($filename){
$files = array_map(function($filename){
return basename($filename);
}, $this->sqlFiles);
return in_array($filename, $files);
}
/**
* Return content of file
* @param string $filename File name
* @return string Return content of file
*/
private function readFile($filename){
return file_get_contents(self::$dir . '/' . $filename);
}
/**
* Return whole sql string
*
* @param $filename string File name
* @return string Return whole sql string
*/
private function getSQL($filename){
return $this->readFile($filename);
}
/**
* Return sql part from file by $sqlPartName
* @param string $sqlPartName Name of sql part
* @param string $filename File name
* @return string Return sql string
*/
private function getSQLPart($sqlPartName, $filename){
$sqlFile = $this->getSQL($filename);
$parts = preg_split("/--@SQLName(.*)\\n/", $sqlFile);
//remove first empty string
array_shift($parts);
//remove spaces from start and end
$parts = array_map(function($item){
return trim($item);
}, $parts);
preg_match_all("/--@SQLName (.*)\\n/", $sqlFile, $partsNames);
$partIndexByName = array_search($sqlPartName, $partsNames[1]);
$sql = $parts[$partIndexByName];
if(!$sql)
throw new Exception("SQL part with given name: $sqlPartName doesn't exist in $filename.");
return $sql;
}
}