@@ -21,21 +21,21 @@ discard block |
||
| 21 | 21 | * You should have received a copy of the GNU General Public License |
| 22 | 22 | * along with this program; if not, write to the Free Software |
| 23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | - */ |
|
| 24 | + */ |
|
| 25 | 25 | |
| 26 | 26 | error_reporting(E_ALL | E_STRICT); |
| 27 | 27 | ini_set('display_errors', 1); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | - */ |
|
| 30 | + * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | + */ |
|
| 32 | 32 | define('DS', DIRECTORY_SEPARATOR); |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | - * The root directory of the application. |
|
| 36 | - * |
|
| 37 | - * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | - */ |
|
| 35 | + * The root directory of the application. |
|
| 36 | + * |
|
| 37 | + * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | + */ |
|
| 39 | 39 | define('ROOT_PATH', dirname(realpath(__FILE__)) . DS . '..' . DS); |
| 40 | 40 | |
| 41 | 41 | //tests dir path |
@@ -43,176 +43,176 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | - * The path to the directory. |
|
| 47 | - * |
|
| 48 | - * That contains your static files (javascript, css, images, etc.) |
|
| 49 | - * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 50 | - */ |
|
| 46 | + * The path to the directory. |
|
| 47 | + * |
|
| 48 | + * That contains your static files (javascript, css, images, etc.) |
|
| 49 | + * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 50 | + */ |
|
| 51 | 51 | define('ASSETS_PATH', 'assets/'); |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | - * The path to the directory of your cache files. |
|
| 55 | - * |
|
| 56 | - * This feature is available currently for database and views. |
|
| 57 | - */ |
|
| 54 | + * The path to the directory of your cache files. |
|
| 55 | + * |
|
| 56 | + * This feature is available currently for database and views. |
|
| 57 | + */ |
|
| 58 | 58 | define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | - * Custom application path for tests |
|
| 62 | - */ |
|
| 61 | + * Custom application path for tests |
|
| 62 | + */ |
|
| 63 | 63 | define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | - * The path to the controller directory of your application. |
|
| 67 | - * |
|
| 68 | - * If you already know the MVC architecture you know what a controller means; |
|
| 69 | - * it is he who makes the business logic of your application in general. |
|
| 70 | - */ |
|
| 66 | + * The path to the controller directory of your application. |
|
| 67 | + * |
|
| 68 | + * If you already know the MVC architecture you know what a controller means; |
|
| 69 | + * it is he who makes the business logic of your application in general. |
|
| 70 | + */ |
|
| 71 | 71 | define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | - * The path to the directory of your model classes of your application. |
|
| 75 | - * |
|
| 76 | - * If you already know the MVC architecture you know what a model means; |
|
| 77 | - * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 78 | - */ |
|
| 74 | + * The path to the directory of your model classes of your application. |
|
| 75 | + * |
|
| 76 | + * If you already know the MVC architecture you know what a model means; |
|
| 77 | + * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 78 | + */ |
|
| 79 | 79 | define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | - * The path to the directory of your views. |
|
| 83 | - * |
|
| 84 | - * If you already know the MVC architecture you know what a view means, |
|
| 85 | - * a view is just a user interface (html page, form, etc.) that is to say |
|
| 86 | - * everything displayed in the browser interface, etc. |
|
| 87 | - */ |
|
| 82 | + * The path to the directory of your views. |
|
| 83 | + * |
|
| 84 | + * If you already know the MVC architecture you know what a view means, |
|
| 85 | + * a view is just a user interface (html page, form, etc.) that is to say |
|
| 86 | + * everything displayed in the browser interface, etc. |
|
| 87 | + */ |
|
| 88 | 88 | define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | - * The path to the configuration directory. |
|
| 92 | - * |
|
| 93 | - * That contains most of the configuration files for your |
|
| 94 | - * application (database, class loading file, functions, etc.) |
|
| 95 | - */ |
|
| 91 | + * The path to the configuration directory. |
|
| 92 | + * |
|
| 93 | + * That contains most of the configuration files for your |
|
| 94 | + * application (database, class loading file, functions, etc.) |
|
| 95 | + */ |
|
| 96 | 96 | define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | - * The core directory |
|
| 100 | - * |
|
| 101 | - * It is recommended to put this folder out of the web directory of your server and |
|
| 102 | - * you should not change its content because in case of update you could lose the modified files. |
|
| 103 | - */ |
|
| 99 | + * The core directory |
|
| 100 | + * |
|
| 101 | + * It is recommended to put this folder out of the web directory of your server and |
|
| 102 | + * you should not change its content because in case of update you could lose the modified files. |
|
| 103 | + */ |
|
| 104 | 104 | define('CORE_PATH', ROOT_PATH . 'core' . DS); |
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | - * The path to the directory of core classes that used by the system. |
|
| 108 | - * |
|
| 109 | - * It contains PHP classes that are used by the framework internally. |
|
| 110 | - */ |
|
| 107 | + * The path to the directory of core classes that used by the system. |
|
| 108 | + * |
|
| 109 | + * It contains PHP classes that are used by the framework internally. |
|
| 110 | + */ |
|
| 111 | 111 | define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | - * The path to the directory of core classes for the cache used by the system. |
|
| 115 | - * |
|
| 116 | - * It contains PHP classes for the cache drivers. |
|
| 117 | - */ |
|
| 114 | + * The path to the directory of core classes for the cache used by the system. |
|
| 115 | + * |
|
| 116 | + * It contains PHP classes for the cache drivers. |
|
| 117 | + */ |
|
| 118 | 118 | define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | - * The path to the directory of core classes for the model used by the system. |
|
| 122 | - * |
|
| 123 | - * It contains PHP classes for the models. |
|
| 124 | - */ |
|
| 121 | + * The path to the directory of core classes for the model used by the system. |
|
| 122 | + * |
|
| 123 | + * It contains PHP classes for the models. |
|
| 124 | + */ |
|
| 125 | 125 | define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
| 126 | 126 | |
| 127 | 127 | /** |
| 128 | - * The path to the directory of functions or helper systems. |
|
| 129 | - * |
|
| 130 | - * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 131 | - */ |
|
| 128 | + * The path to the directory of functions or helper systems. |
|
| 129 | + * |
|
| 130 | + * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 131 | + */ |
|
| 132 | 132 | define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | - * The path to the core directory of languages files. |
|
| 136 | - * |
|
| 137 | - */ |
|
| 135 | + * The path to the core directory of languages files. |
|
| 136 | + * |
|
| 137 | + */ |
|
| 138 | 138 | define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | - * The path to the system library directory. |
|
| 142 | - * |
|
| 143 | - * Which contains the libraries most often used in your web application, as for the |
|
| 144 | - * core directory it is advisable to put it out of the root directory of your application. |
|
| 145 | - */ |
|
| 141 | + * The path to the system library directory. |
|
| 142 | + * |
|
| 143 | + * Which contains the libraries most often used in your web application, as for the |
|
| 144 | + * core directory it is advisable to put it out of the root directory of your application. |
|
| 145 | + */ |
|
| 146 | 146 | define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | - * The path to the system view directory. |
|
| 150 | - * |
|
| 151 | - * That contains the views used for the system, such as error messages, and so on. |
|
| 152 | - */ |
|
| 149 | + * The path to the system view directory. |
|
| 150 | + * |
|
| 151 | + * That contains the views used for the system, such as error messages, and so on. |
|
| 152 | + */ |
|
| 153 | 153 | define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | - * The path to the directory of your PHP personal functions or helper. |
|
| 157 | - * |
|
| 158 | - * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 159 | - * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 160 | - * because if you update the system you may lose them. |
|
| 161 | - */ |
|
| 156 | + * The path to the directory of your PHP personal functions or helper. |
|
| 157 | + * |
|
| 158 | + * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 159 | + * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 160 | + * because if you update the system you may lose them. |
|
| 161 | + */ |
|
| 162 | 162 | define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | - * The path to the app directory of personal language. |
|
| 166 | - * |
|
| 167 | - * This feature is not yet available. |
|
| 168 | - * You can help us do this if you are nice or wish to see the developed framework. |
|
| 169 | - */ |
|
| 165 | + * The path to the app directory of personal language. |
|
| 166 | + * |
|
| 167 | + * This feature is not yet available. |
|
| 168 | + * You can help us do this if you are nice or wish to see the developed framework. |
|
| 169 | + */ |
|
| 170 | 170 | define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
| 171 | 171 | |
| 172 | 172 | /** |
| 173 | - * The path to the directory of your personal libraries |
|
| 174 | - * |
|
| 175 | - * It contains your PHP classes, package, etc. |
|
| 176 | - * Note: you should not put your personal libraries in the system library directory, |
|
| 177 | - * because it is recalled in case of updating the system you might have surprises. |
|
| 178 | - */ |
|
| 173 | + * The path to the directory of your personal libraries |
|
| 174 | + * |
|
| 175 | + * It contains your PHP classes, package, etc. |
|
| 176 | + * Note: you should not put your personal libraries in the system library directory, |
|
| 177 | + * because it is recalled in case of updating the system you might have surprises. |
|
| 178 | + */ |
|
| 179 | 179 | define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
| 180 | 180 | |
| 181 | 181 | /** |
| 182 | - * The path to the directory that contains the log files. |
|
| 183 | - * |
|
| 184 | - * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 185 | - * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 186 | - * details see the documentation of your web server. |
|
| 187 | - * Example for Unix or linux with apache web server: |
|
| 188 | - * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 189 | - * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 190 | - */ |
|
| 182 | + * The path to the directory that contains the log files. |
|
| 183 | + * |
|
| 184 | + * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 185 | + * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 186 | + * details see the documentation of your web server. |
|
| 187 | + * Example for Unix or linux with apache web server: |
|
| 188 | + * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 189 | + * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 190 | + */ |
|
| 191 | 191 | define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | - * The path to the modules directory. |
|
| 195 | - * |
|
| 196 | - * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 197 | - * in HMVC architecture (hierichical, controllers, models, views). |
|
| 198 | - */ |
|
| 194 | + * The path to the modules directory. |
|
| 195 | + * |
|
| 196 | + * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 197 | + * in HMVC architecture (hierichical, controllers, models, views). |
|
| 198 | + */ |
|
| 199 | 199 | define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
| 200 | 200 | |
| 201 | 201 | /** |
| 202 | - * The path to the directory of sources external to your application. |
|
| 203 | - * |
|
| 204 | - * If you have already used "composer" you know what that means. |
|
| 205 | - */ |
|
| 202 | + * The path to the directory of sources external to your application. |
|
| 203 | + * |
|
| 204 | + * If you have already used "composer" you know what that means. |
|
| 205 | + */ |
|
| 206 | 206 | define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
| 207 | 207 | |
| 208 | 208 | /** |
| 209 | - * The front controller of your application. |
|
| 210 | - * |
|
| 211 | - * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 212 | - * your application by using the rewrite module URL of your web server . |
|
| 213 | - * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 214 | - * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 215 | - */ |
|
| 209 | + * The front controller of your application. |
|
| 210 | + * |
|
| 211 | + * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 212 | + * your application by using the rewrite module URL of your web server . |
|
| 213 | + * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 214 | + * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 215 | + */ |
|
| 216 | 216 | define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -221,14 +221,14 @@ discard block |
||
| 221 | 221 | define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | - * The environment of your application (production, test, development). |
|
| 225 | - * |
|
| 226 | - * if your application is still in development you use the value "development" |
|
| 227 | - * so you will have the display of the error messages, etc. |
|
| 228 | - * Once you finish the development of your application that is to put it online |
|
| 229 | - * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 230 | - * the loading of the system, will be fast. |
|
| 231 | - */ |
|
| 224 | + * The environment of your application (production, test, development). |
|
| 225 | + * |
|
| 226 | + * if your application is still in development you use the value "development" |
|
| 227 | + * so you will have the display of the error messages, etc. |
|
| 228 | + * Once you finish the development of your application that is to put it online |
|
| 229 | + * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 230 | + * the loading of the system, will be fast. |
|
| 231 | + */ |
|
| 232 | 232 | define('ENVIRONMENT', 'testing'); |
| 233 | 233 | |
| 234 | 234 | |
@@ -262,13 +262,13 @@ discard block |
||
| 262 | 262 | require_once 'include/testsUtil.php'; |
| 263 | 263 | |
| 264 | 264 | /** |
| 265 | - * Setting of the PHP error message handling function |
|
| 266 | - */ |
|
| 265 | + * Setting of the PHP error message handling function |
|
| 266 | + */ |
|
| 267 | 267 | set_error_handler('php_error_handler'); |
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | - * Setting of the PHP error exception handling function |
|
| 271 | - */ |
|
| 270 | + * Setting of the PHP error exception handling function |
|
| 271 | + */ |
|
| 272 | 272 | set_exception_handler('php_exception_handler'); |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -277,8 +277,8 @@ discard block |
||
| 277 | 277 | register_shutdown_function('php_shudown_handler'); |
| 278 | 278 | |
| 279 | 279 | /** |
| 280 | - * Register the tests autoload |
|
| 281 | - */ |
|
| 280 | + * Register the tests autoload |
|
| 281 | + */ |
|
| 282 | 282 | spl_autoload_register('tests_autoload'); |
| 283 | 283 | |
| 284 | 284 | |
@@ -1,556 +1,556 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | - * validation-in-model support, event callbacks and more. |
|
| 31 | - * |
|
| 32 | - * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | - * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | - */ |
|
| 35 | - |
|
| 36 | - class Model{ |
|
| 37 | - |
|
| 38 | - /* -------------------------------------------------------------- |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | + * validation-in-model support, event callbacks and more. |
|
| 31 | + * |
|
| 32 | + * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | + * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | + */ |
|
| 35 | + |
|
| 36 | + class Model{ |
|
| 37 | + |
|
| 38 | + /* -------------------------------------------------------------- |
|
| 39 | 39 | * VARIABLES |
| 40 | 40 | * ------------------------------------------------------------ */ |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * This model's default database table. Automatically |
|
| 44 | - * guessed by pluralising the model name. |
|
| 45 | - */ |
|
| 46 | - protected $_table; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The database connection object. Will be set to the default |
|
| 50 | - * connection. This allows individual models to use different DBs |
|
| 51 | - * without overwriting the global database connection. |
|
| 52 | - */ |
|
| 53 | - protected $_database; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * This model's default primary key or unique identifier. |
|
| 57 | - * Used by the get(), update() and delete() functions. |
|
| 58 | - */ |
|
| 59 | - protected $primary_key = 'id'; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Support for soft deletes and this model's 'deleted' key |
|
| 63 | - */ |
|
| 64 | - protected $soft_delete = false; |
|
| 65 | - protected $soft_delete_key = 'is_deleted'; |
|
| 66 | - protected $_temporary_with_deleted = FALSE; |
|
| 67 | - protected $_temporary_only_deleted = FALSE; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The various callbacks available to the model. Each are |
|
| 71 | - * simple lists of method names (methods will be run on $this). |
|
| 72 | - */ |
|
| 73 | - protected $before_create = array(); |
|
| 74 | - protected $after_create = array(); |
|
| 75 | - protected $before_update = array(); |
|
| 76 | - protected $after_update = array(); |
|
| 77 | - protected $before_get = array(); |
|
| 78 | - protected $after_get = array(); |
|
| 79 | - protected $before_delete = array(); |
|
| 80 | - protected $after_delete = array(); |
|
| 81 | - |
|
| 82 | - protected $callback_parameters = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Protected, non-modifiable attributes |
|
| 86 | - */ |
|
| 87 | - protected $protected_attributes = array(); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | - * => array to customise the class name and primary key |
|
| 92 | - */ |
|
| 93 | - protected $belongs_to = array(); |
|
| 94 | - protected $has_many = array(); |
|
| 95 | - |
|
| 96 | - protected $_with = array(); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * An array of validation rules. This needs to be the same format |
|
| 100 | - * as validation rules passed to the FormValidation library. |
|
| 101 | - */ |
|
| 102 | - protected $validate = array(); |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Optionally skip the validation. Used in conjunction with |
|
| 106 | - * skip_validation() to skip data validation for any future calls. |
|
| 107 | - */ |
|
| 108 | - protected $skip_validation = FALSE; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * By default we return our results as objects. If we need to override |
|
| 112 | - * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | - */ |
|
| 114 | - protected $return_type = 'object'; |
|
| 115 | - protected $_temporary_return_type = NULL; |
|
| 42 | + /** |
|
| 43 | + * This model's default database table. Automatically |
|
| 44 | + * guessed by pluralising the model name. |
|
| 45 | + */ |
|
| 46 | + protected $_table; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The database connection object. Will be set to the default |
|
| 50 | + * connection. This allows individual models to use different DBs |
|
| 51 | + * without overwriting the global database connection. |
|
| 52 | + */ |
|
| 53 | + protected $_database; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * This model's default primary key or unique identifier. |
|
| 57 | + * Used by the get(), update() and delete() functions. |
|
| 58 | + */ |
|
| 59 | + protected $primary_key = 'id'; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Support for soft deletes and this model's 'deleted' key |
|
| 63 | + */ |
|
| 64 | + protected $soft_delete = false; |
|
| 65 | + protected $soft_delete_key = 'is_deleted'; |
|
| 66 | + protected $_temporary_with_deleted = FALSE; |
|
| 67 | + protected $_temporary_only_deleted = FALSE; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The various callbacks available to the model. Each are |
|
| 71 | + * simple lists of method names (methods will be run on $this). |
|
| 72 | + */ |
|
| 73 | + protected $before_create = array(); |
|
| 74 | + protected $after_create = array(); |
|
| 75 | + protected $before_update = array(); |
|
| 76 | + protected $after_update = array(); |
|
| 77 | + protected $before_get = array(); |
|
| 78 | + protected $after_get = array(); |
|
| 79 | + protected $before_delete = array(); |
|
| 80 | + protected $after_delete = array(); |
|
| 81 | + |
|
| 82 | + protected $callback_parameters = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Protected, non-modifiable attributes |
|
| 86 | + */ |
|
| 87 | + protected $protected_attributes = array(); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | + * => array to customise the class name and primary key |
|
| 92 | + */ |
|
| 93 | + protected $belongs_to = array(); |
|
| 94 | + protected $has_many = array(); |
|
| 95 | + |
|
| 96 | + protected $_with = array(); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * An array of validation rules. This needs to be the same format |
|
| 100 | + * as validation rules passed to the FormValidation library. |
|
| 101 | + */ |
|
| 102 | + protected $validate = array(); |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Optionally skip the validation. Used in conjunction with |
|
| 106 | + * skip_validation() to skip data validation for any future calls. |
|
| 107 | + */ |
|
| 108 | + protected $skip_validation = FALSE; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * By default we return our results as objects. If we need to override |
|
| 112 | + * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | + */ |
|
| 114 | + protected $return_type = 'object'; |
|
| 115 | + protected $_temporary_return_type = NULL; |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - /** |
|
| 118 | + /** |
|
| 119 | 119 | The database cache time |
| 120 | - */ |
|
| 121 | - protected $dbCacheTime = 0; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Instance of the Loader class |
|
| 125 | - * @var Loader |
|
| 126 | - */ |
|
| 127 | - protected $loaderInstance = null; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Instance of the FormValidation library |
|
| 131 | - * @var FormValidation |
|
| 132 | - */ |
|
| 133 | - protected $formValidationInstance = null; |
|
| 120 | + */ |
|
| 121 | + protected $dbCacheTime = 0; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Instance of the Loader class |
|
| 125 | + * @var Loader |
|
| 126 | + */ |
|
| 127 | + protected $loaderInstance = null; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Instance of the FormValidation library |
|
| 131 | + * @var FormValidation |
|
| 132 | + */ |
|
| 133 | + protected $formValidationInstance = null; |
|
| 134 | 134 | |
| 135 | - /* -------------------------------------------------------------- |
|
| 135 | + /* -------------------------------------------------------------- |
|
| 136 | 136 | * GENERIC METHODS |
| 137 | 137 | * ------------------------------------------------------------ */ |
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | - * try our best to guess the table name. |
|
| 142 | - */ |
|
| 143 | - public function __construct(Database $db = null){ |
|
| 144 | - if (is_object($db)){ |
|
| 145 | - $this->setDatabaseInstance($db); |
|
| 146 | - } |
|
| 147 | - else{ |
|
| 148 | - $obj = & get_instance(); |
|
| 149 | - if (isset($obj->database) && is_object($obj->database)){ |
|
| 150 | - /** |
|
| 151 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | - * to prevent duplication |
|
| 153 | - */ |
|
| 154 | - $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | - array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | - $this->_temporary_return_type = $this->return_type; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /* -------------------------------------------------------------- |
|
| 139 | + /** |
|
| 140 | + * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | + * try our best to guess the table name. |
|
| 142 | + */ |
|
| 143 | + public function __construct(Database $db = null){ |
|
| 144 | + if (is_object($db)){ |
|
| 145 | + $this->setDatabaseInstance($db); |
|
| 146 | + } |
|
| 147 | + else{ |
|
| 148 | + $obj = & get_instance(); |
|
| 149 | + if (isset($obj->database) && is_object($obj->database)){ |
|
| 150 | + /** |
|
| 151 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | + * to prevent duplication |
|
| 153 | + */ |
|
| 154 | + $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | + array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | + $this->_temporary_return_type = $this->return_type; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /* -------------------------------------------------------------- |
|
| 164 | 164 | * CRUD INTERFACE |
| 165 | 165 | * ------------------------------------------------------------ */ |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | - */ |
|
| 170 | - public function get($primary_value) |
|
| 171 | - { |
|
| 172 | - return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | - * any valid value to DatabaseQueryBuilder->where(). |
|
| 178 | - */ |
|
| 179 | - public function get_by() |
|
| 180 | - { |
|
| 181 | - $where = func_get_args(); |
|
| 182 | - |
|
| 183 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | - { |
|
| 185 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | - } |
|
| 187 | - $this->_set_where($where); |
|
| 188 | - |
|
| 189 | - $this->trigger('before_get'); |
|
| 167 | + /** |
|
| 168 | + * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | + */ |
|
| 170 | + public function get($primary_value) |
|
| 171 | + { |
|
| 172 | + return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | + * any valid value to DatabaseQueryBuilder->where(). |
|
| 178 | + */ |
|
| 179 | + public function get_by() |
|
| 180 | + { |
|
| 181 | + $where = func_get_args(); |
|
| 182 | + |
|
| 183 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | + { |
|
| 185 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | + } |
|
| 187 | + $this->_set_where($where); |
|
| 188 | + |
|
| 189 | + $this->trigger('before_get'); |
|
| 190 | 190 | $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
| 191 | - $this->getQueryBuilder()->from($this->_table); |
|
| 191 | + $this->getQueryBuilder()->from($this->_table); |
|
| 192 | 192 | $row = $this->_database->get($type); |
| 193 | - $this->_temporary_return_type = $this->return_type; |
|
| 194 | - $row = $this->trigger('after_get', $row); |
|
| 195 | - $this->_with = array(); |
|
| 196 | - return $row; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Fetch an array of records based on an array of primary values. |
|
| 201 | - */ |
|
| 202 | - public function get_many($values) |
|
| 203 | - { |
|
| 204 | - $this->getQueryBuilder()->in($this->primary_key, $values); |
|
| 205 | - return $this->get_all(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | - */ |
|
| 211 | - public function get_many_by() |
|
| 212 | - { |
|
| 213 | - $where = func_get_args(); |
|
| 214 | - $this->_set_where($where); |
|
| 215 | - return $this->get_all(); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | - * to $this->_database->get() with scoped methods. |
|
| 221 | - */ |
|
| 222 | - public function get_all() |
|
| 223 | - { |
|
| 224 | - $this->trigger('before_get'); |
|
| 225 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | - { |
|
| 227 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | - } |
|
| 193 | + $this->_temporary_return_type = $this->return_type; |
|
| 194 | + $row = $this->trigger('after_get', $row); |
|
| 195 | + $this->_with = array(); |
|
| 196 | + return $row; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Fetch an array of records based on an array of primary values. |
|
| 201 | + */ |
|
| 202 | + public function get_many($values) |
|
| 203 | + { |
|
| 204 | + $this->getQueryBuilder()->in($this->primary_key, $values); |
|
| 205 | + return $this->get_all(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | + */ |
|
| 211 | + public function get_many_by() |
|
| 212 | + { |
|
| 213 | + $where = func_get_args(); |
|
| 214 | + $this->_set_where($where); |
|
| 215 | + return $this->get_all(); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | + * to $this->_database->get() with scoped methods. |
|
| 221 | + */ |
|
| 222 | + public function get_all() |
|
| 223 | + { |
|
| 224 | + $this->trigger('before_get'); |
|
| 225 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | + { |
|
| 227 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | + } |
|
| 229 | 229 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
| 230 | - $this->getQueryBuilder()->from($this->_table); |
|
| 230 | + $this->getQueryBuilder()->from($this->_table); |
|
| 231 | 231 | $result = $this->_database->getAll($type); |
| 232 | - $this->_temporary_return_type = $this->return_type; |
|
| 233 | - |
|
| 234 | - foreach ($result as $key => &$row) |
|
| 235 | - { |
|
| 236 | - $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 237 | - } |
|
| 238 | - $this->_with = array(); |
|
| 239 | - return $result; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Insert a new row into the table. $data should be an associative array |
|
| 244 | - * of data to be inserted. Returns newly created ID. |
|
| 232 | + $this->_temporary_return_type = $this->return_type; |
|
| 233 | + |
|
| 234 | + foreach ($result as $key => &$row) |
|
| 235 | + { |
|
| 236 | + $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 237 | + } |
|
| 238 | + $this->_with = array(); |
|
| 239 | + return $result; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Insert a new row into the table. $data should be an associative array |
|
| 244 | + * of data to be inserted. Returns newly created ID. |
|
| 245 | 245 | * @see Database::insert |
| 246 | - */ |
|
| 247 | - public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 248 | - { |
|
| 249 | - if ($skip_validation === FALSE) |
|
| 250 | - { |
|
| 251 | - $data = $this->validate($data); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if ($data !== FALSE) |
|
| 255 | - { |
|
| 256 | - $data = $this->trigger('before_create', $data); |
|
| 257 | - $this->getQueryBuilder()->from($this->_table); |
|
| 246 | + */ |
|
| 247 | + public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 248 | + { |
|
| 249 | + if ($skip_validation === FALSE) |
|
| 250 | + { |
|
| 251 | + $data = $this->validate($data); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if ($data !== FALSE) |
|
| 255 | + { |
|
| 256 | + $data = $this->trigger('before_create', $data); |
|
| 257 | + $this->getQueryBuilder()->from($this->_table); |
|
| 258 | 258 | $this->_database->insert($data, $escape); |
| 259 | - $insert_id = $this->_database->insertId(); |
|
| 260 | - $this->trigger('after_create', $insert_id); |
|
| 259 | + $insert_id = $this->_database->insertId(); |
|
| 260 | + $this->trigger('after_create', $insert_id); |
|
| 261 | 261 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 262 | 262 | return ! $insert_id ? true : $insert_id; |
| 263 | - } |
|
| 264 | - else |
|
| 265 | - { |
|
| 266 | - return FALSE; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 272 | - */ |
|
| 273 | - public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 274 | - { |
|
| 275 | - $ids = array(); |
|
| 276 | - foreach ($data as $key => $row) |
|
| 277 | - { |
|
| 278 | - $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 279 | - } |
|
| 280 | - return $ids; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Updated a record based on the primary value. |
|
| 285 | - */ |
|
| 286 | - public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 287 | - { |
|
| 288 | - $data = $this->trigger('before_update', $data); |
|
| 289 | - if ($skip_validation === FALSE) |
|
| 290 | - { |
|
| 291 | - $data = $this->validate($data); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - if ($data !== FALSE) |
|
| 295 | - { |
|
| 296 | - $this->getQueryBuilder()->where($this->primary_key, $primary_value) |
|
| 297 | - ->from($this->_table); |
|
| 298 | - $result = $this->_database->update($data, $escape); |
|
| 299 | - $this->trigger('after_update', array($data, $result)); |
|
| 300 | - return $result; |
|
| 301 | - } |
|
| 302 | - else |
|
| 303 | - { |
|
| 304 | - return FALSE; |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Update many records, based on an array of primary values. |
|
| 310 | - */ |
|
| 311 | - public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 312 | - { |
|
| 313 | - $data = $this->trigger('before_update', $data); |
|
| 314 | - if ($skip_validation === FALSE) |
|
| 315 | - { |
|
| 316 | - $data = $this->validate($data); |
|
| 317 | - } |
|
| 318 | - if ($data !== FALSE) |
|
| 319 | - { |
|
| 320 | - $this->getQueryBuilder()->in($this->primary_key, $primary_values) |
|
| 321 | - ->from($this->_table); |
|
| 263 | + } |
|
| 264 | + else |
|
| 265 | + { |
|
| 266 | + return FALSE; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 272 | + */ |
|
| 273 | + public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 274 | + { |
|
| 275 | + $ids = array(); |
|
| 276 | + foreach ($data as $key => $row) |
|
| 277 | + { |
|
| 278 | + $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 279 | + } |
|
| 280 | + return $ids; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Updated a record based on the primary value. |
|
| 285 | + */ |
|
| 286 | + public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 287 | + { |
|
| 288 | + $data = $this->trigger('before_update', $data); |
|
| 289 | + if ($skip_validation === FALSE) |
|
| 290 | + { |
|
| 291 | + $data = $this->validate($data); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + if ($data !== FALSE) |
|
| 295 | + { |
|
| 296 | + $this->getQueryBuilder()->where($this->primary_key, $primary_value) |
|
| 297 | + ->from($this->_table); |
|
| 322 | 298 | $result = $this->_database->update($data, $escape); |
| 323 | - $this->trigger('after_update', array($data, $result)); |
|
| 324 | - return $result; |
|
| 325 | - } |
|
| 326 | - else |
|
| 327 | - { |
|
| 328 | - return FALSE; |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Updated a record based on an arbitrary WHERE clause. |
|
| 334 | - */ |
|
| 335 | - public function update_by() |
|
| 336 | - { |
|
| 337 | - $args = func_get_args(); |
|
| 338 | - $data = array(); |
|
| 339 | - if (count($args) == 2){ |
|
| 340 | - if (is_array($args[1])){ |
|
| 341 | - $data = array_pop($args); |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - else if (count($args) == 3){ |
|
| 345 | - if (is_array($args[2])){ |
|
| 346 | - $data = array_pop($args); |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - $data = $this->trigger('before_update', $data); |
|
| 350 | - if ($this->validate($data) !== FALSE) |
|
| 351 | - { |
|
| 352 | - $this->_set_where($args); |
|
| 353 | - $this->getQueryBuilder()->from($this->_table); |
|
| 299 | + $this->trigger('after_update', array($data, $result)); |
|
| 300 | + return $result; |
|
| 301 | + } |
|
| 302 | + else |
|
| 303 | + { |
|
| 304 | + return FALSE; |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Update many records, based on an array of primary values. |
|
| 310 | + */ |
|
| 311 | + public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 312 | + { |
|
| 313 | + $data = $this->trigger('before_update', $data); |
|
| 314 | + if ($skip_validation === FALSE) |
|
| 315 | + { |
|
| 316 | + $data = $this->validate($data); |
|
| 317 | + } |
|
| 318 | + if ($data !== FALSE) |
|
| 319 | + { |
|
| 320 | + $this->getQueryBuilder()->in($this->primary_key, $primary_values) |
|
| 321 | + ->from($this->_table); |
|
| 322 | + $result = $this->_database->update($data, $escape); |
|
| 323 | + $this->trigger('after_update', array($data, $result)); |
|
| 324 | + return $result; |
|
| 325 | + } |
|
| 326 | + else |
|
| 327 | + { |
|
| 328 | + return FALSE; |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Updated a record based on an arbitrary WHERE clause. |
|
| 334 | + */ |
|
| 335 | + public function update_by() |
|
| 336 | + { |
|
| 337 | + $args = func_get_args(); |
|
| 338 | + $data = array(); |
|
| 339 | + if (count($args) == 2){ |
|
| 340 | + if (is_array($args[1])){ |
|
| 341 | + $data = array_pop($args); |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + else if (count($args) == 3){ |
|
| 345 | + if (is_array($args[2])){ |
|
| 346 | + $data = array_pop($args); |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + $data = $this->trigger('before_update', $data); |
|
| 350 | + if ($this->validate($data) !== FALSE) |
|
| 351 | + { |
|
| 352 | + $this->_set_where($args); |
|
| 353 | + $this->getQueryBuilder()->from($this->_table); |
|
| 354 | 354 | $result = $this->_database->update($data); |
| 355 | - $this->trigger('after_update', array($data, $result)); |
|
| 356 | - return $result; |
|
| 357 | - } |
|
| 358 | - else |
|
| 359 | - { |
|
| 360 | - return FALSE; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Update all records |
|
| 366 | - */ |
|
| 367 | - public function update_all($data = array(), $escape = true) |
|
| 368 | - { |
|
| 369 | - $data = $this->trigger('before_update', $data); |
|
| 370 | - $this->getQueryBuilder()->from($this->_table); |
|
| 355 | + $this->trigger('after_update', array($data, $result)); |
|
| 356 | + return $result; |
|
| 357 | + } |
|
| 358 | + else |
|
| 359 | + { |
|
| 360 | + return FALSE; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Update all records |
|
| 366 | + */ |
|
| 367 | + public function update_all($data = array(), $escape = true) |
|
| 368 | + { |
|
| 369 | + $data = $this->trigger('before_update', $data); |
|
| 370 | + $this->getQueryBuilder()->from($this->_table); |
|
| 371 | 371 | $result = $this->_database->update($data, $escape); |
| 372 | - $this->trigger('after_update', array($data, $result)); |
|
| 373 | - return $result; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Delete a row from the table by the primary value |
|
| 378 | - */ |
|
| 379 | - public function delete($id) |
|
| 380 | - { |
|
| 381 | - $this->trigger('before_delete', $id); |
|
| 382 | - $this->getQueryBuilder()->where($this->primary_key, $id); |
|
| 372 | + $this->trigger('after_update', array($data, $result)); |
|
| 373 | + return $result; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Delete a row from the table by the primary value |
|
| 378 | + */ |
|
| 379 | + public function delete($id) |
|
| 380 | + { |
|
| 381 | + $this->trigger('before_delete', $id); |
|
| 382 | + $this->getQueryBuilder()->where($this->primary_key, $id); |
|
| 383 | 383 | $result = false; |
| 384 | - if ($this->soft_delete) |
|
| 385 | - { |
|
| 386 | - $this->getQueryBuilder()->from($this->_table); |
|
| 384 | + if ($this->soft_delete) |
|
| 385 | + { |
|
| 386 | + $this->getQueryBuilder()->from($this->_table); |
|
| 387 | 387 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 388 | - } |
|
| 389 | - else |
|
| 390 | - { |
|
| 391 | - $this->getQueryBuilder()->from($this->_table); |
|
| 388 | + } |
|
| 389 | + else |
|
| 390 | + { |
|
| 391 | + $this->getQueryBuilder()->from($this->_table); |
|
| 392 | 392 | $result = $this->_database->delete(); |
| 393 | - } |
|
| 394 | - |
|
| 395 | - $this->trigger('after_delete', $result); |
|
| 396 | - return $result; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Delete a row from the database table by an arbitrary WHERE clause |
|
| 401 | - */ |
|
| 402 | - public function delete_by() |
|
| 403 | - { |
|
| 404 | - $where = func_get_args(); |
|
| 405 | - $where = $this->trigger('before_delete', $where); |
|
| 406 | - $this->_set_where($where); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + $this->trigger('after_delete', $result); |
|
| 396 | + return $result; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Delete a row from the database table by an arbitrary WHERE clause |
|
| 401 | + */ |
|
| 402 | + public function delete_by() |
|
| 403 | + { |
|
| 404 | + $where = func_get_args(); |
|
| 405 | + $where = $this->trigger('before_delete', $where); |
|
| 406 | + $this->_set_where($where); |
|
| 407 | 407 | $result = false; |
| 408 | - if ($this->soft_delete) |
|
| 409 | - { |
|
| 410 | - $this->getQueryBuilder()->from($this->_table); |
|
| 408 | + if ($this->soft_delete) |
|
| 409 | + { |
|
| 410 | + $this->getQueryBuilder()->from($this->_table); |
|
| 411 | 411 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 412 | - } |
|
| 413 | - else |
|
| 414 | - { |
|
| 415 | - $this->getQueryBuilder()->from($this->_table); |
|
| 412 | + } |
|
| 413 | + else |
|
| 414 | + { |
|
| 415 | + $this->getQueryBuilder()->from($this->_table); |
|
| 416 | 416 | $result = $this->_database->delete(); |
| 417 | - } |
|
| 418 | - $this->trigger('after_delete', $result); |
|
| 419 | - return $result; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Delete many rows from the database table by multiple primary values |
|
| 424 | - */ |
|
| 425 | - public function delete_many($primary_values) |
|
| 426 | - { |
|
| 427 | - $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 428 | - $this->getQueryBuilder()->in($this->primary_key, $primary_values); |
|
| 417 | + } |
|
| 418 | + $this->trigger('after_delete', $result); |
|
| 419 | + return $result; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Delete many rows from the database table by multiple primary values |
|
| 424 | + */ |
|
| 425 | + public function delete_many($primary_values) |
|
| 426 | + { |
|
| 427 | + $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 428 | + $this->getQueryBuilder()->in($this->primary_key, $primary_values); |
|
| 429 | 429 | $result = false; |
| 430 | - if ($this->soft_delete) |
|
| 431 | - { |
|
| 432 | - $this->getQueryBuilder()->from($this->_table); |
|
| 430 | + if ($this->soft_delete) |
|
| 431 | + { |
|
| 432 | + $this->getQueryBuilder()->from($this->_table); |
|
| 433 | 433 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 434 | - } |
|
| 435 | - else |
|
| 436 | - { |
|
| 437 | - $this->getQueryBuilder()->from($this->_table); |
|
| 434 | + } |
|
| 435 | + else |
|
| 436 | + { |
|
| 437 | + $this->getQueryBuilder()->from($this->_table); |
|
| 438 | 438 | $result = $this->_database->delete(); |
| 439 | - } |
|
| 440 | - $this->trigger('after_delete', $result); |
|
| 441 | - return $result; |
|
| 442 | - } |
|
| 439 | + } |
|
| 440 | + $this->trigger('after_delete', $result); |
|
| 441 | + return $result; |
|
| 442 | + } |
|
| 443 | 443 | |
| 444 | 444 | |
| 445 | - /** |
|
| 446 | - * Truncates the table |
|
| 447 | - */ |
|
| 448 | - public function truncate() |
|
| 449 | - { |
|
| 445 | + /** |
|
| 446 | + * Truncates the table |
|
| 447 | + */ |
|
| 448 | + public function truncate() |
|
| 449 | + { |
|
| 450 | 450 | $this->getQueryBuilder()->from($this->_table); |
| 451 | 451 | $result = $this->_database->delete(); |
| 452 | - return $result; |
|
| 453 | - } |
|
| 452 | + return $result; |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | - /* -------------------------------------------------------------- |
|
| 455 | + /* -------------------------------------------------------------- |
|
| 456 | 456 | * RELATIONSHIPS |
| 457 | 457 | * ------------------------------------------------------------ */ |
| 458 | 458 | |
| 459 | - public function with($relationship) |
|
| 460 | - { |
|
| 461 | - $this->_with[] = $relationship; |
|
| 462 | - if (!in_array('relate', $this->after_get)) |
|
| 463 | - { |
|
| 464 | - $this->after_get[] = 'relate'; |
|
| 465 | - } |
|
| 466 | - return $this; |
|
| 467 | - } |
|
| 459 | + public function with($relationship) |
|
| 460 | + { |
|
| 461 | + $this->_with[] = $relationship; |
|
| 462 | + if (!in_array('relate', $this->after_get)) |
|
| 463 | + { |
|
| 464 | + $this->after_get[] = 'relate'; |
|
| 465 | + } |
|
| 466 | + return $this; |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | 469 | /** |
| 470 | - * Relationship |
|
| 471 | - */ |
|
| 472 | - public function relate($row) |
|
| 473 | - { |
|
| 474 | - if (empty($row)) |
|
| 475 | - { |
|
| 476 | - return $row; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - $row = $this->relateBelongsTo($row); |
|
| 480 | - $row = $this->relateHasMany($row); |
|
| 481 | - return $row; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /* -------------------------------------------------------------- |
|
| 470 | + * Relationship |
|
| 471 | + */ |
|
| 472 | + public function relate($row) |
|
| 473 | + { |
|
| 474 | + if (empty($row)) |
|
| 475 | + { |
|
| 476 | + return $row; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + $row = $this->relateBelongsTo($row); |
|
| 480 | + $row = $this->relateHasMany($row); |
|
| 481 | + return $row; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /* -------------------------------------------------------------- |
|
| 485 | 485 | * UTILITY METHODS |
| 486 | 486 | * ------------------------------------------------------------ */ |
| 487 | 487 | |
| 488 | - /** |
|
| 489 | - * Retrieve and generate a form_dropdown friendly array |
|
| 490 | - */ |
|
| 491 | - public function dropdown() |
|
| 492 | - { |
|
| 493 | - $args = func_get_args(); |
|
| 494 | - if (count($args) == 2) |
|
| 495 | - { |
|
| 496 | - list($key, $value) = $args; |
|
| 497 | - } |
|
| 498 | - else |
|
| 499 | - { |
|
| 500 | - $key = $this->primary_key; |
|
| 501 | - $value = $args[0]; |
|
| 502 | - } |
|
| 503 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 504 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 505 | - { |
|
| 506 | - $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
|
| 507 | - } |
|
| 508 | - $this->getQueryBuilder() |
|
| 488 | + /** |
|
| 489 | + * Retrieve and generate a form_dropdown friendly array |
|
| 490 | + */ |
|
| 491 | + public function dropdown() |
|
| 492 | + { |
|
| 493 | + $args = func_get_args(); |
|
| 494 | + if (count($args) == 2) |
|
| 495 | + { |
|
| 496 | + list($key, $value) = $args; |
|
| 497 | + } |
|
| 498 | + else |
|
| 499 | + { |
|
| 500 | + $key = $this->primary_key; |
|
| 501 | + $value = $args[0]; |
|
| 502 | + } |
|
| 503 | + $this->trigger('before_dropdown', array( $key, $value )); |
|
| 504 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 505 | + { |
|
| 506 | + $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
|
| 507 | + } |
|
| 508 | + $this->getQueryBuilder() |
|
| 509 | 509 | ->select(array($key, $value)) |
| 510 | 510 | ->from($this->_table); |
| 511 | 511 | $result = $this->_database->getAll(); |
| 512 | - $options = array(); |
|
| 513 | - foreach ($result as $row) |
|
| 514 | - { |
|
| 515 | - $options[$row->{$key}] = $row->{$value}; |
|
| 516 | - } |
|
| 517 | - $options = $this->trigger('after_dropdown', $options); |
|
| 518 | - return $options; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 523 | - */ |
|
| 524 | - public function count_by() |
|
| 525 | - { |
|
| 526 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 527 | - { |
|
| 528 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 529 | - } |
|
| 530 | - $where = func_get_args(); |
|
| 531 | - $this->_set_where($where); |
|
| 532 | - $this->getQueryBuilder()->from($this->_table); |
|
| 512 | + $options = array(); |
|
| 513 | + foreach ($result as $row) |
|
| 514 | + { |
|
| 515 | + $options[$row->{$key}] = $row->{$value}; |
|
| 516 | + } |
|
| 517 | + $options = $this->trigger('after_dropdown', $options); |
|
| 518 | + return $options; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 523 | + */ |
|
| 524 | + public function count_by() |
|
| 525 | + { |
|
| 526 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 527 | + { |
|
| 528 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 529 | + } |
|
| 530 | + $where = func_get_args(); |
|
| 531 | + $this->_set_where($where); |
|
| 532 | + $this->getQueryBuilder()->from($this->_table); |
|
| 533 | 533 | $this->_database->getAll(); |
| 534 | - return $this->_database->numRows(); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Fetch a total count of rows, disregarding any previous conditions |
|
| 539 | - */ |
|
| 540 | - public function count_all() |
|
| 541 | - { |
|
| 542 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 543 | - { |
|
| 544 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 545 | - } |
|
| 534 | + return $this->_database->numRows(); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * Fetch a total count of rows, disregarding any previous conditions |
|
| 539 | + */ |
|
| 540 | + public function count_all() |
|
| 541 | + { |
|
| 542 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 543 | + { |
|
| 544 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 545 | + } |
|
| 546 | 546 | $this->getQueryBuilder()->from($this->_table); |
| 547 | 547 | $this->_database->getAll(); |
| 548 | - return $this->_database->numRows(); |
|
| 549 | - } |
|
| 548 | + return $this->_database->numRows(); |
|
| 549 | + } |
|
| 550 | 550 | |
| 551 | 551 | /** |
| 552 | - * Enabled cache temporary |
|
| 553 | - */ |
|
| 552 | + * Enabled cache temporary |
|
| 553 | + */ |
|
| 554 | 554 | public function cached($ttl = 0){ |
| 555 | 555 | if ($ttl > 0){ |
| 556 | 556 | $this->_database = $this->_database->cached($ttl); |
@@ -558,424 +558,424 @@ discard block |
||
| 558 | 558 | return $this; |
| 559 | 559 | } |
| 560 | 560 | |
| 561 | - /** |
|
| 562 | - * Tell the class to skip the insert validation |
|
| 563 | - */ |
|
| 564 | - public function skip_validation() |
|
| 565 | - { |
|
| 566 | - $this->skip_validation = TRUE; |
|
| 567 | - return $this; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * Get the skip validation status |
|
| 572 | - */ |
|
| 573 | - public function get_skip_validation() |
|
| 574 | - { |
|
| 575 | - return $this->skip_validation; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Return the next auto increment of the table. Only tested on MySQL. |
|
| 580 | - */ |
|
| 581 | - public function get_next_id() |
|
| 582 | - { |
|
| 561 | + /** |
|
| 562 | + * Tell the class to skip the insert validation |
|
| 563 | + */ |
|
| 564 | + public function skip_validation() |
|
| 565 | + { |
|
| 566 | + $this->skip_validation = TRUE; |
|
| 567 | + return $this; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * Get the skip validation status |
|
| 572 | + */ |
|
| 573 | + public function get_skip_validation() |
|
| 574 | + { |
|
| 575 | + return $this->skip_validation; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Return the next auto increment of the table. Only tested on MySQL. |
|
| 580 | + */ |
|
| 581 | + public function get_next_id() |
|
| 582 | + { |
|
| 583 | 583 | $this->getQueryBuilder()->select('AUTO_INCREMENT') |
| 584 | 584 | ->from('information_schema.TABLES') |
| 585 | 585 | ->where('TABLE_NAME', $this->_table) |
| 586 | 586 | ->where('TABLE_SCHEMA', $this->_database->getDatabaseName()); |
| 587 | - return (int) $this->_database->get()->AUTO_INCREMENT; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Getter for the table name |
|
| 592 | - */ |
|
| 593 | - public function table() |
|
| 594 | - { |
|
| 595 | - return $this->_table; |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /* -------------------------------------------------------------- |
|
| 587 | + return (int) $this->_database->get()->AUTO_INCREMENT; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Getter for the table name |
|
| 592 | + */ |
|
| 593 | + public function table() |
|
| 594 | + { |
|
| 595 | + return $this->_table; |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /* -------------------------------------------------------------- |
|
| 599 | 599 | * GLOBAL SCOPES |
| 600 | 600 | * ------------------------------------------------------------ */ |
| 601 | 601 | |
| 602 | - /** |
|
| 603 | - * Return the next call as an array rather than an object |
|
| 604 | - */ |
|
| 605 | - public function as_array() |
|
| 606 | - { |
|
| 607 | - $this->_temporary_return_type = 'array'; |
|
| 608 | - return $this; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Return the next call as an object rather than an array |
|
| 613 | - */ |
|
| 614 | - public function as_object() |
|
| 615 | - { |
|
| 616 | - $this->_temporary_return_type = 'object'; |
|
| 617 | - return $this; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Don't care about soft deleted rows on the next call |
|
| 622 | - */ |
|
| 623 | - public function with_deleted() |
|
| 624 | - { |
|
| 625 | - $this->_temporary_with_deleted = TRUE; |
|
| 626 | - return $this; |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - /** |
|
| 630 | - * Only get deleted rows on the next call |
|
| 631 | - */ |
|
| 632 | - public function only_deleted() |
|
| 633 | - { |
|
| 634 | - $this->_temporary_only_deleted = TRUE; |
|
| 635 | - return $this; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /* -------------------------------------------------------------- |
|
| 602 | + /** |
|
| 603 | + * Return the next call as an array rather than an object |
|
| 604 | + */ |
|
| 605 | + public function as_array() |
|
| 606 | + { |
|
| 607 | + $this->_temporary_return_type = 'array'; |
|
| 608 | + return $this; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Return the next call as an object rather than an array |
|
| 613 | + */ |
|
| 614 | + public function as_object() |
|
| 615 | + { |
|
| 616 | + $this->_temporary_return_type = 'object'; |
|
| 617 | + return $this; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Don't care about soft deleted rows on the next call |
|
| 622 | + */ |
|
| 623 | + public function with_deleted() |
|
| 624 | + { |
|
| 625 | + $this->_temporary_with_deleted = TRUE; |
|
| 626 | + return $this; |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * Only get deleted rows on the next call |
|
| 631 | + */ |
|
| 632 | + public function only_deleted() |
|
| 633 | + { |
|
| 634 | + $this->_temporary_only_deleted = TRUE; |
|
| 635 | + return $this; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /* -------------------------------------------------------------- |
|
| 639 | 639 | * OBSERVERS |
| 640 | 640 | * ------------------------------------------------------------ */ |
| 641 | 641 | |
| 642 | - /** |
|
| 643 | - * MySQL DATETIME created_at and updated_at |
|
| 644 | - */ |
|
| 645 | - public function created_at($row) |
|
| 646 | - { |
|
| 647 | - if (is_object($row)) |
|
| 648 | - { |
|
| 649 | - $row->created_at = date('Y-m-d H:i:s'); |
|
| 650 | - } |
|
| 651 | - else |
|
| 652 | - { |
|
| 653 | - $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 654 | - } |
|
| 655 | - return $row; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - public function updated_at($row) |
|
| 659 | - { |
|
| 660 | - if (is_object($row)) |
|
| 661 | - { |
|
| 662 | - $row->updated_at = date('Y-m-d H:i:s'); |
|
| 663 | - } |
|
| 664 | - else |
|
| 665 | - { |
|
| 666 | - $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 667 | - } |
|
| 668 | - return $row; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - /** |
|
| 672 | - * Serialises data for you automatically, allowing you to pass |
|
| 673 | - * through objects and let it handle the serialisation in the background |
|
| 674 | - */ |
|
| 675 | - public function serialize($row) |
|
| 676 | - { |
|
| 677 | - foreach ($this->callback_parameters as $column) |
|
| 678 | - { |
|
| 679 | - $row[$column] = serialize($row[$column]); |
|
| 680 | - } |
|
| 681 | - return $row; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - public function unserialize($row) |
|
| 685 | - { |
|
| 686 | - foreach ($this->callback_parameters as $column) |
|
| 687 | - { |
|
| 688 | - if (is_array($row)) |
|
| 689 | - { |
|
| 690 | - $row[$column] = unserialize($row[$column]); |
|
| 691 | - } |
|
| 692 | - else |
|
| 693 | - { |
|
| 694 | - $row->$column = unserialize($row->$column); |
|
| 695 | - } |
|
| 696 | - } |
|
| 697 | - return $row; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * Protect attributes by removing them from $row array |
|
| 702 | - */ |
|
| 703 | - public function protect_attributes($row) |
|
| 704 | - { |
|
| 705 | - foreach ($this->protected_attributes as $attr) |
|
| 706 | - { |
|
| 707 | - if (is_object($row)) |
|
| 708 | - { |
|
| 642 | + /** |
|
| 643 | + * MySQL DATETIME created_at and updated_at |
|
| 644 | + */ |
|
| 645 | + public function created_at($row) |
|
| 646 | + { |
|
| 647 | + if (is_object($row)) |
|
| 648 | + { |
|
| 649 | + $row->created_at = date('Y-m-d H:i:s'); |
|
| 650 | + } |
|
| 651 | + else |
|
| 652 | + { |
|
| 653 | + $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 654 | + } |
|
| 655 | + return $row; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + public function updated_at($row) |
|
| 659 | + { |
|
| 660 | + if (is_object($row)) |
|
| 661 | + { |
|
| 662 | + $row->updated_at = date('Y-m-d H:i:s'); |
|
| 663 | + } |
|
| 664 | + else |
|
| 665 | + { |
|
| 666 | + $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 667 | + } |
|
| 668 | + return $row; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + /** |
|
| 672 | + * Serialises data for you automatically, allowing you to pass |
|
| 673 | + * through objects and let it handle the serialisation in the background |
|
| 674 | + */ |
|
| 675 | + public function serialize($row) |
|
| 676 | + { |
|
| 677 | + foreach ($this->callback_parameters as $column) |
|
| 678 | + { |
|
| 679 | + $row[$column] = serialize($row[$column]); |
|
| 680 | + } |
|
| 681 | + return $row; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + public function unserialize($row) |
|
| 685 | + { |
|
| 686 | + foreach ($this->callback_parameters as $column) |
|
| 687 | + { |
|
| 688 | + if (is_array($row)) |
|
| 689 | + { |
|
| 690 | + $row[$column] = unserialize($row[$column]); |
|
| 691 | + } |
|
| 692 | + else |
|
| 693 | + { |
|
| 694 | + $row->$column = unserialize($row->$column); |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | + return $row; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * Protect attributes by removing them from $row array |
|
| 702 | + */ |
|
| 703 | + public function protect_attributes($row) |
|
| 704 | + { |
|
| 705 | + foreach ($this->protected_attributes as $attr) |
|
| 706 | + { |
|
| 707 | + if (is_object($row)) |
|
| 708 | + { |
|
| 709 | 709 | if (isset($row->$attr)){ |
| 710 | 710 | unset($row->$attr); |
| 711 | 711 | } |
| 712 | - } |
|
| 713 | - else |
|
| 714 | - { |
|
| 712 | + } |
|
| 713 | + else |
|
| 714 | + { |
|
| 715 | 715 | if (isset($row[$attr])){ |
| 716 | 716 | unset($row[$attr]); |
| 717 | 717 | } |
| 718 | - } |
|
| 719 | - } |
|
| 720 | - return $row; |
|
| 721 | - } |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + return $row; |
|
| 721 | + } |
|
| 722 | 722 | |
| 723 | 723 | /** |
| 724 | - * Return the database instance |
|
| 725 | - * @return Database the database instance |
|
| 726 | - */ |
|
| 727 | - public function getDatabaseInstance(){ |
|
| 728 | - return $this->_database; |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - /** |
|
| 732 | - * set the Database instance for future use |
|
| 733 | - * @param Database $db the database object |
|
| 734 | - */ |
|
| 735 | - public function setDatabaseInstance($db){ |
|
| 736 | - $this->_database = $db; |
|
| 737 | - if ($this->dbCacheTime > 0){ |
|
| 738 | - $this->_database->setCache($this->dbCacheTime); |
|
| 739 | - } |
|
| 740 | - return $this; |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Return the loader instance |
|
| 745 | - * @return Loader the loader instance |
|
| 746 | - */ |
|
| 747 | - public function getLoader(){ |
|
| 748 | - return $this->loaderInstance; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - /** |
|
| 752 | - * Set the loader instance for future use |
|
| 753 | - * @param Loader $loader the loader object |
|
| 724 | + * Return the database instance |
|
| 725 | + * @return Database the database instance |
|
| 726 | + */ |
|
| 727 | + public function getDatabaseInstance(){ |
|
| 728 | + return $this->_database; |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + /** |
|
| 732 | + * set the Database instance for future use |
|
| 733 | + * @param Database $db the database object |
|
| 734 | + */ |
|
| 735 | + public function setDatabaseInstance($db){ |
|
| 736 | + $this->_database = $db; |
|
| 737 | + if ($this->dbCacheTime > 0){ |
|
| 738 | + $this->_database->setCache($this->dbCacheTime); |
|
| 739 | + } |
|
| 740 | + return $this; |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Return the loader instance |
|
| 745 | + * @return Loader the loader instance |
|
| 746 | + */ |
|
| 747 | + public function getLoader(){ |
|
| 748 | + return $this->loaderInstance; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + /** |
|
| 752 | + * Set the loader instance for future use |
|
| 753 | + * @param Loader $loader the loader object |
|
| 754 | 754 | * @return object |
| 755 | - */ |
|
| 756 | - public function setLoader($loader){ |
|
| 757 | - $this->loaderInstance = $loader; |
|
| 758 | - return $this; |
|
| 759 | - } |
|
| 755 | + */ |
|
| 756 | + public function setLoader($loader){ |
|
| 757 | + $this->loaderInstance = $loader; |
|
| 758 | + return $this; |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + /** |
|
| 762 | + * Return the queryBuilder instance this is the shortcut to database queryBuilder |
|
| 763 | + * @return object the DatabaseQueryBuilder instance |
|
| 764 | + */ |
|
| 765 | + public function getQueryBuilder(){ |
|
| 766 | + return $this->_database->getQueryBuilder(); |
|
| 767 | + } |
|
| 760 | 768 | |
| 761 | 769 | /** |
| 762 | - * Return the queryBuilder instance this is the shortcut to database queryBuilder |
|
| 763 | - * @return object the DatabaseQueryBuilder instance |
|
| 764 | - */ |
|
| 765 | - public function getQueryBuilder(){ |
|
| 766 | - return $this->_database->getQueryBuilder(); |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - /** |
|
| 770 | - * Set the DatabaseQueryBuilder instance for future use |
|
| 771 | - * @param object $queryBuilder the DatabaseQueryBuilder object |
|
| 770 | + * Set the DatabaseQueryBuilder instance for future use |
|
| 771 | + * @param object $queryBuilder the DatabaseQueryBuilder object |
|
| 772 | 772 | * @return object |
| 773 | - */ |
|
| 774 | - public function setQueryBuilder($queryBuilder){ |
|
| 775 | - $this->_database->setQueryBuilder($queryBuilder); |
|
| 776 | - return $this; |
|
| 777 | - } |
|
| 773 | + */ |
|
| 774 | + public function setQueryBuilder($queryBuilder){ |
|
| 775 | + $this->_database->setQueryBuilder($queryBuilder); |
|
| 776 | + return $this; |
|
| 777 | + } |
|
| 778 | 778 | |
| 779 | 779 | |
| 780 | - /** |
|
| 781 | - * Return the FormValidation instance |
|
| 782 | - * @return FormValidation the form validation instance |
|
| 783 | - */ |
|
| 784 | - public function getFormValidation(){ |
|
| 785 | - return $this->formValidationInstance; |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - /** |
|
| 789 | - * Set the form validation instance for future use |
|
| 790 | - * @param FormValidation $fv the form validation object |
|
| 780 | + /** |
|
| 781 | + * Return the FormValidation instance |
|
| 782 | + * @return FormValidation the form validation instance |
|
| 783 | + */ |
|
| 784 | + public function getFormValidation(){ |
|
| 785 | + return $this->formValidationInstance; |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + /** |
|
| 789 | + * Set the form validation instance for future use |
|
| 790 | + * @param FormValidation $fv the form validation object |
|
| 791 | 791 | * @return object |
| 792 | - */ |
|
| 793 | - public function setFormValidation($fv){ |
|
| 794 | - $this->formValidationInstance = $fv; |
|
| 795 | - return $this; |
|
| 796 | - } |
|
| 792 | + */ |
|
| 793 | + public function setFormValidation($fv){ |
|
| 794 | + $this->formValidationInstance = $fv; |
|
| 795 | + return $this; |
|
| 796 | + } |
|
| 797 | 797 | |
| 798 | - /* -------------------------------------------------------------- |
|
| 798 | + /* -------------------------------------------------------------- |
|
| 799 | 799 | * QUERY BUILDER DIRECT ACCESS METHODS |
| 800 | 800 | * ------------------------------------------------------------ */ |
| 801 | 801 | |
| 802 | - /** |
|
| 803 | - * A wrapper to $this->getQueryBuilder()->orderBy() |
|
| 804 | - */ |
|
| 805 | - public function order_by($criteria, $order = 'ASC') |
|
| 806 | - { |
|
| 807 | - if ( is_array($criteria) ) |
|
| 808 | - { |
|
| 809 | - foreach ($criteria as $key => $value) |
|
| 810 | - { |
|
| 811 | - $this->getQueryBuilder()->orderBy($key, $value); |
|
| 812 | - } |
|
| 813 | - } |
|
| 814 | - else |
|
| 815 | - { |
|
| 816 | - $this->getQueryBuilder()->orderBy($criteria, $order); |
|
| 817 | - } |
|
| 818 | - return $this; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * A wrapper to $this->getQueryBuilder()->limit() |
|
| 823 | - */ |
|
| 824 | - public function limit($offset = 0, $limit = 10) |
|
| 825 | - { |
|
| 826 | - $this->getQueryBuilder()->limit($offset, $limit); |
|
| 827 | - return $this; |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - /* -------------------------------------------------------------- |
|
| 802 | + /** |
|
| 803 | + * A wrapper to $this->getQueryBuilder()->orderBy() |
|
| 804 | + */ |
|
| 805 | + public function order_by($criteria, $order = 'ASC') |
|
| 806 | + { |
|
| 807 | + if ( is_array($criteria) ) |
|
| 808 | + { |
|
| 809 | + foreach ($criteria as $key => $value) |
|
| 810 | + { |
|
| 811 | + $this->getQueryBuilder()->orderBy($key, $value); |
|
| 812 | + } |
|
| 813 | + } |
|
| 814 | + else |
|
| 815 | + { |
|
| 816 | + $this->getQueryBuilder()->orderBy($criteria, $order); |
|
| 817 | + } |
|
| 818 | + return $this; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * A wrapper to $this->getQueryBuilder()->limit() |
|
| 823 | + */ |
|
| 824 | + public function limit($offset = 0, $limit = 10) |
|
| 825 | + { |
|
| 826 | + $this->getQueryBuilder()->limit($offset, $limit); |
|
| 827 | + return $this; |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + /* -------------------------------------------------------------- |
|
| 831 | 831 | * INTERNAL METHODS |
| 832 | 832 | * ------------------------------------------------------------ */ |
| 833 | 833 | |
| 834 | 834 | /** |
| 835 | - * relate for the relation "belongs_to" |
|
| 836 | - * @return mixed |
|
| 837 | - */ |
|
| 835 | + * relate for the relation "belongs_to" |
|
| 836 | + * @return mixed |
|
| 837 | + */ |
|
| 838 | 838 | protected function relateBelongsTo($row){ |
| 839 | 839 | foreach ($this->belongs_to as $key => $value) |
| 840 | - { |
|
| 841 | - if (is_string($value)) |
|
| 842 | - { |
|
| 843 | - $relationship = $value; |
|
| 844 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 845 | - } |
|
| 846 | - else |
|
| 847 | - { |
|
| 848 | - $relationship = $key; |
|
| 849 | - $options = $value; |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - if (in_array($relationship, $this->_with)) |
|
| 853 | - { |
|
| 854 | - if (is_object($this->loaderInstance)){ |
|
| 855 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 856 | - } |
|
| 857 | - else{ |
|
| 858 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 859 | - } |
|
| 860 | - if (is_object($row)) |
|
| 861 | - { |
|
| 862 | - $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 863 | - } |
|
| 864 | - else |
|
| 865 | - { |
|
| 866 | - $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 867 | - } |
|
| 868 | - } |
|
| 869 | - } |
|
| 840 | + { |
|
| 841 | + if (is_string($value)) |
|
| 842 | + { |
|
| 843 | + $relationship = $value; |
|
| 844 | + $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 845 | + } |
|
| 846 | + else |
|
| 847 | + { |
|
| 848 | + $relationship = $key; |
|
| 849 | + $options = $value; |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + if (in_array($relationship, $this->_with)) |
|
| 853 | + { |
|
| 854 | + if (is_object($this->loaderInstance)){ |
|
| 855 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 856 | + } |
|
| 857 | + else{ |
|
| 858 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 859 | + } |
|
| 860 | + if (is_object($row)) |
|
| 861 | + { |
|
| 862 | + $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 863 | + } |
|
| 864 | + else |
|
| 865 | + { |
|
| 866 | + $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 867 | + } |
|
| 868 | + } |
|
| 869 | + } |
|
| 870 | 870 | return $row; |
| 871 | 871 | } |
| 872 | 872 | |
| 873 | 873 | /** |
| 874 | - * relate for the relation "has_many" |
|
| 875 | - * @return mixed |
|
| 876 | - */ |
|
| 874 | + * relate for the relation "has_many" |
|
| 875 | + * @return mixed |
|
| 876 | + */ |
|
| 877 | 877 | protected function relateHasMany($row){ |
| 878 | 878 | foreach ($this->has_many as $key => $value) |
| 879 | - { |
|
| 880 | - if (is_string($value)) |
|
| 881 | - { |
|
| 882 | - $relationship = $value; |
|
| 883 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 884 | - } |
|
| 885 | - else |
|
| 886 | - { |
|
| 887 | - $relationship = $key; |
|
| 888 | - $options = $value; |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - if (in_array($relationship, $this->_with)) |
|
| 892 | - { |
|
| 893 | - if (is_object($this->loaderInstance)){ |
|
| 894 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 895 | - } |
|
| 896 | - else{ |
|
| 897 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 898 | - } |
|
| 899 | - if (is_object($row)) |
|
| 900 | - { |
|
| 901 | - $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 902 | - } |
|
| 903 | - else |
|
| 904 | - { |
|
| 905 | - $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 906 | - } |
|
| 907 | - } |
|
| 908 | - } |
|
| 879 | + { |
|
| 880 | + if (is_string($value)) |
|
| 881 | + { |
|
| 882 | + $relationship = $value; |
|
| 883 | + $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 884 | + } |
|
| 885 | + else |
|
| 886 | + { |
|
| 887 | + $relationship = $key; |
|
| 888 | + $options = $value; |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + if (in_array($relationship, $this->_with)) |
|
| 892 | + { |
|
| 893 | + if (is_object($this->loaderInstance)){ |
|
| 894 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 895 | + } |
|
| 896 | + else{ |
|
| 897 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 898 | + } |
|
| 899 | + if (is_object($row)) |
|
| 900 | + { |
|
| 901 | + $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 902 | + } |
|
| 903 | + else |
|
| 904 | + { |
|
| 905 | + $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 906 | + } |
|
| 907 | + } |
|
| 908 | + } |
|
| 909 | 909 | return $row; |
| 910 | 910 | } |
| 911 | 911 | |
| 912 | - /** |
|
| 913 | - * Trigger an event and call its observers. Pass through the event name |
|
| 914 | - * (which looks for an instance variable $this->event_name), an array of |
|
| 915 | - * parameters to pass through and an optional 'last in interation' boolean |
|
| 916 | - */ |
|
| 917 | - protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 918 | - { |
|
| 919 | - if (isset($this->$event) && is_array($this->$event)) |
|
| 920 | - { |
|
| 921 | - foreach ($this->$event as $method) |
|
| 922 | - { |
|
| 923 | - if (strpos($method, '(')) |
|
| 924 | - { |
|
| 925 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 926 | - $method = $matches[1]; |
|
| 927 | - $this->callback_parameters = explode(',', $matches[3]); |
|
| 928 | - } |
|
| 929 | - $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 930 | - } |
|
| 931 | - } |
|
| 932 | - return $data; |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - /** |
|
| 936 | - * Run validation on the passed data |
|
| 937 | - */ |
|
| 938 | - protected function validate(array $data) |
|
| 939 | - { |
|
| 940 | - if ($this->skip_validation) |
|
| 941 | - { |
|
| 942 | - return $data; |
|
| 943 | - } |
|
| 944 | - if (!empty($this->validate)) |
|
| 945 | - { |
|
| 946 | - $fv = null; |
|
| 947 | - if (is_object($this->formValidationInstance)){ |
|
| 948 | - $fv = $this->formValidationInstance; |
|
| 949 | - } |
|
| 950 | - else{ |
|
| 951 | - Loader::library('FormValidation'); |
|
| 952 | - $fv = $this->formvalidation; |
|
| 953 | - $this->setFormValidation($fv); |
|
| 954 | - } |
|
| 912 | + /** |
|
| 913 | + * Trigger an event and call its observers. Pass through the event name |
|
| 914 | + * (which looks for an instance variable $this->event_name), an array of |
|
| 915 | + * parameters to pass through and an optional 'last in interation' boolean |
|
| 916 | + */ |
|
| 917 | + protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 918 | + { |
|
| 919 | + if (isset($this->$event) && is_array($this->$event)) |
|
| 920 | + { |
|
| 921 | + foreach ($this->$event as $method) |
|
| 922 | + { |
|
| 923 | + if (strpos($method, '(')) |
|
| 924 | + { |
|
| 925 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 926 | + $method = $matches[1]; |
|
| 927 | + $this->callback_parameters = explode(',', $matches[3]); |
|
| 928 | + } |
|
| 929 | + $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 930 | + } |
|
| 931 | + } |
|
| 932 | + return $data; |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + /** |
|
| 936 | + * Run validation on the passed data |
|
| 937 | + */ |
|
| 938 | + protected function validate(array $data) |
|
| 939 | + { |
|
| 940 | + if ($this->skip_validation) |
|
| 941 | + { |
|
| 942 | + return $data; |
|
| 943 | + } |
|
| 944 | + if (!empty($this->validate)) |
|
| 945 | + { |
|
| 946 | + $fv = null; |
|
| 947 | + if (is_object($this->formValidationInstance)){ |
|
| 948 | + $fv = $this->formValidationInstance; |
|
| 949 | + } |
|
| 950 | + else{ |
|
| 951 | + Loader::library('FormValidation'); |
|
| 952 | + $fv = $this->formvalidation; |
|
| 953 | + $this->setFormValidation($fv); |
|
| 954 | + } |
|
| 955 | 955 | |
| 956 | - $fv->setData($data); |
|
| 957 | - $fv->setRules($this->validate); |
|
| 958 | - |
|
| 959 | - if ($fv->run()) |
|
| 960 | - { |
|
| 961 | - return $data; |
|
| 962 | - } |
|
| 963 | - else |
|
| 964 | - { |
|
| 965 | - return FALSE; |
|
| 966 | - } |
|
| 967 | - } |
|
| 968 | - else |
|
| 969 | - { |
|
| 970 | - return $data; |
|
| 971 | - } |
|
| 972 | - } |
|
| 956 | + $fv->setData($data); |
|
| 957 | + $fv->setRules($this->validate); |
|
| 958 | + |
|
| 959 | + if ($fv->run()) |
|
| 960 | + { |
|
| 961 | + return $data; |
|
| 962 | + } |
|
| 963 | + else |
|
| 964 | + { |
|
| 965 | + return FALSE; |
|
| 966 | + } |
|
| 967 | + } |
|
| 968 | + else |
|
| 969 | + { |
|
| 970 | + return $data; |
|
| 971 | + } |
|
| 972 | + } |
|
| 973 | 973 | |
| 974 | 974 | |
| 975 | 975 | /** |
| 976 | - * Set WHERE parameters, when is array |
|
| 977 | - * @param array $params |
|
| 978 | - */ |
|
| 976 | + * Set WHERE parameters, when is array |
|
| 977 | + * @param array $params |
|
| 978 | + */ |
|
| 979 | 979 | protected function _set_where_array(array $params){ |
| 980 | 980 | foreach ($params as $field => $filter) |
| 981 | 981 | { |
@@ -998,52 +998,52 @@ discard block |
||
| 998 | 998 | } |
| 999 | 999 | |
| 1000 | 1000 | |
| 1001 | - /** |
|
| 1002 | - * Set WHERE parameters, cleverly |
|
| 1003 | - */ |
|
| 1004 | - protected function _set_where($params) |
|
| 1005 | - { |
|
| 1006 | - if (count($params) == 1 && is_array($params[0])) |
|
| 1007 | - { |
|
| 1008 | - $this->_set_where_array($params[0]); |
|
| 1009 | - } |
|
| 1010 | - else if (count($params) == 1) |
|
| 1011 | - { |
|
| 1012 | - $this->getQueryBuilder()->where($params[0]); |
|
| 1013 | - } |
|
| 1014 | - else if (count($params) == 2) |
|
| 1015 | - { |
|
| 1016 | - if (is_array($params[1])) |
|
| 1017 | - { |
|
| 1018 | - $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1019 | - } |
|
| 1020 | - else |
|
| 1021 | - { |
|
| 1022 | - $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1023 | - } |
|
| 1024 | - } |
|
| 1025 | - else if (count($params) == 3) |
|
| 1026 | - { |
|
| 1027 | - $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
| 1028 | - } |
|
| 1029 | - else |
|
| 1030 | - { |
|
| 1031 | - if (is_array($params[1])) |
|
| 1032 | - { |
|
| 1033 | - $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1034 | - } |
|
| 1035 | - else |
|
| 1036 | - { |
|
| 1037 | - $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1038 | - } |
|
| 1039 | - } |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - /** |
|
| 1001 | + /** |
|
| 1002 | + * Set WHERE parameters, cleverly |
|
| 1003 | + */ |
|
| 1004 | + protected function _set_where($params) |
|
| 1005 | + { |
|
| 1006 | + if (count($params) == 1 && is_array($params[0])) |
|
| 1007 | + { |
|
| 1008 | + $this->_set_where_array($params[0]); |
|
| 1009 | + } |
|
| 1010 | + else if (count($params) == 1) |
|
| 1011 | + { |
|
| 1012 | + $this->getQueryBuilder()->where($params[0]); |
|
| 1013 | + } |
|
| 1014 | + else if (count($params) == 2) |
|
| 1015 | + { |
|
| 1016 | + if (is_array($params[1])) |
|
| 1017 | + { |
|
| 1018 | + $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1019 | + } |
|
| 1020 | + else |
|
| 1021 | + { |
|
| 1022 | + $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1023 | + } |
|
| 1024 | + } |
|
| 1025 | + else if (count($params) == 3) |
|
| 1026 | + { |
|
| 1027 | + $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
| 1028 | + } |
|
| 1029 | + else |
|
| 1030 | + { |
|
| 1031 | + if (is_array($params[1])) |
|
| 1032 | + { |
|
| 1033 | + $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1034 | + } |
|
| 1035 | + else |
|
| 1036 | + { |
|
| 1037 | + $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1038 | + } |
|
| 1039 | + } |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + /** |
|
| 1043 | 1043 | Shortcut to controller |
| 1044 | - */ |
|
| 1045 | - public function __get($key){ |
|
| 1046 | - return get_instance()->{$key}; |
|
| 1047 | - } |
|
| 1044 | + */ |
|
| 1045 | + public function __get($key){ |
|
| 1046 | + return get_instance()->{$key}; |
|
| 1047 | + } |
|
| 1048 | 1048 | |
| 1049 | - } |
|
| 1049 | + } |
|
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | 3 | /** |
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
@@ -22,266 +22,266 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | class Database{ |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | - */ |
|
| 32 | - private $pdo = null; |
|
| 31 | + */ |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | - */ |
|
| 37 | + */ |
|
| 38 | 38 | private $databaseName = null; |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The number of rows returned by the last query |
| 42 | 42 | * @var int |
| 43 | - */ |
|
| 44 | - private $numRows = 0; |
|
| 43 | + */ |
|
| 44 | + private $numRows = 0; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The last insert id for the primary key column that have auto increment or sequence |
| 48 | 48 | * @var mixed |
| 49 | - */ |
|
| 50 | - private $insertId = null; |
|
| 49 | + */ |
|
| 50 | + private $insertId = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The full SQL query statment after build for each command |
| 54 | 54 | * @var string |
| 55 | - */ |
|
| 56 | - private $query = null; |
|
| 55 | + */ |
|
| 56 | + private $query = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The error returned for the last query |
| 60 | 60 | * @var string |
| 61 | - */ |
|
| 62 | - private $error = null; |
|
| 61 | + */ |
|
| 62 | + private $error = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The result returned for the last query |
| 66 | 66 | * @var mixed |
| 67 | - */ |
|
| 68 | - private $result = array(); |
|
| 67 | + */ |
|
| 68 | + private $result = array(); |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 72 | 72 | * @var int |
| 73 | - */ |
|
| 73 | + */ |
|
| 74 | 74 | private $cacheTtl = 0; |
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The cache current time to live. 0 means no need to use the cache feature |
| 78 | 78 | * @var int |
| 79 | - */ |
|
| 80 | - private $temporaryCacheTtl = 0; |
|
| 79 | + */ |
|
| 80 | + private $temporaryCacheTtl = 0; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The number of executed query for the current request |
| 84 | 84 | * @var int |
| 85 | - */ |
|
| 86 | - private $queryCount = 0; |
|
| 85 | + */ |
|
| 86 | + private $queryCount = 0; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The default data to be used in the statments query INSERT, UPDATE |
| 90 | 90 | * @var array |
| 91 | - */ |
|
| 92 | - private $data = array(); |
|
| 91 | + */ |
|
| 92 | + private $data = array(); |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The database configuration |
| 96 | 96 | * @var array |
| 97 | - */ |
|
| 98 | - private $config = array(); |
|
| 97 | + */ |
|
| 98 | + private $config = array(); |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The logger instance |
| 102 | 102 | * @var object |
| 103 | 103 | */ |
| 104 | - private $logger = null; |
|
| 104 | + private $logger = null; |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * The cache instance |
|
| 108 | - * @var object |
|
| 109 | - */ |
|
| 110 | - private $cacheInstance = null; |
|
| 106 | + /** |
|
| 107 | + * The cache instance |
|
| 108 | + * @var object |
|
| 109 | + */ |
|
| 110 | + private $cacheInstance = null; |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * The benchmark instance |
|
| 114 | - * @var object |
|
| 115 | - */ |
|
| 116 | - private $benchmarkInstance = null; |
|
| 112 | + /** |
|
| 113 | + * The benchmark instance |
|
| 114 | + * @var object |
|
| 115 | + */ |
|
| 116 | + private $benchmarkInstance = null; |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | - * The DatabaseQueryBuilder instance |
|
| 120 | - * @var object |
|
| 121 | - */ |
|
| 122 | - private $queryBuilder = null; |
|
| 119 | + * The DatabaseQueryBuilder instance |
|
| 120 | + * @var object |
|
| 121 | + */ |
|
| 122 | + private $queryBuilder = null; |
|
| 123 | 123 | |
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Construct new database |
|
| 127 | - * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 128 | - */ |
|
| 129 | - public function __construct($overwriteConfig = array()){ |
|
| 130 | - //Set Log instance to use |
|
| 131 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 125 | + /** |
|
| 126 | + * Construct new database |
|
| 127 | + * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 128 | + */ |
|
| 129 | + public function __construct($overwriteConfig = array()){ |
|
| 130 | + //Set Log instance to use |
|
| 131 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 132 | 132 | |
| 133 | - //Set DatabaseQueryBuilder instance to use |
|
| 134 | - $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 133 | + //Set DatabaseQueryBuilder instance to use |
|
| 134 | + $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 135 | 135 | |
| 136 | - //Set database configuration |
|
| 137 | - $this->setDatabaseConfiguration($overwriteConfig); |
|
| 136 | + //Set database configuration |
|
| 137 | + $this->setDatabaseConfiguration($overwriteConfig); |
|
| 138 | 138 | |
| 139 | - //cache time to live |
|
| 140 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * This is used to connect to database |
|
| 145 | - * @return bool |
|
| 146 | - */ |
|
| 147 | - public function connect(){ |
|
| 148 | - $config = $this->getDatabaseConfiguration(); |
|
| 149 | - if (! empty($config)){ |
|
| 150 | - try{ |
|
| 151 | - $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 152 | - $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 153 | - $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 154 | - $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 155 | - return true; |
|
| 156 | - } |
|
| 157 | - catch (PDOException $e){ |
|
| 158 | - $this->logger->fatal($e->getMessage()); |
|
| 159 | - show_error('Cannot connect to Database.'); |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - show_error('Database configuration is not set.'); |
|
| 165 | - return false; |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Return the number of rows returned by the current query |
|
| 172 | - * @return int |
|
| 173 | - */ |
|
| 174 | - public function numRows(){ |
|
| 175 | - return $this->numRows; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Return the last insert id value |
|
| 180 | - * @return mixed |
|
| 181 | - */ |
|
| 182 | - public function insertId(){ |
|
| 183 | - return $this->insertId; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 188 | - */ |
|
| 189 | - public function error(){ |
|
| 139 | + //cache time to live |
|
| 140 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * This is used to connect to database |
|
| 145 | + * @return bool |
|
| 146 | + */ |
|
| 147 | + public function connect(){ |
|
| 148 | + $config = $this->getDatabaseConfiguration(); |
|
| 149 | + if (! empty($config)){ |
|
| 150 | + try{ |
|
| 151 | + $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 152 | + $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 153 | + $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 154 | + $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 155 | + return true; |
|
| 156 | + } |
|
| 157 | + catch (PDOException $e){ |
|
| 158 | + $this->logger->fatal($e->getMessage()); |
|
| 159 | + show_error('Cannot connect to Database.'); |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + show_error('Database configuration is not set.'); |
|
| 165 | + return false; |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Return the number of rows returned by the current query |
|
| 172 | + * @return int |
|
| 173 | + */ |
|
| 174 | + public function numRows(){ |
|
| 175 | + return $this->numRows; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Return the last insert id value |
|
| 180 | + * @return mixed |
|
| 181 | + */ |
|
| 182 | + public function insertId(){ |
|
| 183 | + return $this->insertId; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 188 | + */ |
|
| 189 | + public function error(){ |
|
| 190 | 190 | if ($this->error){ |
| 191 | 191 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 192 | 192 | } |
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get the result of one record rows returned by the current query |
|
| 197 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 198 | - * If is string will determine the result type "array" or "object" |
|
| 199 | - * @return mixed the query SQL string or the record result |
|
| 200 | - */ |
|
| 201 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 202 | - $this->getQueryBuilder()->limit(1); |
|
| 203 | - $query = $this->getAll(true); |
|
| 204 | - if ($returnSQLQueryOrResultType === true){ |
|
| 205 | - return $query; |
|
| 206 | - } |
|
| 207 | - else{ |
|
| 208 | - return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Get the result of record rows list returned by the current query |
|
| 214 | - * @param boolean|string $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 215 | - * If is string will determine the result type "array" or "object" |
|
| 216 | - * @return mixed the query SQL string or the record result |
|
| 217 | - */ |
|
| 218 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get the result of one record rows returned by the current query |
|
| 197 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 198 | + * If is string will determine the result type "array" or "object" |
|
| 199 | + * @return mixed the query SQL string or the record result |
|
| 200 | + */ |
|
| 201 | + public function get($returnSQLQueryOrResultType = false){ |
|
| 202 | + $this->getQueryBuilder()->limit(1); |
|
| 203 | + $query = $this->getAll(true); |
|
| 204 | + if ($returnSQLQueryOrResultType === true){ |
|
| 205 | + return $query; |
|
| 206 | + } |
|
| 207 | + else{ |
|
| 208 | + return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Get the result of record rows list returned by the current query |
|
| 214 | + * @param boolean|string $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 215 | + * If is string will determine the result type "array" or "object" |
|
| 216 | + * @return mixed the query SQL string or the record result |
|
| 217 | + */ |
|
| 218 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
| 219 | 219 | $query = $this->getQueryBuilder()->getQuery(); |
| 220 | 220 | if ($returnSQLQueryOrResultType === true){ |
| 221 | - return $query; |
|
| 222 | - } |
|
| 223 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Insert new record in the database |
|
| 228 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 229 | - * @param boolean $escape whether to escape or not the values |
|
| 230 | - * @return mixed the insert id of the new record or null |
|
| 231 | - */ |
|
| 232 | - public function insert($data = array(), $escape = true){ |
|
| 233 | - if (empty($data) && $this->getData()){ |
|
| 234 | - //as when using $this->setData() may be the data already escaped |
|
| 235 | - $escape = false; |
|
| 236 | - $data = $this->getData(); |
|
| 237 | - } |
|
| 238 | - $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 239 | - $result = $this->query($query); |
|
| 240 | - if ($result){ |
|
| 241 | - $this->insertId = $this->pdo->lastInsertId(); |
|
| 221 | + return $query; |
|
| 222 | + } |
|
| 223 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Insert new record in the database |
|
| 228 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 229 | + * @param boolean $escape whether to escape or not the values |
|
| 230 | + * @return mixed the insert id of the new record or null |
|
| 231 | + */ |
|
| 232 | + public function insert($data = array(), $escape = true){ |
|
| 233 | + if (empty($data) && $this->getData()){ |
|
| 234 | + //as when using $this->setData() may be the data already escaped |
|
| 235 | + $escape = false; |
|
| 236 | + $data = $this->getData(); |
|
| 237 | + } |
|
| 238 | + $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 239 | + $result = $this->query($query); |
|
| 240 | + if ($result){ |
|
| 241 | + $this->insertId = $this->pdo->lastInsertId(); |
|
| 242 | 242 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 243 | - return ! $this->insertId() ? true : $this->insertId(); |
|
| 244 | - } |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Update record in the database |
|
| 250 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 251 | - * @param boolean $escape whether to escape or not the values |
|
| 252 | - * @return mixed the update status |
|
| 253 | - */ |
|
| 254 | - public function update($data = array(), $escape = true){ |
|
| 255 | - if (empty($data) && $this->getData()){ |
|
| 256 | - //as when using $this->setData() may be the data already escaped |
|
| 257 | - $escape = false; |
|
| 258 | - $data = $this->getData(); |
|
| 259 | - } |
|
| 260 | - $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 261 | - return $this->query($query); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Delete the record in database |
|
| 266 | - * @return mixed the delete status |
|
| 267 | - */ |
|
| 268 | - public function delete(){ |
|
| 243 | + return ! $this->insertId() ? true : $this->insertId(); |
|
| 244 | + } |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Update record in the database |
|
| 250 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 251 | + * @param boolean $escape whether to escape or not the values |
|
| 252 | + * @return mixed the update status |
|
| 253 | + */ |
|
| 254 | + public function update($data = array(), $escape = true){ |
|
| 255 | + if (empty($data) && $this->getData()){ |
|
| 256 | + //as when using $this->setData() may be the data already escaped |
|
| 257 | + $escape = false; |
|
| 258 | + $data = $this->getData(); |
|
| 259 | + } |
|
| 260 | + $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 261 | + return $this->query($query); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Delete the record in database |
|
| 266 | + * @return mixed the delete status |
|
| 267 | + */ |
|
| 268 | + public function delete(){ |
|
| 269 | 269 | $query = $this->getQueryBuilder()->delete()->getQuery(); |
| 270 | - return $this->query($query); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Set database cache time to live |
|
| 275 | - * @param integer $ttl the cache time to live in second |
|
| 276 | - * @return object the current Database instance |
|
| 277 | - */ |
|
| 278 | - public function setCache($ttl = 0){ |
|
| 279 | - if ($ttl > 0){ |
|
| 280 | - $this->cacheTtl = $ttl; |
|
| 281 | - $this->temporaryCacheTtl = $ttl; |
|
| 282 | - } |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 270 | + return $this->query($query); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Set database cache time to live |
|
| 275 | + * @param integer $ttl the cache time to live in second |
|
| 276 | + * @return object the current Database instance |
|
| 277 | + */ |
|
| 278 | + public function setCache($ttl = 0){ |
|
| 279 | + if ($ttl > 0){ |
|
| 280 | + $this->cacheTtl = $ttl; |
|
| 281 | + $this->temporaryCacheTtl = $ttl; |
|
| 282 | + } |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | 286 | /** |
| 287 | 287 | * Enabled cache temporary for the current query not globally |
@@ -289,275 +289,275 @@ discard block |
||
| 289 | 289 | * @return object the current Database instance |
| 290 | 290 | */ |
| 291 | 291 | public function cached($ttl = 0){ |
| 292 | - if ($ttl > 0){ |
|
| 293 | - $this->temporaryCacheTtl = $ttl; |
|
| 294 | - } |
|
| 295 | - return $this; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Escape the data before execute query useful for security. |
|
| 300 | - * @param mixed $data the data to be escaped |
|
| 301 | - * @param boolean $escaped whether we can do escape of not |
|
| 302 | - * @return mixed the data after escaped or the same data if not |
|
| 303 | - */ |
|
| 304 | - public function escape($data, $escaped = true){ |
|
| 305 | - return $escaped ? |
|
| 306 | - $this->getPdo()->quote(trim($data)) |
|
| 307 | - : $data; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Return the number query executed count for the current request |
|
| 312 | - * @return int |
|
| 313 | - */ |
|
| 314 | - public function queryCount(){ |
|
| 315 | - return $this->queryCount; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Return the current query SQL string |
|
| 320 | - * @return string |
|
| 321 | - */ |
|
| 322 | - public function getQuery(){ |
|
| 323 | - return $this->query; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Return the application database name |
|
| 328 | - * @return string |
|
| 329 | - */ |
|
| 330 | - public function getDatabaseName(){ |
|
| 331 | - return $this->databaseName; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Return the PDO instance |
|
| 336 | - * @return object |
|
| 337 | - */ |
|
| 338 | - public function getPdo(){ |
|
| 339 | - return $this->pdo; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Set the PDO instance |
|
| 344 | - * @param object $pdo the pdo object |
|
| 292 | + if ($ttl > 0){ |
|
| 293 | + $this->temporaryCacheTtl = $ttl; |
|
| 294 | + } |
|
| 295 | + return $this; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Escape the data before execute query useful for security. |
|
| 300 | + * @param mixed $data the data to be escaped |
|
| 301 | + * @param boolean $escaped whether we can do escape of not |
|
| 302 | + * @return mixed the data after escaped or the same data if not |
|
| 303 | + */ |
|
| 304 | + public function escape($data, $escaped = true){ |
|
| 305 | + return $escaped ? |
|
| 306 | + $this->getPdo()->quote(trim($data)) |
|
| 307 | + : $data; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Return the number query executed count for the current request |
|
| 312 | + * @return int |
|
| 313 | + */ |
|
| 314 | + public function queryCount(){ |
|
| 315 | + return $this->queryCount; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Return the current query SQL string |
|
| 320 | + * @return string |
|
| 321 | + */ |
|
| 322 | + public function getQuery(){ |
|
| 323 | + return $this->query; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Return the application database name |
|
| 328 | + * @return string |
|
| 329 | + */ |
|
| 330 | + public function getDatabaseName(){ |
|
| 331 | + return $this->databaseName; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Return the PDO instance |
|
| 336 | + * @return object |
|
| 337 | + */ |
|
| 338 | + public function getPdo(){ |
|
| 339 | + return $this->pdo; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Set the PDO instance |
|
| 344 | + * @param object $pdo the pdo object |
|
| 345 | 345 | * @return object Database |
| 346 | - */ |
|
| 347 | - public function setPdo(PDO $pdo){ |
|
| 348 | - $this->pdo = $pdo; |
|
| 349 | - return $this; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Return the Log instance |
|
| 355 | - * @return Log |
|
| 356 | - */ |
|
| 357 | - public function getLogger(){ |
|
| 358 | - return $this->logger; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Set the log instance |
|
| 363 | - * @param Log $logger the log object |
|
| 346 | + */ |
|
| 347 | + public function setPdo(PDO $pdo){ |
|
| 348 | + $this->pdo = $pdo; |
|
| 349 | + return $this; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Return the Log instance |
|
| 355 | + * @return Log |
|
| 356 | + */ |
|
| 357 | + public function getLogger(){ |
|
| 358 | + return $this->logger; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Set the log instance |
|
| 363 | + * @param Log $logger the log object |
|
| 364 | 364 | * @return object Database |
| 365 | - */ |
|
| 366 | - public function setLogger($logger){ |
|
| 367 | - $this->logger = $logger; |
|
| 368 | - return $this; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Return the cache instance |
|
| 373 | - * @return CacheInterface |
|
| 374 | - */ |
|
| 375 | - public function getCacheInstance(){ |
|
| 376 | - return $this->cacheInstance; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Set the cache instance |
|
| 381 | - * @param CacheInterface $cache the cache object |
|
| 365 | + */ |
|
| 366 | + public function setLogger($logger){ |
|
| 367 | + $this->logger = $logger; |
|
| 368 | + return $this; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Return the cache instance |
|
| 373 | + * @return CacheInterface |
|
| 374 | + */ |
|
| 375 | + public function getCacheInstance(){ |
|
| 376 | + return $this->cacheInstance; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Set the cache instance |
|
| 381 | + * @param CacheInterface $cache the cache object |
|
| 382 | 382 | * @return object Database |
| 383 | - */ |
|
| 384 | - public function setCacheInstance($cache){ |
|
| 385 | - $this->cacheInstance = $cache; |
|
| 386 | - return $this; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Return the benchmark instance |
|
| 391 | - * @return Benchmark |
|
| 392 | - */ |
|
| 393 | - public function getBenchmark(){ |
|
| 394 | - return $this->benchmarkInstance; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Set the benchmark instance |
|
| 399 | - * @param Benchmark $benchmark the benchmark object |
|
| 383 | + */ |
|
| 384 | + public function setCacheInstance($cache){ |
|
| 385 | + $this->cacheInstance = $cache; |
|
| 386 | + return $this; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Return the benchmark instance |
|
| 391 | + * @return Benchmark |
|
| 392 | + */ |
|
| 393 | + public function getBenchmark(){ |
|
| 394 | + return $this->benchmarkInstance; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Set the benchmark instance |
|
| 399 | + * @param Benchmark $benchmark the benchmark object |
|
| 400 | 400 | * @return object Database |
| 401 | - */ |
|
| 402 | - public function setBenchmark($benchmark){ |
|
| 403 | - $this->benchmarkInstance = $benchmark; |
|
| 404 | - return $this; |
|
| 405 | - } |
|
| 401 | + */ |
|
| 402 | + public function setBenchmark($benchmark){ |
|
| 403 | + $this->benchmarkInstance = $benchmark; |
|
| 404 | + return $this; |
|
| 405 | + } |
|
| 406 | 406 | |
| 407 | 407 | |
| 408 | 408 | /** |
| 409 | - * Return the DatabaseQueryBuilder instance |
|
| 410 | - * @return object DatabaseQueryBuilder |
|
| 411 | - */ |
|
| 412 | - public function getQueryBuilder(){ |
|
| 413 | - return $this->queryBuilder; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Set the DatabaseQueryBuilder instance |
|
| 418 | - * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
|
| 419 | - */ |
|
| 420 | - public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
| 421 | - $this->queryBuilder = $queryBuilder; |
|
| 422 | - return $this; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Return the data to be used for insert, update, etc. |
|
| 427 | - * @return array |
|
| 428 | - */ |
|
| 429 | - public function getData(){ |
|
| 430 | - return $this->data; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Set the data to be used for insert, update, etc. |
|
| 435 | - * @param string|array $key the data key identified |
|
| 436 | - * @param mixed $value the data value |
|
| 437 | - * @param boolean $escape whether to escape or not the $value |
|
| 438 | - * @return object the current Database instance |
|
| 439 | - */ |
|
| 440 | - public function setData($key, $value = null, $escape = true){ |
|
| 409 | + * Return the DatabaseQueryBuilder instance |
|
| 410 | + * @return object DatabaseQueryBuilder |
|
| 411 | + */ |
|
| 412 | + public function getQueryBuilder(){ |
|
| 413 | + return $this->queryBuilder; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Set the DatabaseQueryBuilder instance |
|
| 418 | + * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
|
| 419 | + */ |
|
| 420 | + public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
| 421 | + $this->queryBuilder = $queryBuilder; |
|
| 422 | + return $this; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Return the data to be used for insert, update, etc. |
|
| 427 | + * @return array |
|
| 428 | + */ |
|
| 429 | + public function getData(){ |
|
| 430 | + return $this->data; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Set the data to be used for insert, update, etc. |
|
| 435 | + * @param string|array $key the data key identified |
|
| 436 | + * @param mixed $value the data value |
|
| 437 | + * @param boolean $escape whether to escape or not the $value |
|
| 438 | + * @return object the current Database instance |
|
| 439 | + */ |
|
| 440 | + public function setData($key, $value = null, $escape = true){ |
|
| 441 | 441 | if(is_array($key)){ |
| 442 | - foreach($key as $k => $v){ |
|
| 443 | - $this->setData($k, $v, $escape); |
|
| 444 | - } |
|
| 442 | + foreach($key as $k => $v){ |
|
| 443 | + $this->setData($k, $v, $escape); |
|
| 444 | + } |
|
| 445 | 445 | } else { |
| 446 | - $this->data[$key] = $this->escape($value, $escape); |
|
| 446 | + $this->data[$key] = $this->escape($value, $escape); |
|
| 447 | 447 | } |
| 448 | - return $this; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Execute an SQL query |
|
| 453 | - * @param string $query the query SQL string |
|
| 454 | - * @param boolean|array $all if boolean this indicate whether to return all record or not, if array |
|
| 455 | - * will |
|
| 456 | - * @param boolean $array return the result as array |
|
| 457 | - * @return mixed the query result |
|
| 458 | - */ |
|
| 459 | - public function query($query, $all = true, $array = false){ |
|
| 460 | - $this->reset(); |
|
| 461 | - $query = $this->getPreparedQuery($query, $all); |
|
| 462 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 448 | + return $this; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Execute an SQL query |
|
| 453 | + * @param string $query the query SQL string |
|
| 454 | + * @param boolean|array $all if boolean this indicate whether to return all record or not, if array |
|
| 455 | + * will |
|
| 456 | + * @param boolean $array return the result as array |
|
| 457 | + * @return mixed the query result |
|
| 458 | + */ |
|
| 459 | + public function query($query, $all = true, $array = false){ |
|
| 460 | + $this->reset(); |
|
| 461 | + $query = $this->getPreparedQuery($query, $all); |
|
| 462 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 463 | 463 | |
| 464 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 465 | - |
|
| 466 | - $this->logger->info( |
|
| 467 | - 'Execute SQL query ['.$this->query.'], return type: ' |
|
| 468 | - . ($array?'ARRAY':'OBJECT') .', return as list: ' |
|
| 469 | - . (is_bool($all) && $all ? 'YES':'NO') |
|
| 470 | - ); |
|
| 471 | - //cache expire time |
|
| 472 | - $cacheExpire = $this->temporaryCacheTtl; |
|
| 464 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 465 | + |
|
| 466 | + $this->logger->info( |
|
| 467 | + 'Execute SQL query ['.$this->query.'], return type: ' |
|
| 468 | + . ($array?'ARRAY':'OBJECT') .', return as list: ' |
|
| 469 | + . (is_bool($all) && $all ? 'YES':'NO') |
|
| 470 | + ); |
|
| 471 | + //cache expire time |
|
| 472 | + $cacheExpire = $this->temporaryCacheTtl; |
|
| 473 | 473 | |
| 474 | - //return to the initial cache time |
|
| 475 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 474 | + //return to the initial cache time |
|
| 475 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 476 | 476 | |
| 477 | - //config for cache |
|
| 478 | - $cacheEnable = get_config('cache_enable'); |
|
| 477 | + //config for cache |
|
| 478 | + $cacheEnable = get_config('cache_enable'); |
|
| 479 | 479 | |
| 480 | - //the database cache content |
|
| 481 | - $cacheContent = null; |
|
| 480 | + //the database cache content |
|
| 481 | + $cacheContent = null; |
|
| 482 | 482 | |
| 483 | - //if can use cache feature for this query |
|
| 484 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 483 | + //if can use cache feature for this query |
|
| 484 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 485 | 485 | |
| 486 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 487 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 488 | - $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
|
| 489 | - } |
|
| 486 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 487 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 488 | + $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - if ( !$cacheContent){ |
|
| 492 | - $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 493 | - if (is_object($sqlQuery)){ |
|
| 494 | - if ($isSqlSELECTQuery){ |
|
| 495 | - $this->setQueryResultForSelect($sqlQuery, $all, $array); |
|
| 496 | - $this->setCacheContentForQuery( |
|
| 497 | - $this->query, |
|
| 498 | - $this->getCacheBenchmarkKeyForQuery($this->query, $all, $array), |
|
| 499 | - $this->result, |
|
| 500 | - $dbCacheStatus && $isSqlSELECTQuery, |
|
| 501 | - $cacheExpire |
|
| 502 | - ); |
|
| 503 | - if (! $this->result){ |
|
| 504 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 505 | - } |
|
| 506 | - } else { |
|
| 507 | - $this->setQueryResultForNonSelect($sqlQuery); |
|
| 508 | - if (! $this->result){ |
|
| 509 | - $this->setQueryError(); |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - } else if ($isSqlSELECTQuery){ |
|
| 514 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 515 | - $this->result = $cacheContent; |
|
| 516 | - $this->numRows = count($this->result); |
|
| 517 | - } |
|
| 518 | - return $this->result; |
|
| 519 | - } |
|
| 491 | + if ( !$cacheContent){ |
|
| 492 | + $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 493 | + if (is_object($sqlQuery)){ |
|
| 494 | + if ($isSqlSELECTQuery){ |
|
| 495 | + $this->setQueryResultForSelect($sqlQuery, $all, $array); |
|
| 496 | + $this->setCacheContentForQuery( |
|
| 497 | + $this->query, |
|
| 498 | + $this->getCacheBenchmarkKeyForQuery($this->query, $all, $array), |
|
| 499 | + $this->result, |
|
| 500 | + $dbCacheStatus && $isSqlSELECTQuery, |
|
| 501 | + $cacheExpire |
|
| 502 | + ); |
|
| 503 | + if (! $this->result){ |
|
| 504 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 505 | + } |
|
| 506 | + } else { |
|
| 507 | + $this->setQueryResultForNonSelect($sqlQuery); |
|
| 508 | + if (! $this->result){ |
|
| 509 | + $this->setQueryError(); |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + } else if ($isSqlSELECTQuery){ |
|
| 514 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 515 | + $this->result = $cacheContent; |
|
| 516 | + $this->numRows = count($this->result); |
|
| 517 | + } |
|
| 518 | + return $this->result; |
|
| 519 | + } |
|
| 520 | 520 | |
| 521 | 521 | /** |
| 522 | - * Run the database SQL query and return the PDOStatment object |
|
| 523 | - * @see Database::query |
|
| 524 | - * |
|
| 525 | - * @return object|void |
|
| 526 | - */ |
|
| 527 | - public function runSqlQuery($query, $all, $array){ |
|
| 528 | - //for database query execution time |
|
| 529 | - $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 530 | - $benchmarkInstance = $this->getBenchmark(); |
|
| 531 | - if (! is_object($benchmarkInstance)){ |
|
| 532 | - $obj = & get_instance(); |
|
| 533 | - $benchmarkInstance = $obj->benchmark; |
|
| 534 | - $this->setBenchmark($benchmarkInstance); |
|
| 535 | - } |
|
| 522 | + * Run the database SQL query and return the PDOStatment object |
|
| 523 | + * @see Database::query |
|
| 524 | + * |
|
| 525 | + * @return object|void |
|
| 526 | + */ |
|
| 527 | + public function runSqlQuery($query, $all, $array){ |
|
| 528 | + //for database query execution time |
|
| 529 | + $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 530 | + $benchmarkInstance = $this->getBenchmark(); |
|
| 531 | + if (! is_object($benchmarkInstance)){ |
|
| 532 | + $obj = & get_instance(); |
|
| 533 | + $benchmarkInstance = $obj->benchmark; |
|
| 534 | + $this->setBenchmark($benchmarkInstance); |
|
| 535 | + } |
|
| 536 | 536 | |
| 537 | - $benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 538 | - //Now execute the query |
|
| 539 | - $sqlQuery = $this->pdo->query($query); |
|
| 537 | + $benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 538 | + //Now execute the query |
|
| 539 | + $sqlQuery = $this->pdo->query($query); |
|
| 540 | 540 | |
| 541 | - //get response time for this query |
|
| 542 | - $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 543 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 544 | - if ($responseTime >= 1 ){ |
|
| 545 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 546 | - } |
|
| 547 | - //count the number of query execution to server |
|
| 548 | - $this->queryCount++; |
|
| 541 | + //get response time for this query |
|
| 542 | + $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 543 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 544 | + if ($responseTime >= 1 ){ |
|
| 545 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 546 | + } |
|
| 547 | + //count the number of query execution to server |
|
| 548 | + $this->queryCount++; |
|
| 549 | 549 | |
| 550 | - if ($sqlQuery !== false){ |
|
| 551 | - return $sqlQuery; |
|
| 552 | - } |
|
| 553 | - $this->setQueryError(); |
|
| 554 | - } |
|
| 550 | + if ($sqlQuery !== false){ |
|
| 551 | + return $sqlQuery; |
|
| 552 | + } |
|
| 553 | + $this->setQueryError(); |
|
| 554 | + } |
|
| 555 | 555 | |
| 556 | 556 | |
| 557 | 557 | /** |
| 558 | - * Return the database configuration |
|
| 559 | - * @return array |
|
| 560 | - */ |
|
| 558 | + * Return the database configuration |
|
| 559 | + * @return array |
|
| 560 | + */ |
|
| 561 | 561 | public function getDatabaseConfiguration(){ |
| 562 | 562 | return $this->config; |
| 563 | 563 | } |
@@ -566,29 +566,29 @@ discard block |
||
| 566 | 566 | * Setting the database configuration using the configuration file and additional configuration from param |
| 567 | 567 | * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
| 568 | 568 | * @param boolean $useConfigFile whether to use database configuration file |
| 569 | - * @return object Database |
|
| 569 | + * @return object Database |
|
| 570 | 570 | */ |
| 571 | - public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 572 | - $db = array(); |
|
| 573 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 574 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 575 | - require CONFIG_PATH . 'database.php'; |
|
| 576 | - } |
|
| 571 | + public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 572 | + $db = array(); |
|
| 573 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 574 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 575 | + require CONFIG_PATH . 'database.php'; |
|
| 576 | + } |
|
| 577 | 577 | |
| 578 | - if (! empty($overwriteConfig)){ |
|
| 579 | - $db = array_merge($db, $overwriteConfig); |
|
| 580 | - } |
|
| 581 | - $config = array( |
|
| 582 | - 'driver' => 'mysql', |
|
| 583 | - 'username' => 'root', |
|
| 584 | - 'password' => '', |
|
| 585 | - 'database' => '', |
|
| 586 | - 'hostname' => 'localhost', |
|
| 587 | - 'charset' => 'utf8', |
|
| 588 | - 'collation' => 'utf8_general_ci', |
|
| 589 | - 'prefix' => '', |
|
| 590 | - 'port' => '' |
|
| 591 | - ); |
|
| 578 | + if (! empty($overwriteConfig)){ |
|
| 579 | + $db = array_merge($db, $overwriteConfig); |
|
| 580 | + } |
|
| 581 | + $config = array( |
|
| 582 | + 'driver' => 'mysql', |
|
| 583 | + 'username' => 'root', |
|
| 584 | + 'password' => '', |
|
| 585 | + 'database' => '', |
|
| 586 | + 'hostname' => 'localhost', |
|
| 587 | + 'charset' => 'utf8', |
|
| 588 | + 'collation' => 'utf8_general_ci', |
|
| 589 | + 'prefix' => '', |
|
| 590 | + 'port' => '' |
|
| 591 | + ); |
|
| 592 | 592 | |
| 593 | 593 | $config = array_merge($config, $db); |
| 594 | 594 | if (strstr($config['hostname'], ':')){ |
@@ -621,77 +621,77 @@ discard block |
||
| 621 | 621 | } |
| 622 | 622 | |
| 623 | 623 | /** |
| 624 | - * Set the result for SELECT query using PDOStatment |
|
| 625 | - * @see Database::query |
|
| 626 | - */ |
|
| 627 | - protected function setQueryResultForSelect($pdoStatment, $all, $array){ |
|
| 628 | - //if need return all result like list of record |
|
| 629 | - if (is_bool($all) && $all){ |
|
| 630 | - $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
|
| 631 | - } |
|
| 632 | - else{ |
|
| 633 | - $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
|
| 634 | - } |
|
| 635 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 636 | - if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 637 | - $this->numRows = count($this->result); |
|
| 638 | - } |
|
| 639 | - else{ |
|
| 640 | - $this->numRows = $pdoStatment->rowCount(); |
|
| 641 | - } |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Set the result for other command than SELECT query using PDOStatment |
|
| 646 | - * @see Database::query |
|
| 647 | - */ |
|
| 648 | - protected function setQueryResultForNonSelect($pdoStatment){ |
|
| 649 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 650 | - if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 651 | - $this->result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 652 | - $this->numRows = 1; //TODO use the correct method to get the exact affected row |
|
| 653 | - } |
|
| 654 | - else{ |
|
| 655 | - $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 656 | - $this->numRows = $pdoStatment->rowCount(); |
|
| 657 | - } |
|
| 658 | - } |
|
| 624 | + * Set the result for SELECT query using PDOStatment |
|
| 625 | + * @see Database::query |
|
| 626 | + */ |
|
| 627 | + protected function setQueryResultForSelect($pdoStatment, $all, $array){ |
|
| 628 | + //if need return all result like list of record |
|
| 629 | + if (is_bool($all) && $all){ |
|
| 630 | + $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
|
| 631 | + } |
|
| 632 | + else{ |
|
| 633 | + $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
|
| 634 | + } |
|
| 635 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 636 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 637 | + $this->numRows = count($this->result); |
|
| 638 | + } |
|
| 639 | + else{ |
|
| 640 | + $this->numRows = $pdoStatment->rowCount(); |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Set the result for other command than SELECT query using PDOStatment |
|
| 646 | + * @see Database::query |
|
| 647 | + */ |
|
| 648 | + protected function setQueryResultForNonSelect($pdoStatment){ |
|
| 649 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 650 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 651 | + $this->result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 652 | + $this->numRows = 1; //TODO use the correct method to get the exact affected row |
|
| 653 | + } |
|
| 654 | + else{ |
|
| 655 | + $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 656 | + $this->numRows = $pdoStatment->rowCount(); |
|
| 657 | + } |
|
| 658 | + } |
|
| 659 | 659 | |
| 660 | 660 | |
| 661 | 661 | |
| 662 | - /** |
|
| 663 | - * This method is used to get the PDO DSN string using the configured driver |
|
| 664 | - * @return string the DSN string |
|
| 665 | - */ |
|
| 666 | - protected function getDsnFromDriver(){ |
|
| 667 | - $config = $this->getDatabaseConfiguration(); |
|
| 668 | - if (! empty($config)){ |
|
| 669 | - $driver = $config['driver']; |
|
| 670 | - $driverDsnMap = array( |
|
| 671 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 672 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 673 | - . 'dbname=' . $config['database'], |
|
| 674 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 675 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 676 | - . 'dbname=' . $config['database'], |
|
| 677 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 678 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 679 | - . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 680 | - . '/' . $config['database'] |
|
| 681 | - ); |
|
| 682 | - return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
|
| 683 | - } |
|
| 684 | - return null; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Transform the prepared query like (?, ?, ?) into string format |
|
| 689 | - * @see Database::query |
|
| 690 | - * |
|
| 691 | - * @return string |
|
| 692 | - */ |
|
| 693 | - protected function getPreparedQuery($query, $data){ |
|
| 694 | - if (is_array($data)){ |
|
| 662 | + /** |
|
| 663 | + * This method is used to get the PDO DSN string using the configured driver |
|
| 664 | + * @return string the DSN string |
|
| 665 | + */ |
|
| 666 | + protected function getDsnFromDriver(){ |
|
| 667 | + $config = $this->getDatabaseConfiguration(); |
|
| 668 | + if (! empty($config)){ |
|
| 669 | + $driver = $config['driver']; |
|
| 670 | + $driverDsnMap = array( |
|
| 671 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 672 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 673 | + . 'dbname=' . $config['database'], |
|
| 674 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 675 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 676 | + . 'dbname=' . $config['database'], |
|
| 677 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 678 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 679 | + . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 680 | + . '/' . $config['database'] |
|
| 681 | + ); |
|
| 682 | + return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
|
| 683 | + } |
|
| 684 | + return null; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Transform the prepared query like (?, ?, ?) into string format |
|
| 689 | + * @see Database::query |
|
| 690 | + * |
|
| 691 | + * @return string |
|
| 692 | + */ |
|
| 693 | + protected function getPreparedQuery($query, $data){ |
|
| 694 | + if (is_array($data)){ |
|
| 695 | 695 | $x = explode('?', $query); |
| 696 | 696 | $q = ''; |
| 697 | 697 | foreach($x as $k => $v){ |
@@ -700,121 +700,121 @@ discard block |
||
| 700 | 700 | } |
| 701 | 701 | } |
| 702 | 702 | return $q; |
| 703 | - } |
|
| 704 | - return $query; |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * Get the cache content for this query |
|
| 709 | - * @see Database::query |
|
| 710 | - * |
|
| 711 | - * @return mixed |
|
| 712 | - */ |
|
| 713 | - protected function getCacheContentForQuery($query, $all, $array){ |
|
| 714 | - $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 715 | - if (! is_object($this->getCacheInstance())){ |
|
| 716 | - //can not call method with reference in argument |
|
| 717 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 718 | - //use temporary variable |
|
| 719 | - $instance = & get_instance()->cache; |
|
| 720 | - $this->setCacheInstance($instance); |
|
| 721 | - } |
|
| 722 | - return $this->getCacheInstance()->get($cacheKey); |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * Save the result of query into cache |
|
| 727 | - * @param string $query the SQL query |
|
| 728 | - * @param string $key the cache key |
|
| 729 | - * @param mixed $result the query result to save |
|
| 730 | - * @param boolean $status whether can save the query result into cache |
|
| 731 | - * @param int $expire the cache TTL |
|
| 732 | - */ |
|
| 733 | - protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 734 | - if ($status){ |
|
| 735 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 736 | - if (! is_object($this->getCacheInstance())){ |
|
| 737 | - //can not call method with reference in argument |
|
| 738 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 739 | - //use temporary variable |
|
| 740 | - $instance = & get_instance()->cache; |
|
| 741 | - $this->setCacheInstance($instance); |
|
| 742 | - } |
|
| 743 | - $this->getCacheInstance()->set($key, $result, $expire); |
|
| 744 | - } |
|
| 745 | - } |
|
| 703 | + } |
|
| 704 | + return $query; |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * Get the cache content for this query |
|
| 709 | + * @see Database::query |
|
| 710 | + * |
|
| 711 | + * @return mixed |
|
| 712 | + */ |
|
| 713 | + protected function getCacheContentForQuery($query, $all, $array){ |
|
| 714 | + $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 715 | + if (! is_object($this->getCacheInstance())){ |
|
| 716 | + //can not call method with reference in argument |
|
| 717 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 718 | + //use temporary variable |
|
| 719 | + $instance = & get_instance()->cache; |
|
| 720 | + $this->setCacheInstance($instance); |
|
| 721 | + } |
|
| 722 | + return $this->getCacheInstance()->get($cacheKey); |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * Save the result of query into cache |
|
| 727 | + * @param string $query the SQL query |
|
| 728 | + * @param string $key the cache key |
|
| 729 | + * @param mixed $result the query result to save |
|
| 730 | + * @param boolean $status whether can save the query result into cache |
|
| 731 | + * @param int $expire the cache TTL |
|
| 732 | + */ |
|
| 733 | + protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 734 | + if ($status){ |
|
| 735 | + $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 736 | + if (! is_object($this->getCacheInstance())){ |
|
| 737 | + //can not call method with reference in argument |
|
| 738 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 739 | + //use temporary variable |
|
| 740 | + $instance = & get_instance()->cache; |
|
| 741 | + $this->setCacheInstance($instance); |
|
| 742 | + } |
|
| 743 | + $this->getCacheInstance()->set($key, $result, $expire); |
|
| 744 | + } |
|
| 745 | + } |
|
| 746 | 746 | |
| 747 | 747 | |
| 748 | - /** |
|
| 749 | - * Set error for database query execution |
|
| 750 | - */ |
|
| 751 | - protected function setQueryError(){ |
|
| 752 | - $error = $this->pdo->errorInfo(); |
|
| 753 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 754 | - $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
|
| 755 | - //show error message |
|
| 756 | - $this->error(); |
|
| 757 | - } |
|
| 748 | + /** |
|
| 749 | + * Set error for database query execution |
|
| 750 | + */ |
|
| 751 | + protected function setQueryError(){ |
|
| 752 | + $error = $this->pdo->errorInfo(); |
|
| 753 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 754 | + $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
|
| 755 | + //show error message |
|
| 756 | + $this->error(); |
|
| 757 | + } |
|
| 758 | 758 | |
| 759 | 759 | /** |
| 760 | - * Return the cache key for the given query |
|
| 761 | - * @see Database::query |
|
| 762 | - * |
|
| 763 | - * @return string |
|
| 764 | - */ |
|
| 765 | - protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 766 | - if (is_array($all)){ |
|
| 767 | - $all = 'array'; |
|
| 768 | - } |
|
| 769 | - return md5($query . $all . $array); |
|
| 770 | - } |
|
| 760 | + * Return the cache key for the given query |
|
| 761 | + * @see Database::query |
|
| 762 | + * |
|
| 763 | + * @return string |
|
| 764 | + */ |
|
| 765 | + protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 766 | + if (is_array($all)){ |
|
| 767 | + $all = 'array'; |
|
| 768 | + } |
|
| 769 | + return md5($query . $all . $array); |
|
| 770 | + } |
|
| 771 | 771 | |
| 772 | 772 | /** |
| 773 | - * Set the Log instance using argument or create new instance |
|
| 774 | - * @param object $logger the Log instance if not null |
|
| 775 | - */ |
|
| 776 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 777 | - if ($logger !== null){ |
|
| 778 | - $this->setLogger($logger); |
|
| 779 | - } |
|
| 780 | - else{ |
|
| 781 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 782 | - $this->logger->setLogger('Library::Database'); |
|
| 783 | - } |
|
| 784 | - } |
|
| 773 | + * Set the Log instance using argument or create new instance |
|
| 774 | + * @param object $logger the Log instance if not null |
|
| 775 | + */ |
|
| 776 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 777 | + if ($logger !== null){ |
|
| 778 | + $this->setLogger($logger); |
|
| 779 | + } |
|
| 780 | + else{ |
|
| 781 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 782 | + $this->logger->setLogger('Library::Database'); |
|
| 783 | + } |
|
| 784 | + } |
|
| 785 | 785 | |
| 786 | 786 | /** |
| 787 | - * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 788 | - * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 789 | - */ |
|
| 787 | + * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 788 | + * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 789 | + */ |
|
| 790 | 790 | protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
| 791 | 791 | if ($queryBuilder !== null){ |
| 792 | - $this->setQueryBuilder($queryBuilder); |
|
| 792 | + $this->setQueryBuilder($queryBuilder); |
|
| 793 | 793 | } |
| 794 | 794 | else{ |
| 795 | 795 | $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes'); |
| 796 | 796 | } |
| 797 | 797 | } |
| 798 | 798 | |
| 799 | - /** |
|
| 800 | - * Reset the database class attributs to the initail values before each query. |
|
| 801 | - */ |
|
| 802 | - private function reset(){ |
|
| 799 | + /** |
|
| 800 | + * Reset the database class attributs to the initail values before each query. |
|
| 801 | + */ |
|
| 802 | + private function reset(){ |
|
| 803 | 803 | //query builder reset |
| 804 | - $this->getQueryBuilder()->reset(); |
|
| 805 | - $this->numRows = 0; |
|
| 806 | - $this->insertId = null; |
|
| 807 | - $this->query = null; |
|
| 808 | - $this->error = null; |
|
| 809 | - $this->result = array(); |
|
| 810 | - $this->data = array(); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - /** |
|
| 814 | - * The class destructor |
|
| 815 | - */ |
|
| 816 | - public function __destruct(){ |
|
| 817 | - $this->pdo = null; |
|
| 818 | - } |
|
| 804 | + $this->getQueryBuilder()->reset(); |
|
| 805 | + $this->numRows = 0; |
|
| 806 | + $this->insertId = null; |
|
| 807 | + $this->query = null; |
|
| 808 | + $this->error = null; |
|
| 809 | + $this->result = array(); |
|
| 810 | + $this->data = array(); |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + /** |
|
| 814 | + * The class destructor |
|
| 815 | + */ |
|
| 816 | + public function __destruct(){ |
|
| 817 | + $this->pdo = null; |
|
| 818 | + } |
|
| 819 | 819 | |
| 820 | 820 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | 3 | /** |
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
@@ -22,740 +22,740 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | class DatabaseQueryBuilder{ |
| 27 | 27 | /** |
| 28 | 28 | * The SQL SELECT statment |
| 29 | 29 | * @var string |
| 30 | - */ |
|
| 30 | + */ |
|
| 31 | 31 | private $select = '*'; |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * The SQL FROM statment |
| 35 | 35 | * @var string |
| 36 | - */ |
|
| 37 | - private $from = null; |
|
| 36 | + */ |
|
| 37 | + private $from = null; |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * The SQL WHERE statment |
| 41 | 41 | * @var string |
| 42 | - */ |
|
| 43 | - private $where = null; |
|
| 42 | + */ |
|
| 43 | + private $where = null; |
|
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | 46 | * The SQL LIMIT statment |
| 47 | 47 | * @var string |
| 48 | - */ |
|
| 49 | - private $limit = null; |
|
| 48 | + */ |
|
| 49 | + private $limit = null; |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * The SQL JOIN statment |
| 53 | 53 | * @var string |
| 54 | - */ |
|
| 55 | - private $join = null; |
|
| 54 | + */ |
|
| 55 | + private $join = null; |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * The SQL ORDER BY statment |
| 59 | 59 | * @var string |
| 60 | - */ |
|
| 61 | - private $orderBy = null; |
|
| 60 | + */ |
|
| 61 | + private $orderBy = null; |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * The SQL GROUP BY statment |
| 65 | 65 | * @var string |
| 66 | - */ |
|
| 67 | - private $groupBy = null; |
|
| 66 | + */ |
|
| 67 | + private $groupBy = null; |
|
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * The SQL HAVING statment |
| 71 | 71 | * @var string |
| 72 | - */ |
|
| 73 | - private $having = null; |
|
| 72 | + */ |
|
| 73 | + private $having = null; |
|
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * The full SQL query statment after build for each command |
| 77 | 77 | * @var string |
| 78 | - */ |
|
| 79 | - private $query = null; |
|
| 78 | + */ |
|
| 79 | + private $query = null; |
|
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | 82 | * The list of SQL valid operators |
| 83 | 83 | * @var array |
| 84 | - */ |
|
| 85 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 84 | + */ |
|
| 85 | + private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The prefix used in each database table |
| 90 | 90 | * @var string |
| 91 | - */ |
|
| 92 | - private $prefix = null; |
|
| 91 | + */ |
|
| 92 | + private $prefix = null; |
|
| 93 | 93 | |
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | - * The PDO instance |
|
| 97 | - * @var object |
|
| 98 | - */ |
|
| 96 | + * The PDO instance |
|
| 97 | + * @var object |
|
| 98 | + */ |
|
| 99 | 99 | private $pdo = null; |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * The database driver name used |
| 103 | 103 | * @var string |
| 104 | - */ |
|
| 104 | + */ |
|
| 105 | 105 | private $driver = null; |
| 106 | 106 | |
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Construct new DatabaseQueryBuilder |
|
| 110 | - * @param object $pdo the PDO object |
|
| 111 | - */ |
|
| 112 | - public function __construct(PDO $pdo = null){ |
|
| 113 | - if (is_object($pdo)){ |
|
| 114 | - $this->setPdo($pdo); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Set the SQL FROM statment |
|
| 120 | - * @param string|array $table the table name or array of table list |
|
| 121 | - * @return object the current DatabaseQueryBuilder instance |
|
| 122 | - */ |
|
| 123 | - public function from($table){ |
|
| 108 | + /** |
|
| 109 | + * Construct new DatabaseQueryBuilder |
|
| 110 | + * @param object $pdo the PDO object |
|
| 111 | + */ |
|
| 112 | + public function __construct(PDO $pdo = null){ |
|
| 113 | + if (is_object($pdo)){ |
|
| 114 | + $this->setPdo($pdo); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Set the SQL FROM statment |
|
| 120 | + * @param string|array $table the table name or array of table list |
|
| 121 | + * @return object the current DatabaseQueryBuilder instance |
|
| 122 | + */ |
|
| 123 | + public function from($table){ |
|
| 124 | 124 | if (is_array($table)){ |
| 125 | - $froms = ''; |
|
| 126 | - foreach($table as $key){ |
|
| 127 | - $froms .= $this->getPrefix() . $key . ', '; |
|
| 128 | - } |
|
| 129 | - $this->from = rtrim($froms, ', '); |
|
| 130 | - } else { |
|
| 131 | - $this->from = $this->getPrefix() . $table; |
|
| 132 | - } |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Set the SQL SELECT statment |
|
| 138 | - * @param string|array $fields the field name or array of field list |
|
| 139 | - * @return object the current DatabaseQueryBuilder instance |
|
| 140 | - */ |
|
| 141 | - public function select($fields){ |
|
| 142 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | - $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | - return $this; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Set the SQL SELECT DISTINCT statment |
|
| 149 | - * @param string $field the field name to distinct |
|
| 150 | - * @return object the current DatabaseQueryBuilder instance |
|
| 151 | - */ |
|
| 152 | - public function distinct($field){ |
|
| 153 | - $distinct = ' DISTINCT ' . $field; |
|
| 154 | - $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | - return $this; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Set the SQL function COUNT in SELECT statment |
|
| 160 | - * @param string $field the field name |
|
| 161 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | - * @return object the current DatabaseQueryBuilder instance |
|
| 163 | - */ |
|
| 164 | - public function count($field = '*', $name = null){ |
|
| 165 | - return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 166 | - } |
|
| 125 | + $froms = ''; |
|
| 126 | + foreach($table as $key){ |
|
| 127 | + $froms .= $this->getPrefix() . $key . ', '; |
|
| 128 | + } |
|
| 129 | + $this->from = rtrim($froms, ', '); |
|
| 130 | + } else { |
|
| 131 | + $this->from = $this->getPrefix() . $table; |
|
| 132 | + } |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Set the SQL SELECT statment |
|
| 138 | + * @param string|array $fields the field name or array of field list |
|
| 139 | + * @return object the current DatabaseQueryBuilder instance |
|
| 140 | + */ |
|
| 141 | + public function select($fields){ |
|
| 142 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | + $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | + return $this; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Set the SQL SELECT DISTINCT statment |
|
| 149 | + * @param string $field the field name to distinct |
|
| 150 | + * @return object the current DatabaseQueryBuilder instance |
|
| 151 | + */ |
|
| 152 | + public function distinct($field){ |
|
| 153 | + $distinct = ' DISTINCT ' . $field; |
|
| 154 | + $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | + return $this; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Set the SQL function COUNT in SELECT statment |
|
| 160 | + * @param string $field the field name |
|
| 161 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | + * @return object the current DatabaseQueryBuilder instance |
|
| 163 | + */ |
|
| 164 | + public function count($field = '*', $name = null){ |
|
| 165 | + return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Set the SQL function MIN in SELECT statment |
|
| 170 | - * @param string $field the field name |
|
| 171 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 172 | - * @return object the current DatabaseQueryBuilder instance |
|
| 173 | - */ |
|
| 174 | - public function min($field, $name = null){ |
|
| 175 | - return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Set the SQL function MAX in SELECT statment |
|
| 180 | - * @param string $field the field name |
|
| 181 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 182 | - * @return object the current DatabaseQueryBuilder instance |
|
| 183 | - */ |
|
| 184 | - public function max($field, $name = null){ |
|
| 185 | - return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Set the SQL function SUM in SELECT statment |
|
| 190 | - * @param string $field the field name |
|
| 191 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 192 | - * @return object the current DatabaseQueryBuilder instance |
|
| 193 | - */ |
|
| 194 | - public function sum($field, $name = null){ |
|
| 195 | - return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Set the SQL function AVG in SELECT statment |
|
| 200 | - * @param string $field the field name |
|
| 201 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 202 | - * @return object the current DatabaseQueryBuilder instance |
|
| 203 | - */ |
|
| 204 | - public function avg($field, $name = null){ |
|
| 205 | - return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Set the SQL JOIN statment |
|
| 211 | - * @param string $table the join table name |
|
| 212 | - * @param string $field1 the first field for join conditions |
|
| 213 | - * @param string $op the join condition operator. If is null the default will be "=" |
|
| 214 | - * @param string $field2 the second field for join conditions |
|
| 215 | - * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 216 | - * @return object the current DatabaseQueryBuilder instance |
|
| 217 | - */ |
|
| 218 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 219 | - $on = $field1; |
|
| 220 | - $table = $this->getPrefix() . $table; |
|
| 221 | - if (! is_null($op)){ |
|
| 222 | - $on = (! in_array($op, $this->operatorList) |
|
| 168 | + /** |
|
| 169 | + * Set the SQL function MIN in SELECT statment |
|
| 170 | + * @param string $field the field name |
|
| 171 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 172 | + * @return object the current DatabaseQueryBuilder instance |
|
| 173 | + */ |
|
| 174 | + public function min($field, $name = null){ |
|
| 175 | + return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Set the SQL function MAX in SELECT statment |
|
| 180 | + * @param string $field the field name |
|
| 181 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 182 | + * @return object the current DatabaseQueryBuilder instance |
|
| 183 | + */ |
|
| 184 | + public function max($field, $name = null){ |
|
| 185 | + return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Set the SQL function SUM in SELECT statment |
|
| 190 | + * @param string $field the field name |
|
| 191 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 192 | + * @return object the current DatabaseQueryBuilder instance |
|
| 193 | + */ |
|
| 194 | + public function sum($field, $name = null){ |
|
| 195 | + return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Set the SQL function AVG in SELECT statment |
|
| 200 | + * @param string $field the field name |
|
| 201 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 202 | + * @return object the current DatabaseQueryBuilder instance |
|
| 203 | + */ |
|
| 204 | + public function avg($field, $name = null){ |
|
| 205 | + return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Set the SQL JOIN statment |
|
| 211 | + * @param string $table the join table name |
|
| 212 | + * @param string $field1 the first field for join conditions |
|
| 213 | + * @param string $op the join condition operator. If is null the default will be "=" |
|
| 214 | + * @param string $field2 the second field for join conditions |
|
| 215 | + * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 216 | + * @return object the current DatabaseQueryBuilder instance |
|
| 217 | + */ |
|
| 218 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 219 | + $on = $field1; |
|
| 220 | + $table = $this->getPrefix() . $table; |
|
| 221 | + if (! is_null($op)){ |
|
| 222 | + $on = (! in_array($op, $this->operatorList) |
|
| 223 | 223 | ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
| 224 | 224 | : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
| 225 | - } |
|
| 226 | - if (empty($this->join)){ |
|
| 227 | - $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 228 | - } |
|
| 229 | - else{ |
|
| 230 | - $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 231 | - } |
|
| 232 | - return $this; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Set the SQL INNER JOIN statment |
|
| 237 | - * @see DatabaseQueryBuilder::join() |
|
| 238 | - * @return object the current DatabaseQueryBuilder instance |
|
| 239 | - */ |
|
| 240 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 241 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Set the SQL LEFT JOIN statment |
|
| 246 | - * @see DatabaseQueryBuilder::join() |
|
| 247 | - * @return object the current DatabaseQueryBuilder instance |
|
| 248 | - */ |
|
| 249 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 250 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Set the SQL RIGHT JOIN statment |
|
| 255 | - * @see DatabaseQueryBuilder::join() |
|
| 256 | - * @return object the current DatabaseQueryBuilder instance |
|
| 257 | - */ |
|
| 258 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 259 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Set the SQL FULL OUTER JOIN statment |
|
| 264 | - * @see DatabaseQueryBuilder::join() |
|
| 265 | - * @return object the current DatabaseQueryBuilder instance |
|
| 266 | - */ |
|
| 267 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 268 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Set the SQL LEFT OUTER JOIN statment |
|
| 273 | - * @see DatabaseQueryBuilder::join() |
|
| 274 | - * @return object the current DatabaseQueryBuilder instance |
|
| 275 | - */ |
|
| 276 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 277 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Set the SQL RIGHT OUTER JOIN statment |
|
| 282 | - * @see DatabaseQueryBuilder::join() |
|
| 283 | - * @return object the current DatabaseQueryBuilder instance |
|
| 284 | - */ |
|
| 285 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 286 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Set the SQL WHERE CLAUSE for IS NULL |
|
| 291 | - * @param string|array $field the field name or array of field list |
|
| 292 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 293 | - * @return object the current DatabaseQueryBuilder instance |
|
| 294 | - */ |
|
| 295 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 296 | - if (is_array($field)){ |
|
| 297 | - foreach($field as $f){ |
|
| 298 | - $this->whereIsNull($f, $andOr); |
|
| 299 | - } |
|
| 300 | - } else { |
|
| 301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | - } |
|
| 303 | - return $this; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 308 | - * @param string|array $field the field name or array of field list |
|
| 309 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 310 | - * @return object the current DatabaseQueryBuilder instance |
|
| 311 | - */ |
|
| 312 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 313 | - if (is_array($field)){ |
|
| 314 | - foreach($field as $f){ |
|
| 315 | - $this->whereIsNotNull($f, $andOr); |
|
| 316 | - } |
|
| 317 | - } else { |
|
| 318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | - } |
|
| 320 | - return $this; |
|
| 321 | - } |
|
| 225 | + } |
|
| 226 | + if (empty($this->join)){ |
|
| 227 | + $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 228 | + } |
|
| 229 | + else{ |
|
| 230 | + $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 231 | + } |
|
| 232 | + return $this; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Set the SQL INNER JOIN statment |
|
| 237 | + * @see DatabaseQueryBuilder::join() |
|
| 238 | + * @return object the current DatabaseQueryBuilder instance |
|
| 239 | + */ |
|
| 240 | + public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 241 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Set the SQL LEFT JOIN statment |
|
| 246 | + * @see DatabaseQueryBuilder::join() |
|
| 247 | + * @return object the current DatabaseQueryBuilder instance |
|
| 248 | + */ |
|
| 249 | + public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 250 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Set the SQL RIGHT JOIN statment |
|
| 255 | + * @see DatabaseQueryBuilder::join() |
|
| 256 | + * @return object the current DatabaseQueryBuilder instance |
|
| 257 | + */ |
|
| 258 | + public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 259 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Set the SQL FULL OUTER JOIN statment |
|
| 264 | + * @see DatabaseQueryBuilder::join() |
|
| 265 | + * @return object the current DatabaseQueryBuilder instance |
|
| 266 | + */ |
|
| 267 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 268 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Set the SQL LEFT OUTER JOIN statment |
|
| 273 | + * @see DatabaseQueryBuilder::join() |
|
| 274 | + * @return object the current DatabaseQueryBuilder instance |
|
| 275 | + */ |
|
| 276 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 277 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Set the SQL RIGHT OUTER JOIN statment |
|
| 282 | + * @see DatabaseQueryBuilder::join() |
|
| 283 | + * @return object the current DatabaseQueryBuilder instance |
|
| 284 | + */ |
|
| 285 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 286 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Set the SQL WHERE CLAUSE for IS NULL |
|
| 291 | + * @param string|array $field the field name or array of field list |
|
| 292 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 293 | + * @return object the current DatabaseQueryBuilder instance |
|
| 294 | + */ |
|
| 295 | + public function whereIsNull($field, $andOr = 'AND'){ |
|
| 296 | + if (is_array($field)){ |
|
| 297 | + foreach($field as $f){ |
|
| 298 | + $this->whereIsNull($f, $andOr); |
|
| 299 | + } |
|
| 300 | + } else { |
|
| 301 | + $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | + } |
|
| 303 | + return $this; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 308 | + * @param string|array $field the field name or array of field list |
|
| 309 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 310 | + * @return object the current DatabaseQueryBuilder instance |
|
| 311 | + */ |
|
| 312 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 313 | + if (is_array($field)){ |
|
| 314 | + foreach($field as $f){ |
|
| 315 | + $this->whereIsNotNull($f, $andOr); |
|
| 316 | + } |
|
| 317 | + } else { |
|
| 318 | + $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | + } |
|
| 320 | + return $this; |
|
| 321 | + } |
|
| 322 | 322 | |
| 323 | - /** |
|
| 324 | - * Set the SQL WHERE CLAUSE statment |
|
| 325 | - * @param string|array $where the where field or array of field list |
|
| 326 | - * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 327 | - * @param mixed $val the where value |
|
| 328 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
| 329 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 330 | - * @param boolean $escape whether to escape or not the $val |
|
| 331 | - * @return object the current DatabaseQueryBuilder instance |
|
| 332 | - */ |
|
| 333 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 334 | - $whereStr = ''; |
|
| 335 | - if (is_array($where)){ |
|
| 336 | - $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 337 | - } |
|
| 338 | - else{ |
|
| 339 | - if (is_array($op)){ |
|
| 340 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 341 | - } else { |
|
| 342 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - $this->setWhereStr($whereStr, $andOr); |
|
| 346 | - return $this; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Set the SQL WHERE CLAUSE statment using OR |
|
| 351 | - * @see DatabaseQueryBuilder::where() |
|
| 352 | - * @return object the current DatabaseQueryBuilder instance |
|
| 353 | - */ |
|
| 354 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 355 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 361 | - * @see DatabaseQueryBuilder::where() |
|
| 362 | - * @return object the current DatabaseQueryBuilder instance |
|
| 363 | - */ |
|
| 364 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 365 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 370 | - * @see DatabaseQueryBuilder::where() |
|
| 371 | - * @return object the current DatabaseQueryBuilder instance |
|
| 372 | - */ |
|
| 373 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 374 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Set the opened parenthesis for the complex SQL query |
|
| 379 | - * @param string $type the type of this grouped (NOT, etc.) |
|
| 380 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 381 | - * @return object the current DatabaseQueryBuilder instance |
|
| 382 | - */ |
|
| 383 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 384 | - if (empty($this->where)){ |
|
| 385 | - $this->where = $type . ' ('; |
|
| 386 | - } else { |
|
| 387 | - if (substr(trim($this->where), -1) == '('){ |
|
| 388 | - $this->where .= $type . ' ('; |
|
| 389 | - } else { |
|
| 390 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - return $this; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 398 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 399 | - * @return object the current DatabaseQueryBuilder instance |
|
| 400 | - */ |
|
| 401 | - public function notGroupStart(){ |
|
| 402 | - return $this->groupStart('NOT'); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 407 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 408 | - * @return object the current DatabaseQueryBuilder instance |
|
| 409 | - */ |
|
| 410 | - public function orGroupStart(){ |
|
| 411 | - return $this->groupStart('', ' OR'); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | - * @return object the current DatabaseQueryBuilder instance |
|
| 418 | - */ |
|
| 419 | - public function orNotGroupStart(){ |
|
| 420 | - return $this->groupStart('NOT', ' OR'); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Close the parenthesis for the grouped SQL |
|
| 425 | - * @return object the current DatabaseQueryBuilder instance |
|
| 426 | - */ |
|
| 427 | - public function groupEnd(){ |
|
| 428 | - $this->where .= ')'; |
|
| 429 | - return $this; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Set the SQL WHERE CLAUSE statment for IN |
|
| 434 | - * @param string $field the field name for IN statment |
|
| 435 | - * @param array $keys the list of values used |
|
| 436 | - * @param string $type the condition separator type (NOT) |
|
| 437 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 438 | - * @param boolean $escape whether to escape or not the values |
|
| 439 | - * @return object the current DatabaseQueryBuilder instance |
|
| 440 | - */ |
|
| 441 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 442 | - $_keys = array(); |
|
| 443 | - foreach ($keys as $k => $v){ |
|
| 444 | - if (is_null($v)){ |
|
| 445 | - $v = ''; |
|
| 446 | - } |
|
| 447 | - $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
|
| 448 | - } |
|
| 449 | - $keys = implode(', ', $_keys); |
|
| 450 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | - $this->setWhereStr($whereStr, $andOr); |
|
| 452 | - return $this; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 457 | - * @see DatabaseQueryBuilder::in() |
|
| 458 | - * @return object the current DatabaseQueryBuilder instance |
|
| 459 | - */ |
|
| 460 | - public function notIn($field, array $keys, $escape = true){ |
|
| 461 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 466 | - * @see DatabaseQueryBuilder::in() |
|
| 467 | - * @return object the current DatabaseQueryBuilder instance |
|
| 468 | - */ |
|
| 469 | - public function orIn($field, array $keys, $escape = true){ |
|
| 470 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 475 | - * @see DatabaseQueryBuilder::in() |
|
| 476 | - * @return object the current DatabaseQueryBuilder instance |
|
| 477 | - */ |
|
| 478 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 479 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 484 | - * @param string $field the field used for the BETWEEN statment |
|
| 485 | - * @param mixed $value1 the BETWEEN begin value |
|
| 486 | - * @param mixed $value2 the BETWEEN end value |
|
| 487 | - * @param string $type the condition separator type (NOT) |
|
| 488 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 489 | - * @param boolean $escape whether to escape or not the values |
|
| 490 | - * @return object the current DatabaseQueryBuilder instance |
|
| 491 | - */ |
|
| 492 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 493 | - if (is_null($value1)){ |
|
| 494 | - $value1 = ''; |
|
| 495 | - } |
|
| 496 | - if (is_null($value2)){ |
|
| 497 | - $value2 = ''; |
|
| 498 | - } |
|
| 499 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | - $this->setWhereStr($whereStr, $andOr); |
|
| 501 | - return $this; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 506 | - * @see DatabaseQueryBuilder::between() |
|
| 507 | - * @return object the current DatabaseQueryBuilder instance |
|
| 508 | - */ |
|
| 509 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 510 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - /** |
|
| 514 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 515 | - * @see DatabaseQueryBuilder::between() |
|
| 516 | - * @return object the current DatabaseQueryBuilder instance |
|
| 517 | - */ |
|
| 518 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 519 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 524 | - * @see DatabaseQueryBuilder::between() |
|
| 525 | - * @return object the current DatabaseQueryBuilder instance |
|
| 526 | - */ |
|
| 527 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 528 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 533 | - * @param string $field the field name used in LIKE statment |
|
| 534 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 535 | - * @param string $type the condition separator type (NOT) |
|
| 536 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 537 | - * @param boolean $escape whether to escape or not the values |
|
| 538 | - * @return object the current DatabaseQueryBuilder instance |
|
| 539 | - */ |
|
| 540 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 541 | - if (empty($data)){ |
|
| 542 | - $data = ''; |
|
| 543 | - } |
|
| 544 | - $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | - return $this; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 550 | - * @see DatabaseQueryBuilder::like() |
|
| 551 | - * @return object the current DatabaseQueryBuilder instance |
|
| 552 | - */ |
|
| 553 | - public function orLike($field, $data, $escape = true){ |
|
| 554 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 559 | - * @see DatabaseQueryBuilder::like() |
|
| 560 | - * @return object the current DatabaseQueryBuilder instance |
|
| 561 | - */ |
|
| 562 | - public function notLike($field, $data, $escape = true){ |
|
| 563 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 568 | - * @see DatabaseQueryBuilder::like() |
|
| 569 | - * @return object the current DatabaseQueryBuilder instance |
|
| 570 | - */ |
|
| 571 | - public function orNotLike($field, $data, $escape = true){ |
|
| 572 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Set the SQL LIMIT statment |
|
| 577 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 578 | - * like LIMIT n; |
|
| 579 | - * @param int $limitEnd the limit count |
|
| 580 | - * @return object the current DatabaseQueryBuilder instance |
|
| 581 | - */ |
|
| 582 | - public function limit($limit, $limitEnd = null){ |
|
| 583 | - if (empty($limit)){ |
|
| 584 | - $limit = 0; |
|
| 585 | - } |
|
| 586 | - if (! is_null($limitEnd)){ |
|
| 587 | - $this->limit = $limit . ', ' . $limitEnd; |
|
| 588 | - } |
|
| 589 | - else{ |
|
| 590 | - $this->limit = $limit; |
|
| 591 | - } |
|
| 592 | - return $this; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Set the SQL ORDER BY CLAUSE statment |
|
| 597 | - * @param string $orderBy the field name used for order |
|
| 598 | - * @param string $orderDir the order direction (ASC or DESC) |
|
| 599 | - * @return object the current DatabaseQueryBuilder instance |
|
| 600 | - */ |
|
| 601 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 603 | - $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 604 | - } |
|
| 605 | - else{ |
|
| 606 | - $this->orderBy = empty($this->orderBy) |
|
| 323 | + /** |
|
| 324 | + * Set the SQL WHERE CLAUSE statment |
|
| 325 | + * @param string|array $where the where field or array of field list |
|
| 326 | + * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 327 | + * @param mixed $val the where value |
|
| 328 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
| 329 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 330 | + * @param boolean $escape whether to escape or not the $val |
|
| 331 | + * @return object the current DatabaseQueryBuilder instance |
|
| 332 | + */ |
|
| 333 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 334 | + $whereStr = ''; |
|
| 335 | + if (is_array($where)){ |
|
| 336 | + $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 337 | + } |
|
| 338 | + else{ |
|
| 339 | + if (is_array($op)){ |
|
| 340 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 341 | + } else { |
|
| 342 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + $this->setWhereStr($whereStr, $andOr); |
|
| 346 | + return $this; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Set the SQL WHERE CLAUSE statment using OR |
|
| 351 | + * @see DatabaseQueryBuilder::where() |
|
| 352 | + * @return object the current DatabaseQueryBuilder instance |
|
| 353 | + */ |
|
| 354 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 355 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 361 | + * @see DatabaseQueryBuilder::where() |
|
| 362 | + * @return object the current DatabaseQueryBuilder instance |
|
| 363 | + */ |
|
| 364 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 365 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 370 | + * @see DatabaseQueryBuilder::where() |
|
| 371 | + * @return object the current DatabaseQueryBuilder instance |
|
| 372 | + */ |
|
| 373 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 374 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Set the opened parenthesis for the complex SQL query |
|
| 379 | + * @param string $type the type of this grouped (NOT, etc.) |
|
| 380 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 381 | + * @return object the current DatabaseQueryBuilder instance |
|
| 382 | + */ |
|
| 383 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
| 384 | + if (empty($this->where)){ |
|
| 385 | + $this->where = $type . ' ('; |
|
| 386 | + } else { |
|
| 387 | + if (substr(trim($this->where), -1) == '('){ |
|
| 388 | + $this->where .= $type . ' ('; |
|
| 389 | + } else { |
|
| 390 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + return $this; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 398 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 399 | + * @return object the current DatabaseQueryBuilder instance |
|
| 400 | + */ |
|
| 401 | + public function notGroupStart(){ |
|
| 402 | + return $this->groupStart('NOT'); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 407 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 408 | + * @return object the current DatabaseQueryBuilder instance |
|
| 409 | + */ |
|
| 410 | + public function orGroupStart(){ |
|
| 411 | + return $this->groupStart('', ' OR'); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | + * @return object the current DatabaseQueryBuilder instance |
|
| 418 | + */ |
|
| 419 | + public function orNotGroupStart(){ |
|
| 420 | + return $this->groupStart('NOT', ' OR'); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Close the parenthesis for the grouped SQL |
|
| 425 | + * @return object the current DatabaseQueryBuilder instance |
|
| 426 | + */ |
|
| 427 | + public function groupEnd(){ |
|
| 428 | + $this->where .= ')'; |
|
| 429 | + return $this; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Set the SQL WHERE CLAUSE statment for IN |
|
| 434 | + * @param string $field the field name for IN statment |
|
| 435 | + * @param array $keys the list of values used |
|
| 436 | + * @param string $type the condition separator type (NOT) |
|
| 437 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 438 | + * @param boolean $escape whether to escape or not the values |
|
| 439 | + * @return object the current DatabaseQueryBuilder instance |
|
| 440 | + */ |
|
| 441 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 442 | + $_keys = array(); |
|
| 443 | + foreach ($keys as $k => $v){ |
|
| 444 | + if (is_null($v)){ |
|
| 445 | + $v = ''; |
|
| 446 | + } |
|
| 447 | + $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
|
| 448 | + } |
|
| 449 | + $keys = implode(', ', $_keys); |
|
| 450 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | + $this->setWhereStr($whereStr, $andOr); |
|
| 452 | + return $this; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 457 | + * @see DatabaseQueryBuilder::in() |
|
| 458 | + * @return object the current DatabaseQueryBuilder instance |
|
| 459 | + */ |
|
| 460 | + public function notIn($field, array $keys, $escape = true){ |
|
| 461 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 466 | + * @see DatabaseQueryBuilder::in() |
|
| 467 | + * @return object the current DatabaseQueryBuilder instance |
|
| 468 | + */ |
|
| 469 | + public function orIn($field, array $keys, $escape = true){ |
|
| 470 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 475 | + * @see DatabaseQueryBuilder::in() |
|
| 476 | + * @return object the current DatabaseQueryBuilder instance |
|
| 477 | + */ |
|
| 478 | + public function orNotIn($field, array $keys, $escape = true){ |
|
| 479 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 484 | + * @param string $field the field used for the BETWEEN statment |
|
| 485 | + * @param mixed $value1 the BETWEEN begin value |
|
| 486 | + * @param mixed $value2 the BETWEEN end value |
|
| 487 | + * @param string $type the condition separator type (NOT) |
|
| 488 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 489 | + * @param boolean $escape whether to escape or not the values |
|
| 490 | + * @return object the current DatabaseQueryBuilder instance |
|
| 491 | + */ |
|
| 492 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 493 | + if (is_null($value1)){ |
|
| 494 | + $value1 = ''; |
|
| 495 | + } |
|
| 496 | + if (is_null($value2)){ |
|
| 497 | + $value2 = ''; |
|
| 498 | + } |
|
| 499 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | + $this->setWhereStr($whereStr, $andOr); |
|
| 501 | + return $this; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 506 | + * @see DatabaseQueryBuilder::between() |
|
| 507 | + * @return object the current DatabaseQueryBuilder instance |
|
| 508 | + */ |
|
| 509 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 510 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + /** |
|
| 514 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 515 | + * @see DatabaseQueryBuilder::between() |
|
| 516 | + * @return object the current DatabaseQueryBuilder instance |
|
| 517 | + */ |
|
| 518 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 519 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 524 | + * @see DatabaseQueryBuilder::between() |
|
| 525 | + * @return object the current DatabaseQueryBuilder instance |
|
| 526 | + */ |
|
| 527 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 528 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 533 | + * @param string $field the field name used in LIKE statment |
|
| 534 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 535 | + * @param string $type the condition separator type (NOT) |
|
| 536 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 537 | + * @param boolean $escape whether to escape or not the values |
|
| 538 | + * @return object the current DatabaseQueryBuilder instance |
|
| 539 | + */ |
|
| 540 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 541 | + if (empty($data)){ |
|
| 542 | + $data = ''; |
|
| 543 | + } |
|
| 544 | + $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | + return $this; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 550 | + * @see DatabaseQueryBuilder::like() |
|
| 551 | + * @return object the current DatabaseQueryBuilder instance |
|
| 552 | + */ |
|
| 553 | + public function orLike($field, $data, $escape = true){ |
|
| 554 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 559 | + * @see DatabaseQueryBuilder::like() |
|
| 560 | + * @return object the current DatabaseQueryBuilder instance |
|
| 561 | + */ |
|
| 562 | + public function notLike($field, $data, $escape = true){ |
|
| 563 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 568 | + * @see DatabaseQueryBuilder::like() |
|
| 569 | + * @return object the current DatabaseQueryBuilder instance |
|
| 570 | + */ |
|
| 571 | + public function orNotLike($field, $data, $escape = true){ |
|
| 572 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * Set the SQL LIMIT statment |
|
| 577 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 578 | + * like LIMIT n; |
|
| 579 | + * @param int $limitEnd the limit count |
|
| 580 | + * @return object the current DatabaseQueryBuilder instance |
|
| 581 | + */ |
|
| 582 | + public function limit($limit, $limitEnd = null){ |
|
| 583 | + if (empty($limit)){ |
|
| 584 | + $limit = 0; |
|
| 585 | + } |
|
| 586 | + if (! is_null($limitEnd)){ |
|
| 587 | + $this->limit = $limit . ', ' . $limitEnd; |
|
| 588 | + } |
|
| 589 | + else{ |
|
| 590 | + $this->limit = $limit; |
|
| 591 | + } |
|
| 592 | + return $this; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Set the SQL ORDER BY CLAUSE statment |
|
| 597 | + * @param string $orderBy the field name used for order |
|
| 598 | + * @param string $orderDir the order direction (ASC or DESC) |
|
| 599 | + * @return object the current DatabaseQueryBuilder instance |
|
| 600 | + */ |
|
| 601 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 603 | + $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 604 | + } |
|
| 605 | + else{ |
|
| 606 | + $this->orderBy = empty($this->orderBy) |
|
| 607 | 607 | ? ($orderBy . ' ' . strtoupper($orderDir)) |
| 608 | 608 | : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
| 609 | - } |
|
| 610 | - return $this; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * Set the SQL GROUP BY CLAUSE statment |
|
| 615 | - * @param string|array $field the field name used or array of field list |
|
| 616 | - * @return object the current DatabaseQueryBuilder instance |
|
| 617 | - */ |
|
| 618 | - public function groupBy($field){ |
|
| 619 | - if (is_array($field)){ |
|
| 620 | - $this->groupBy = implode(', ', $field); |
|
| 621 | - } |
|
| 622 | - else{ |
|
| 623 | - $this->groupBy = $field; |
|
| 624 | - } |
|
| 625 | - return $this; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Set the SQL HAVING CLAUSE statment |
|
| 630 | - * @param string $field the field name used for HAVING statment |
|
| 631 | - * @param string|array $op the operator used or array |
|
| 632 | - * @param mixed $val the value for HAVING comparaison |
|
| 633 | - * @param boolean $escape whether to escape or not the values |
|
| 634 | - * @return object the current DatabaseQueryBuilder instance |
|
| 635 | - */ |
|
| 636 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 637 | - if (is_array($op)){ |
|
| 638 | - $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
|
| 639 | - } |
|
| 640 | - else if (! in_array($op, $this->operatorList)){ |
|
| 641 | - if (is_null($op)){ |
|
| 642 | - $op = ''; |
|
| 643 | - } |
|
| 644 | - $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
|
| 645 | - } |
|
| 646 | - else{ |
|
| 647 | - if (is_null($val)){ |
|
| 648 | - $val = ''; |
|
| 649 | - } |
|
| 650 | - $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
|
| 651 | - } |
|
| 652 | - return $this; |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * Insert new record in the database |
|
| 657 | - * @param array $data the record data |
|
| 658 | - * @param boolean $escape whether to escape or not the values |
|
| 659 | - * @return object the current DatabaseQueryBuilder instance |
|
| 660 | - */ |
|
| 661 | - public function insert($data = array(), $escape = true){ |
|
| 662 | - $columns = array_keys($data); |
|
| 663 | - $column = implode(',', $columns); |
|
| 664 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 665 | - |
|
| 666 | - $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | - return $this; |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * Update record in the database |
|
| 672 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 673 | - * @param boolean $escape whether to escape or not the values |
|
| 674 | - * @return object the current DatabaseQueryBuilder instance |
|
| 675 | - */ |
|
| 676 | - public function update($data = array(), $escape = true){ |
|
| 677 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | - $values = array(); |
|
| 679 | - foreach ($data as $column => $val){ |
|
| 680 | - $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
|
| 681 | - } |
|
| 682 | - $query .= implode(', ', $values); |
|
| 683 | - if (! empty($this->where)){ |
|
| 684 | - $query .= ' WHERE ' . $this->where; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - if (! empty($this->orderBy)){ |
|
| 688 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - if (! empty($this->limit)){ |
|
| 692 | - $query .= ' LIMIT ' . $this->limit; |
|
| 693 | - } |
|
| 694 | - $this->query = $query; |
|
| 695 | - return $this; |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Delete the record in database |
|
| 700 | - * @return object the current DatabaseQueryBuilder instance |
|
| 701 | - */ |
|
| 702 | - public function delete(){ |
|
| 703 | - $query = 'DELETE FROM ' . $this->from; |
|
| 704 | - $isTruncate = $query; |
|
| 705 | - if (! empty($this->where)){ |
|
| 609 | + } |
|
| 610 | + return $this; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * Set the SQL GROUP BY CLAUSE statment |
|
| 615 | + * @param string|array $field the field name used or array of field list |
|
| 616 | + * @return object the current DatabaseQueryBuilder instance |
|
| 617 | + */ |
|
| 618 | + public function groupBy($field){ |
|
| 619 | + if (is_array($field)){ |
|
| 620 | + $this->groupBy = implode(', ', $field); |
|
| 621 | + } |
|
| 622 | + else{ |
|
| 623 | + $this->groupBy = $field; |
|
| 624 | + } |
|
| 625 | + return $this; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Set the SQL HAVING CLAUSE statment |
|
| 630 | + * @param string $field the field name used for HAVING statment |
|
| 631 | + * @param string|array $op the operator used or array |
|
| 632 | + * @param mixed $val the value for HAVING comparaison |
|
| 633 | + * @param boolean $escape whether to escape or not the values |
|
| 634 | + * @return object the current DatabaseQueryBuilder instance |
|
| 635 | + */ |
|
| 636 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
| 637 | + if (is_array($op)){ |
|
| 638 | + $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
|
| 639 | + } |
|
| 640 | + else if (! in_array($op, $this->operatorList)){ |
|
| 641 | + if (is_null($op)){ |
|
| 642 | + $op = ''; |
|
| 643 | + } |
|
| 644 | + $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
|
| 645 | + } |
|
| 646 | + else{ |
|
| 647 | + if (is_null($val)){ |
|
| 648 | + $val = ''; |
|
| 649 | + } |
|
| 650 | + $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
|
| 651 | + } |
|
| 652 | + return $this; |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * Insert new record in the database |
|
| 657 | + * @param array $data the record data |
|
| 658 | + * @param boolean $escape whether to escape or not the values |
|
| 659 | + * @return object the current DatabaseQueryBuilder instance |
|
| 660 | + */ |
|
| 661 | + public function insert($data = array(), $escape = true){ |
|
| 662 | + $columns = array_keys($data); |
|
| 663 | + $column = implode(',', $columns); |
|
| 664 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 665 | + |
|
| 666 | + $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | + return $this; |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * Update record in the database |
|
| 672 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 673 | + * @param boolean $escape whether to escape or not the values |
|
| 674 | + * @return object the current DatabaseQueryBuilder instance |
|
| 675 | + */ |
|
| 676 | + public function update($data = array(), $escape = true){ |
|
| 677 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | + $values = array(); |
|
| 679 | + foreach ($data as $column => $val){ |
|
| 680 | + $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
|
| 681 | + } |
|
| 682 | + $query .= implode(', ', $values); |
|
| 683 | + if (! empty($this->where)){ |
|
| 684 | + $query .= ' WHERE ' . $this->where; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + if (! empty($this->orderBy)){ |
|
| 688 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + if (! empty($this->limit)){ |
|
| 692 | + $query .= ' LIMIT ' . $this->limit; |
|
| 693 | + } |
|
| 694 | + $this->query = $query; |
|
| 695 | + return $this; |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Delete the record in database |
|
| 700 | + * @return object the current DatabaseQueryBuilder instance |
|
| 701 | + */ |
|
| 702 | + public function delete(){ |
|
| 703 | + $query = 'DELETE FROM ' . $this->from; |
|
| 704 | + $isTruncate = $query; |
|
| 705 | + if (! empty($this->where)){ |
|
| 706 | 706 | $query .= ' WHERE ' . $this->where; |
| 707 | - } |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | - if (! empty($this->orderBy)){ |
|
| 710 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | - } |
|
| 709 | + if (! empty($this->orderBy)){ |
|
| 710 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | + } |
|
| 712 | 712 | |
| 713 | - if (! empty($this->limit)){ |
|
| 714 | - $query .= ' LIMIT ' . $this->limit; |
|
| 715 | - } |
|
| 713 | + if (! empty($this->limit)){ |
|
| 714 | + $query .= ' LIMIT ' . $this->limit; |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | 717 | if ($isTruncate == $query && $this->driver != 'sqlite'){ |
| 718 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 718 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | 719 | } |
| 720 | 720 | $this->query = $query; |
| 721 | 721 | return $this; |
| 722 | - } |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * Escape the data before execute query useful for security. |
|
| 726 | - * @param mixed $data the data to be escaped |
|
| 727 | - * @param boolean $escaped whether we can do escape of not |
|
| 728 | - * @return mixed the data after escaped or the same data if not |
|
| 729 | - */ |
|
| 730 | - public function escape($data, $escaped = true){ |
|
| 731 | - return $escaped |
|
| 732 | - ? $this->getPdo()->quote(trim($data)) |
|
| 733 | - : $data; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Return the current SQL query string |
|
| 739 | - * @return string |
|
| 740 | - */ |
|
| 741 | - public function getQuery(){ |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * Escape the data before execute query useful for security. |
|
| 726 | + * @param mixed $data the data to be escaped |
|
| 727 | + * @param boolean $escaped whether we can do escape of not |
|
| 728 | + * @return mixed the data after escaped or the same data if not |
|
| 729 | + */ |
|
| 730 | + public function escape($data, $escaped = true){ |
|
| 731 | + return $escaped |
|
| 732 | + ? $this->getPdo()->quote(trim($data)) |
|
| 733 | + : $data; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Return the current SQL query string |
|
| 739 | + * @return string |
|
| 740 | + */ |
|
| 741 | + public function getQuery(){ |
|
| 742 | 742 | //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
| 743 | 743 | if(empty($this->query)){ |
| 744 | 744 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
| 745 | 745 | if (! empty($this->join)){ |
| 746 | - $query .= $this->join; |
|
| 746 | + $query .= $this->join; |
|
| 747 | 747 | } |
| 748 | 748 | |
| 749 | 749 | if (! empty($this->where)){ |
| 750 | - $query .= ' WHERE ' . $this->where; |
|
| 750 | + $query .= ' WHERE ' . $this->where; |
|
| 751 | 751 | } |
| 752 | 752 | |
| 753 | 753 | if (! empty($this->groupBy)){ |
| 754 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 754 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 755 | 755 | } |
| 756 | 756 | |
| 757 | 757 | if (! empty($this->having)){ |
| 758 | - $query .= ' HAVING ' . $this->having; |
|
| 758 | + $query .= ' HAVING ' . $this->having; |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | if (! empty($this->orderBy)){ |
@@ -763,198 +763,198 @@ discard block |
||
| 763 | 763 | } |
| 764 | 764 | |
| 765 | 765 | if (! empty($this->limit)){ |
| 766 | - $query .= ' LIMIT ' . $this->limit; |
|
| 766 | + $query .= ' LIMIT ' . $this->limit; |
|
| 767 | 767 | } |
| 768 | 768 | $this->query = $query; |
| 769 | 769 | } |
| 770 | - return $this->query; |
|
| 771 | - } |
|
| 770 | + return $this->query; |
|
| 771 | + } |
|
| 772 | 772 | |
| 773 | 773 | |
| 774 | 774 | /** |
| 775 | - * Return the PDO instance |
|
| 776 | - * @return PDO |
|
| 777 | - */ |
|
| 778 | - public function getPdo(){ |
|
| 779 | - return $this->pdo; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Set the PDO instance |
|
| 784 | - * @param PDO $pdo the pdo object |
|
| 775 | + * Return the PDO instance |
|
| 776 | + * @return PDO |
|
| 777 | + */ |
|
| 778 | + public function getPdo(){ |
|
| 779 | + return $this->pdo; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Set the PDO instance |
|
| 784 | + * @param PDO $pdo the pdo object |
|
| 785 | 785 | * @return object DatabaseQueryBuilder |
| 786 | - */ |
|
| 787 | - public function setPdo(PDO $pdo = null){ |
|
| 788 | - $this->pdo = $pdo; |
|
| 789 | - return $this; |
|
| 790 | - } |
|
| 786 | + */ |
|
| 787 | + public function setPdo(PDO $pdo = null){ |
|
| 788 | + $this->pdo = $pdo; |
|
| 789 | + return $this; |
|
| 790 | + } |
|
| 791 | 791 | |
| 792 | 792 | /** |
| 793 | - * Return the database table prefix |
|
| 794 | - * @return string |
|
| 795 | - */ |
|
| 796 | - public function getPrefix(){ |
|
| 797 | - return $this->prefix; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * Set the database table prefix |
|
| 802 | - * @param string $prefix the new prefix |
|
| 803 | - * @return object DatabaseQueryBuilder |
|
| 804 | - */ |
|
| 805 | - public function setPrefix($prefix){ |
|
| 806 | - $this->prefix = $prefix; |
|
| 807 | - return $this; |
|
| 808 | - } |
|
| 793 | + * Return the database table prefix |
|
| 794 | + * @return string |
|
| 795 | + */ |
|
| 796 | + public function getPrefix(){ |
|
| 797 | + return $this->prefix; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * Set the database table prefix |
|
| 802 | + * @param string $prefix the new prefix |
|
| 803 | + * @return object DatabaseQueryBuilder |
|
| 804 | + */ |
|
| 805 | + public function setPrefix($prefix){ |
|
| 806 | + $this->prefix = $prefix; |
|
| 807 | + return $this; |
|
| 808 | + } |
|
| 809 | 809 | |
| 810 | 810 | /** |
| 811 | - * Return the database driver |
|
| 812 | - * @return string |
|
| 813 | - */ |
|
| 814 | - public function getDriver(){ |
|
| 815 | - return $this->driver; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * Set the database driver |
|
| 820 | - * @param string $driver the new driver |
|
| 821 | - * @return object DatabaseQueryBuilder |
|
| 822 | - */ |
|
| 823 | - public function setDriver($driver){ |
|
| 824 | - $this->driver = $driver; |
|
| 825 | - return $this; |
|
| 826 | - } |
|
| 811 | + * Return the database driver |
|
| 812 | + * @return string |
|
| 813 | + */ |
|
| 814 | + public function getDriver(){ |
|
| 815 | + return $this->driver; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * Set the database driver |
|
| 820 | + * @param string $driver the new driver |
|
| 821 | + * @return object DatabaseQueryBuilder |
|
| 822 | + */ |
|
| 823 | + public function setDriver($driver){ |
|
| 824 | + $this->driver = $driver; |
|
| 825 | + return $this; |
|
| 826 | + } |
|
| 827 | 827 | |
| 828 | 828 | /** |
| 829 | - * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | - * @return object the current DatabaseQueryBuilder instance |
|
| 831 | - */ |
|
| 832 | - public function reset(){ |
|
| 833 | - $this->select = '*'; |
|
| 834 | - $this->from = null; |
|
| 835 | - $this->where = null; |
|
| 836 | - $this->limit = null; |
|
| 837 | - $this->orderBy = null; |
|
| 838 | - $this->groupBy = null; |
|
| 839 | - $this->having = null; |
|
| 840 | - $this->join = null; |
|
| 841 | - $this->query = null; |
|
| 842 | - return $this; |
|
| 843 | - } |
|
| 829 | + * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | + * @return object the current DatabaseQueryBuilder instance |
|
| 831 | + */ |
|
| 832 | + public function reset(){ |
|
| 833 | + $this->select = '*'; |
|
| 834 | + $this->from = null; |
|
| 835 | + $this->where = null; |
|
| 836 | + $this->limit = null; |
|
| 837 | + $this->orderBy = null; |
|
| 838 | + $this->groupBy = null; |
|
| 839 | + $this->having = null; |
|
| 840 | + $this->join = null; |
|
| 841 | + $this->query = null; |
|
| 842 | + return $this; |
|
| 843 | + } |
|
| 844 | 844 | |
| 845 | 845 | /** |
| 846 | - * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | - * @see DatabaseQueryBuilder::having |
|
| 848 | - * |
|
| 849 | - * @return string |
|
| 850 | - */ |
|
| 851 | - protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
|
| 852 | - $x = explode('?', $field); |
|
| 853 | - $w = ''; |
|
| 854 | - foreach($x as $k => $v){ |
|
| 855 | - if (!empty($v)){ |
|
| 856 | - if (! isset($op[$k])){ |
|
| 857 | - $op[$k] = ''; |
|
| 858 | - } |
|
| 859 | - $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | - } |
|
| 861 | - } |
|
| 862 | - return $w; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * Get the SQL WHERE clause using array column => value |
|
| 868 | - * @see DatabaseQueryBuilder::where |
|
| 869 | - * |
|
| 870 | - * @return string |
|
| 871 | - */ |
|
| 872 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 873 | - $_where = array(); |
|
| 874 | - foreach ($where as $column => $data){ |
|
| 875 | - if (is_null($data)){ |
|
| 876 | - $data = ''; |
|
| 877 | - } |
|
| 878 | - $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
|
| 879 | - } |
|
| 880 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | - return $where; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - /** |
|
| 885 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | - * @see DatabaseQueryBuilder::where |
|
| 887 | - * |
|
| 888 | - * @return string |
|
| 889 | - */ |
|
| 890 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 891 | - $x = explode('?', $where); |
|
| 892 | - $w = ''; |
|
| 893 | - foreach($x as $k => $v){ |
|
| 894 | - if (! empty($v)){ |
|
| 895 | - if (isset($op[$k]) && is_null($op[$k])){ |
|
| 896 | - $op[$k] = ''; |
|
| 897 | - } |
|
| 898 | - $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
|
| 899 | - } |
|
| 900 | - } |
|
| 901 | - return $w; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - /** |
|
| 905 | - * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 906 | - * @see DatabaseQueryBuilder::where |
|
| 907 | - * |
|
| 908 | - * @return string |
|
| 909 | - */ |
|
| 910 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 911 | - $w = ''; |
|
| 912 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | - if (is_null($op)){ |
|
| 914 | - $op = ''; |
|
| 915 | - } |
|
| 916 | - $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 917 | - } else { |
|
| 918 | - if (is_null($val)){ |
|
| 919 | - $val = ''; |
|
| 920 | - } |
|
| 921 | - $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 922 | - } |
|
| 923 | - return $w; |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Set the $this->where property |
|
| 928 | - * @param string $whereStr the WHERE clause string |
|
| 929 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | - */ |
|
| 931 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 932 | - if (empty($this->where)){ |
|
| 933 | - $this->where = $whereStr; |
|
| 934 | - } else { |
|
| 935 | - if (substr(trim($this->where), -1) == '('){ |
|
| 936 | - $this->where = $this->where . ' ' . $whereStr; |
|
| 937 | - } else { |
|
| 938 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 939 | - } |
|
| 940 | - } |
|
| 941 | - } |
|
| 846 | + * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | + * @see DatabaseQueryBuilder::having |
|
| 848 | + * |
|
| 849 | + * @return string |
|
| 850 | + */ |
|
| 851 | + protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
|
| 852 | + $x = explode('?', $field); |
|
| 853 | + $w = ''; |
|
| 854 | + foreach($x as $k => $v){ |
|
| 855 | + if (!empty($v)){ |
|
| 856 | + if (! isset($op[$k])){ |
|
| 857 | + $op[$k] = ''; |
|
| 858 | + } |
|
| 859 | + $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | + } |
|
| 861 | + } |
|
| 862 | + return $w; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * Get the SQL WHERE clause using array column => value |
|
| 868 | + * @see DatabaseQueryBuilder::where |
|
| 869 | + * |
|
| 870 | + * @return string |
|
| 871 | + */ |
|
| 872 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 873 | + $_where = array(); |
|
| 874 | + foreach ($where as $column => $data){ |
|
| 875 | + if (is_null($data)){ |
|
| 876 | + $data = ''; |
|
| 877 | + } |
|
| 878 | + $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
|
| 879 | + } |
|
| 880 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | + return $where; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + /** |
|
| 885 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | + * @see DatabaseQueryBuilder::where |
|
| 887 | + * |
|
| 888 | + * @return string |
|
| 889 | + */ |
|
| 890 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 891 | + $x = explode('?', $where); |
|
| 892 | + $w = ''; |
|
| 893 | + foreach($x as $k => $v){ |
|
| 894 | + if (! empty($v)){ |
|
| 895 | + if (isset($op[$k]) && is_null($op[$k])){ |
|
| 896 | + $op[$k] = ''; |
|
| 897 | + } |
|
| 898 | + $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
|
| 899 | + } |
|
| 900 | + } |
|
| 901 | + return $w; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + /** |
|
| 905 | + * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 906 | + * @see DatabaseQueryBuilder::where |
|
| 907 | + * |
|
| 908 | + * @return string |
|
| 909 | + */ |
|
| 910 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 911 | + $w = ''; |
|
| 912 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | + if (is_null($op)){ |
|
| 914 | + $op = ''; |
|
| 915 | + } |
|
| 916 | + $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 917 | + } else { |
|
| 918 | + if (is_null($val)){ |
|
| 919 | + $val = ''; |
|
| 920 | + } |
|
| 921 | + $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 922 | + } |
|
| 923 | + return $w; |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Set the $this->where property |
|
| 928 | + * @param string $whereStr the WHERE clause string |
|
| 929 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | + */ |
|
| 931 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 932 | + if (empty($this->where)){ |
|
| 933 | + $this->where = $whereStr; |
|
| 934 | + } else { |
|
| 935 | + if (substr(trim($this->where), -1) == '('){ |
|
| 936 | + $this->where = $this->where . ' ' . $whereStr; |
|
| 937 | + } else { |
|
| 938 | + $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 939 | + } |
|
| 940 | + } |
|
| 941 | + } |
|
| 942 | 942 | |
| 943 | 943 | |
| 944 | 944 | /** |
| 945 | - * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | - * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | - * @see DatabaseQueryBuilder::min |
|
| 948 | - * @see DatabaseQueryBuilder::max |
|
| 949 | - * @see DatabaseQueryBuilder::sum |
|
| 950 | - * @see DatabaseQueryBuilder::count |
|
| 951 | - * @see DatabaseQueryBuilder::avg |
|
| 952 | - * @return object |
|
| 953 | - */ |
|
| 954 | - protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
|
| 955 | - $clause = strtoupper($clause); |
|
| 956 | - $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | - return $this; |
|
| 959 | - } |
|
| 945 | + * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | + * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | + * @see DatabaseQueryBuilder::min |
|
| 948 | + * @see DatabaseQueryBuilder::max |
|
| 949 | + * @see DatabaseQueryBuilder::sum |
|
| 950 | + * @see DatabaseQueryBuilder::count |
|
| 951 | + * @see DatabaseQueryBuilder::avg |
|
| 952 | + * @return object |
|
| 953 | + */ |
|
| 954 | + protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
|
| 955 | + $clause = strtoupper($clause); |
|
| 956 | + $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | + return $this; |
|
| 959 | + } |
|
| 960 | 960 | } |