20 #include "MysqlPreparedStatement.h"
21 #include "KDbConnection.h"
27 MysqlPreparedStatement::MysqlPreparedStatement(MysqlConnectionInternal* conn)
29 , MysqlConnectionInternal(conn->connection)
30 #ifdef KDB_USE_MYSQL_STMT
34 , m_resetRequired(false)
44 bool MysqlPreparedStatement::init()
46 #ifdef KDB_USE_MYSQL_STMT
47 m_statement = mysql_stmt_init(mysql);
52 res = mysql_stmt_prepare(m_statement,
53 (
const char*)m_tempStatementString, m_tempStatementString.length());
59 m_realParamCount = mysql_stmt_param_count(m_statement);
60 if (m_realParamCount <= 0) {
64 m_mysqlBind =
new MYSQL_BIND[ m_realParamCount ];
65 memset(m_mysqlBind, 0,
sizeof(MYSQL_BIND)*m_realParamCount);
70 MysqlPreparedStatement::~MysqlPreparedStatement()
75 void MysqlPreparedStatement::done()
77 #ifdef KDB_USE_MYSQL_STMT
80 mysql_stmt_close(m_statement);
94 #ifdef KDB_USE_MYSQL_STMT
96 m_mysqlBind[arg].buffer_type = MYSQL_TYPE_NULL; \
97 m_mysqlBind[arg].buffer = 0; \
98 m_mysqlBind[arg].buffer_length = 0; \
99 m_mysqlBind[arg].is_null = &dummyNull; \
100 m_mysqlBind[arg].length = &str_length; }
102 bool MysqlPreparedStatement::bindValue(
KDbField *field,
const QVariant& value,
int arg)
112 m_stringBuffer[ 1024 ]; ? ? ?
113 char *str = qstrncpy(m_stringBuffer, (
const char*)value.
toString().
toUtf8(), 1024);
114 m_mysqlBind[arg].buffer_type = MYSQL_TYPE_STRING;
115 m_mysqlBind[arg].buffer = m_stringBuffer;
116 m_mysqlBind[arg].is_null = (my_bool*)0;
117 m_mysqlBind[arg].buffer_length = 1024;
118 m_mysqlBind[arg].length = &str_length;
122 switch (field->
type()) {
128 const int intValue = value.
toInt(&ok);
131 m_mysqlBind[arg].buffer_type = MYSQL_TYPE_TINY;
133 m_mysqlBind[arg].buffer_type = MYSQL_TYPE_SHORT;
135 m_mysqlBind[arg].buffer_type = MYSQL_TYPE_LONG;
137 m_mysqlBind[arg].is_null = (my_bool*)0;
138 m_mysqlBind[arg].length = 0;
140 res = sqlite3_bind_int(prepared_st_handle, arg, intValue);
141 if (SQLITE_OK != res) {
151 res = sqlite3_bind_double(prepared_st_handle, arg, value.
toDouble());
152 if (SQLITE_OK != res) {
162 res = sqlite3_bind_int64(prepared_st_handle, arg, value);
163 if (SQLITE_OK != res) {
168 res = sqlite3_bind_null(prepared_st_handle, arg);
169 if (SQLITE_OK != res) {
177 res = sqlite3_bind_text(prepared_st_handle, arg,
179 1, SQLITE_TRANSIENT );
180 if (SQLITE_OK != res) {
186 res = sqlite3_bind_text(prepared_st_handle, arg,
188 sizeof(
"HH:MM:SS"), SQLITE_TRANSIENT );
189 if (SQLITE_OK != res) {
195 res = sqlite3_bind_text(prepared_st_handle, arg,
197 sizeof(
"YYYY-MM-DD"), SQLITE_TRANSIENT );
198 if (SQLITE_OK != res) {
203 case KDbField::DateTime:
204 res = sqlite3_bind_text(prepared_st_handle, arg,
206 sizeof(
"YYYY-MM-DDTHH:MM:SS"), SQLITE_TRANSIENT );
207 if (SQLITE_OK != res) {
214 res = sqlite3_bind_blob(prepared_st_handle, arg,
215 (
const char*)byteArray, byteArray.size(), SQLITE_TRANSIENT );
216 if (SQLITE_OK != res) {
223 mysqlWarning() <<
"unsupported field type:"
224 << field->
type() <<
"- NULL value bound to column #" << arg;
225 res = sqlite3_bind_null(prepared_st_handle, arg);
226 if (SQLITE_OK != res) {
240 Q_UNUSED(selectFieldList);
242 #ifdef KDB_USE_MYSQL_STMT
243 if (!m_statement || m_realParamCount <= 0)
245 if (mysql_stmt_errno(m_statement) == CR_SERVER_LOST) {
255 if (m_resetRequired) {
256 mysql_stmt_reset(m_statement);
257 res = sqlite3_reset(prepared_st_handle);
258 if (SQLITE_OK != res) {
262 m_resetRequired =
false;
266 bool dummyNull =
true;
267 unsigned long str_length;
272 if (m_type == SelectStatement)
273 fieldList = m_whereFields;
274 else if (m_type == InsertStatement)
275 fieldList = m_fields->fields();
281 itFields != fieldList->
constEnd() && arg < m_realParamCount; ++it, ++itFields, par++) {
282 if (!bindValue(*itFields, it == parameters.
constEnd() ?
QVariant() : *it, par))
287 res = sqlite3_step(prepared_st_handle);
288 m_resetRequired =
true;
289 if (m_type == InsertStatement && res == SQLITE_DONE) {
292 if (m_type == SelectStatement) {
298 m_resetRequired =
true;
300 const int missingValues = insertFieldList->
fieldCount() - parameters.
count();
302 if (missingValues > 0) {
304 for (
int i = 0; i < missingValues; i++) {
308 result = connection->insertRecord(insertFieldList, myParameters);
311 #endif // !KDB_USE_MYSQL_STMT