@@ -76,37 +76,47 @@ node_db_mysql::Result::Result(MYSQL* connection) throw(node_db::Exception&)
76
76
previousRow(NULL ),
77
77
nextRow(NULL ) {
78
78
this ->result = mysql_store_result (this ->connection );
79
- if (result == NULL && mysql_field_count (this ->connection ) != 0 ) {
80
- throw node_db::Exception (mysql_error (this ->connection ));
81
- } else if (result != NULL ) {
82
- this ->empty = false ;
83
-
84
- MYSQL_FIELD* fields = mysql_fetch_fields (this ->result );
85
- if (fields == NULL ) {
86
- throw node_db::Exception (" Could not buffer columns" );
87
- }
88
79
89
- this ->totalColumns = mysql_num_fields (this ->result );
90
- if (this ->totalColumns > 0 ) {
91
- this ->columns = new Column*[this ->totalColumns ];
92
- if (this ->columns == NULL ) {
93
- throw node_db::Exception (" Could not allocate storage for columns" );
80
+ try {
81
+ if (result == NULL && mysql_field_count (this ->connection ) != 0 ) {
82
+ throw node_db::Exception (mysql_error (this ->connection ));
83
+ } else if (result != NULL ) {
84
+ this ->empty = false ;
85
+
86
+ MYSQL_FIELD* fields = mysql_fetch_fields (this ->result );
87
+ if (fields == NULL ) {
88
+ throw node_db::Exception (" Could not buffer columns" );
94
89
}
95
90
96
- for (uint16_t i = 0 ; i < this ->totalColumns ; i++) {
97
- this ->columns [i] = new Column (fields[i]);
98
- if (this ->columns [i] == NULL ) {
99
- delete [] this ->columns ;
100
- throw node_db::Exception (" Could not allocate storage for column" );
91
+ this ->totalColumns = mysql_num_fields (this ->result );
92
+ if (this ->totalColumns > 0 ) {
93
+ this ->columns = new Column*[this ->totalColumns ];
94
+ if (this ->columns == NULL ) {
95
+ throw node_db::Exception (" Could not allocate storage for columns" );
96
+ }
97
+
98
+ for (uint16_t i = 0 ; i < this ->totalColumns ; i++) {
99
+ this ->columns [i] = new Column (fields[i]);
100
+ if (this ->columns [i] == NULL ) {
101
+ this ->totalColumns = i;
102
+ throw node_db::Exception (" Could not allocate storage for column" );
103
+ }
101
104
}
102
105
}
103
- }
104
106
105
- this ->nextRow = this ->row ();
107
+ this ->nextRow = this ->row ();
108
+ }
109
+ } catch (...) {
110
+ this ->free ();
111
+ throw ;
106
112
}
107
113
}
108
114
109
115
node_db_mysql::Result::~Result () {
116
+ this ->free ();
117
+ }
118
+
119
+ void node_db_mysql::Result::free () throw() {
110
120
if (this ->columns != NULL ) {
111
121
for (uint16_t i = 0 ; i < this ->totalColumns ; i++) {
112
122
delete this ->columns [i];
0 commit comments