utils.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2005 RingCentral Inc.
  3. * Created by Dmitry Semyonov <[email protected]>
  4. *
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include <oci.h>
  28. #include "../../dprint.h"
  29. #include "common.h"
  30. #include "utils.h"
  31. sb4 checkerr(dvoid *errhp, sword status, int line)
  32. {
  33. sb4 errcode = 0;
  34. if (status == OCI_SUCCESS)
  35. return 0;
  36. #define CASE_LOG_STR(_err) \
  37. case _err: \
  38. ERR("Error - " #_err); \
  39. break
  40. switch (status)
  41. {
  42. CASE_LOG_STR(OCI_SUCCESS_WITH_INFO);
  43. CASE_LOG_STR(OCI_NEED_DATA);
  44. CASE_LOG_STR(OCI_NO_DATA);
  45. CASE_LOG_STR(OCI_INVALID_HANDLE);
  46. CASE_LOG_STR(OCI_STILL_EXECUTING);
  47. CASE_LOG_STR(OCI_CONTINUE);
  48. case OCI_ERROR:
  49. {
  50. text errbuf[512];
  51. errbuf[0] = '\0';
  52. (void) OCIErrorGet(errhp, 1, NULL, &errcode, errbuf, sizeof(errbuf),
  53. OCI_HTYPE_ERROR);
  54. /* Special case. (Search 24334 in dbase.c for explanation.) */
  55. if (errcode == 24334)
  56. {
  57. DBG1("ORA-24334. Most likely not an error.\n");
  58. return 24334;
  59. }
  60. ERR("Error - %.*s", sizeof(errbuf), errbuf);
  61. break;
  62. }
  63. default:
  64. ERR("Error - unhandled status: %d", status);
  65. }
  66. ERR(". Line %d\n", line);
  67. return errcode;
  68. }
  69. const char *sqlt_to_str(ub2 sqlt)
  70. {
  71. #define CASE_STR(_val) case _val: return #_val
  72. switch (sqlt)
  73. {
  74. CASE_STR(SQLT_CHR);
  75. CASE_STR(SQLT_STR);
  76. CASE_STR(SQLT_INT);
  77. CASE_STR(SQLT_UIN);
  78. CASE_STR(SQLT_FLT);
  79. CASE_STR(SQLT_DAT);
  80. CASE_STR(SQLT_DATE);
  81. CASE_STR(SQLT_TIMESTAMP);
  82. CASE_STR(SQLT_TIMESTAMP_TZ);
  83. CASE_STR(SQLT_TIMESTAMP_LTZ);
  84. CASE_STR(SQLT_NUM);
  85. default:
  86. return "Unknown SQLT_xxx";
  87. }
  88. }
  89. /*
  90. * URL example: "oracle://serro:47serro11@localhost:port/ser"
  91. * XXX: Buffer lengths are not checked yet!
  92. */
  93. int parse_sql_url(const char *_sqlurl, char *sz_user, char *sz_passwd,
  94. char *sz_db)
  95. {
  96. /* Skip 'oracle://' part */
  97. while ( *_sqlurl != '\0' && *_sqlurl++ != ':' );
  98. if ( *_sqlurl++ != '/' || *_sqlurl++ != '/' )
  99. return -1;
  100. /* Get username */
  101. do {
  102. *sz_user++ = *_sqlurl++;
  103. } while ( *_sqlurl != ':' && *_sqlurl != '\0' );
  104. if ( *_sqlurl++ != ':' )
  105. return -2;
  106. *sz_user = '\0';
  107. /* Get password */
  108. do {
  109. *sz_passwd++ = *_sqlurl++;
  110. } while ( *_sqlurl != '@' && *_sqlurl != '\0' );
  111. if ( *_sqlurl++ != '@' )
  112. return -3;
  113. *sz_passwd = '\0';
  114. /* Skip 'localhost:port' part */
  115. while ( *_sqlurl != '\0' && *_sqlurl++ != '/' );
  116. if ( *_sqlurl == '\0' )
  117. return -4;
  118. /* Get db */
  119. do {
  120. *sz_db++ = *_sqlurl++;
  121. } while ( *_sqlurl != '\0' );
  122. *sz_db = '\0';
  123. return 0;
  124. }