NCBI C++ ToolKit
lang_cmd.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: lang_cmd.cpp 102152 2024-04-09 13:58:47Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Anton Butanayev
27  *
28  * File Description
29  * Driver for MySQL server
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
35 #include <dbapi/error_codes.hpp>
36 
37 
38 #define NCBI_USE_ERRCODE_X Dbapi_Mysql_Cmds
39 
40 #undef NCBI_DATABASE_THROW
41 #define NCBI_DATABASE_THROW(ex_class, message, err_code, severity) \
42  NCBI_DATABASE_THROW_ANNOTATED(ex_class, message, err_code, severity, \
43  GetDbgInfo(), GetConnection(), GetLastParams())
44 // No use of NCBI_DATABASE_RETHROW or DATABASE_DRIVER_*_EX here.
45 
47 
48 
50  const string& lang_query) :
51  impl::CBaseCmd(conn, lang_query),
52  m_Connect(&conn),
53  m_DbgInfo(new TDbgInfo(conn.GetDbgInfo())),
54  m_HasMoreResults(false),
55  m_IsActive(false)
56 {
57  if (conn.m_ActiveCmd) {
58  conn.m_ActiveCmd->m_IsActive = false;
59  }
60  conn.m_ActiveCmd = this;
61 }
62 
63 
65 {
66  if (mysql_real_query
67  (&m_Connect->m_MySQL, GetQuery().data(), GetQuery().length()) != 0) {
68  DATABASE_DRIVER_WARNING( "Failed: mysql_real_query", 800003 );
69  }
71 
72  my_ulonglong nof_Rows = mysql_affected_rows(&this->m_Connect->m_MySQL);
73  // There is not too much sence in comparing unsigned value with -1.
74  // m_HasMoreResults = nof_Rows == -1 || nof_Rows > 0;
75  m_HasMoreResults = nof_Rows > 0;
76  return true;
77 }
78 
79 
81 {
82  return false;
83 }
84 
85 
87 {
88  m_HasMoreResults = false;
91 }
92 
93 
95 {
96  return m_HasMoreResults;
97 }
98 
99 
101 {
102  if(mysql_errno(&m_Connect->m_MySQL) == 0) {
103  return false;
104  }
105  return true;
106 }
107 
109 {
110  try {
111  DetachInterface();
112 
113  GetConnection().DropCmd(*this);
114 
115  Cancel();
116  }
118  if (m_IsActive) {
120  }
121 }
122 
124 {
125  return static_cast<int>( mysql_insert_id(&this->m_Connect->m_MySQL) );
126 }
127 
129 {
130  return static_cast<int>( mysql_affected_rows(&this->m_Connect->m_MySQL) );
131 }
132 
133 string CMySQL_LangCmd::EscapeString(const char* str, unsigned long len)
134 {
135  AutoArray<char> buff(len*2 + 1);
136  mysql_real_escape_string(&this->m_Connect->m_MySQL, buff.get(), str, len);
137  return buff.get();
138 }
139 
140 
142 
143 
CMySQL_LangCmd * m_ActiveCmd
Definition: interfaces.hpp:144
CMySQL_LangCmd(CMySQL_Connection &conn, const string &lang_query)
Definition: lang_cmd.cpp:49
string EscapeString(const char *str, unsigned long len)
Definition: lang_cmd.cpp:133
CMySQL_Connection & GetConnection(void)
Definition: interfaces.hpp:192
virtual bool Cancel()
Cancel the command execution.
Definition: lang_cmd.cpp:80
virtual int RowCount() const
Get the number of rows affected by the command Special case: negative on error or if there is no way ...
Definition: lang_cmd.cpp:128
virtual bool HasFailed() const
Definition: lang_cmd.cpp:100
int LastInsertId() const
Definition: lang_cmd.cpp:123
virtual bool Send()
Send command to the server.
Definition: lang_cmd.cpp:64
virtual ~CMySQL_LangCmd()
Definition: lang_cmd.cpp:108
virtual CDB_Result * Result()
Get result set.
Definition: lang_cmd.cpp:86
virtual bool HasMoreResults() const
Definition: lang_cmd.cpp:94
CMySQL_Connection * m_Connect
Definition: interfaces.hpp:204
const string & GetQuery(void) const
const CDB_Params & GetBindParamsImpl(void) const
void DetachInterface(void)
static CDB_Result * Create_Result(CResult &result)
void DropCmd(impl::CCommand &cmd)
void LockBinding(void)
Definition: parameters.hpp:87
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
#define false
Definition: bool.h:36
static const char * str(char *buf, int n)
Definition: stats.c:84
char data[12]
Definition: iconv.c:80
element_type * get(void) const
Get pointer.
Definition: ncbimisc.hpp:581
#define NULL
Definition: ncbistd.hpp:225
#define DATABASE_DRIVER_WARNING(message, err_code)
Definition: exception.hpp:746
#define NCBI_CURRENT_FUNCTION
Get current function name.
Definition: ncbidiag.hpp:142
#define NCBI_CATCH_ALL_X(err_subcode, message)
Definition: ncbiexpt.hpp:619
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
Definition of all error codes used in dbapi libraries (dbapi_driver.lib and others).
int len
#define GetDbgInfo()
Definition: lang_cmd.cpp:53
#define _ASSERT
Modified on Sun Apr 21 03:43:56 2024 by modify_doxy.py rev. 669887