Skip to content

Commit 589917f

Browse files
committed
Add procedure RDB$SQL.PARSE_UNQUALIFIED_NAMES.
1 parent 7347827 commit 589917f

File tree

3 files changed

+113
-7
lines changed

3 files changed

+113
-7
lines changed

doc/sql.extensions/README.sql_package.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ Output parameters:
2525
- `KEY_LENGTH` type `INTEGER` - key length for the record source
2626
- `ACCESS_PATH` type `RDB$DESCRIPTION NOT NULL` - friendly plan description
2727

28-
```
28+
```sql
2929
select *
3030
from rdb$sql.explain('select * from employee where emp_no = ?');
3131
```
3232

33-
```
33+
```sql
3434
select *
3535
from rdb$sql.explain(q'{
3636
select *
@@ -43,5 +43,21 @@ select *
4343
}');
4444
```
4545

46+
## Procedure `PARSE_UNQUALIFIED_NAMES`
47+
48+
`RDB$SQL.PARSE_UNQUALIFIED_NAMES` is a selectable procedure that parses a list of unqualified SQL names and returns
49+
one row for each name. The input must follow parse rules for names and the output of unquoted names are uppercased.
50+
51+
```sql
52+
select *
53+
from rdb$sql.parse_unqualified_names('schema1, schema2, "schema3", "schema 4", "schema ""5"""');
54+
55+
-- SCHEMA1
56+
-- SCHEMA2
57+
-- schema3
58+
-- "schema 4"
59+
-- "schema "5"
60+
```
61+
4662
# Authors
4763
- Adriano dos Santos Fernandes

src/jrd/sys-packages/SqlPackage.cpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ IExternalResultSet* SqlPackage::explainProcedure(ThrowStatusExceptionWrapper* st
4242
}
4343

4444

45-
//--------------------------------------
46-
47-
4845
SqlPackage::ExplainResultSet::ExplainResultSet(ThrowStatusExceptionWrapper* status,
4946
IExternalContext* context, const ExplainInput::Type* in, ExplainOutput::Type* aOut)
5047
: out(aOut)
@@ -171,6 +168,42 @@ FB_BOOLEAN SqlPackage::ExplainResultSet::fetch(ThrowStatusExceptionWrapper* stat
171168
//--------------------------------------
172169

173170

171+
IExternalResultSet* SqlPackage::parseUnqualifiedNamesProcedure(ThrowStatusExceptionWrapper* status,
172+
IExternalContext* context, const ParseUnqualifiedNamesInput::Type* in, ParseUnqualifiedNamesOutput::Type* out)
173+
{
174+
return FB_NEW ParseUnqualifiedNamesResultSet(status, context, in, out);
175+
}
176+
177+
178+
SqlPackage::ParseUnqualifiedNamesResultSet::ParseUnqualifiedNamesResultSet(ThrowStatusExceptionWrapper* status,
179+
IExternalContext* context, const ParseUnqualifiedNamesInput::Type* in, ParseUnqualifiedNamesOutput::Type* aOut)
180+
: out(aOut)
181+
{
182+
if (!in->namesNull)
183+
{
184+
const string namesStr(in->names.str, in->names.length);
185+
MetaString::parseList(namesStr, resultEntries);
186+
}
187+
188+
resultIterator = resultEntries.begin();
189+
}
190+
191+
FB_BOOLEAN SqlPackage::ParseUnqualifiedNamesResultSet::fetch(ThrowStatusExceptionWrapper* status)
192+
{
193+
if (resultIterator == resultEntries.end())
194+
return false;
195+
196+
out->name.set(resultIterator->c_str(), resultIterator->length());
197+
out->nameNull = FB_FALSE;
198+
++resultIterator;
199+
200+
return true;
201+
}
202+
203+
204+
//--------------------------------------
205+
206+
174207
SqlPackage::SqlPackage(MemoryPool& pool)
175208
: SystemPackage(
176209
pool,
@@ -204,6 +237,21 @@ SqlPackage::SqlPackage(MemoryPool& pool)
204237
{"ACCESS_PATH", fld_description, false}
205238
}
206239
),
240+
SystemProcedure(
241+
pool,
242+
"PARSE_UNQUALIFIED_NAMES",
243+
SystemProcedureFactory<
244+
ParseUnqualifiedNamesInput, ParseUnqualifiedNamesOutput, parseUnqualifiedNamesProcedure>(),
245+
prc_selectable,
246+
// input parameters
247+
{
248+
{"NAMES", fld_text_max, true}
249+
},
250+
// output parameters
251+
{
252+
{"NAME", fld_r_name, false}
253+
}
254+
),
207255
},
208256
// functions
209257
{

src/jrd/sys-packages/SqlPackage.h

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "firebird.h"
2727
#include "firebird/Message.h"
2828
#include "../common/classes/array.h"
29+
#include "../common/classes/MetaString.h"
30+
#include "../common/classes/objects_array.h"
2931
#include "../jrd/SystemPackages.h"
3032

3133
namespace Jrd {
@@ -88,10 +90,50 @@ class SqlPackage final : public SystemPackage
8890
Firebird::Array<ExplainOutput::Type>::const_iterator resultIterator = nullptr;
8991
};
9092

91-
//----------
92-
9393
static Firebird::IExternalResultSet* explainProcedure(Firebird::ThrowStatusExceptionWrapper* status,
9494
Firebird::IExternalContext* context, const ExplainInput::Type* in, ExplainOutput::Type* out);
95+
96+
//----------
97+
98+
FB_MESSAGE(ParseUnqualifiedNamesInput, Firebird::ThrowStatusExceptionWrapper,
99+
(FB_INTL_VARCHAR(MAX_VARY_COLUMN_SIZE / METADATA_BYTES_PER_CHAR * METADATA_BYTES_PER_CHAR, CS_METADATA), names)
100+
);
101+
102+
FB_MESSAGE(ParseUnqualifiedNamesOutput, Firebird::ThrowStatusExceptionWrapper,
103+
(FB_INTL_VARCHAR(METADATA_IDENTIFIER_CHAR_LEN * METADATA_BYTES_PER_CHAR, CS_METADATA), name)
104+
);
105+
106+
class ParseUnqualifiedNamesResultSet :
107+
public
108+
Firebird::DisposeIface<
109+
Firebird::IExternalResultSetImpl<
110+
ParseUnqualifiedNamesResultSet,
111+
Firebird::ThrowStatusExceptionWrapper
112+
>
113+
>
114+
{
115+
public:
116+
ParseUnqualifiedNamesResultSet(Firebird::ThrowStatusExceptionWrapper* status, Firebird::IExternalContext* context,
117+
const ParseUnqualifiedNamesInput::Type* in, ParseUnqualifiedNamesOutput::Type* out);
118+
119+
public:
120+
void dispose() override
121+
{
122+
delete this;
123+
}
124+
125+
public:
126+
FB_BOOLEAN fetch(Firebird::ThrowStatusExceptionWrapper* status) override;
127+
128+
private:
129+
ParseUnqualifiedNamesOutput::Type* out;
130+
Firebird::ObjectsArray<Firebird::MetaString> resultEntries{*getDefaultMemoryPool()};
131+
Firebird::ObjectsArray<Firebird::MetaString>::const_iterator resultIterator;
132+
};
133+
134+
static Firebird::IExternalResultSet* parseUnqualifiedNamesProcedure(Firebird::ThrowStatusExceptionWrapper* status,
135+
Firebird::IExternalContext* context,
136+
const ParseUnqualifiedNamesInput::Type* in, ParseUnqualifiedNamesOutput::Type* out);
95137
};
96138

97139

0 commit comments

Comments
 (0)