1<?php
2
3namespace Safe;
4
5use Safe\Exceptions\IbaseException;
6
7/**
8 * This function will discard a BLOB if it has not yet been closed by
9 * fbird_blob_close.
10 *
11 * @param resource $blob_handle A BLOB handle opened with fbird_blob_create.
12 * @throws IbaseException
13 *
14 */
15function fbird_blob_cancel($blob_handle): void
16{
17    error_clear_last();
18    $result = \fbird_blob_cancel($blob_handle);
19    if ($result === false) {
20        throw IbaseException::createFromPhpError();
21    }
22}
23
24
25/**
26 *
27 *
28 * @param resource $service_handle The handle on the database server service.
29 * @param string $user_name The login name of the new database user.
30 * @param string $password The password of the new user.
31 * @param string $first_name The first name of the new database user.
32 * @param string $middle_name The middle name of the new database user.
33 * @param string $last_name The last name of the new database user.
34 * @throws IbaseException
35 *
36 */
37function ibase_add_user($service_handle, string $user_name, string $password, string $first_name = null, string $middle_name = null, string $last_name = null): void
38{
39    error_clear_last();
40    if ($last_name !== null) {
41        $result = \ibase_add_user($service_handle, $user_name, $password, $first_name, $middle_name, $last_name);
42    } elseif ($middle_name !== null) {
43        $result = \ibase_add_user($service_handle, $user_name, $password, $first_name, $middle_name);
44    } elseif ($first_name !== null) {
45        $result = \ibase_add_user($service_handle, $user_name, $password, $first_name);
46    } else {
47        $result = \ibase_add_user($service_handle, $user_name, $password);
48    }
49    if ($result === false) {
50        throw IbaseException::createFromPhpError();
51    }
52}
53
54
55/**
56 * This function passes the arguments to the (remote) database server. There it starts a new backup process. Therefore you
57 * won't get any responses.
58 *
59 * @param resource $service_handle A previously opened connection to the database server.
60 * @param string $source_db The absolute file path to the database on the database server. You can also use a database alias.
61 * @param string $dest_file The path to the backup file on the database server.
62 * @param int $options Additional options to pass to the database server for backup.
63 * The options parameter can be a combination
64 * of the following constants:
65 * IBASE_BKP_IGNORE_CHECKSUMS,
66 * IBASE_BKP_IGNORE_LIMBO,
67 * IBASE_BKP_METADATA_ONLY,
68 * IBASE_BKP_NO_GARBAGE_COLLECT,
69 * IBASE_BKP_OLD_DESCRIPTIONS,
70 * IBASE_BKP_NON_TRANSPORTABLE or
71 * IBASE_BKP_CONVERT.
72 * Read the section about  for further information.
73 * @param bool $verbose Since the backup process is done on the database server, you don't have any chance
74 * to get its output. This argument is useless.
75 * @return mixed Returns TRUE on success.
76 *
77 * Since the backup process is done on the (remote) server, this function just passes the arguments to it.
78 * While the arguments are legal, you won't get FALSE.
79 * @throws IbaseException
80 *
81 */
82function ibase_backup($service_handle, string $source_db, string $dest_file, int $options = 0, bool $verbose = false)
83{
84    error_clear_last();
85    $result = \ibase_backup($service_handle, $source_db, $dest_file, $options, $verbose);
86    if ($result === false) {
87        throw IbaseException::createFromPhpError();
88    }
89    return $result;
90}
91
92
93/**
94 * This function will discard a BLOB if it has not yet been closed by
95 * ibase_blob_close.
96 *
97 * @param resource $blob_handle A BLOB handle opened with ibase_blob_create.
98 * @throws IbaseException
99 *
100 */
101function ibase_blob_cancel($blob_handle): void
102{
103    error_clear_last();
104    $result = \ibase_blob_cancel($blob_handle);
105    if ($result === false) {
106        throw IbaseException::createFromPhpError();
107    }
108}
109
110
111/**
112 * ibase_blob_create creates a new BLOB for filling with
113 * data.
114 *
115 * @param resource $link_identifier An InterBase link identifier. If omitted, the last opened link is
116 * assumed.
117 * @return resource Returns a BLOB handle for later use with
118 * ibase_blob_add.
119 * @throws IbaseException
120 *
121 */
122function ibase_blob_create($link_identifier = null)
123{
124    error_clear_last();
125    $result = \ibase_blob_create($link_identifier);
126    if ($result === false) {
127        throw IbaseException::createFromPhpError();
128    }
129    return $result;
130}
131
132
133/**
134 * This function returns at most len bytes from a BLOB
135 * that has been opened for reading by ibase_blob_open.
136 *
137 * @param resource $blob_handle A BLOB handle opened with ibase_blob_open.
138 * @param int $len Size of returned data.
139 * @return string Returns at most len bytes from the BLOB.
140 * @throws IbaseException
141 *
142 */
143function ibase_blob_get($blob_handle, int $len): string
144{
145    error_clear_last();
146    $result = \ibase_blob_get($blob_handle, $len);
147    if ($result === false) {
148        throw IbaseException::createFromPhpError();
149    }
150    return $result;
151}
152
153
154/**
155 * Closes the link to an InterBase database that's associated with
156 * a connection id returned from ibase_connect.
157 * Default transaction on link is committed, other transactions are
158 * rolled back.
159 *
160 * @param resource $connection_id An InterBase link identifier returned from
161 * ibase_connect. If omitted, the last opened link
162 * is assumed.
163 * @throws IbaseException
164 *
165 */
166function ibase_close($connection_id = null): void
167{
168    error_clear_last();
169    $result = \ibase_close($connection_id);
170    if ($result === false) {
171        throw IbaseException::createFromPhpError();
172    }
173}
174
175
176/**
177 * Commits a transaction without closing it.
178 *
179 * @param resource $link_or_trans_identifier If called without an argument, this function commits the default
180 * transaction of the default link. If the argument is a connection
181 * identifier, the default transaction of the corresponding connection
182 * will be committed. If the argument is a transaction identifier, the
183 * corresponding transaction will be committed. The transaction context
184 * will be retained, so statements executed from within this transaction
185 * will not be invalidated.
186 * @throws IbaseException
187 *
188 */
189function ibase_commit_ret($link_or_trans_identifier = null): void
190{
191    error_clear_last();
192    $result = \ibase_commit_ret($link_or_trans_identifier);
193    if ($result === false) {
194        throw IbaseException::createFromPhpError();
195    }
196}
197
198
199/**
200 * Commits a transaction.
201 *
202 * @param resource $link_or_trans_identifier If called without an argument, this function commits the default
203 * transaction of the default link. If the argument is a connection
204 * identifier, the default transaction of the corresponding connection
205 * will be committed. If the argument is a transaction identifier, the
206 * corresponding transaction will be committed.
207 * @throws IbaseException
208 *
209 */
210function ibase_commit($link_or_trans_identifier = null): void
211{
212    error_clear_last();
213    $result = \ibase_commit($link_or_trans_identifier);
214    if ($result === false) {
215        throw IbaseException::createFromPhpError();
216    }
217}
218
219
220/**
221 * Establishes a connection to an Firebird/InterBase server.
222 *
223 * In case a second call is made to ibase_connect with
224 * the same arguments, no new link will be established, but instead, the link
225 * identifier of the already opened link will be returned. The link to the
226 * server will be closed as soon as the execution of the script ends, unless
227 * it's closed earlier by explicitly calling ibase_close.
228 *
229 * @param string $database The database argument has to be a valid path to
230 * database file on the server it resides on. If the server is not local,
231 * it must be prefixed with either 'hostname:' (TCP/IP), 'hostname/port:'
232 * (TCP/IP with interbase server on custom TCP port), '//hostname/'
233 * (NetBEUI), depending on the connection
234 * protocol used.
235 * @param string $username The user name. Can be set with the
236 * ibase.default_user php.ini directive.
237 * @param string $password The password for username. Can be set with the
238 * ibase.default_password php.ini directive.
239 * @param string $charset charset is the default character set for a
240 * database.
241 * @param int $buffers buffers is the number of database buffers to
242 * allocate for the server-side cache. If 0 or omitted, server chooses
243 * its own default.
244 * @param int $dialect dialect selects the default SQL dialect for any
245 * statement executed within a connection, and it defaults to the highest
246 * one supported by client libraries.
247 * @param string $role Functional only with InterBase 5 and up.
248 * @param int $sync
249 * @return resource Returns an Firebird/InterBase link identifier on success.
250 * @throws IbaseException
251 *
252 */
253function ibase_connect(string $database = null, string $username = null, string $password = null, string $charset = null, int $buffers = null, int $dialect = null, string $role = null, int $sync = null)
254{
255    error_clear_last();
256    if ($sync !== null) {
257        $result = \ibase_connect($database, $username, $password, $charset, $buffers, $dialect, $role, $sync);
258    } elseif ($role !== null) {
259        $result = \ibase_connect($database, $username, $password, $charset, $buffers, $dialect, $role);
260    } elseif ($dialect !== null) {
261        $result = \ibase_connect($database, $username, $password, $charset, $buffers, $dialect);
262    } elseif ($buffers !== null) {
263        $result = \ibase_connect($database, $username, $password, $charset, $buffers);
264    } elseif ($charset !== null) {
265        $result = \ibase_connect($database, $username, $password, $charset);
266    } elseif ($password !== null) {
267        $result = \ibase_connect($database, $username, $password);
268    } elseif ($username !== null) {
269        $result = \ibase_connect($database, $username);
270    } elseif ($database !== null) {
271        $result = \ibase_connect($database);
272    } else {
273        $result = \ibase_connect();
274    }
275    if ($result === false) {
276        throw IbaseException::createFromPhpError();
277    }
278    return $result;
279}
280
281
282/**
283 *
284 *
285 * @param resource $service_handle The handle on the database server service.
286 * @param string $user_name The login name of the user you want to delete from the database.
287 * @throws IbaseException
288 *
289 */
290function ibase_delete_user($service_handle, string $user_name): void
291{
292    error_clear_last();
293    $result = \ibase_delete_user($service_handle, $user_name);
294    if ($result === false) {
295        throw IbaseException::createFromPhpError();
296    }
297}
298
299
300/**
301 * This functions drops a database that was opened by either ibase_connect
302 * or ibase_pconnect. The database is closed and deleted from the server.
303 *
304 * @param resource $connection An InterBase link identifier. If omitted, the last opened link is
305 * assumed.
306 * @throws IbaseException
307 *
308 */
309function ibase_drop_db($connection = null): void
310{
311    error_clear_last();
312    $result = \ibase_drop_db($connection);
313    if ($result === false) {
314        throw IbaseException::createFromPhpError();
315    }
316}
317
318
319/**
320 * This function causes the registered event handler specified by
321 * event to be cancelled. The callback function will
322 * no longer be called for the events it was registered to handle.
323 *
324 * @param resource $event An event resource, created by
325 * ibase_set_event_handler.
326 * @throws IbaseException
327 *
328 */
329function ibase_free_event_handler($event): void
330{
331    error_clear_last();
332    $result = \ibase_free_event_handler($event);
333    if ($result === false) {
334        throw IbaseException::createFromPhpError();
335    }
336}
337
338
339/**
340 * Frees a prepared query.
341 *
342 * @param resource $query A query prepared with ibase_prepare.
343 * @throws IbaseException
344 *
345 */
346function ibase_free_query($query): void
347{
348    error_clear_last();
349    $result = \ibase_free_query($query);
350    if ($result === false) {
351        throw IbaseException::createFromPhpError();
352    }
353}
354
355
356/**
357 * Frees a result set.
358 *
359 * @param resource $result_identifier A result set created by ibase_query or
360 * ibase_execute.
361 * @throws IbaseException
362 *
363 */
364function ibase_free_result($result_identifier): void
365{
366    error_clear_last();
367    $result = \ibase_free_result($result_identifier);
368    if ($result === false) {
369        throw IbaseException::createFromPhpError();
370    }
371}
372
373
374/**
375 *
376 *
377 * @param resource $service_handle
378 * @param string $db
379 * @param int $action
380 * @param int $argument
381 * @throws IbaseException
382 *
383 */
384function ibase_maintain_db($service_handle, string $db, int $action, int $argument = 0): void
385{
386    error_clear_last();
387    $result = \ibase_maintain_db($service_handle, $db, $action, $argument);
388    if ($result === false) {
389        throw IbaseException::createFromPhpError();
390    }
391}
392
393
394/**
395 *
396 *
397 * @param resource $service_handle The handle on the database server service.
398 * @param string $user_name The login name of the database user to modify.
399 * @param string $password The user's new password.
400 * @param string $first_name The user's new first name.
401 * @param string $middle_name The user's new middle name.
402 * @param string $last_name The user's new last name.
403 * @throws IbaseException
404 *
405 */
406function ibase_modify_user($service_handle, string $user_name, string $password, string $first_name = null, string $middle_name = null, string $last_name = null): void
407{
408    error_clear_last();
409    if ($last_name !== null) {
410        $result = \ibase_modify_user($service_handle, $user_name, $password, $first_name, $middle_name, $last_name);
411    } elseif ($middle_name !== null) {
412        $result = \ibase_modify_user($service_handle, $user_name, $password, $first_name, $middle_name);
413    } elseif ($first_name !== null) {
414        $result = \ibase_modify_user($service_handle, $user_name, $password, $first_name);
415    } else {
416        $result = \ibase_modify_user($service_handle, $user_name, $password);
417    }
418    if ($result === false) {
419        throw IbaseException::createFromPhpError();
420    }
421}
422
423
424/**
425 * This function assigns a name to a result set. This name can be used later in
426 * UPDATE|DELETE ... WHERE CURRENT OF name statements.
427 *
428 * @param resource $result An InterBase result set.
429 * @param string $name The name to be assigned.
430 * @throws IbaseException
431 *
432 */
433function ibase_name_result($result, string $name): void
434{
435    error_clear_last();
436    $result = \ibase_name_result($result, $name);
437    if ($result === false) {
438        throw IbaseException::createFromPhpError();
439    }
440}
441
442
443/**
444 * Opens a persistent connection to an InterBase database.
445 *
446 * ibase_pconnect acts very much like
447 * ibase_connect with two major differences.
448 *
449 * First, when connecting, the function will first try to find a (persistent)
450 * link that's already opened with the same parameters. If one is found, an
451 * identifier for it will be returned instead of opening a new connection.
452 *
453 * Second, the connection to the InterBase server will not be closed when the
454 * execution of the script ends. Instead, the link will remain open for
455 * future use (ibase_close will not close links
456 * established by ibase_pconnect). This type of link is
457 * therefore called 'persistent'.
458 *
459 * @param string $database The database argument has to be a valid path to
460 * database file on the server it resides on. If the server is not local,
461 * it must be prefixed with either 'hostname:' (TCP/IP), '//hostname/'
462 * (NetBEUI) or 'hostname@' (IPX/SPX), depending on the connection
463 * protocol used.
464 * @param string $username The user name. Can be set with the
465 * ibase.default_user php.ini directive.
466 * @param string $password The password for username. Can be set with the
467 * ibase.default_password php.ini directive.
468 * @param string $charset charset is the default character set for a
469 * database.
470 * @param int $buffers buffers is the number of database buffers to
471 * allocate for the server-side cache. If 0 or omitted, server chooses
472 * its own default.
473 * @param int $dialect dialect selects the default SQL dialect for any
474 * statement executed within a connection, and it defaults to the highest
475 * one supported by client libraries. Functional only with InterBase 6
476 * and up.
477 * @param string $role Functional only with InterBase 5 and up.
478 * @param int $sync
479 * @return resource Returns an InterBase link identifier on success.
480 * @throws IbaseException
481 *
482 */
483function ibase_pconnect(string $database = null, string $username = null, string $password = null, string $charset = null, int $buffers = null, int $dialect = null, string $role = null, int $sync = null)
484{
485    error_clear_last();
486    if ($sync !== null) {
487        $result = \ibase_pconnect($database, $username, $password, $charset, $buffers, $dialect, $role, $sync);
488    } elseif ($role !== null) {
489        $result = \ibase_pconnect($database, $username, $password, $charset, $buffers, $dialect, $role);
490    } elseif ($dialect !== null) {
491        $result = \ibase_pconnect($database, $username, $password, $charset, $buffers, $dialect);
492    } elseif ($buffers !== null) {
493        $result = \ibase_pconnect($database, $username, $password, $charset, $buffers);
494    } elseif ($charset !== null) {
495        $result = \ibase_pconnect($database, $username, $password, $charset);
496    } elseif ($password !== null) {
497        $result = \ibase_pconnect($database, $username, $password);
498    } elseif ($username !== null) {
499        $result = \ibase_pconnect($database, $username);
500    } elseif ($database !== null) {
501        $result = \ibase_pconnect($database);
502    } else {
503        $result = \ibase_pconnect();
504    }
505    if ($result === false) {
506        throw IbaseException::createFromPhpError();
507    }
508    return $result;
509}
510
511
512/**
513 * This function passes the arguments to the (remote) database server. There it starts a new restore process. Therefore you
514 * won't get any responses.
515 *
516 * @param resource $service_handle A previously opened connection to the database server.
517 * @param string $source_file The absolute path on the server where the backup file is located.
518 * @param string $dest_db The path to create the new database on the server. You can also use database alias.
519 * @param int $options Additional options to pass to the database server for restore.
520 * The options parameter can be a combination
521 * of the following constants:
522 * IBASE_RES_DEACTIVATE_IDX,
523 * IBASE_RES_NO_SHADOW,
524 * IBASE_RES_NO_VALIDITY,
525 * IBASE_RES_ONE_AT_A_TIME,
526 * IBASE_RES_REPLACE,
527 * IBASE_RES_CREATE,
528 * IBASE_RES_USE_ALL_SPACE,
529 * IBASE_PRP_PAGE_BUFFERS,
530 * IBASE_PRP_SWEEP_INTERVAL,
531 * IBASE_RES_CREATE.
532 * Read the section about  for further information.
533 * @param bool $verbose Since the restore process is done on the database server, you don't have any chance
534 * to get its output. This argument is useless.
535 * @return mixed Returns TRUE on success.
536 *
537 * Since the restore process is done on the (remote) server, this function just passes the arguments to it.
538 * While the arguments are legal, you won't get FALSE.
539 * @throws IbaseException
540 *
541 */
542function ibase_restore($service_handle, string $source_file, string $dest_db, int $options = 0, bool $verbose = false)
543{
544    error_clear_last();
545    $result = \ibase_restore($service_handle, $source_file, $dest_db, $options, $verbose);
546    if ($result === false) {
547        throw IbaseException::createFromPhpError();
548    }
549    return $result;
550}
551
552
553/**
554 * Rolls back a transaction without closing it.
555 *
556 * @param resource $link_or_trans_identifier If called without an argument, this function rolls back the default
557 * transaction of the default link. If the argument is a connection
558 * identifier, the default transaction of the corresponding connection
559 * will be rolled back. If the argument is a transaction identifier, the
560 * corresponding transaction will be rolled back. The transaction context
561 * will be retained, so statements executed from within this transaction
562 * will not be invalidated.
563 * @throws IbaseException
564 *
565 */
566function ibase_rollback_ret($link_or_trans_identifier = null): void
567{
568    error_clear_last();
569    $result = \ibase_rollback_ret($link_or_trans_identifier);
570    if ($result === false) {
571        throw IbaseException::createFromPhpError();
572    }
573}
574
575
576/**
577 * Rolls back a transaction.
578 *
579 * @param resource $link_or_trans_identifier If called without an argument, this function rolls back the default
580 * transaction of the default link. If the argument is a connection
581 * identifier, the default transaction of the corresponding connection
582 * will be rolled back. If the argument is a transaction identifier, the
583 * corresponding transaction will be rolled back.
584 * @throws IbaseException
585 *
586 */
587function ibase_rollback($link_or_trans_identifier = null): void
588{
589    error_clear_last();
590    $result = \ibase_rollback($link_or_trans_identifier);
591    if ($result === false) {
592        throw IbaseException::createFromPhpError();
593    }
594}
595
596
597/**
598 *
599 *
600 * @param string $host The name or ip address of the database host. You can define the port by adding
601 * '/' and port number. If no port is specified, port 3050 will be used.
602 * @param string $dba_username The name of any valid user.
603 * @param string $dba_password The user's password.
604 * @return resource Returns a Interbase / Firebird link identifier on success.
605 * @throws IbaseException
606 *
607 */
608function ibase_service_attach(string $host, string $dba_username, string $dba_password)
609{
610    error_clear_last();
611    $result = \ibase_service_attach($host, $dba_username, $dba_password);
612    if ($result === false) {
613        throw IbaseException::createFromPhpError();
614    }
615    return $result;
616}
617
618
619/**
620 *
621 *
622 * @param resource $service_handle A previously created connection to the database server.
623 * @throws IbaseException
624 *
625 */
626function ibase_service_detach($service_handle): void
627{
628    error_clear_last();
629    $result = \ibase_service_detach($service_handle);
630    if ($result === false) {
631        throw IbaseException::createFromPhpError();
632    }
633}
634