code reorg and doxygen documentation
[csql.git] / src / storage / DbMgrFkImpl.cxx
blob21acaae4237953ecfa26b63e3ca07ce1b8ba0109
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<Database.h>
17 #include<DatabaseManager.h>
18 #include<DatabaseManagerImpl.h>
19 #include<os.h>
20 #include<Table.h>
21 #include<TableImpl.h>
22 #include<Transaction.h>
23 #include<CatalogTables.h>
24 #include<Index.h>
25 #include<Lock.h>
26 #include<Debug.h>
27 #include<Config.h>
28 #include<Process.h>
30 DbRetVal DatabaseManagerImpl::createForeignKey(char *fKName,ForeignKeyInfo *info)
32 DbRetVal rv = OK;
33 int totFkFlds = info->fkFldList.size();
34 int totPkFlds = info->pkFldList.size();
35 if (totFkFlds==0 && totPkFlds==0) {
36 printError(ErrBadCall, "No Field name specified");
37 return ErrBadCall;
39 void *tptr =NULL;
40 void *chunk = NULL;
41 void *vcchunk = NULL;
42 rv = systemDatabase_->getXCheckpointMutex();
43 if (OK != rv)
45 printError(ErrSysInternal, "Unable to get database mutex");
46 return ErrSysInternal;
48 CatalogTableTABLE cTable(systemDatabase_);
49 cTable.getChunkAndTblPtr(info->fkTableName, chunk, tptr, vcchunk);
50 if (NULL == tptr)
52 systemDatabase_->releaseCheckpointMutex();
53 printError(ErrNotExists, "Table does not exist %s", info->fkTableName);
54 return ErrNotExists;
56 char **fptr = new char* [totFkFlds];
57 CatalogTableFIELD cField(systemDatabase_);
58 rv = cField.getFieldPtrs(info->fkFldList, tptr, fptr);
59 if (OK != rv)
61 delete[] fptr;
62 systemDatabase_->releaseCheckpointMutex();
63 if (rv != ErrBadCall) {
64 printError(ErrNotExists, "Field does not exist");
65 return ErrNotExists;
68 void *tPkptr =NULL;
69 void *chunkPk = NULL;
70 void *vcchunkPk = NULL;
71 CatalogTableTABLE c2Table(systemDatabase_);
72 c2Table.getChunkAndTblPtr(info->pkTableName, chunkPk, tPkptr, vcchunkPk);
73 if (NULL == tPkptr)
75 systemDatabase_->releaseCheckpointMutex();
76 printError(ErrNotExists, "Table does not exist %s", info->pkTableName);
77 return ErrNotExists;
79 char **fPkptr = new char* [totPkFlds];
80 CatalogTableFIELD c2Field(systemDatabase_);
81 rv = c2Field.getFieldPtrs(info->pkFldList, tPkptr, fPkptr);
82 if (OK != rv)
84 delete[] fptr;
85 delete[] fPkptr;
86 systemDatabase_->releaseCheckpointMutex();
87 if (rv != ErrBadCall) {
88 printError(ErrNotExists, "Field does not exist");
89 return ErrNotExists;
92 //Create New chunkdatanode
93 CatalogTableFK cFK(systemDatabase_);
94 rv = cFK.insert(fKName, tptr, tPkptr);//TODO
95 if (OK != rv)
97 delete[] fptr;
98 delete[] fPkptr;
99 systemDatabase_->releaseCheckpointMutex();
100 printError(ErrSysInternal, "Catalog table updation failed in CFK table");
101 return ErrSysInternal;
104 CatalogTableFKFIELD cFKField(systemDatabase_);
105 rv = cFKField.insert(fKName,fptr,fPkptr,totFkFlds);
106 if (OK != rv)
108 delete[] fptr;
109 delete[] fPkptr;
110 cFK.remove(tptr);
111 systemDatabase_->releaseCheckpointMutex();
112 printError(ErrSysInternal, "Catalog table updation failed in CFKFIELD table");
113 return ErrSysInternal;
115 systemDatabase_->releaseCheckpointMutex();
116 delete[] fptr;
117 delete[] fPkptr;
118 return rv;
121 DbRetVal DatabaseManagerImpl::dropForeignKey(void *tptr,bool trylock)
123 DbRetVal rv = OK;
124 if(trylock){
125 rv = systemDatabase_->getXCheckpointMutex();
126 if (OK != rv)
128 printError(ErrSysInternal, "Unable to get database mutex");
129 return ErrSysInternal;
132 void *fkChunk=NULL;
133 CatalogTableFK cFK(systemDatabase_);
134 int total = cFK.getNoOfPkTable(tptr);
135 //printDebug(DM_TEST,"total fk chunk %d",total);
136 for (int i=0;i< total; i++)
138 fkChunk = cFK.getFkCTable(tptr);
139 if(NULL==fkChunk)
141 if(trylock){
142 systemDatabase_->releaseCheckpointMutex();
144 printError(ErrSysInternal, "Catalog table not finds CFKFIELD table");
145 return ErrSysInternal;
147 CatalogTableFKFIELD cFKField(systemDatabase_);
148 rv = cFKField.remove(fkChunk);
149 if (OK != rv)
151 if(trylock){
152 systemDatabase_->releaseCheckpointMutex();
154 printError(ErrSysInternal, "Catalog table updation failed in CFKFIELD table");
155 return ErrSysInternal;
157 rv =cFK.remove(fkChunk);
158 if (OK != rv)
160 if(trylock){
161 systemDatabase_->releaseCheckpointMutex();
163 printError(ErrSysInternal, "Catalog table updation failed for INDEX table");
164 return ErrSysInternal;
167 if(trylock){
168 systemDatabase_->releaseCheckpointMutex();
170 return rv;