PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext/async_mysql/ext_async_mysql.php

https://gitlab.com/Blueprint-Marketing/hhvm
PHP | 770 lines | 228 code | 92 blank | 450 comment | 0 complexity | a9a615f8ae2891072e9449c378fc2153 MD5 | raw file
  1. <?hh
  2. /*
  3. +----------------------------------------------------------------------+
  4. | HipHop for PHP |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. */
  17. /**
  18. * An asynchronous MySQL client
  19. *
  20. */
  21. final class AsyncMysqlClient {
  22. /**
  23. * AsyncMysqlClient objects cannot be directly created.
  24. *
  25. */
  26. private function __construct(): void {
  27. throw new InvalidOperationException(
  28. __CLASS__ . " objects cannot be directly created");
  29. }
  30. /**
  31. * Sets the limit of all pools using this client
  32. *
  33. * @param int $limit - The limit for all pools
  34. *
  35. */
  36. <<__HipHopSpecific, __Native>>
  37. public static function setPoolsConnectionLimit(int $limit): void;
  38. /**
  39. * Begin an async connection to a MySQL instance
  40. *
  41. * @param string $host - The hostname to connect to
  42. * @param int $port - The port to connect to
  43. * @param string $dbname - The initial database when connecting
  44. * @param string $user - The user to connect as
  45. * @param string $password - The password to connect with
  46. * @param int $timeout_micros - Timeout, in microseconds, for the connect;
  47. * -1 for default, 0 for no timeout
  48. *
  49. */
  50. <<__HipHopSpecific, __Native>>
  51. public static function connect(string $host,
  52. int $port,
  53. string $dbname,
  54. string $user,
  55. string $password,
  56. int $timeout_micros = -1
  57. ): ExternalThreadEventWaitHandle;
  58. /**
  59. * Create a new async connection from a synchronous MySQL instance
  60. *
  61. * @param mixed $connection - The synchronous MySQL connection.
  62. *
  63. */
  64. <<__HipHopSpecific, __Native>>
  65. public static function adoptConnection(mixed $connection
  66. ): AsyncMysqlConnection;
  67. }
  68. /**
  69. * An asynchronous MySQL connection pool
  70. *
  71. */
  72. <<__NativeData("AsyncMysqlConnectionPool")>>
  73. class AsyncMysqlConnectionPool {
  74. /**
  75. * @param array $pool_options - Options for pool
  76. *
  77. */
  78. <<__Native>>
  79. public function __construct(array $pool_options): void;
  80. /**
  81. * Returns the stats values for the pool
  82. *
  83. * @return array - Each position maps to a different stat value
  84. *
  85. */
  86. <<__HipHopSpecific, __Native>>
  87. public function getPoolStats(): array;
  88. /**
  89. * Begin an async connection to a MySQL instance
  90. *
  91. * @param string $host - The hostname to connect to
  92. * @param int $port - The port to connect to
  93. * @param string $dbname - The initial database when connecting
  94. * @param string $user - The user to connect as
  95. * @param string $password - The password to connect with
  96. * @param int $timeout_micros - Timeout, in microseconds, for the connect;
  97. * -1 for default, 0 for no timeout
  98. * @param string $extra_key - Extra parameter to separate connections even
  99. * better
  100. *
  101. */
  102. <<__HipHopSpecific, __Native>>
  103. public function connect(string $host,
  104. int $port,
  105. string $dbname,
  106. string $user,
  107. string $password,
  108. int $timeout_micros = -1,
  109. string $extra_key = ""
  110. ): ExternalThreadEventWaitHandle;
  111. }
  112. /**
  113. * An active connection to a MySQL instance
  114. *
  115. */
  116. <<__NativeData("AsyncMysqlConnection")>>
  117. final class AsyncMysqlConnection {
  118. private function __construct(): void {
  119. throw new InvalidOperationException(
  120. __CLASS__ . " objects cannot be directly created");
  121. }
  122. /**
  123. * Begin running a query; returns a WaitHandle
  124. *
  125. * @param string $query - The query itself
  126. * @param int $timeout_micros - Timeout, in microseconds, for the query to
  127. * complete in; -1 for default, 0 for no timeout
  128. *
  129. */
  130. <<__HipHopSpecific, __Native>>
  131. function query(string $query,
  132. int $timeout_micros = -1): ExternalThreadEventWaitHandle;
  133. /**
  134. * Execute a query with placeholders and parameters.
  135. *
  136. * For example:
  137. * queryf("SELECT %C FROM %T WHERE %C %=s", $col1, $table, $col2, $value);
  138. * Supported placeholders:
  139. * %T table name
  140. * %C column name
  141. * %s nullable string (will be escaped)
  142. * %d integer
  143. * %f float
  144. * %=s nullable string comparison - expands to either:
  145. * = 'escaped_string'
  146. * IS NULL
  147. * %=d nullable integer comparison
  148. * %=f nullable float comparison
  149. * %Q raw SQL query. The typechecker intentionally does not recognize
  150. * this, however, you can use it in combination with // UNSAFE
  151. * if absolutely required
  152. */
  153. <<__HipHopSpecific, __Native>>
  154. function queryf(string $pattern,
  155. ...$args): ExternalThreadEventWaitHandle;
  156. /**
  157. * Begin running a query with multiple statements; returns a WaitHandle
  158. *
  159. * @param array $queries - A vector of queries
  160. * @param int $timeout_micros - Timeout, in microseconds, for all queries to
  161. * complete in; -1 for default, 0 for no timeout
  162. *
  163. */
  164. <<__HipHopSpecific, __Native>>
  165. function multiQuery(array $queries,
  166. int $timeout_micros = -1): ExternalThreadEventWaitHandle;
  167. /**
  168. * Escape a string to be safe for including in a query.
  169. *
  170. * Equivalent to mysql_real_escape_string().
  171. */
  172. <<__HipHopSpecific, __Native>>
  173. function escapeString(string $data): string;
  174. /**
  175. * Close this connection
  176. *
  177. */
  178. <<__HipHopSpecific, __Native>>
  179. function close(): void;
  180. /**
  181. * Return a synchronous MySQL connection, destroying this connection in the
  182. * process.
  183. *
  184. */
  185. <<__HipHopSpecific, __Native>>
  186. function releaseConnection(): mixed;
  187. /**
  188. * The server version of the server connected
  189. *
  190. */
  191. <<__HipHopSpecific, __Native>>
  192. function serverInfo(): string;
  193. /**
  194. * The number of errors, warnings, and notes returned during execution of
  195. * the previous SQL statement
  196. *
  197. */
  198. <<__HipHopSpecific, __Native>>
  199. function warningCount(): int;
  200. /**
  201. * The host this connection is connected to
  202. *
  203. */
  204. <<__HipHopSpecific, __Native>>
  205. function host(): string;
  206. /**
  207. * The port this connection is connected to
  208. *
  209. */
  210. <<__HipHopSpecific, __Native>>
  211. function port(): int;
  212. /**
  213. * Sets if the connection can be recycled without any clean up
  214. *
  215. * @param bool $reusable - If it is reusable or not
  216. *
  217. */
  218. <<__HipHopSpecific, __Native>>
  219. function setReusable(bool $reusable): void;
  220. /**
  221. * Returns whether or not the connection is set as reusable by the pool
  222. *
  223. */
  224. <<__HipHopSpecific, __Native>>
  225. function isReusable(): bool;
  226. }
  227. /**
  228. * A base class for errors and query results. This class contains timing
  229. * information about a query or connection.
  230. *
  231. */
  232. abstract class AsyncMysqlResult {
  233. private function __construct(): void {
  234. throw new InvalidOperationException(
  235. __CLASS__ . " objects cannot be directly created");
  236. }
  237. /**
  238. * Time this operation took, in microseconds
  239. *
  240. */
  241. abstract function elapsedMicros(): int;
  242. /**
  243. * The time this operation began, in seconds since epoch
  244. *
  245. */
  246. abstract function startTime(): float;
  247. /**
  248. * The time this operation completed, in seconds since epoch
  249. *
  250. */
  251. abstract function endTime(): float;
  252. }
  253. /**
  254. * A class containing error information for a failed connect or query
  255. *
  256. */
  257. <<__NativeData("AsyncMysqlErrorResult")>>
  258. class AsyncMysqlErrorResult extends AsyncMysqlResult {
  259. private function __construct(): void {
  260. throw new InvalidOperationException(
  261. __CLASS__ . " objects cannot be directly created");
  262. }
  263. /**
  264. * Time this operation took, in microseconds
  265. *
  266. */
  267. <<__HipHopSpecific, __Native>>
  268. function elapsedMicros(): int;
  269. /**
  270. * The time this operation began, in seconds since epoch
  271. *
  272. */
  273. <<__HipHopSpecific, __Native>>
  274. function startTime(): float;
  275. /**
  276. * The time this operation completed, in seconds since epoch
  277. *
  278. */
  279. <<__HipHopSpecific, __Native>>
  280. function endTime(): float;
  281. /**
  282. * The MySQL error number (see MySQL's errmsg.h for details, or the C API's
  283. * mysql_errno() documentation)
  284. *
  285. */
  286. <<__HipHopSpecific, __Native>>
  287. function mysql_errno(): int;
  288. /**
  289. * A human-readable string for the error encountered
  290. *
  291. */
  292. <<__HipHopSpecific, __Native>>
  293. function mysql_error(): string;
  294. /**
  295. * The type of failure (either 'TimedOut', representing a timeout, or
  296. * 'Failed', representing the server rejecting our connection or query)
  297. *
  298. */
  299. <<__HipHopSpecific, __Native>>
  300. function failureType(): string;
  301. }
  302. /**
  303. * A class containing info about results for statements that ran before mysql
  304. * error
  305. *
  306. */
  307. <<__NativeData("AsyncMysqlQueryErrorResult")>>
  308. final class AsyncMysqlQueryErrorResult extends AsyncMysqlErrorResult {
  309. private function __construct(): void {
  310. throw new InvalidOperationException(
  311. __CLASS__ . " objects cannot be directly created");
  312. }
  313. /**
  314. * Returns the number of successfully executed queries
  315. *
  316. */
  317. <<__HipHopSpecific, __Native>>
  318. function numSuccessfulQueries(): int;
  319. /**
  320. * Returns the results that were fetched by the statements that succeeded
  321. *
  322. */
  323. <<__HipHopSpecific, __Native>>
  324. function getSuccessfulResults(): Vector;
  325. }
  326. /**
  327. * The result of a successfully executed query
  328. *
  329. */
  330. <<__NativeData("AsyncMysqlQueryResult")>>
  331. final class AsyncMysqlQueryResult extends AsyncMysqlResult {
  332. private function __construct(): void {
  333. throw new InvalidOperationException(
  334. __CLASS__ . " objects cannot be directly created");
  335. }
  336. /**
  337. * Time this operation took, in microseconds
  338. *
  339. */
  340. <<__HipHopSpecific, __Native>>
  341. function elapsedMicros(): int;
  342. /**
  343. * The time this operation began, in seconds since epoch
  344. *
  345. */
  346. <<__HipHopSpecific, __Native>>
  347. function startTime(): float;
  348. /**
  349. * The time this operation completed, in seconds since epoch
  350. *
  351. */
  352. <<__HipHopSpecific, __Native>>
  353. function endTime(): float;
  354. /**
  355. * The number of rows affected (see the C API's mysql_num_rows()
  356. * documentation)
  357. *
  358. */
  359. <<__HipHopSpecific, __Native>>
  360. function numRowsAffected(): int;
  361. /**
  362. * The last ID inserted, if one existed for this query (see the C API's
  363. * mysql_insert_id() documentation)
  364. *
  365. */
  366. <<__HipHopSpecific, __Native>>
  367. function lastInsertId(): int;
  368. /**
  369. * The number of rows in this result
  370. *
  371. */
  372. <<__HipHopSpecific, __Native>>
  373. function numRows(): int;
  374. /**
  375. * The rows returned by this query, as a Vector of Map objects which map
  376. * column names to possibly-null string values.
  377. *
  378. */
  379. <<__HipHopSpecific, __Native>>
  380. function mapRows(): Vector;
  381. /**
  382. * The rows returned by this query, as a Vector of Vector objects which hold
  383. * the possibly-null string values of each column in the order of the
  384. * original query.
  385. *
  386. */
  387. <<__HipHopSpecific, __Native>>
  388. function vectorRows(): Vector;
  389. /**
  390. * The rows returned by this query, as a Vector of Map objects which map
  391. * column names to possibly-null mixed values.
  392. *
  393. */
  394. <<__HipHopSpecific, __Native>>
  395. function mapRowsTyped(): Vector;
  396. /**
  397. * The rows returned by this query, as a Vector of Vector objects which hold
  398. * the possibly-null mixed values of each column in the order of the
  399. * original query.
  400. *
  401. */
  402. <<__HipHopSpecific, __Native>>
  403. function vectorRowsTyped(): Vector;
  404. /**
  405. * Returns a Vector<AsyncMysqlRowBlock> representing all row blocks returned
  406. * by this query.
  407. *
  408. */
  409. <<__HipHopSpecific, __Native>>
  410. function rowBlocks(): Vector;
  411. }
  412. /**
  413. * A class to represent a Row Block.
  414. *
  415. */
  416. <<__NativeData("AsyncMysqlRowBlock")>>
  417. final class AsyncMysqlRowBlock implements IteratorAggregate, Countable {
  418. private function __construct(): void {
  419. throw new InvalidOperationException(
  420. __CLASS__ . " objects cannot be directly created");
  421. }
  422. /**
  423. * Get a field value
  424. *
  425. * @param int $row - the row index
  426. * @param mixed $field - the field index(int) or field name(string).
  427. *
  428. */
  429. <<__HipHopSpecific, __Native>>
  430. function at(int $row, mixed $field): mixed;
  431. /**
  432. * Get a certain field from a certain row as integer.
  433. *
  434. * @param int $row - the row index
  435. * @param mixed $field - the field index(int) or field name(string).
  436. *
  437. */
  438. <<__HipHopSpecific, __Native>>
  439. function getFieldAsInt(int $row, mixed $field): int;
  440. /**
  441. * Get a certain field from a certain row as double.
  442. *
  443. * @param int $row - the row index
  444. * @param mixed $field - the field index(int) or field name(string)
  445. *
  446. */
  447. <<__HipHopSpecific, __Native>>
  448. function getFieldAsDouble(int $row, mixed $field): float;
  449. /**
  450. * Get a certain field from a certain row as String.
  451. *
  452. * @param int $row - the row index
  453. *
  454. * @param mixed $field - the field index(int) or field name(string).
  455. *
  456. */
  457. <<__HipHopSpecific, __Native>>
  458. function getFieldAsString(int $row, mixed $field): string;
  459. /**
  460. * Returns true if a field is null.
  461. *
  462. * @param int $row - the row index
  463. *
  464. * @param mixed $field - the field index(int) or field name(string).
  465. *
  466. */
  467. <<__HipHopSpecific, __Native>>
  468. function isNull(int $row, mixed $field): bool;
  469. /**
  470. * The type of the field as a string.
  471. *
  472. * @param mixed $field - the field index(int) or field name(string).
  473. *
  474. */
  475. <<__HipHopSpecific, __Native>>
  476. function fieldType(mixed $field): int;
  477. /**
  478. * The flags of the field.
  479. *
  480. * @param mixed $field - the field index(int) or field name(string).
  481. *
  482. */
  483. <<__HipHopSpecific, __Native>>
  484. function fieldFlags(mixed $field): int;
  485. /**
  486. * The name of the field.
  487. *
  488. * @param int $field - the field index
  489. *
  490. */
  491. <<__HipHopSpecific, __Native>>
  492. function fieldName(int $field): string;
  493. /**
  494. * Returns true if no rows are returned.
  495. *
  496. */
  497. <<__HipHopSpecific, __Native>>
  498. function isEmpty(): bool;
  499. /**
  500. * The number of fields.
  501. *
  502. */
  503. <<__HipHopSpecific, __Native>>
  504. function fieldsCount(): int;
  505. /**
  506. * The number of rows.
  507. *
  508. */
  509. <<__HipHopSpecific, __Native>>
  510. function count(): int;
  511. /**
  512. * Get the iterator for the rows in the block.
  513. *
  514. */
  515. <<__HipHopSpecific, __Native>>
  516. function getIterator(): AsyncMysqlRowBlockIterator;
  517. /**
  518. * Get a certain row.
  519. *
  520. * @param int $row - the row index
  521. *
  522. */
  523. <<__HipHopSpecific, __Native>>
  524. function getRow(int $row): AsyncMySqlRow;
  525. }
  526. /**
  527. * A class to represent an Iterator over the rows of a AsyncMysqlRowBlock.
  528. *
  529. */
  530. <<__NativeData("AsyncMysqlRowBlockIterator")>>
  531. final class AsyncMysqlRowBlockIterator implements HH\KeyedIterator {
  532. private function __construct(): void {
  533. throw new InvalidOperationException(
  534. __CLASS__ . " objects cannot be directly created");
  535. }
  536. /**
  537. * Check if the current row number is valid
  538. *
  539. */
  540. <<__HipHopSpecific, __Native>>
  541. function valid(): bool;
  542. /**
  543. * Advance the iterator to the next row.
  544. *
  545. */
  546. <<__HipHopSpecific, __Native>>
  547. function next(): void;
  548. /**
  549. * Get the current row.
  550. *
  551. */
  552. <<__HipHopSpecific, __Native>>
  553. function current(): AsyncMysqlRow;
  554. /**
  555. * Get the current row number
  556. *
  557. */
  558. <<__HipHopSpecific, __Native>>
  559. function key(): int;
  560. /**
  561. * Reset the iterator to the first row.
  562. *
  563. */
  564. <<__HipHopSpecific, __Native>>
  565. function rewind(): void;
  566. }
  567. /**
  568. * A class to represent a Row.
  569. *
  570. */
  571. <<__NativeData("AsyncMysqlRow")>>
  572. final class AsyncMysqlRow implements MysqlRow {
  573. private function __construct(): void {
  574. throw new InvalidOperationException(
  575. __CLASS__ . " objects cannot be directly created");
  576. }
  577. /**
  578. * Get field indexed by the `field`.
  579. *
  580. * @param mixed $field - the field index(int) or field name(string).
  581. *
  582. */
  583. <<__HipHopSpecific, __Native>>
  584. function at(mixed $field): mixed;
  585. /**
  586. * Get a certain field as integer.
  587. *
  588. * @param mixed $field - the field index(int) or field name(string).
  589. *
  590. */
  591. <<__HipHopSpecific, __Native>>
  592. function getFieldAsInt(mixed $field): int;
  593. /**
  594. * Get a certain field as double.
  595. *
  596. * @param mixed $field - the field index(int) or field name(string).
  597. *
  598. */
  599. <<__HipHopSpecific, __Native>>
  600. function getFieldAsDouble(mixed $field): float;
  601. /**
  602. * Get a certain field as String.
  603. *
  604. * @param mixed $field - the field index(int) or field name(string).
  605. *
  606. */
  607. <<__HipHopSpecific, __Native>>
  608. function getFieldAsString(mixed $field): string;
  609. /**
  610. * Returns true if a field is null.
  611. *
  612. * @param mixed $field - the field index(int) or field name(string).
  613. *
  614. */
  615. <<__HipHopSpecific, __Native>>
  616. function isNull(mixed $field): bool;
  617. /**
  618. * The type of the field as a string.
  619. *
  620. * @param mixed $field - the field index(int) or field name(string).
  621. *
  622. */
  623. <<__HipHopSpecific, __Native>>
  624. function fieldType(mixed $field): int;
  625. /**
  626. * Get the number of fields.
  627. *
  628. */
  629. <<__HipHopSpecific, __Native>>
  630. function count(): int;
  631. /**
  632. * Get the iterator over the fields in the row.
  633. *
  634. */
  635. <<__HipHopSpecific, __Native>>
  636. function getIterator(): AsyncMysqlRowIterator;
  637. }
  638. /**
  639. * A class to represent an Iterator over the fields in a row.
  640. *
  641. */
  642. <<__NativeData("AsyncMysqlRowIterator")>>
  643. final class AsyncMysqlRowIterator implements HH\KeyedIterator {
  644. private function __construct(): void {
  645. throw new InvalidOperationException(
  646. __CLASS__ . " objects cannot be directly created");
  647. }
  648. /**
  649. * Check if the current field number is valid
  650. *
  651. */
  652. <<__HipHopSpecific, __Native>>
  653. function valid(): bool;
  654. /**
  655. * Advance the iterator to the next field.
  656. *
  657. */
  658. <<__HipHopSpecific, __Native>>
  659. function next(): void;
  660. /**
  661. * Get the current field.
  662. *
  663. */
  664. <<__HipHopSpecific, __Native>>
  665. function current(): string;
  666. /**
  667. * Get the current field number
  668. *
  669. */
  670. <<__HipHopSpecific, __Native>>
  671. function key(): int;
  672. /**
  673. * Reset the iterator to the first field.
  674. *
  675. */
  676. <<__HipHopSpecific, __Native>>
  677. function rewind(): void;
  678. }