PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/output/connections/Connection.php

https://gitlab.com/Lidbary/PHPRunner
PHP | 605 lines | 318 code | 73 blank | 214 comment | 29 complexity | 8e0dd61ccb45e2c0e269798d621f5098 MD5 | raw file
  1. <?php
  2. class Connection
  3. {
  4. /**
  5. * The db connection link identifier
  6. * @type Mixed
  7. */
  8. public $conn = null; // TODO: make it protected
  9. /**
  10. * The database type identifier
  11. * @type Number
  12. */
  13. public $dbType;
  14. /**
  15. * The db connection id
  16. * @type Number
  17. */
  18. public $connId;
  19. /**
  20. * @type DBFunctions
  21. */
  22. protected $_functions;
  23. /**
  24. * @type DBInfo
  25. */
  26. protected $_info;
  27. /**
  28. * asp compatibility
  29. */
  30. public $SQLUpdateMode;
  31. function Connection( $params )
  32. {
  33. include_once getabspath("connections/QueryResult.php");
  34. $this->assignConnectionParams( $params );
  35. // set the db connection
  36. $this->connect();
  37. $this->setDbFunctions( $params["leftWrap"], $params["rightWrap"] );
  38. $this->setDbInfo( $params["ODBCString"] );
  39. }
  40. /**
  41. * Set db connection's properties
  42. * @param Array params
  43. */
  44. protected function assignConnectionParams( $params )
  45. {
  46. $this->dbType = $params["dbType"];
  47. $this->connId = $params["connId"];
  48. }
  49. /**
  50. * Set the DBFunction object
  51. * @param String leftWrapper
  52. * @param String rightWrapper
  53. */
  54. protected function setDbFunctions( $leftWrapper, $rightWrapper )
  55. {
  56. include_once getabspath("connections/dbfunctions/DBFunctions.php");
  57. $extraParams = $this->getDbFunctionsExtraParams();
  58. switch( $this->dbType )
  59. {
  60. case nDATABASE_MySQL:
  61. include_once getabspath("connections/dbfunctions/MySQLFunctions.php");
  62. $this->_functions = new MySQLFunctions( $leftWrapper, $rightWrapper, $extraParams );
  63. break;
  64. case nDATABASE_Oracle:
  65. include_once getabspath("connections/dbfunctions/OracleFunctions.php");
  66. $this->_functions = new OracleFunctions( $leftWrapper, $rightWrapper, $extraParams );
  67. break;
  68. case nDATABASE_MSSQLServer:
  69. include_once getabspath("connections/dbfunctions/MSSQLFunctions.php");
  70. $this->_functions = new MSSQLFunctions( $leftWrapper, $rightWrapper, $extraParams );
  71. break;
  72. case nDATABASE_Access:
  73. include_once getabspath("connections/dbfunctions/ODBCFunctions.php");
  74. $this->_functions = new ODBCFunctions( $leftWrapper, $rightWrapper, $extraParams );
  75. break;
  76. case nDATABASE_PostgreSQL:
  77. include_once getabspath("connections/dbfunctions/PostgreFunctions.php");
  78. $this->_functions = new PostgreFunctions( $leftWrapper, $rightWrapper, $extraParams );
  79. break;
  80. case nDATABASE_Informix:
  81. include_once getabspath("connections/dbfunctions/InformixFunctions.php");
  82. $this->_functions = new InformixFunctions( $leftWrapper, $rightWrapper, $extraParams );
  83. break;
  84. case nDATABASE_SQLite3:
  85. include_once getabspath("connections/dbfunctions/SQLite3Functions.php");
  86. $this->_functions = new SQLite3Functions( $leftWrapper, $rightWrapper, $extraParams );
  87. break;
  88. case nDATABASE_DB2:
  89. case 18: // iSeries
  90. include_once getabspath("connections/dbfunctions/DB2Functions.php");
  91. $this->_functions = new DB2Functions( $leftWrapper, $rightWrapper, $extraParams );
  92. break;
  93. default:
  94. include_once getabspath("connections/dbfunctions/GenericFunctions.php");
  95. $this->_functions = new GenericFunctions( $leftWrapper, $rightWrapper, $extraParams );
  96. }
  97. }
  98. /**
  99. * Get extra connection params that may be connected
  100. * with the db connection link directly
  101. * @return Array
  102. */
  103. protected function getDbFunctionsExtraParams()
  104. {
  105. return array();
  106. }
  107. /**
  108. * Set the DbInfo object
  109. * @param String ODBCString
  110. */
  111. protected function setDbInfo( $ODBCString )
  112. {
  113. include_once getabspath("connections/dbinfo/DBInfo.php");
  114. switch( $this->dbType )
  115. {
  116. case nDATABASE_MySQL:
  117. if( useMySQLiLib() )
  118. {
  119. include_once getabspath("connections/dbinfo/MySQLiInfo.php");
  120. $this->_info = new MySQLiInfo( $this );
  121. }
  122. else
  123. {
  124. include_once getabspath("connections/dbinfo/MySQLInfo.php");
  125. $this->_info = new MySQLInfo( $this );
  126. }
  127. break;
  128. case nDATABASE_Oracle:
  129. include_once getabspath("connections/dbinfo/OracleInfo.php");
  130. $this->_info = new OracleInfo( $this );
  131. break;
  132. case nDATABASE_MSSQLServer:
  133. include_once getabspath("connections/dbinfo/MSSQLInfo.php");
  134. $this->_info = new MSSQLInfo( $this );
  135. break;
  136. case nDATABASE_Access:
  137. if( stripos($ODBCString, 'Provider=') !== false )
  138. {
  139. include_once getabspath("connections/dbinfo/ADOInfo.php");
  140. $this->_info = new ADOInfo( $this );
  141. }
  142. else
  143. {
  144. include_once getabspath("connections/dbinfo/ODBCInfo.php");
  145. $this->_info = new ODBCInfo( $this );
  146. }
  147. break;
  148. case nDATABASE_PostgreSQL:
  149. include_once getabspath("connections/dbinfo/PostgreInfo.php");
  150. $this->_info = new PostgreInfo( $this );
  151. break;
  152. case nDATABASE_Informix:
  153. include_once getabspath("connections/dbinfo/InformixInfo.php");
  154. $this->_info = new InformixInfo( $this );
  155. break;
  156. case nDATABASE_SQLite3:
  157. include_once getabspath("connections/dbinfo/SQLLite3Info.php");
  158. $this->_info = new SQLLite3Info( $this );
  159. break;
  160. case nDATABASE_DB2:
  161. include_once getabspath("connections/dbinfo/DB2Info.php");
  162. $this->_info = new DB2Info( $this );
  163. }
  164. }
  165. /**
  166. * An interface stub.
  167. * Open a connection to db
  168. */
  169. public function connect()
  170. {
  171. //db_connect
  172. }
  173. /**
  174. * An interface stub
  175. * Close the db connection
  176. */
  177. public function close()
  178. {
  179. //db_close
  180. }
  181. /**
  182. * An interface stub
  183. * Send an SQL query
  184. * @param String sql
  185. * @return QueryResult
  186. */
  187. public function query( $sql )
  188. {
  189. //db_query
  190. //return new QueryResult( $this, $qHandle );
  191. }
  192. /**
  193. * An interface stub
  194. * Execute an SQL query
  195. * @param String sql
  196. */
  197. public function exec( $sql )
  198. {
  199. //db_exec
  200. }
  201. /**
  202. * An interface stub
  203. * Get a description of the last error
  204. * @return String
  205. */
  206. public function lastError()
  207. {
  208. //db_error
  209. }
  210. /**
  211. * Get the auto generated id used in the last query
  212. * @param String key (optional)
  213. * @param String table (optional)
  214. * @param String oraSequenceName (optional)
  215. * @return Number
  216. */
  217. public function getInsertedId( $key = null, $table = null , $oraSequenceName = false )
  218. {
  219. $insertedIdSQL = $this->_functions->getInsertedIdSQL( $key, $table, $oraSequenceName );
  220. if ( $insertedIdSQL )
  221. {
  222. $qResult = $this->query( $insertedIdSQL );
  223. if( $qResult )
  224. {
  225. $lastId = $qResult->fetchNumeric();
  226. return $lastId[0];
  227. }
  228. }
  229. return 0;
  230. }
  231. /**
  232. * The stub openSchema method overrided in the ADO connection class
  233. * @param Number querytype
  234. * @return Mixed
  235. */
  236. public function openSchema( $querytype )
  237. {
  238. return null;
  239. }
  240. /**
  241. * An interface stub
  242. * Fetch a result row as an associative array
  243. * @param Mixed qHanle The query handle
  244. * @return Array | Boolean
  245. */
  246. public function fetch_array( $qHanle )
  247. {
  248. //db_fetch_array
  249. }
  250. /**
  251. * An interface stub
  252. * Fetch a result row as a numeric array
  253. * @param Mixed qHanle The query handle
  254. * @return Array | Boolean
  255. */
  256. public function fetch_numarray( $qHanle )
  257. {
  258. //db_fetch_numarray
  259. }
  260. /**
  261. * An interface stub
  262. * Free resources associated with a query result set
  263. * @param Mixed qHanle The query handle
  264. */
  265. public function closeQuery( $qHanle )
  266. {
  267. //db_closequery
  268. }
  269. /**
  270. * An interface stub.
  271. * Get number of fields in a result
  272. * @param Mixed qHanle The query handle
  273. * @return Number
  274. */
  275. public function num_fields( $qHandle )
  276. {
  277. //db_numfields
  278. }
  279. /**
  280. * An interface stub.
  281. * Get the name of the specified field in a result
  282. * @param Mixed qHanle The query handle
  283. * @param Number offset
  284. * @return String
  285. */
  286. public function field_name( $qHanle, $offset )
  287. {
  288. //db_fieldname
  289. }
  290. /**
  291. * An interface stub
  292. * @param Mixed qHandle
  293. * @param Number pageSize
  294. * @param Number page
  295. */
  296. public function seekPage($qHandle, $pageSize, $page)
  297. {
  298. //db_pageseek
  299. }
  300. /**
  301. * @param String str
  302. * @return String
  303. */
  304. public function escapeLIKEpattern( $str )
  305. {
  306. return $this->_functions->escapeLIKEpattern( $str );
  307. }
  308. /**
  309. * @param String str
  310. * @return String
  311. */
  312. public function prepareString( $str )
  313. {
  314. return $this->_functions->prepareString( $str );
  315. }
  316. /**
  317. * @param String str
  318. * @return String
  319. */
  320. public function addSlashes( $str )
  321. {
  322. return $this->_functions->addslashes( $str );
  323. }
  324. /**
  325. * @param String str
  326. * @return String
  327. */
  328. public function addSlashesBinary( $str )
  329. {
  330. return $this->_functions->addSlashesBinary( $str );
  331. }
  332. /**
  333. * @param String str
  334. * @return String
  335. */
  336. public function stripSlashesBinary( $str )
  337. {
  338. return $this->_functions->stripSlashesBinary( $str );
  339. }
  340. /**
  341. * @param String fName
  342. * @return String
  343. */
  344. public function addFieldWrappers( $fName )
  345. {
  346. return $this->_functions->addFieldWrappers( $fName );
  347. }
  348. /**
  349. * @param String tName
  350. * @return String
  351. */
  352. public function addTableWrappers( $tName )
  353. {
  354. return $this->_functions->addTableWrappers( $tName );
  355. }
  356. /**
  357. * @param String str
  358. * @return String
  359. */
  360. public function upper( $str )
  361. {
  362. return $this->_functions->upper( $str );
  363. }
  364. /**
  365. * @param Mixed value
  366. * @return String
  367. */
  368. public function addDateQuotes( $value )
  369. {
  370. return $this->_functions->addDateQuotes( $value );
  371. }
  372. /**
  373. * @param Mixed value
  374. * @param Number type ( optional )
  375. * @return String
  376. */
  377. public function field2char( $value, $type = 3 )
  378. {
  379. return $this->_functions->field2char( $value, $type );
  380. }
  381. /**
  382. * It's invoked when search is running on the 'View as:Time' field
  383. * @param Mixed value
  384. * @param Number type
  385. * @return String
  386. */
  387. public function field2time( $value, $type )
  388. {
  389. return $this->_functions->field2time( $value, $type );
  390. }
  391. /**
  392. * @param String tName
  393. * @return String
  394. */
  395. public function timeToSecWrapper( $tName )
  396. {
  397. return $this->_functions->timeToSecWrapper( $tName );
  398. }
  399. /**
  400. * @return Array
  401. */
  402. public function getTableList()
  403. {
  404. return $this->_info->db_gettablelist();
  405. }
  406. /**
  407. * @param String
  408. * @return Array
  409. */
  410. public function getFieldsList( $sql )
  411. {
  412. return $this->_info->db_getfieldslist( $sql );
  413. }
  414. /**
  415. * Check if the db supports subqueries
  416. * @return Boolean
  417. */
  418. public function checkDBSubqueriesSupport()
  419. {
  420. return true;
  421. }
  422. /**
  423. * @param String sql
  424. * @param Number pageStart
  425. * @param Number pageSize
  426. * @param Boolean applyLimit
  427. */
  428. public function queryPage($strSQL, $pageStart, $pageSize, $applyLimit)
  429. {
  430. if( $this->dbType == nDATABASE_MySQL )
  431. {
  432. if( $applyLimit )
  433. $strSQL.= " limit ".(($pageStart - 1) * $pageSize).",".$pageSize;
  434. return $this->query( $strSQL )->getQueryHandle();
  435. }
  436. if( $this->dbType == nDATABASE_MSSQLServer || $this->dbType == nDATABASE_Access )
  437. {
  438. if( $applyLimit )
  439. $strSQL = AddTop($strSQL, $pageStart * $pageSize);
  440. $qResult = $this->query( $strSQL );
  441. $qResult->seekPage( $pageSize, $pageStart );
  442. return $qResult->getQueryHandle();
  443. }
  444. if( $this->dbType == nDATABASE_Oracle )
  445. {
  446. if( $applyLimit )
  447. $strSQL = AddRowNumber($strSQL, $pageStart * $pageSize);
  448. $qResult = $this->query( $strSQL );
  449. $qResult->seekPage( $pageSize, $pageStart );
  450. return $qResult->getQueryHandle();
  451. }
  452. if( $this->dbType == nDATABASE_PostgreSQL )
  453. {
  454. if( $applyLimit )
  455. $strSQL.= " limit ".$pageSize." offset ".(($pageStart - 1) * $pageSize);
  456. return $this->query( $strSQL )->getQueryHandle();
  457. }
  458. if( $this->dbType == nDATABASE_DB2 )
  459. {
  460. if( $applyLimit )
  461. {
  462. $strSQL = "with DB2_QUERY as (".$strSQL.") select * from DB2_QUERY where DB2_ROW_NUMBER between "
  463. .(($pageStart - 1) * $pageSize + 1)." and ".($pageStart * $pageSize);
  464. }
  465. return $this->query( $strSQL )->getQueryHandle();
  466. }
  467. if( $this->dbType == nDATABASE_Informix )
  468. {
  469. if( $applyLimit )
  470. $strSQL = AddTopIfx($strSQL, $pageStart * $pageSize);
  471. $qResult = $this->query( $strSQL );
  472. $qResult->seekPage( $pageSize, $pageStart );
  473. return $qResult->getQueryHandle();
  474. }
  475. if( $this->dbType == nDATABASE_SQLite3 )
  476. {
  477. if( $applyLimit )
  478. $strSQL.= " limit ".(($pageStart - 1) * $pageSize).",".$pageSize;
  479. return $this->query( $strSQL )->getQueryHandle();
  480. }
  481. $qResult = $this->query( $strSQL );
  482. $qResult->seekPage( $pageSize, $pageStart );
  483. return $qResult->getQueryHandle();
  484. }
  485. /**
  486. * An interface stub
  487. * Execute an SQL query with blob fields processing
  488. * @param String sql
  489. * @param Array blobs
  490. * @param Array blobTypes
  491. * @return Boolean
  492. */
  493. public function execWithBlobProcessing( $sql, $blobs, $blobTypes = array() )
  494. {
  495. return $this->exec( $sql );
  496. }
  497. /**
  498. * Get the number of rows fetched by an SQL query
  499. * @param String sql A part of an SQL query or a full SQL query
  500. * @param Boolean The flag indicating if the full SQL query (that can be used as a subquery)
  501. * or the part of an sql query ( from + where clauses ) is passed as the first param
  502. */
  503. public function getFetchedRowsNumber( $sql, $useAsSubquery )
  504. {
  505. if( $useAsSubquery )
  506. $countSql = "select count(*) from (".$sql.") a";
  507. else
  508. $countSql = "select count(*) ".$sql;
  509. $countdata = $this->query( $countSql )->fetchNumeric();
  510. return $countdata[0];
  511. }
  512. /**
  513. * Check if SQL queries containing joined subquery are optimized
  514. * @return Boolean
  515. */
  516. public function checkIfJoinSubqueriesOptimized()
  517. {
  518. return true;
  519. }
  520. /**
  521. * Set and print debug information
  522. * @param String sql
  523. */
  524. function debugInfo( $sql )
  525. {
  526. global $strLastSQL, $dDebug;
  527. $strLastSQL = $sql;
  528. if( $dDebug === true )
  529. echo $sql."<br>";
  530. }
  531. }
  532. ?>