comparison Orthanc/SQLite/FunctionContext.cpp @ 0:02f7a0400a91

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Feb 2015 13:45:35 +0100
parents
children 54d5dd1df2e5
comparison
equal deleted inserted replaced
-1:000000000000 0:02f7a0400a91
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 *
4 * Copyright (C) 2012-2015 Sebastien Jodogne <s.jodogne@gmail.com>,
5 * Medical Physics Department, CHU of Liege, Belgium
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of the CHU of Liege, nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 **/
33
34
35 #if ORTHANC_SQLITE_STANDALONE != 1
36 #include "../PrecompiledHeaders.h"
37 #endif
38
39 #include "FunctionContext.h"
40 #include "OrthancSQLiteException.h"
41
42 #include <sqlite3.h>
43
44 namespace Orthanc
45 {
46 namespace SQLite
47 {
48 FunctionContext::FunctionContext(struct sqlite3_context* context,
49 int argc,
50 struct ::Mem** argv)
51 {
52 assert(context != NULL);
53 assert(argc >= 0);
54 assert(argv != NULL);
55
56 context_ = context;
57 argc_ = static_cast<unsigned int>(argc);
58 argv_ = argv;
59 }
60
61 void FunctionContext::CheckIndex(unsigned int index) const
62 {
63 if (index >= argc_)
64 {
65 throw OrthancSQLiteException("Parameter out of range");
66 }
67 }
68
69 ColumnType FunctionContext::GetColumnType(unsigned int index) const
70 {
71 CheckIndex(index);
72 return static_cast<SQLite::ColumnType>(sqlite3_value_type(argv_[index]));
73 }
74
75 int FunctionContext::GetIntValue(unsigned int index) const
76 {
77 CheckIndex(index);
78 return sqlite3_value_int(argv_[index]);
79 }
80
81 int64_t FunctionContext::GetInt64Value(unsigned int index) const
82 {
83 CheckIndex(index);
84 return sqlite3_value_int64(argv_[index]);
85 }
86
87 double FunctionContext::GetDoubleValue(unsigned int index) const
88 {
89 CheckIndex(index);
90 return sqlite3_value_double(argv_[index]);
91 }
92
93 std::string FunctionContext::GetStringValue(unsigned int index) const
94 {
95 CheckIndex(index);
96 return std::string(reinterpret_cast<const char*>(sqlite3_value_text(argv_[index])));
97 }
98
99 bool FunctionContext::IsNullValue(unsigned int index) const
100 {
101 CheckIndex(index);
102 return sqlite3_value_type(argv_[index]) == SQLITE_NULL;
103 }
104
105 void FunctionContext::SetNullResult()
106 {
107 sqlite3_result_null(context_);
108 }
109
110 void FunctionContext::SetIntResult(int value)
111 {
112 sqlite3_result_int(context_, value);
113 }
114
115 void FunctionContext::SetDoubleResult(double value)
116 {
117 sqlite3_result_double(context_, value);
118 }
119
120 void FunctionContext::SetStringResult(const std::string& str)
121 {
122 sqlite3_result_text(context_, str.data(), str.size(), SQLITE_TRANSIENT);
123 }
124 }
125 }