@@ -3,10 +3,6 @@ module mysql
3
3
import orm
4
4
import time
5
5
6
- type Prims = f32 | f64 | i16 | i64 | i8 | int | string | u16 | u32 | u64 | u8
7
-
8
- // sql expr
9
-
10
6
// @select is used internally by V's ORM for processing `SELECT ` queries
11
7
pub fn (db Connection) @select (config orm.SelectConfig, data orm.QueryData, where orm.QueryData) ! [][]orm.Primitive {
12
8
query := orm.orm_select_gen (config, '`' , false , '?' , 0 , where)
@@ -28,8 +24,8 @@ pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, wher
28
24
mut dataptr := []& u8 {}
29
25
30
26
for i in 0 .. num_fields {
31
- f := unsafe { fields[i] }
32
- match unsafe { FieldType (f .@type) } {
27
+ field := unsafe { fields[i] }
28
+ match unsafe { FieldType (field .@type) } {
33
29
.type_tiny {
34
30
dataptr << unsafe { malloc (1 ) }
35
31
}
@@ -51,38 +47,40 @@ pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, wher
51
47
.type_time, .type_date, .type_datetime, .type_time2 , .type_datetime2 {
52
48
dataptr << unsafe { malloc (sizeof (C.MYSQL_TIME)) }
53
49
}
54
- .type_string, .type_blob {
55
- dataptr << unsafe { malloc (512 ) }
56
- }
57
- .type_var_string {
58
- dataptr << unsafe { malloc (2 ) }
50
+ .type_string, .type_var_string, .type_blob, .type_tiny_blob, .type_medium_blob,
51
+ .type_long_blob {
52
+ // Memory will be allocated later dynamically depending on the length of the value.
53
+ dataptr << & u8 (0 )
59
54
}
60
55
else {
61
- return error ('\' ${unsafe { FieldType(f .@type) } }\' is not yet implemented. Please create a new issue at https://github.com/vlang/v/issues/new' )
56
+ return error ('\' ${unsafe { FieldType(field .@type) } }\' is not yet implemented. Please create a new issue at https://github.com/vlang/v/issues/new' )
62
57
}
63
58
}
64
59
}
65
60
66
- lens := []u32 {len: int (num_fields), init: 0 }
67
- stmt.bind_res (fields, dataptr, lens , num_fields)
61
+ lengths := []u32 {len: int (num_fields), init: 0 }
62
+ stmt.bind_res (fields, dataptr, lengths , num_fields)
68
63
69
- mut row := 0
70
64
mut types := config.types.clone ()
71
65
mut field_types := []FieldType{}
72
66
if config.is_count {
73
67
types = [orm.type_idx['u64' ]]
74
68
}
69
+ // Map stores column indexes and their binds in order to extract values
70
+ // for these columns separately, with individual memory allocation for each value.
71
+ mut string_binds_map := map [int ]C.MYSQL_BIND{}
75
72
76
73
for i, mut mysql_bind in stmt.res {
77
- f := unsafe { fields[i] }
78
- field_types << unsafe { FieldType (f.@type) }
74
+ field := unsafe { fields[i] }
75
+ field_type := unsafe { FieldType (field.@type) }
76
+ field_types << field_type
77
+
79
78
match types[i] {
80
79
orm.type_string {
81
- mysql_bind.buffer_type = C.MYSQL_TYPE_BLOB
82
- mysql_bind.buffer_length = FieldType.type_blob.get_len ()
80
+ string_binds_map[i] = mysql_bind
83
81
}
84
82
orm.time {
85
- match unsafe { FieldType (f.@type) } {
83
+ match field_type {
86
84
.type_long {
87
85
mysql_bind.buffer_type = C.MYSQL_TYPE_LONG
88
86
}
@@ -92,7 +90,7 @@ pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, wher
92
90
}
93
91
.type_string, .type_blob {}
94
92
else {
95
- return error ('Unknown type ${f .@type} ' )
93
+ return error ('Unknown type ${field .@type} ' )
96
94
}
97
95
}
98
96
}
@@ -102,13 +100,23 @@ pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, wher
102
100
103
101
stmt.bind_result_buffer ()!
104
102
stmt.store_result ()!
103
+
105
104
for {
106
105
status = stmt.fetch_stmt ()!
107
106
108
107
if status == 1 || status == 100 {
109
108
break
110
109
}
111
- row++
110
+
111
+ for index, mut bind in string_binds_map {
112
+ string_length := lengths[index] + 1
113
+ dataptr[index] = unsafe { malloc (string_length) }
114
+ bind.buffer = dataptr[index]
115
+ bind.buffer_length = string_length
116
+ bind.length = unsafe { nil }
117
+
118
+ stmt.fetch_column (bind, index)!
119
+ }
112
120
113
121
data_list := buffer_to_primitive (dataptr, types, field_types)!
114
122
ret << data_list
0 commit comments