1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
17 #include<DatabaseManager.h>
18 #include<DatabaseManagerImpl.h>
22 #include<Transaction.h>
23 #include<CatalogTables.h>
30 DbRetVal
DatabaseManagerImpl::createForeignKey(char *fKName
,ForeignKeyInfo
*info
)
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");
42 rv
= systemDatabase_
->getXCheckpointMutex();
45 printError(ErrSysInternal
, "Unable to get database mutex");
46 return ErrSysInternal
;
48 CatalogTableTABLE
cTable(systemDatabase_
);
49 cTable
.getChunkAndTblPtr(info
->fkTableName
, chunk
, tptr
, vcchunk
);
52 systemDatabase_
->releaseCheckpointMutex();
53 printError(ErrNotExists
, "Table does not exist %s", info
->fkTableName
);
56 char **fptr
= new char* [totFkFlds
];
57 CatalogTableFIELD
cField(systemDatabase_
);
58 rv
= cField
.getFieldPtrs(info
->fkFldList
, tptr
, fptr
);
62 systemDatabase_
->releaseCheckpointMutex();
63 if (rv
!= ErrBadCall
) {
64 printError(ErrNotExists
, "Field does not exist");
70 void *vcchunkPk
= NULL
;
71 CatalogTableTABLE
c2Table(systemDatabase_
);
72 c2Table
.getChunkAndTblPtr(info
->pkTableName
, chunkPk
, tPkptr
, vcchunkPk
);
75 systemDatabase_
->releaseCheckpointMutex();
76 printError(ErrNotExists
, "Table does not exist %s", info
->pkTableName
);
79 char **fPkptr
= new char* [totPkFlds
];
80 CatalogTableFIELD
c2Field(systemDatabase_
);
81 rv
= c2Field
.getFieldPtrs(info
->pkFldList
, tPkptr
, fPkptr
);
86 systemDatabase_
->releaseCheckpointMutex();
87 if (rv
!= ErrBadCall
) {
88 printError(ErrNotExists
, "Field does not exist");
92 //Create New chunkdatanode
93 CatalogTableFK
cFK(systemDatabase_
);
94 rv
= cFK
.insert(fKName
, tptr
, tPkptr
);//TODO
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
);
111 systemDatabase_
->releaseCheckpointMutex();
112 printError(ErrSysInternal
, "Catalog table updation failed in CFKFIELD table");
113 return ErrSysInternal
;
115 systemDatabase_
->releaseCheckpointMutex();
121 DbRetVal
DatabaseManagerImpl::dropForeignKey(void *tptr
,bool trylock
)
125 rv
= systemDatabase_
->getXCheckpointMutex();
128 printError(ErrSysInternal
, "Unable to get database mutex");
129 return ErrSysInternal
;
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
);
142 systemDatabase_
->releaseCheckpointMutex();
144 printError(ErrSysInternal
, "Catalog table not finds CFKFIELD table");
145 return ErrSysInternal
;
147 CatalogTableFKFIELD
cFKField(systemDatabase_
);
148 rv
= cFKField
.remove(fkChunk
);
152 systemDatabase_
->releaseCheckpointMutex();
154 printError(ErrSysInternal
, "Catalog table updation failed in CFKFIELD table");
155 return ErrSysInternal
;
157 rv
=cFK
.remove(fkChunk
);
161 systemDatabase_
->releaseCheckpointMutex();
163 printError(ErrSysInternal
, "Catalog table updation failed for INDEX table");
164 return ErrSysInternal
;
168 systemDatabase_
->releaseCheckpointMutex();