prepare v2.2.3
[m6w6/ext-pq] / src / php_pqexc.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18
19 #include <Zend/zend_exceptions.h>
20 #include <ext/spl/spl_exceptions.h>
21
22 #include "php_pq.h"
23 #include "php_pq_object.h"
24 #include "php_pqexc.h"
25
26 static zend_class_entry *php_pqexc_interface_class_entry;
27 static zend_class_entry *php_pqexc_invalid_argument_class_entry;
28 static zend_class_entry *php_pqexc_runtime_class_entry;
29 static zend_class_entry *php_pqexc_bad_methodcall_class_entry;
30 static zend_class_entry *php_pqexc_domain_class_entry;
31
32 static zend_function_entry php_pqexc_methods[] = {
33 {0}
34 };
35
36 zend_class_entry *exce(php_pqexc_type_t type)
37 {
38 switch (type) {
39 default:
40 case EX_INVALID_ARGUMENT:
41 return php_pqexc_invalid_argument_class_entry;
42 case EX_RUNTIME:
43 case EX_CONNECTION_FAILED:
44 case EX_IO:
45 case EX_ESCAPE:
46 return php_pqexc_runtime_class_entry;
47 case EX_UNINITIALIZED:
48 case EX_BAD_METHODCALL:
49 return php_pqexc_bad_methodcall_class_entry;
50 case EX_DOMAIN:
51 case EX_SQL:
52 return php_pqexc_domain_class_entry;
53 }
54 }
55
56 zend_object *throw_exce(php_pqexc_type_t type, const char *fmt, ...)
57 {
58 char *msg;
59 zend_object *zexc;
60 va_list argv;
61
62 va_start(argv, fmt);
63 vspprintf(&msg, 0, fmt, argv);
64 va_end(argv);
65
66 zexc = zend_throw_exception(exce(type), msg, type);
67 efree(msg);
68
69 return zexc;
70 }
71
72 PHP_MINIT_FUNCTION(pqexc)
73 {
74 zend_class_entry ce = {0};
75
76 INIT_NS_CLASS_ENTRY(ce, "pq", "Exception", php_pqexc_methods);
77 php_pqexc_interface_class_entry = zend_register_internal_interface(&ce);
78 zend_class_implements(php_pqexc_interface_class_entry, 1, zend_ce_throwable);
79
80 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("INVALID_ARGUMENT"), EX_INVALID_ARGUMENT);
81 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("RUNTIME"), EX_RUNTIME);
82 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("CONNECTION_FAILED"), EX_CONNECTION_FAILED);
83 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("IO"), EX_IO);
84 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("ESCAPE"), EX_ESCAPE);
85 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("BAD_METHODCALL"), EX_BAD_METHODCALL);
86 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("UNINITIALIZED"), EX_UNINITIALIZED);
87 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("DOMAIN"), EX_DOMAIN);
88 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("SQL"), EX_SQL);
89
90 memset(&ce, 0, sizeof(ce));
91 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "InvalidArgumentException", php_pqexc_methods);
92 php_pqexc_invalid_argument_class_entry = zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException);
93 zend_class_implements(php_pqexc_invalid_argument_class_entry, 1, php_pqexc_interface_class_entry);
94
95 memset(&ce, 0, sizeof(ce));
96 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "RuntimeException", php_pqexc_methods);
97 php_pqexc_runtime_class_entry = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException);
98 zend_class_implements(php_pqexc_runtime_class_entry, 1, php_pqexc_interface_class_entry);
99
100 memset(&ce, 0, sizeof(ce));
101 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "BadMethodCallException", php_pqexc_methods);
102 php_pqexc_bad_methodcall_class_entry = zend_register_internal_class_ex(&ce, spl_ce_BadMethodCallException);
103 zend_class_implements(php_pqexc_bad_methodcall_class_entry, 1, php_pqexc_interface_class_entry);
104
105 memset(&ce, 0, sizeof(ce));
106 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "DomainException", php_pqexc_methods);
107 php_pqexc_domain_class_entry = zend_register_internal_class_ex(&ce, spl_ce_DomainException);
108 zend_class_implements(php_pqexc_domain_class_entry, 1, php_pqexc_interface_class_entry);
109 zend_declare_property_null(php_pqexc_domain_class_entry, ZEND_STRL("sqlstate"), ZEND_ACC_PUBLIC);
110
111 return SUCCESS;
112 }
113
114 /*
115 * Local variables:
116 * tab-width: 4
117 * c-basic-offset: 4
118 * End:
119 * vim600: noet sw=4 ts=4 fdm=marker
120 * vim<600: noet sw=4 ts=4
121 */