oracledb 26.0.0-beta.1

Lightweight driver for Oracle Database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//-----------------------------------------------------------------------------
// Copyright (c) 2026, Oracle and/or its affiliates.
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// statement.rs
//
// Defines the structure representing a statement.
//-----------------------------------------------------------------------------

mod bind_info;
mod cache;
mod holder;
mod options;
mod public;
mod sql_parser;

use std::collections::HashMap;
use std::collections::HashSet;

use crate::bind_params::BindParameters;
use crate::client::Client;
use crate::constants;
use crate::db_value::ToDbValue;
use crate::error::Error;
use crate::metadata::Metadata;
use crate::response::Response;

#[derive(Clone)]
pub(crate) struct CachedStatement {
    sql: String,
    cursor_id: u16,
    no_prefetch: bool,
    requires_define: bool,
    is_query: bool,
    is_plsql: bool,
    is_ddl: bool,
    is_dml: bool,
    is_returning: bool,
    binds: Vec<BindInfo>,
    bind_names: Vec<String>,
    bind_names_set: HashSet<String>,
    binds_changed: bool,
    out_metadata: Vec<Metadata>,
    cache_slot_num: usize,
    is_nested: bool,
    options: StatementOptions,
}

impl CachedStatement {
    /// Adds a bind variable to the statement.
    fn add_bind(&mut self, name: String) {
        let exists = self.bind_names_set.contains(&name);
        if !self.is_plsql || !exists {
            self.binds.push(BindInfo {
                name: name.clone(),
                is_return_bind: self.is_returning,
                metadata: None,
                bind_direction: constants::TTC_BIND_DIR_INPUT,
            });
            if !exists {
                self.bind_names.push(name.clone());
                self.bind_names_set.insert(name);
            }
        }
    }

    /// Determines the statement type.
    fn determine_statement_type(&mut self, keyword: &str) {
        match keyword.to_uppercase().as_str() {
            "DECLARE" | "BEGIN" | "CALL" => {
                self.is_plsql = true;
            }
            "SELECT" | "WITH" => {
                self.is_query = true;
            }
            "INSERT" | "UPDATE" | "DELETE" | "MERGE" => {
                self.is_dml = true;
            }
            "CREATE" | "ALTER" | "DROP" | "GRANT" | "REVOKE" | "ANALYZE"
            | "AUDIT" | "COMMENT" | "TRUNCATE" => {
                self.is_ddl = true;
            }
            _ => {}
        }
    }

    /// Returns the binds defined for the statement.
    pub(crate) fn binds(&self) -> &Vec<BindInfo> {
        &self.binds
    }

    /// Returns a boolean indicating if binds have changed since the last
    /// time the statement was executed.
    pub(crate) fn binds_changed(&self) -> bool {
        self.binds_changed
    }

    /// Returns the cache slot number used. A value of zero indicates that the
    /// statement is not actually cached.
    pub(crate) fn cache_slot_num(&self) -> usize {
        self.cache_slot_num
    }

    /// Checks that the supplied parameters satisfy the binds defined by the
    /// statement.
    pub(crate) fn check_binds(
        &mut self,
        params: &BindParameters,
    ) -> Result<(), Error> {
        params.validate(&mut self.binds)?;
        if !self.is_query {
            self.out_metadata.clear();
        }
        for bind_info in self.binds.iter() {
            if bind_info.is_output_bind() {
                self.out_metadata.push(bind_info.metadata.clone().unwrap());
            }
        }
        Ok(())
    }

    /// Checks that the supplied named parameters satisfy the binds defined by
    /// the statement.
    pub(crate) fn check_named_binds<'a>(
        &mut self,
        params: &[(&str, &'a dyn ToDbValue)],
    ) -> Result<Vec<&'a dyn ToDbValue>, Error> {
        // first, build a hash map containing the binds and their values since
        // the order in which they are supplied may differ from the order in
        // which they are supplied in the statement; in addition, PL/SQL
        // statements only allow one bind per name whereas SQL statements allow
        // multiple binds per name and the bind must be repeated when being
        // sent to the server
        let mut bind_map: HashMap<String, &dyn ToDbValue> = HashMap::new();
        for (name, value) in params.iter() {
            let normalized_name =
                if name.starts_with('"') && name.ends_with('"') {
                    name[1..name.len() - 1].to_string()
                } else {
                    name.to_uppercase()
                };
            bind_map.insert(normalized_name, *value);
        }

        // second, check that the number of binds matches, and if not, raise an
        // appropriate exception
        if bind_map.len() != self.bind_names.len() {
            for name in bind_map.keys() {
                if !self.bind_names_set.contains(name) {
                    return Err(Error::invalid_bind_name(name));
                }
            }
        }

        // finally, scan the binds and ensure that a named bind has been
        // supplied for each of them
        let mut checked_binds: Vec<&dyn ToDbValue> = Vec::new();
        for bind_info in self.binds.iter_mut() {
            if let Some(param) = bind_map.get(&bind_info.name) {
                checked_binds.push(*param);
            } else {
                return Err(Error::missing_bind_value(&bind_info.name));
            }
        }

        Ok(checked_binds)
    }

    /// Clears the database cursor id.
    pub(crate) fn clear_cursor(&mut self) {
        self.cursor_id = 0;
    }

    /// Clears the flag indicating that a define is required.
    pub(crate) fn clear_requires_define(&mut self) {
        self.requires_define = false;
    }

    /// Creates a clone of the statement with the specified options.
    pub(crate) fn clone_with_options(
        &self,
        options: &StatementOptions,
    ) -> Self {
        let mut cloned = self.clone();
        cloned.options = options.clone();
        cloned
    }

    /// Creates a new empty statement with the specified SQL and options.
    pub(crate) fn create_empty(
        sql: String,
        is_nested: bool,
        options: &StatementOptions,
    ) -> Self {
        let is_query = sql.is_empty();
        Self {
            sql,
            cursor_id: 0,
            no_prefetch: false,
            requires_define: false,
            is_query,
            is_plsql: false,
            is_ddl: false,
            is_dml: false,
            is_returning: false,
            binds: Vec::new(),
            bind_names: Vec::new(),
            bind_names_set: HashSet::new(),
            binds_changed: false,
            out_metadata: Vec::new(),
            cache_slot_num: 0,
            is_nested,
            options: options.clone(),
        }
    }

    /// Returns the id of the database cursor.
    pub(crate) fn cursor_id(&self) -> u16 {
        self.cursor_id
    }

    /// Returns whether or not the statement has any bind variables.
    pub(crate) fn has_binds(&self) -> bool {
        !self.binds.is_empty()
    }

    /// Returns whether or not a database cursor is associated with the
    /// statement.
    pub(crate) fn has_cursor(&self) -> bool {
        self.cursor_id != 0
    }

    /// Returns whether or not the statement has input binds.
    pub(crate) fn has_input_binds(&self) -> bool {
        for bind_info in &self.binds {
            if bind_info.is_input_bind() {
                return true;
            }
        }
        false
    }

    /// Returns whether or not the statement is in the statement cache.
    pub(crate) fn is_cached(&self) -> bool {
        self.cache_slot_num != 0
    }

    /// Returns whether or not the statement is a DDL statement.
    pub(crate) fn is_ddl(&self) -> bool {
        self.is_ddl
    }

    /// Returns whether or not the statement is a nested statement.
    pub(crate) fn is_nested(&self) -> bool {
        self.is_nested
    }

    /// Returns whether or not the statement is a PL/SQL statement.
    pub(crate) fn is_plsql(&self) -> bool {
        self.is_plsql
    }

    /// Returns whether or not the statement is a query.
    pub(crate) fn is_query(&self) -> bool {
        self.is_query
    }

    /// Creates a new statement from the provided SQL.
    pub(crate) fn new(
        sql: &str,
        options: &StatementOptions,
    ) -> Result<Self, Error> {
        let mut statement = Self::create_empty(sql.into(), false, options);
        sql_parser::SqlParser::new(sql).parse(&mut statement)?;
        Ok(statement)
    }

    /// Returns whether prefetching has been disabled.
    pub(crate) fn no_prefetch(&self) -> bool {
        self.no_prefetch
    }

    /// Returns the options associated with the statement.
    pub(crate) fn options(&self) -> &StatementOptions {
        &self.options
    }

    /// Returns a reference to the vector of metadata for out variables.
    pub(crate) fn out_metadata(&self) -> &Vec<Metadata> {
        &self.out_metadata
    }

    /// Populates statement metadata from the describe info returned by the
    /// database.
    pub(crate) fn populate_from_describe_info(
        &mut self,
        client: &Client,
        resp: &mut Response,
    ) -> Result<(), Error> {
        resp.read_ub4()?; // max row size
        let num_columns = resp.read_ub4()?;
        if num_columns > 0 {
            resp.read_u8()?;
        }
        self.out_metadata.clear();
        for _ in 0..num_columns {
            let mut metadata = Metadata::from_response(resp, client)?;
            if metadata.requires_define() {
                self.requires_define = true;
                self.no_prefetch = true;
                if !self.options.fetch_lobs() {
                    metadata = metadata.define_metadata();
                }
            }
            self.out_metadata.push(metadata);
        }
        let _current_date = resp.read_bytes_with_double_length()?;
        let _dcbflag = resp.read_ub4()?;
        let _dcbmdbz = resp.read_ub4()?;
        let _dcbmnpr = resp.read_ub4()?;
        let _dcbmxpr = resp.read_ub4()?;
        let _dcbqcky = resp.read_bytes_with_double_length()?;
        Ok(())
    }

    /// Returns whether or not the statement requires a define.
    pub(crate) fn requires_define(&self) -> bool {
        self.requires_define
    }

    /// Returns a boolean indicating if the statement requires a single execute
    /// in order to be processed correctly by the server. If a PL/SQL block has
    /// not been executed before, the determination of input/output binds has
    /// not been completed and so a single execution is required in order to
    /// complete that determination.
    pub(crate) fn requires_single_execute(&self) -> bool {
        self.is_plsql && (self.cursor_id == 0 || self.binds_changed)
    }

    /// Sets the bind directions from the response from the database.
    pub(crate) fn set_bind_directions(
        &mut self,
        resp: &mut Response,
    ) -> Result<(), Error> {
        self.out_metadata.clear();
        for bind_info in self.binds.iter_mut() {
            bind_info.bind_direction = resp.read_u8()?;
            if bind_info.is_output_bind() {
                let metadata = bind_info.metadata.as_ref().unwrap();
                self.out_metadata.push(metadata.clone());
            }
        }
        Ok(())
    }

    /// Sets the cache slot number for the statement.
    pub(crate) fn set_cache_slot_num(&mut self, slot_num: usize) {
        self.cache_slot_num = slot_num;
    }

    /// Sets the database cursor id associated with the statement.
    pub(crate) fn set_cursor_id(&mut self, cursor_id: u16) {
        self.cursor_id = cursor_id;
    }

    /// Returns the SQL associated with the statement.
    pub(crate) fn sql(&self) -> &str {
        &self.sql
    }

    /// Returns the length of the SQL associated with the statement in the
    /// format required by the database.
    pub(crate) fn sql_len(&self) -> u32 {
        self.sql.len().try_into().unwrap()
    }
}

pub(crate) use bind_info::BindInfo;
pub(crate) use cache::StatementCache;
pub(crate) use holder::StatementHolder;
pub(crate) use options::StatementOptions;
pub use public::Statement;