OpenASIP  2.0
SQLite.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file SQLite.cc
26  *
27  * Implementation of SQLite class.
28  *
29  * @author Pekka Jääskeläinen 2004 (pekka.jaaskelainen-no.spam-tut.fi)
30  *
31  * @note rating: red
32  */
33 
34 #include <string>
35 using std::string;
36 
37 #include "sqlite3.h"
38 
39 #include "SQLite.hh"
40 
41 /**
42  * Constructor.
43  */
44 SQLite::SQLite() : activeConnection_(NULL) {
45 }
46 
47 /**
48  * Destructor.
49  */
52 }
53 
54 /**
55  * Connects to a database.
56  *
57  * Possible old connection is closed. SQLite implementation doesn't support
58  * multiple active connections currently. If database file is not found,
59  * creation of new database file with the given name is attempted.
60  *
61  * @param database The filename of the SQLite database.
62  * @param login Login name to use for the database connection.
63  * @param password Password to use for the database connection.
64  * @param readOnly Should the database be opened in read-only mode only?
65  * @return Connection "handle" which is used to query the DB, etc.
66  * @exception RelationalDBException Thrown if connection failed.
67  */
70  const std::string& database, const std::string&, const std::string&, bool) {
71  if (activeConnection_ != NULL) {
73  activeConnection_ = NULL;
74  }
75 
76  sqlite3* connection = NULL;
77  int value = sqlite3_open(database.c_str(), &connection);
78 
79  if (value != SQLITE_OK) {
80  string error = sqlite3_errmsg(connection);
81  error += " - file:\"" + database +"\"";
83  __FILE__, __LINE__, "SQLite::connect()", error);
84  }
85 
86  activeConnection_ = new SQLiteConnection(connection);
87  return *activeConnection_;
88 }
89 
90 /**
91  * Closes a database connection.
92  *
93  * Close should be idempotent, that is, it can be called arbitrary amount of
94  * times to a connection without an error.
95  *
96  * @param connection The database connection to close.
97  * @exception RelationalDBException Thrown if closing the connection failed.
98  */
99 void
101  if (activeConnection_ == NULL) {
102  return;
103  }
104 
105  delete activeConnection_;
106  activeConnection_ = NULL;
107 }
SQLite::SQLite
SQLite()
Definition: SQLite.cc:44
RelationalDBConnection
Definition: RelationalDBConnection.hh:48
RelationalDBException
Definition: Exception.hh:692
SQLite::connect
virtual RelationalDBConnection & connect(const std::string &database, const std::string &login="", const std::string &password="", bool readOnly=false)
Definition: SQLite.cc:69
SQLite::~SQLite
virtual ~SQLite()
Definition: SQLite.cc:50
SQLite::close
virtual void close(const RelationalDBConnection &connection)
Definition: SQLite.cc:100
SQLite::activeConnection_
SQLiteConnection * activeConnection_
Only one simultaneous active connection per SQLite instance allowed currently.
Definition: SQLite.hh:59
SQLite.hh
SQLiteConnection
Definition: SQLiteConnection.hh:47