db_connection.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:5k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #ifndef _db_connection_h_
- #define _db_connection_h_
- #include <string>
- #include <vector>
- #include <map>
- #include <iostream>
- #include <time.h>
- #include "string_of_text.h"
- #include "vs_types.h"
- #include "basic_exception.h"
- #include "channel_info.h"
- /** subclass if iostream, hide from user the communication details
- */
- class db_iostream : public iostream {
- bool before_mark;
- bool everything_read;
- vector< string > messages;
- public:
- db_iostream();
- db_iostream( streambuf *sb ) : iostream( sb ), everything_read( true ) {}
- void wait_input() { everything_read = false; before_mark = true; }
- /** s will remains empty only if it is nothing to read */
- istream& operator >>( string &s );
- /** before reading, check for everything_read flag.
- If nothing to wait from input, return.
- After sending the command that should return something, user must
- call wait_input, which indicates that sequence of -> and <-
- should appear in input before '>>>'
- */
- void skip_to_prompt();
- void skip_to_prompt( vector< string > &messages );
- void append_messages( vector< string > &msgs );
- };
- /** superuser use this to edit server preferences */
- class ServerConfigItem {
- public:
- string key;
- string value;
- string help;
- public:
- ServerConfigItem() {}
- ServerConfigItem( const string &k, const string &v, const string &h ) :
- key( k ), value( v ), help( h ) {}
- ServerConfigItem( const ServerConfigItem &c ) {
- *this = c;
- }
- const ServerConfigItem &operator =( const ServerConfigItem &c )
- {
- if( this != &c )
- {
- key = c.key;
- value = c.value;
- help = c.help;
- }
- return *this;
- }
- };
- typedef vector< ServerConfigItem > ServerConfigItemVec;
- /** encapsulate all connections to database server
- */
- class DbConnection {
- /** holds i/o stream */
- db_iostream *dbSockStream;
- /** tree of movies */
- map< int, MovieItem > movies;
-
- public:
- static const char _database_name_[] = "database";
- static const char _debug_level_[] = "debug";
- static const char _list_movies_[] = "listmovies";
- static const char _list_captions_[] = "listcaptions";
- static const char _list_mov_files_[] = "listmovfiles";
- static const char _superuser_[] = "superuser";
- static const char _save_properties_[] = "saveproperties";
- static const char _list_properties_[] = "listproperties";
- static const char _set_property_[] = "setproperty";
- static const char _list_channels_[] = "listchannels";
- public:
- DbConnection();
- ~DbConnection();
- void connect( const string &server, unsigned int port );
- bool login_superuser( const string &passwd, vector< string > &messages );
- // work with data
- const map< int, MovieItem > &get_movies() const { return movies; }
- const MovieItem &get_movie_by_id( int id );
- void wait_input()
- {
- dbSockStream->wait_input();
- }
- void skip_to_prompt( vector< string > &messages );
- // real actions
- void set_debug( vector< string > &messages, int debugL );
- string get_database_name( vector< string > &messages );
- void load_new_database( vector< string > &messages, const string &dbname );
- void load_movie_text( vector< string >&messages, int id, TextOfMovieT *tom);
- /** the db_connection class keeps the list of files for each movie. This
- call only select files by time and fill back the empty list movieFiles.
- const session it passed to let access the preferences
- */
- void getMovieFileListByTime( int id, const struct tm &t,
- MovieFilesT &movieFiles,
- const Session &session,
- vector< string > &messages );
- void loadMovieFileList( int id, MovieItem &mi, const Session &session,
- vector< string > &messages );
- void loadServerProperties( vector< string > &messages,
- ServerConfigItemVec &props );
- void saveServerProperties( vector< string > &messages,
- ServerConfigItemVec &props );
- void fill_channellist( vector< string > &messages,
- channel_infos_mapT &channel_infos_map );
- /** tell database server that this movie should be recorded now */
- void start_recording( int channel_id, vector< string > &messages );
- void stop_recording( int channel_id, vector< string > &messages );
- };
- /** subclass of basic exceptions
- */
- class NetworkException : public BasicException {
- public:
- NetworkException( const char *format, ... );
- };
- /** subclass of class BasicException */
- class SyntaxException : public BasicException {
- public:
- SyntaxException( const char *format, ... );
- };
- class DatabaseException : public BasicException {
- public:
- DatabaseException( const char *format, ... );
- };
- #endif _db_connection_h_