示例#1
0
void Statement::Prepare() {
	if (! _db.IsConnected()) {
		throw DatabaseException("Error in Statement::Prepare", 0, "----", "Database is not connected");
	}

	_numberAffectedRows = 0;

	if ((_stmt = mysql_stmt_init(_db._db)) == NULL) {
		throw DatabaseException(_db._db, "Error in Statement::Prepare during initialize");
	}

	if (mysql_stmt_prepare(_stmt, _sqlStatement.c_str(), _sqlStatement.length()) != 0) {
		mysql_stmt_close(_stmt);
		throw DatabaseException(_stmt, "Error in Statement::Prepare during prepare");
	}

	_bind = NULL;
	unsigned long parameterCount = ParameterCount();
	if (parameterCount > 0) {
		_bind = (MYSQL_BIND *) malloc(sizeof(MYSQL_BIND) * parameterCount);
		memset(_bind, 0, sizeof(MYSQL_BIND) * parameterCount);
	}

	_numberResultColumns = 0;
	_hasBlobField = false;

	MYSQL_RES *metaData;
	if ((metaData = mysql_stmt_result_metadata(_stmt)) == NULL) {
		return;
	}

	_resultBind = NULL;
	_numberResultColumns = mysql_num_fields(metaData);
	if (_numberResultColumns > 0) {
		_resultBind = (MYSQL_BIND *) malloc(sizeof(MYSQL_BIND) * _numberResultColumns);
		memset(_resultBind, 0, sizeof(MYSQL_BIND) * _numberResultColumns);
	}

	int fieldPos = 0;
	MYSQL_FIELD *field;
	while ((field = mysql_fetch_field(metaData)) != NULL) {
		ParamBuffer *buffer = NULL;

		if ((field->type == MYSQL_TYPE_VAR_STRING) || 
		    (field->type == MYSQL_TYPE_STRING) ||
		    (field->type == MYSQL_TYPE_DECIMAL) ||
		    (field->type == MYSQL_TYPE_BIT) ||
		    (field->type == MYSQL_TYPE_VARCHAR)) {
			std::string str;
			buffer = new ParamBuffer(str, field->length);
			_resultBind[fieldPos].buffer_type = field->type;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
		}
		else if (field->type == MYSQL_TYPE_TINY) {
			if ((field->flags & UNSIGNED_FLAG) != 0) {
				buffer = new ParamBuffer((const unsigned char) 0);
			} else {
				buffer = new ParamBuffer((const char) 0);
			}
			_resultBind[fieldPos].buffer_type = MYSQL_TYPE_TINY;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
			_resultBind[fieldPos].is_unsigned = buffer->IsUnsigned();
		}
		else if ((field->type == MYSQL_TYPE_SHORT) ||
			 (field->type == MYSQL_TYPE_YEAR)) {
			if ((field->flags & UNSIGNED_FLAG) != 0) {
				buffer = new ParamBuffer((const unsigned short int) 0);
			} else {
				buffer = new ParamBuffer((const short int) 0);
			}
			_resultBind[fieldPos].buffer_type = MYSQL_TYPE_SHORT;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
			_resultBind[fieldPos].is_unsigned = buffer->IsUnsigned();
		}
		else if ((field->type == MYSQL_TYPE_LONG) ||
			 (field->type == MYSQL_TYPE_INT24)) {
			if ((field->flags & UNSIGNED_FLAG) != 0) {
				buffer = new ParamBuffer((const unsigned int) 0);
			} else {
				buffer = new ParamBuffer((const int) 0);
			}
			_resultBind[fieldPos].buffer_type = field->type;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
			_resultBind[fieldPos].is_unsigned = buffer->IsUnsigned();
		}
		else if (field->type == MYSQL_TYPE_FLOAT) {
			_resultBind[fieldPos].buffer_type = field->type;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
			_resultBind[fieldPos].is_unsigned = buffer->IsUnsigned();
		}
		else if (field->type == MYSQL_TYPE_DOUBLE) {
			_resultBind[fieldPos].buffer_type = field->type;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
			_resultBind[fieldPos].is_unsigned = buffer->IsUnsigned();
		}
		else if ((field->type == MYSQL_TYPE_TIMESTAMP) ||
			 (field->type == MYSQL_TYPE_DATE) ||
			 (field->type == MYSQL_TYPE_TIME) ||
			 (field->type == MYSQL_TYPE_DATETIME)) {
			Julian time;
			buffer = new ParamBuffer(time);
			_resultBind[fieldPos].buffer_type = field->type;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
		}
		else if ((field->type == MYSQL_TYPE_BLOB) ||
			 (field->type == MYSQL_TYPE_TINY_BLOB) ||
			 (field->type == MYSQL_TYPE_MEDIUM_BLOB) ||
			 (field->type == MYSQL_TYPE_LONG_BLOB)) {
			_hasBlobField = true;
			Binary data(field->length);
			buffer = new ParamBuffer(data);
			_resultBind[fieldPos].buffer_type = field->type;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
			_resultBind[fieldPos].error = buffer->Error();
		}

		if (buffer != NULL) {
			_resultParams.push_back(buffer);	
		}
		fieldPos++;
	}

	if (_resultParams.size() != _numberResultColumns) {
		mysql_free_result(metaData);
		mysql_stmt_close(_stmt);
		throw DatabaseException("Error in Statement::Prepare", 0, "----", "was not able to bind all parameters");
	}

	if (_hasBlobField) {
		my_bool setMax = 1;
		mysql_stmt_attr_set(_stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &setMax);
	}

	if (_numberResultColumns > 0) {
		if (mysql_stmt_bind_result(_stmt, _resultBind) != 0) {
			mysql_free_result(metaData);
			mysql_stmt_close(_stmt);
			throw DatabaseException(_stmt, "Error in Statement::Prepare while binding results");
		}
	}

	mysql_free_result(metaData);
}
示例#2
0
void Statement::Prepare() {
	_numberAffectedRows = 0;

	if ((_stmt = mysql_stmt_init(_dbcopy)) == NULL) {
		throw DatabaseException(_dbcopy, "Error in Statement::Prepare during initialize");
	}

	if (mysql_stmt_prepare(_stmt, _sqlStatement.c_str(), _sqlStatement.length()) != 0) {
		mysql_stmt_close(_stmt);
		throw DatabaseException(_stmt, "Error in Statement::Prepare during prepare");
	}

	_bind = NULL;
	unsigned long parameterCount = ParameterCount();
	if (parameterCount > 0) {
		_bind = (MYSQL_BIND *) malloc(sizeof(MYSQL_BIND) * parameterCount);
		memset(_bind, 0, sizeof(MYSQL_BIND) * parameterCount);
	}

	_numberResultColumns = 0;
	_hasBlobField = false;

	MYSQL_RES *metaData;
	if ((metaData = mysql_stmt_result_metadata(_stmt)) == NULL) {
		return;
	}

	_resultBind = NULL;
	_numberResultColumns = mysql_num_fields(metaData);
	if (_numberResultColumns > 0) {
		_resultBind = (MYSQL_BIND *) malloc(sizeof(MYSQL_BIND) * _numberResultColumns);
		memset(_resultBind, 0, sizeof(MYSQL_BIND) * _numberResultColumns);
	}

	int fieldPos = 0;
	MYSQL_FIELD *field;
	while ((field = mysql_fetch_field(metaData)) != NULL) {
		ParamBuffer *buffer = NULL;
		if (field->type == MYSQL_TYPE_VAR_STRING) {
			std::string str;
			buffer = new ParamBuffer(str, field->length);
			_resultBind[fieldPos].buffer_type = MYSQL_TYPE_VAR_STRING;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
		}
		else if (field->type == MYSQL_TYPE_TINY) {
			buffer = new ParamBuffer((char) 0);
			_resultBind[fieldPos].buffer_type = MYSQL_TYPE_TINY;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].is_null = buffer->IsNull();
		}
		else if (field->type == MYSQL_TYPE_SHORT) {
			buffer = new ParamBuffer((short int) 0);
			_resultBind[fieldPos].buffer_type = MYSQL_TYPE_SHORT;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].is_null = buffer->IsNull();
		}
		else if (field->type == MYSQL_TYPE_TIMESTAMP) {
			MYSQL_TIME time;
			buffer = new ParamBuffer(time);
			_resultBind[fieldPos].buffer_type = MYSQL_TYPE_TIMESTAMP;	
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].is_null = buffer->IsNull();
		}
		else if (field->type == MYSQL_TYPE_BLOB) {
			_hasBlobField = true;
			Binary data(field->length);
			buffer = new ParamBuffer(data);
			_resultBind[fieldPos].buffer_type = MYSQL_TYPE_BLOB;
			_resultBind[fieldPos].buffer = buffer->Buffer();
			_resultBind[fieldPos].buffer_length = buffer->BufferSize();
			_resultBind[fieldPos].length = buffer->BufferLength();
			_resultBind[fieldPos].is_null = buffer->IsNull();
		}

		if (buffer != NULL) {
			_resultParams.push_back(buffer);	
		}
		fieldPos++;
	}

	if (_resultParams.size() != _numberResultColumns) {
		mysql_free_result(metaData);
		mysql_stmt_close(_stmt);
		throw DatabaseException("Error in Statement::Prepare", 0, "----", "was not able to bind all parameters");
	}

	if (_hasBlobField) {
		my_bool setMax = 1;
		mysql_stmt_attr_set(_stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &setMax);
	}

	if (_numberResultColumns > 0) {
		if (mysql_stmt_bind_result(_stmt, _resultBind) != 0) {
			mysql_free_result(metaData);
			mysql_stmt_close(_stmt);
			throw DatabaseException(_stmt, "Error in Statement::Prepare while binding results");
		}
	}

	mysql_free_result(metaData);
}