conn.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. ** Copyright (c) 2011 D. Richard Hipp
  3. **
  4. ** This program is free software; you can redistribute it and/or
  5. ** modify it under the terms of the Simplified BSD License (also
  6. ** known as the "2-Clause License" or "FreeBSD License".)
  7. **
  8. ** This program is distributed in the hope that it will be useful,
  9. ** but without any warranty; without even the implied warranty of
  10. ** merchantability or fitness for a particular purpose.
  11. **
  12. ** Author contact information:
  13. ** [email protected]
  14. ** http://www.hwaci.com/drh/
  15. **
  16. *************************************************************************
  17. ** Main interfaces
  18. */
  19. #include "xjd1Int.h"
  20. /*
  21. ** Open a new database connection
  22. */
  23. int xjd1_open_with_db(xjd1_context *pContext, sqlite3 *db, xjd1 **ppNewConn){
  24. xjd1 *pConn;
  25. *ppNewConn = pConn = xjd1_malloc( sizeof(*pConn) );
  26. if( pConn==0 ) return XJD1_NOMEM;
  27. memset(pConn, 0, sizeof(*pConn));
  28. pConn->pContext = pContext;
  29. pConn->db = db;
  30. pConn->isSQLite3Borrowed = 1;
  31. return XJD1_OK;
  32. }
  33. int xjd1_open(xjd1_context *pContext, const char *zURI, xjd1 **ppNewConn){
  34. sqlite3 *db;
  35. int rc;
  36. rc = sqlite3_open_v2(zURI, &db,
  37. SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, 0);
  38. if( rc ){
  39. sqlite3_close(db);
  40. return XJD1_ERROR_OPEN_DB;
  41. }
  42. rc = xjd1_open_with_db(pContext, db, ppNewConn);
  43. if(rc != XJD1_OK){
  44. sqlite3_close(db);
  45. return rc;
  46. }
  47. (*ppNewConn)->isSQLite3Borrowed = 0;
  48. return rc;
  49. }
  50. /* Configure a database connection */
  51. int xjd1_config(xjd1 *pConn, int op, ...){
  52. int rc = XJD1_UNKNOWN;
  53. va_list ap;
  54. va_start(ap, op);
  55. switch( op ){
  56. case XJD1_CONFIG_PARSERTRACE: {
  57. pConn->parserTrace = va_arg(ap, int);
  58. rc = XJD1_OK;
  59. break;
  60. }
  61. default: {
  62. break;
  63. }
  64. }
  65. va_end(ap);
  66. return rc;
  67. }
  68. /* Close a database connection. The close does not actually
  69. ** occur until all references to the connection also close. This
  70. ** means that any prepared statements must also be closed.
  71. */
  72. int xjd1_close(xjd1 *pConn){
  73. if( pConn==0 ) return XJD1_OK;
  74. pConn->isDying = 1;
  75. if( pConn->nRef>0 ) return XJD1_OK;
  76. xjd1ContextUnref(pConn->pContext);
  77. if(!pConn->isSQLite3Borrowed) sqlite3_close(pConn->db);
  78. xjd1StringClear(&pConn->errMsg);
  79. xjd1_free(pConn);
  80. return XJD1_OK;
  81. }
  82. /*
  83. ** Report the most recent error.
  84. */
  85. int xjd1_errcode(xjd1 *pConn){
  86. return pConn ? pConn->errCode : XJD1_ERROR;
  87. }
  88. const char *xjd1_errmsg(xjd1 *pConn){
  89. if( pConn==0 ) return "out of memory";
  90. return xjd1StringText(&pConn->errMsg);
  91. }
  92. const char *xjd1_errcode_name(xjd1 *pConn){
  93. const char *z = "???";
  94. switch( xjd1_errcode(pConn) ){
  95. case XJD1_OK: z = "OK"; break;
  96. case XJD1_ERROR: z = "ERROR"; break;
  97. case XJD1_MISUSE: z = "MISUSE"; break;
  98. case XJD1_NOMEM: z = "NOMEM"; break;
  99. case XJD1_UNKNOWN: z = "UNKNOWN"; break;
  100. case XJD1_SYNTAX: z = "SYNTAX"; break;
  101. case XJD1_ROW: z = "ROW"; break;
  102. case XJD1_DONE: z = "DONE"; break;
  103. }
  104. return z;
  105. }
  106. /* Remove a reference to a database connection. When the last reference
  107. ** is removed and the database is closed, then memory is deallocated.
  108. */
  109. PRIVATE void xjd1Unref(xjd1 *pConn){
  110. pConn->nRef--;
  111. if( pConn->nRef<=0 && pConn->isDying ) xjd1_close(pConn);
  112. }
  113. /*
  114. ** Change the error message and error code.
  115. */
  116. PRIVATE void xjd1Error(xjd1 *pConn, int errCode, const char *zFormat, ...){
  117. va_list ap;
  118. if( pConn==0 ) return;
  119. if( !pConn->appendErr ){
  120. xjd1StringTruncate(&pConn->errMsg);
  121. }else if( xjd1StringLen(&pConn->errMsg) ){
  122. xjd1StringAppend(&pConn->errMsg, "\n", 1);
  123. }
  124. pConn->errCode = errCode;
  125. if( zFormat ){
  126. va_start(ap, zFormat);
  127. xjd1StringVAppendF(&pConn->errMsg, zFormat, ap);
  128. va_end(ap);
  129. }else{
  130. const char *z;
  131. switch( errCode ){
  132. case XJD1_NOMEM: z = "out of memory"; break;
  133. default: z = xjd1_errcode_name(pConn); break;
  134. }
  135. xjd1StringAppend(&pConn->errMsg, z, -1);
  136. }
  137. }