Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MariaDB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
90bbcc9b
Commit
90bbcc9b
authored
Dec 20, 2005
by
msvensson@neptunus.(none)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Store the each column from a multi column result set into a separate variable.
parent
b38a183d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
109 additions
and
14 deletions
+109
-14
client/mysqltest.c
client/mysqltest.c
+54
-14
mysql-test/r/mysqltest.result
mysql-test/r/mysqltest.result
+19
-0
mysql-test/t/mysqltest.test
mysql-test/t/mysqltest.test
+36
-0
No files found.
client/mysqltest.c
View file @
90bbcc9b
...
...
@@ -1137,26 +1137,50 @@ static void do_exec(struct st_query *query)
DBUG_VOID_RETURN
;
}
/*
Set variable from the result of a query
SYNOPSIS
var_query_set()
var variable to set from query
query start of query string to execute
query_end end of the query string to execute
int
var_query_set
(
VAR
*
v
,
const
char
*
p
,
const
char
**
p_end
)
DESCRIPTION
let @<var_name> = `<query>`
Execute the query and assign the first row of result to var as
a tab separated strings
Also assign each column of the result set to
variable "$<var_name>_<column_name>"
Thus the tab separated output can be read from $<var_name> and
and each individual column can be read as $<var_name>_<col_name>
*/
int
var_query_set
(
VAR
*
var
,
const
char
*
query
,
const
char
**
query_end
)
{
char
*
end
=
(
char
*
)((
p_end
&&
*
p_end
)
?
*
p_end
:
p
+
strlen
(
p
));
char
*
end
=
(
char
*
)((
query_end
&&
*
query_end
)
?
*
query_end
:
query
+
strlen
(
query
));
MYSQL_RES
*
res
;
MYSQL_ROW
row
;
MYSQL
*
mysql
=
&
cur_con
->
mysql
;
LINT_INIT
(
res
);
while
(
end
>
p
&&
*
end
!=
'`'
)
while
(
end
>
query
&&
*
end
!=
'`'
)
--
end
;
if
(
p
==
end
)
if
(
query
==
end
)
die
(
"Syntax error in query, missing '`'"
);
++
p
;
++
query
;
if
(
mysql_real_query
(
mysql
,
p
,
(
int
)(
end
-
p
))
||
if
(
mysql_real_query
(
mysql
,
query
,
(
int
)(
end
-
query
))
||
!
(
res
=
mysql_store_result
(
mysql
)))
{
*
end
=
0
;
die
(
"Error running query '%s': %s"
,
p
,
mysql_error
(
mysql
));
die
(
"Error running query '%s': %d: %s"
,
query
,
mysql_errno
(
mysql
)
,
mysql_error
(
mysql
));
}
if
((
row
=
mysql_fetch_row
(
res
))
&&
row
[
0
])
...
...
@@ -1169,21 +1193,39 @@ int var_query_set(VAR* v, const char *p, const char** p_end)
uint
i
;
ulong
*
lengths
;
char
*
end
;
MYSQL_FIELD
*
fields
=
mysql_fetch_fields
(
res
);
init_dynamic_string
(
&
result
,
""
,
16384
,
65536
);
lengths
=
mysql_fetch_lengths
(
res
);
for
(
i
=
0
;
i
<
mysql_num_fields
(
res
);
i
++
)
{
if
(
row
[
0
])
{
/* Add to <var_name>_<col_name> */
uint
j
;
char
var_col_name
[
MAX_VAR_NAME
];
uint
length
=
snprintf
(
var_col_name
,
MAX_VAR_NAME
,
"$%s_%s"
,
var
->
name
,
fields
[
i
].
name
);
/* Convert characters not allowed in variable names to '_' */
for
(
j
=
1
;
j
<
length
;
j
++
)
{
if
(
!
my_isvar
(
charset_info
,
var_col_name
[
j
]))
var_col_name
[
j
]
=
'_'
;
}
var_set
(
var_col_name
,
var_col_name
+
length
,
row
[
i
],
row
[
i
]
+
lengths
[
i
]);
/* Add column to tab separated string */
dynstr_append_mem
(
&
result
,
row
[
i
],
lengths
[
i
]);
}
dynstr_append_mem
(
&
result
,
"
\t
"
,
1
);
}
end
=
result
.
str
+
result
.
length
-
1
;
eval_expr
(
v
,
result
.
str
,
(
const
char
**
)
&
end
);
eval_expr
(
v
ar
,
result
.
str
,
(
const
char
**
)
&
end
);
dynstr_free
(
&
result
);
}
else
eval_expr
(
v
,
""
,
0
);
eval_expr
(
v
ar
,
""
,
0
);
mysql_free_result
(
res
);
return
0
;
...
...
@@ -4129,12 +4171,10 @@ static VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
if
(
!
(
tmp_var
->
str_val
=
my_malloc
(
val_alloc_len
+
1
,
MYF
(
MY_WME
))))
die
(
"Out of memory"
);
memcpy
(
tmp_var
->
name
,
name
,
name_len
);
if
(
name
)
strmake
(
tmp_var
->
name
,
name
,
name_len
);
if
(
val
)
{
memcpy
(
tmp_var
->
str_val
,
val
,
val_len
);
tmp_var
->
str_val
[
val_len
]
=
0
;
}
strmake
(
tmp_var
->
str_val
,
val
,
val_len
);
tmp_var
->
name_len
=
name_len
;
tmp_var
->
str_val_len
=
val_len
;
tmp_var
->
alloced_len
=
val_alloc_len
;
...
...
mysql-test/r/mysqltest.result
View file @
90bbcc9b
...
...
@@ -222,6 +222,25 @@ mysqltest: At line 1: Missing arguments to let
mysqltest: At line 1: Missing variable name in let
mysqltest: At line 1: Variable name in =hi does not start with '$'
mysqltest: At line 1: Missing assignment operator in let
var1
hi 1 hi there
hi
1
hi there
var2
2
var2 again
2
2
var3 two columns with same name
1 2 3
2
2
3
mysqltest: At line 1: Missing file name in source
mysqltest: At line 1: Could not open file ./non_existingFile
mysqltest: In included file "./var/tmp/recursive.sql": At line 1: Source directives are nesting too deep
...
...
mysql-test/t/mysqltest.test
View file @
90bbcc9b
...
...
@@ -539,6 +539,42 @@ echo $novar1;
--
error
1
--
exec
echo
"let hi;"
|
$MYSQL_TEST
2
>&
1
# ----------------------------------------------------------------------------
# Test to assign let from query
# let $<var_name>=`<query>`;
# ----------------------------------------------------------------------------
echo
var1
;
let
$var1
=
`select "hi" as "Col", 1 as "Column1", "hi there" as Col3`
;
echo
$var1
;
echo
$var1_Col
;
echo
$var1_Column1
;
echo
$var1_Col3
;
echo
var2
;
let
$var2
=
`select 2 as "Column num 2"`
;
echo
$var2
;
echo
$var2_Column
num
2
;
echo
$var2_Column
;
echo
var2
again
;
let
$var2
=
`select 2 as "Column num 2"`
;
echo
$var2
;
echo
$var2_Column
num
2
;
echo
$var2_Column_num_2
;
echo
$var2_Column
;
echo
var3
two
columns
with
same
name
;
let
$var3
=
`select 1 as "Col", 2 as "Col", 3 as "var3"`
;
echo
$var3
;
echo
$var3_Col
;
echo
$var3_Col
;
echo
$var3_var3
;
#echo failing query in let;
#--error 1
#--exec echo "let $var2= `failing query;`" | $MYSQL_TEST 2>&1
# ----------------------------------------------------------------------------
# Test source command
# ----------------------------------------------------------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment