PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/ebpls-php-lib/ebpls.database.funcs.php

http://ebpls.googlecode.com/
PHP | 565 lines | 274 code | 231 blank | 60 comment | 91 complexity | 12cc2c3467094b8b19086bbc5d600f83 MD5 | raw file
  1. <?
  2. /************************************************************************************
  3. Module : ebpls.database.funcs.php
  4. Dependencies : None
  5. Description :
  6. - generic MySQL builder functions
  7. - provides central functions for building sql statements
  8. - all necessary vlaue cleaning is done inside the sql builder functions
  9. Created By : Stephen Lou B. Banal
  10. Date Created : 3/1/2004 12:07AM
  11. Last Updates :
  12. * 3/3/2004 6:42AM - stephen
  13. - added error constants
  14. -
  15. * 8/29/2004 4:11PM - stephen
  16. - added InnoDB transaction functions support
  17. - added USE INNODB command for InnoDB type of Mysql tables
  18. Notes :
  19. - DO NOT USE THESE METHODS DIRECTLY, USE THE CLASS METHODS PROVIDED TO RETRIEVE DATA FROM DATABASE. - stephen
  20. - to disable debugging set constant DEBUG_DB_FUNCS to FALSE
  21. - need to add ebpls_bypage_select_data function (3/1/2004 12:09AM) - stephen
  22. ************************************************************************************/
  23. $EBPLS_DB_FUNCS_ERROR = NULL;
  24. define(DEBUG_DB_FUNCS, false );
  25. define(ERROR_DB_FUNCS_INSERTFAILED,-100001);
  26. define(ERROR_DB_FUNCS_UPDATEFAILED,-100002);
  27. define(ERROR_DB_FUNCS_DELETEFAILED,-100003);
  28. define(ERROR_DB_FUNCS_SELECTFAILED,-100004);
  29. define(ERROR_DB_FUNCS_BEGINTRANSFAILED,-100005);
  30. define(ERROR_DB_FUNCS_COMMITTRANSFAILED,-100006);
  31. define(USE_INNODB, true );
  32. $gEBPLSGlobalTransValue = false;
  33. function ebpls_db_funcs_debug ( $str ) {
  34. if ( DEBUG_DB_FUNCS ) {
  35. echo "[" . date("Y-m-d H:i:s") . "] : " . htmlentities($str) . "<BR>";
  36. }
  37. }
  38. /**
  39. * Starts transaction.
  40. *
  41. **/
  42. function ebpls_start_transaction( $dbLink ){
  43. global $gEBPLSGlobalTransValue;
  44. if ( $gEBPLSGlobalTransValue ) {
  45. ebpls_db_funcs_debug ( "ebpls_start_transaction : gEBPLSGlobalTransValue still set." );
  46. return;
  47. }
  48. $gEBPLSGlobalTransValue = true;
  49. $res = @mysql_query( "START TRANSACTION", $dbLink );
  50. if ( set_db_error ( $dbLink ) ) {
  51. return ERROR_DB_FUNCS_BEGINTRANSFAILED;
  52. }
  53. return 1;
  54. }
  55. /**
  56. *
  57. * Commits Current transaction
  58. *
  59. **/
  60. function ebpls_commit_transaction( $dbLink ){
  61. global $gEBPLSGlobalTransValue;
  62. if ( !$gEBPLSGlobalTransValue ) {
  63. ebpls_db_funcs_debug ( "ebpls_end_transaction : gEBPLSGlobalTransValue still unset, call ebpls_start_transaction first." );
  64. return;
  65. }
  66. $res = @mysql_query( "COMMIT", $dbLink );
  67. if ( set_db_error ( $dbLink ) ) {
  68. return ERROR_DB_FUNCS_COMMITTRANSFAILED;
  69. }
  70. return 1;
  71. }
  72. /**
  73. * Rollback transaction
  74. *
  75. **/
  76. function ebpls_rollback_transaction( $dbLink ){
  77. global $gEBPLSGlobalTransValue;
  78. if ( !$gEBPLSGlobalTransValue ) {
  79. ebpls_db_funcs_debug ( "ebpls_rollback_transaction : gEBPLSGlobalTransValue still unset, call ebpls_start_transaction first." );
  80. return;
  81. }
  82. $res = @mysql_query( "ROLLBACK", $dbLink );
  83. if ( set_db_error ( $dbLink ) ) {
  84. return ERROR_DB_FUNCS_COMMITTRANSFAILED;
  85. }
  86. return 1;
  87. }
  88. /**
  89. * Formats a string to be sql safe
  90. *
  91. **/
  92. function ebpls_value_sql_clean ( &$value ) {
  93. // always check if automatic quotes setter is on
  94. if ( get_magic_quotes_gpc() == 1 ) return;
  95. if ( is_array($value) ) {
  96. foreach ( $value as $key =>$content ) {
  97. if ( is_array($content) ) {
  98. $value[$key][1] = addSlashes($content[1]);
  99. } else {
  100. $value[$key] = addSlashes($content);
  101. }
  102. }
  103. } else {
  104. if ( is_array($value) ) {
  105. $strTmp = $value[1];
  106. $value[1] = addslashes($strTmp);
  107. } else {
  108. $strTmp = $value;
  109. $value = addslashes($strTmp);
  110. }
  111. }
  112. }
  113. function set_db_error ( $dbLink, $str = NULL ) {
  114. global $EBPLS_DB_FUNCS_ERROR;
  115. if ( null != $dbLink ) {
  116. $error = @mysql_error ( $dbLink );
  117. if ( !($EBPLS_DB_FUNCS_ERROR = $error) ) {
  118. $EBPLS_DB_FUNCS_ERROR = NULL;
  119. }
  120. } else if ( null != $str ) {
  121. $EBPLS_DB_FUNCS_ERROR = $str;
  122. } else {
  123. $EBPLS_DB_FUNCS_ERROR = NULL;
  124. }
  125. return $EBPLS_DB_FUNCS_ERROR;
  126. }
  127. function get_db_error ( ) {
  128. global $EBPLS_DB_FUNCS_ERROR;
  129. return $EBPLS_DB_FUNCS_ERROR;
  130. }
  131. function ebpls_insert_data( $dbLink, $strTable, $strValues ) {
  132. global $gEBPLSGlobalTransValue;
  133. ebpls_value_sql_clean( $strValues );
  134. if ( $strValues!=NULL && count($strValues) > 0 ) {
  135. foreach ( $strValues as $key => $value ) {
  136. $columns[] = $key;
  137. $values[] = "'$value'";
  138. }
  139. $strColumns = implode(",", $columns );
  140. $strValues = join( ",", $values );
  141. } else {
  142. set_db_error( NULL, "No Column values to insert provided");
  143. return ERROR_DB_FUNCS_INSERTFAILED;
  144. }
  145. set_db_error( NULL, "No Column values to insert provided ($strValues)");
  146. $sqlInsert = "INSERT INTO $strTable ($strColumns) values($strValues)";
  147. ebpls_db_funcs_debug ( "ebpls_insert_data INSERT : $sqlInsert" );
  148. $res = @mysql_query($sqlInsert, $dbLink );
  149. if ( set_db_error ( $dbLink ) ) {
  150. return ERROR_DB_FUNCS_INSERTFAILED;
  151. } else {
  152. $id = @mysql_insert_id ( $dbLink );
  153. return $id;
  154. }
  155. }
  156. function ebpls_update_data ( $dbLink, $strTable, $strValues, $strWhere ) {
  157. if ( is_array($strValues) ) {
  158. ebpls_value_sql_clean( $strValues );
  159. foreach ( $strValues as $key => $value ) {
  160. if ( !is_null($value) ) {
  161. $set_cols[] = "$key = '$value'";
  162. }
  163. }
  164. $strColumns = implode(",", $set_cols );
  165. } else {
  166. set_db_error( NULL, "ebpls_update_data invalid strValues value = $strValues");
  167. return -1;
  168. }
  169. if ( is_array($strWhere) ) {
  170. ebpls_value_sql_clean( $strWhere );
  171. foreach ( $strWhere as $key => $value ) {
  172. if ( is_array($value) ) {
  173. if ( strtoupper(trim($value[0])) == "IN" || strtoupper(trim($value[0])) == "NOT IN" ) {
  174. $set_where[] = " $key " . $value[0] . " " . $value[1] . " ";
  175. } else {
  176. $set_where[] = " $key " . $value[0] . " '" . $value[1] . "' ";
  177. }
  178. } else {
  179. $set_where[] = " $key = '$value' ";
  180. }
  181. }
  182. $strWhereClause = implode(" AND ", $set_where );
  183. } else {
  184. set_db_error( NULL, "ebpls_update_data invalid strWhere value = $strWhere");
  185. return -1;
  186. }
  187. $sqlUpdate = "UPDATE $strTable SET $strColumns WHERE $strWhereClause";
  188. ebpls_db_funcs_debug ( "ebpls_update_data INSERT : $sqlUpdate" );
  189. $res = @mysql_query($sqlUpdate, $dbLink );
  190. if ( set_db_error ( $dbLink ) ) {
  191. return ERROR_DB_FUNCS_UPDATEFAILED;
  192. } else {
  193. return @mysql_affected_rows($dbLink);
  194. //return 1;
  195. }
  196. }
  197. function ebpls_delete_data ( $dbLink, $strTable, $strWhere ) {
  198. ebpls_value_sql_clean( $strWhere );
  199. foreach ( $strWhere as $key => $value ) {
  200. if ( is_array($value) ) {
  201. $set_where[] = " $key " . $value[0] . " '" . $value[1] . "'";
  202. } else {
  203. $set_where[] = " $key = '$value' ";
  204. }
  205. }
  206. $strWhereClause = implode(" AND ", $set_where );
  207. $sqlDelete = "DELETE FROM $strTable WHERE $strWhereClause";
  208. ebpls_db_funcs_debug ( "ebpls_delete_data DELETE : $sqlDelete" );
  209. $res = @mysql_query( $sqlDelete, $dbLink );
  210. if ( set_db_error ( $dbLink ) ) {
  211. return ERROR_DB_FUNCS_DELETEFAILED;
  212. } else {
  213. return @mysql_affected_rows($dbLink);
  214. }
  215. }
  216. function ebpls_select_data ( $dbLink, $strTable, $strColumns, $strWhere = NULL, $strGroupBy = NULL, $strOrder = NULL, $strOrderKey = NULL, $strLimitMax = NULL, $strLimitOffSet = NULL, $bGetPagingSql = false ) {
  217. ebpls_value_sql_clean( $strWhere );
  218. if ( $strColumns != NULL && is_array($strColumns) && count($strColumns) > 0 ) {
  219. foreach ( $strColumns as $key => $value ) {
  220. $set_cols[] = "$value";
  221. }
  222. $strSqlColumns = implode(",", $set_cols );
  223. } else {
  224. set_db_error( NULL, "No columns selected");
  225. return ERROR_DB_FUNCS_SELECTFAILED;
  226. }
  227. if ( $strWhere != NULL && count($strWhere) > 0 ) {
  228. foreach ( $strWhere as $key => $value ) {
  229. if ( is_array($value) ) {
  230. if ( trim($value[0]) == "IN" || trim($value[0]) == "NOT IN" ) {
  231. $set_where[] = " $key " . $value[0] . " " . stripslashes($value[1]) . "";
  232. }else{
  233. $set_where[] = " $key " . $value[0] . " '" . $value[1] . "'";
  234. }
  235. } else {
  236. $set_where[] = " $key = '$value' ";
  237. }
  238. }
  239. $strSqlWhereClause = " WHERE " . implode(" AND ", $set_where );
  240. } else {
  241. }
  242. if ( !(null == $strGroupBy) && count($strGroupBy) > 0 ) {
  243. $set_group = implode(",", $strGroupBy );
  244. $strSqlGroup = " GROUP BY $set_group";
  245. }
  246. if ( !(null == $strOrder) && count($strOrder) > 0 ) {
  247. $set_order = implode(",", $strOrder );
  248. $strSqlOrder = " ORDER BY $set_order";
  249. if ( !(null == $strOrderKey) ) {
  250. if ( $strOrderKey != "ASC" && $strOrderKey != "DESC" ) {
  251. set_db_error ( NULL, "Invalid order key value $strOrderKey" );
  252. return -2;
  253. }
  254. $strSqlOrder .= " $strOrderKey ";
  255. }
  256. }
  257. if ( is_numeric($strLimitMax) && is_numeric($strLimitOffSet) ) {
  258. $strSqlLimit = " LIMIT $strLimitOffSet, $strLimitMax ";
  259. } else if ( is_numeric($strLimitMax) && !is_numeric($strLimitOffSet) ) {
  260. $strSqlLimit = " LIMIT $strLimitMax ";
  261. }
  262. if ( $bGetPagingSql ) {
  263. $sqlCount = "SELECT count(*) FROM $strTable $strSqlWhereClause $strSqlGroup $strSqlOrder";
  264. $sqlSelect = "SELECT $strSqlColumns FROM $strTable $strSqlWhereClause $strSqlGroup $strSqlOrder";
  265. return array("count_sql" => $sqlCount, "select_sql"=>$sqlCount);
  266. }
  267. $sqlSelect = "SELECT $strSqlColumns FROM $strTable $strSqlWhereClause $strSqlGroup $strSqlOrder $strSqlLimit";
  268. ebpls_db_funcs_debug ( "ebpls_select_data SELECT : $sqlSelect" );
  269. $res = @mysql_query($sqlSelect, $dbLink );
  270. if ( set_db_error ( $dbLink ) ) {
  271. return ERROR_DB_FUNCS_SELECTFAILED;
  272. } else {
  273. while ( $row = mysql_fetch_array($res) ) {
  274. $select_records[] = $row;
  275. }
  276. return $select_records;
  277. }
  278. }
  279. /*
  280. Note :
  281. - find function will return a two dimensional array
  282. - first element of array having key "page_info" contains all the information regarding the query
  283. - page_info elements
  284. total = number of total records of search
  285. max_pages = number of pages in search
  286. count = number of records on current page
  287. page = current page selected
  288. - second element of array having key "result" contains result of the search
  289. */
  290. function ebpls_select_data_bypage ( $dbLink, $strTable, $strColumns, $strWhere = NULL, $strGroupBy = NULL, $strOrder = NULL, $strOrderKey = NULL, $nPage = 1, $nMaxRecordPerPage = 20 ) {
  291. if ( $nPage <= 0 || $nMaxRecordPerPage <= 0 ) {
  292. set_db_error( NULL, "Invalid page values [pg=$nPage,maxrec=$nMaxRecordPerPage]" );
  293. return ERROR_DB_FUNCS_SELECTFAILED;
  294. }
  295. // build select sql
  296. $sqlArray = ebpls_select_data ( $dbLink, $strTable, $strColumns, $strWhere, $strGroupBy, $strOrder, $strOrderKey, 1, 1, true);
  297. $sqlCount = $sqlArray["count_sql"];
  298. $sqlSelect = $sqlArray["select_sql"];
  299. $res = @mysql_query($sqlCount, $dbLink );
  300. // count number of pages
  301. if ( $res && ( $row = mysql_fetch_array($res) ) ) {
  302. $nTotalRecords = $row[0];
  303. $nPageCount = floor($row[0]/$nMaxRecordPerPage);
  304. if ( ( $row[0]%$nMaxRecordPerPage ) > 0 )
  305. {
  306. $nPageCount++;
  307. }
  308. }
  309. // get offset using page number
  310. if ( $nPage == 1 ) {
  311. $pgOffset = 0;
  312. } else {
  313. $pgOffset = ($nPage-1)*$nMaxRecordPerPage;
  314. }
  315. $res = ebpls_select_data ( $dbLink, $strTable, $strColumns, $strWhere, $strGroupBy, $strOrder, $strOrderKey, $nMaxRecordPerPage, $pgOffset );
  316. if ( $res < 0 ) {
  317. set_db_error( $dbLink );
  318. return $res;
  319. } else {
  320. $page_record["total"] = $nTotalRecords;
  321. $page_record["count"] = count($res);
  322. $page_record["page"] = $nPage;
  323. $page_record["max_pages"] = $nPageCount;
  324. $page_record["page_count"] = $nPageCount;
  325. return array("result"=>$res, "page_info"=>$page_record);
  326. }
  327. }
  328. ?>