public BindVarQueryStatement(MySQLConnection connection, bool closeConnection, string query, MySQLParameterCollection parameterCollection, uint?fetchSize, CursorType cursorType) : base(connection, query, parameterCollection, fetchSize, cursorType) { _connection = connection; var fields = new List <MYSQL_FIELD>(); using (var resultMetadata = new NativeResultMetadata(Stmt)) { for (var i = 0; i < resultMetadata.mysql_num_fields(); i++) { IntPtr fieldPtr = resultMetadata.mysql_fetch_field_direct((uint)i); var field = Marshal.PtrToStructure <MYSQL_FIELD>(fieldPtr); fields.Add(field); } } if (Stmt.mysql_stmt_execute() != 0) { throw new MySqlException(Stmt); } _fields = fields.ToArray(); _closeConnection = closeConnection; IsClosed = false; _rowColumns = new MYSQL_BIND[_fields.Length]; for (var index = 0; index < _fields.Length; index++) { var fieldMetadata = _fields[index]; _rowColumns[index] = new MYSQL_BIND(); //else if (fieldMetadata.Type == enum_field_types.MYSQL_TYPE_NULL && parameters != null && parameters.Count > index)//Caso select distinct donde mysql_stmt_bind_param3 mapea erroneamente a NULL //{ // // TODO: case needs deep review // fieldMetadata.Type = PreparedStatement.DbtoMysqlType(parameters[index].DbType); //} _rowColumns[index].InitForBind(fieldMetadata, Stmt._nativeConnection); } sbyte code = Stmt.mysql_stmt_bind_result(_rowColumns); if (code != 0) { throw new MySqlException(Stmt); } }
public BindVarStatement(MySQLConnection connection, string query, MySQLParameterCollection parameterCollection, uint?fetchSize, CursorType cursorType) { Stmt = connection.NativeConnection.CreateStatement(); if (fetchSize != null) { var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(fetchSize.Value)); Marshal.StructureToPtr(fetchSize.Value, ptr, false); var code1 = Stmt.mysql_stmt_attr_set(StmtAttrTypes.STMT_ATTR_PREFETCH_ROWS, ptr); Marshal.FreeHGlobal(ptr); if (code1 != 0) { throw new MySqlException(Stmt); } } if (cursorType != CursorType.NoCursor) { var value = (enum_cursor_type)cursorType; var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(value)); Marshal.StructureToPtr(value, ptr, false); var code1 = Stmt.mysql_stmt_attr_set(StmtAttrTypes.STMT_ATTR_CURSOR_TYPE, ptr); Marshal.FreeHGlobal(ptr); if (code1 != 0) { throw new MySqlException(Stmt); } } // execute prepare to call mysql_stmt_param_count if (Stmt.mysql_stmt_prepare(query, query.Length) != 0) { throw new MySqlException(Stmt); } // This function will not deliver a valid result until mysql_stmt_prepare() was called. var paramCount = (int)Stmt.mysql_stmt_param_count(); if (paramCount != parameterCollection.Count) { throw new MySqlException($"Invalid parameters, stmt parameters:{paramCount} parameters count:{parameterCollection.Count}"); } // alloc & set binds _bindParams = new MYSQL_BIND[parameterCollection.Count]; for (var i = 0; i < parameterCollection.Count; i++) { _bindParams[i] = new MYSQL_BIND(); var param = parameterCollection[i]; var bindInput = param.GetBindInput(); if (bindInput != null) { _bindParams[i].buffer_type = bindInput.BufferType; if (bindInput.Buffer == null) { _bindParams[i].ResetBuffer(); } else { _bindParams[i].SetBuffer(bindInput.Buffer); } if (bindInput.IsNull.HasValue) { _bindParams[i].SetIsNull(bindInput.IsNull.Value); } else { _bindParams[i].ResetIsNull(); } if (bindInput.Length.HasValue) { _bindParams[i].SetLength((uint)bindInput.Length.Value); } else { _bindParams[i].ResetLength(); } _bindParams[i].is_unsigned = (byte)(bindInput.IsUnsigned ? 1 : 0); } else { throw new NotSupportedException(); } } // link binds to stmt if (Stmt.mysql_stmt_bind_param(_bindParams) != 0) { throw new MySqlException(Stmt); } }