00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "DataOutputDatabaseHandler.h"
00034 #include <FE_Datastore.h>
00035 #include <Vector.h>
00036
00037 DataOutputDatabaseHandler::DataOutputDatabaseHandler(FE_Datastore *database, const char *tName)
00038 :DataOutputHandler(DATAHANDLER_TAGS_DataOutputDatabaseHandler),
00039 theDatabase(database), tableName(0), numColumns(0), columns(0), commitTag(0)
00040 {
00041
00042
00043
00044
00045 if (tName != 0) {
00046 tableName = new char [strlen(tName)+1];
00047 strcpy(tableName, tName);
00048 if (tableName == 0) {
00049 opserr << "DataOutputDatabaseHandler::DataOutputDatabaseHandler - out of memory creating copy of tableName: " << tName << endln;
00050 }
00051 }
00052 }
00053
00054 DataOutputDatabaseHandler::~DataOutputDatabaseHandler()
00055 {
00056 if (tableName != 0)
00057 delete [] tableName;
00058
00059 if (columns != 0) {
00060 for (int j=0; j<numColumns; j++)
00061 delete [] columns[j];
00062 delete [] columns;
00063 }
00064 }
00065
00066 int
00067 DataOutputDatabaseHandler::open(char **dataDescription, int numData)
00068 {
00069
00070
00071
00072
00073 if (theDatabase == 0) {
00074 opserr << "DataOutputStreamHandler::open() - database has not been set\n";
00075 return -1;
00076 }
00077
00078 if (tableName == 0) {
00079 opserr << "DataOutputDatabaseHandler::open() - no tableName passed or failed to get memory\n";
00080 return -1;
00081 }
00082
00083 if (dataDescription == 0) {
00084 opserr << "DataOutputDatabaseHandler::open() - no column description data passed\n";
00085 return -1;
00086 }
00087
00088 if (numData < 0) {
00089 opserr << "DataOutputDatabaseHandler::open() - numColumns (" << numData << ") < 0\n";
00090 return -1;
00091 } else
00092 numColumns = numData;
00093
00094
00095
00096
00097
00098 if (columns != 0) {
00099 for (int j=0; j<numColumns; j++)
00100 delete [] columns[j];
00101 delete [] columns;
00102 columns = 0;
00103 }
00104
00105
00106
00107
00108
00109 columns = new char *[numColumns];
00110 if (columns == 0) {
00111 opserr << "DataOutputDatabaseHandler::open() - out of memory creating array for columns of size (" << numData << ") < 0\n";
00112 numColumns = 0;
00113 return -1;
00114 }
00115
00116
00117 for (int i=0; i<numColumns; i++) {
00118 columns[i] = new char[strlen(dataDescription[i])+1];
00119 if (columns[i] == 0) {
00120 opserr << "DataOutputDatabaseHandler::open() - out of memory creating copy of string " << dataDescription[i] << endln;
00121 for (int j=0; j<i; j++)
00122 delete [] columns[j];
00123 delete [] columns;
00124 numColumns = 0;
00125 return -1;
00126 }
00127 strcpy(columns[i], dataDescription[i]);
00128 }
00129
00130
00131 return theDatabase->createTable(tableName, numColumns, columns);
00132 }
00133
00134 int
00135 DataOutputDatabaseHandler::write(Vector &data)
00136 {
00137 int result = 0;
00138
00139 if (data.Size() == numColumns)
00140 if (theDatabase != 0)
00141 result = theDatabase->insertData(tableName, columns, commitTag, data);
00142 else {
00143 opserr << "DataOutputStreamHandler::write() - database has not been set\n";
00144 return -1;
00145 }
00146 else {
00147 opserr << "DataOutputStreamHandler::write() - Vector not of correct size or open() has not been called\n" << numColumns << " " << data.Size() << endln;
00148 return -1;
00149 }
00150
00151 commitTag++;
00152
00153 return result;
00154 }
00155
00156 int
00157 DataOutputDatabaseHandler::setDatabase(FE_Datastore &database, const char *tName)
00158 {
00159 if (tName == 0 || strlen(tName) == 0) {
00160 opserr << "DataOutputDatabaseHandler::DataOutputDatabaseHandler - no tableName passed\n";
00161 return -1;
00162 }
00163
00164 if (tableName != 0)
00165 delete [] tableName;
00166
00167 tableName = new char [strlen(tName)+1];
00168 strcpy(tableName, tName);
00169 if (tableName == 0) {
00170 opserr << "DataOutputDatabaseHandler::DataOutputDatabaseHandler - out of memory creating copy of tableName: " << tName << endln;
00171 return -1;
00172 }
00173
00174 theDatabase = &database;
00175 return 0;
00176 }
00177
00178
00179 int
00180 DataOutputDatabaseHandler::sendSelf(int commitTag, Channel &theChannel)
00181 {
00182 opserr << "DataOutputDatabaseHandler::sendSelf() - not yet implemented\n";
00183 return -1;
00184 }
00185 int
00186 DataOutputDatabaseHandler::recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker)
00187 {
00188 opserr << "DataOutputDatabaseHandler::sendSelf() - not yet implemented\n";
00189 return -1;
00190 }