Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Schema.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file Schema.hh
11 ///
12 /// @brief table definition for the schema generator framework
13 /// @author Tim Jacobs
14 
15 #ifndef INCLUDED_basic_database_schema_generator_Schema_HH
16 #define INCLUDED_basic_database_schema_generator_Schema_HH
17 
22 #include <iosfwd>
23 #include <string>
24 
27 #include <utility/vector1.hh>
28 
29 
30 namespace basic {
31 namespace database {
32 namespace schema_generator {
33 
34 class Schema
35 {
36 
37 public:
38 
39  Schema(
40  std::string table_name);
41 
42  Schema(
43  std::string table_name,
44  PrimaryKey primary_key);
45 
46  Schema(
47  Schema const & src);
48 
50 
51  void add_column(Column column);
52 
53  void add_constraint(ConstraintOP constraint);
54 
55  void add_index(Index index);
56 
57  //@brief Returns sql statements needed to fully initialize the given schema.
58  //
59  //See table_schema_statements and table_init_statements, statement blocks
60  //must be executed separately in sqlite.
61  std::string print(utility::sql_database::sessionOP db_session) const;
62 
63  //@brief Write schema to database and initialize if needed.
64  //
65  //Retries write once to resolve transaction conflicts.
66  void write(utility::sql_database::sessionOP db_session);
67 
68 protected:
69  //@brief Returns sql statements to declare table, keys, and indices.
70  std::string table_schema_statements(utility::sql_database::sessionOP db_session) const;
71 
72  //@brief Returns sql to initialize table state.
73  //
74  //Currently limited to autoincrement starting values. Must be executed in separate statement
75  //when using sqlite3, as sqlite_sequence table isn't instantiated until the statement containing
76  //CREATE TABLE with autoincrement value is executed.
77  std::string table_init_statements(utility::sql_database::sessionOP db_session) const;
78 
79  //@brief Write and initialize schema if not already declared.
82  std::string schema_statement,
83  std::string init_statements) const;
84 
85 private:
86 
87  void init();
88 
89  std::string table_name_;
95 };
96 
97 } // schema_generator
98 } // namespace database
99 } // namespace utility
100 
101 #endif
std::string print(utility::sql_database::sessionOP db_session) const
Definition: Schema.cc:139
utility::pointer::shared_ptr< Constraint > ConstraintOP
utility::vector1< ForeignKey > foreign_keys_
Definition: Schema.hh:92
std::string table_init_statements(utility::sql_database::sessionOP db_session) const
Definition: Schema.cc:195
void add_constraint(ConstraintOP constraint)
Definition: Schema.cc:126
void write(utility::sql_database::sessionOP db_session)
Definition: Schema.cc:230
void add_foreign_key(ForeignKey key)
Definition: Schema.cc:100
PrimaryKey class for the schema generator framework.
utility::vector1< ConstraintOP > constraints_
Definition: Schema.hh:93
Index class for the schema generator framework.
Fstring::size_type index(Fstring const &s, Fstring const &ss)
First Index Position of a Substring in an Fstring.
Definition: Fstring.hh:2180
Schema(std::string table_name)
Definition: Schema.cc:64
ForeignKey class for the schema generator framework.
tuple database
utility::keys::lookup::key< KeyType > const key
std::string table_schema_statements(utility::sql_database::sessionOP db_session) const
Definition: Schema.cc:144
void check_table_and_perform_write(utility::sql_database::sessionOP db_session, std::string schema_statement, std::string init_statements) const
Definition: Schema.cc:262
utility::vector1< Index > indices_
Definition: Schema.hh:94
vector1: std::vector with 1-based indexing
forward hearder for the Column class in the schema generator framework
pointer::shared_ptr< session > sessionOP