Exemplo n.º 1
0
void printTable(char *tbl){
	if(tbl == NULL){     //mostra todas as tabelas do banco
		FILE *dicionario;
		printf("		List of Relations\n");
		char *tupla = (char *)malloc(sizeof(char)*TAMANHO_NOME_TABELA);

		char directory[LEN_DB_NAME*2];
    	strcpy(directory, connected.db_directory);
    	strcat(directory, "fs_object.dat");

		if((dicionario = fopen(directory,"a+b")) == NULL){
			free(tupla);
			printf("ERROR: cannot open file\n");
			return;
		}

		printf(" %-10s | %-15s | %-10s | %-10s\n", "Schema", "Name", "Type", "Owner");
		printf("------------+-----------------+------------+-------\n");
		int i=0;
		while(fgetc (dicionario) != EOF){
			fseek(dicionario, -1, 1);
			fread(tupla, sizeof(char), TAMANHO_NOME_TABELA, dicionario);
			printf(" %-10s | %-15s | %-10s | %-10s\n", "public", tupla, "tuple", "ibetres");
			fseek(dicionario, 28, 1);
			i++;
		}
		fclose(dicionario);
		free(tupla);
		printf("(%d %s)\n\n", i, (i<=1)? "row": "rows");
	} else{               //mostra todos atributos da tabela *tbl

		if(!verificaNomeTabela(tbl)) {
			printf("Did not find any relation named \"%s\".\n", tbl);
			return;
		}

		printf("	  Table \"public.%s\"\n", tbl);
		printf(" %-18s | %-12s | %-10s\n", "Column", "Type", "Modifiers");
		printf("--------------------+--------------+-----------\n");

		struct fs_objects objeto1;
		tp_table *esquema1;

		abreTabela(tbl, &objeto1, &esquema1);

		tp_table *tab3 = (tp_table *)malloc(sizeof(struct tp_table));
		tab3 = procuraAtributoFK(objeto1); //retorna tp_table
		int l, ipk=0, ifk=0;

		char **pk 			= (char**)malloc(objeto1.qtdCampos*sizeof(char**));
		char **fkTable		= (char**)malloc(objeto1.qtdCampos*sizeof(char**));
		char **fkColumn 	= (char**)malloc(objeto1.qtdCampos*sizeof(char**));
		char **refColumn 	= (char**)malloc(objeto1.qtdCampos*sizeof(char**));

		memset(pk 		, 0, objeto1.qtdCampos);
		memset(fkTable 	, 0, objeto1.qtdCampos);
		memset(fkColumn , 0, objeto1.qtdCampos);
		memset(refColumn, 0, objeto1.qtdCampos);

		int i;
		for(i=0; i<objeto1.qtdCampos; i++) {
			pk[i] 			= (char*)malloc(TAMANHO_NOME_CAMPO*sizeof(char));
			fkTable[i] 		= (char*)malloc(TAMANHO_NOME_CAMPO*sizeof(char));
			fkColumn[i] 	= (char*)malloc(TAMANHO_NOME_CAMPO*sizeof(char));
			refColumn[i] 	= (char*)malloc(TAMANHO_NOME_CAMPO*sizeof(char));

			memset(pk[i] 		, '\0', TAMANHO_NOME_CAMPO);
			memset(fkTable[i] 	, '\0', TAMANHO_NOME_CAMPO);
			memset(fkColumn[i]  , '\0', TAMANHO_NOME_CAMPO);
			memset(refColumn[i] , '\0', TAMANHO_NOME_CAMPO);

		}

		for(l=0; l<objeto1.qtdCampos; l++) {

			if(tab3[l].chave == PK){
				strcpylower(pk[ipk++], tab3[l].nome);
			}
			else if(tab3[l].chave == FK){
				strcpylower(fkTable[ifk]	, tab3[l].tabelaApt);
				strcpylower(fkColumn[ifk]	, tab3[l].attApt);
				strcpylower(refColumn[ifk++], tab3[l].nome);
			}

			printf("  %-17s |", tab3[l].nome);

			if(tab3[l].tipo == 'S')
				printf(" %-8s(%d) |", " varchar", tab3[l].tam);
			else if(tab3[l].tipo == 'I')
				printf(" %-10s   |", " integer");
			else if(tab3[l].tipo == 'C')
				printf(" %-10s   |", " char");
			else if(tab3[l].tipo == 'D')
				printf(" %-10s   |", " double");

			printf(" %-10s ", (tab3[l].chave == PK || tab3[l].chave == FK)? "not null": "null");

			printf("\n");
		}
		if(ipk){	//printf PK's
			printf("Indexes:\n");
			for(l = 0; l < ipk; l++){
				printf("\t\"%s_pkey\" PRIMARY KEY (%s)\n", tbl, pk[l]);
			}
		}
		if(ifk){	//printf FK's
			printf("Foreign-key constrains:\n");
			for(l = 0; l < ifk; l++){
				printf("\t\"%s_%s_fkey\" FOREIGN KEY (%s) REFERENCES %s(%s)\n",tbl, refColumn[l], refColumn[l], fkTable[l], fkColumn[l]);
			}
		}

		free(pk);
		free(fkTable);
		free(fkColumn);
		free(refColumn);
		free(tab3);
		printf("\n");
	}
}
Exemplo n.º 2
0
int excluirTabela(char *nomeTabela) {
    struct fs_objects objeto, objeto1;
    tp_table *esquema, *esquema1;
    int x,erro, i, j, k, l, qtTable;
    char str[20];
    char dat[5] = ".dat";
    memset(str, '\0', 20);

    if (!verificaNomeTabela(nomeTabela)) {
        printf("ERROR: table \"%s\" does not exist.\n", nomeTabela);
        return ERRO_NOME_TABELA;
    }

    strcpylower(str, nomeTabela);
    strcat(str, dat);              //Concatena e junta o nome com .dat

    abreTabela(nomeTabela, &objeto, &esquema);
    qtTable = quantidadeTabelas();

    char **tupla = (char **)malloc(sizeof(char **)*qtTable);
    memset(tupla, 0, qtTable);

    for (i=0; i < qtTable; i++) {
        tupla[i] = (char *)malloc(sizeof(char)*TAMANHO_NOME_TABELA);
        memset(tupla[i], '\0', TAMANHO_NOME_TABELA);
    }

    tp_table *tab2 = (tp_table *)malloc(sizeof(struct tp_table));
    tab2 = procuraAtributoFK(objeto);   //retorna o tipo de chave que e cada campo

    FILE *dicionario;

    char directory[LEN_DB_NAME*2];
    memset(directory, '\0', LEN_DB_NAME*2);

    strcpy(directory, connected.db_directory);
    strcat(directory, "fs_object.dat");

    if((dicionario = fopen(directory,"a+b")) == NULL)
        return ERRO_ABRIR_ARQUIVO;

    k=0;
    while(fgetc (dicionario) != EOF){
        fseek(dicionario, -1, 1);

        //coloca o nome de todas as tabelas em tupla
        fread(tupla[k], sizeof(char), TAMANHO_NOME_TABELA , dicionario);
        k++;

        fseek(dicionario, 28, 1);
    }

    fclose(dicionario);

    for(i = 0; i < objeto.qtdCampos; i++){
        if(tab2[i].chave == PK){
            for(j=0; j<qtTable; j++) {                      //se tiver chave primaria verifica se ela e chave
                if(objcmp(tupla[j], nomeTabela) != 0) {     //estrangeira em outra tabela

                    abreTabela(tupla[j], &objeto1, &esquema1);

                    tp_table *tab3 = (tp_table *)malloc(sizeof(struct tp_table));
                    tab3 = procuraAtributoFK(objeto1);

                    for(l=0; l<objeto1.qtdCampos; l++) {
                        if(tab3[l].chave == FK) { //verifica se a outra tabela possui chave estrangeira. se sim, verifica se e da tabela anterior.
                            if(objcmp(nomeTabela, tab3[l].tabelaApt) == 0) {
                                printf("ERROR: cannot drop table \"%s\" because other objects depend on it.\n", nomeTabela);
                                return ERRO_CHAVE_ESTRANGEIRA;
                            }
                        }
                    }
                    free(tab3);
                }
            }
        }
    }

    free(tab2);

    tp_buffer *bufferpoll = initbuffer();

    if(bufferpoll == ERRO_DE_ALOCACAO){
        printf("ERROR: no memory available to allocate buffer.\n");
        return ERRO_LEITURA_DADOS;
    }

    erro = SUCCESS;
    for(x = 0; erro == SUCCESS; x++)
        erro = colocaTuplaBuffer(bufferpoll, x, esquema, objeto);

    if(procuraSchemaArquivo(objeto) != 0) {
        free(bufferpoll);
        return ERRO_REMOVER_ARQUIVO_SCHEMA;
    }

    if(procuraObjectArquivo(nomeTabela) != 0) {
        free(bufferpoll);
        return ERRO_REMOVER_ARQUIVO_OBJECT;
    }

   	strcpy(directory, connected.db_directory);
    strcat(directory, str);

    remove(directory);

    free(bufferpoll);

    printf("DROP TABLE\n");

    return SUCCESS;
}
Exemplo n.º 3
0
int finalizaInsert(char *nome, column *c) {
    column *auxC, *temp;
    int i = 0, x = 0, t, erro, j = 0;
    FILE *dados;

    struct fs_objects objeto,dicio; // Le dicionario
    tp_table *auxT ; // Le esquema
    auxT = abreTabela(nome, &dicio, &auxT);

    table *tab     = (table *)malloc(sizeof(table));
    tp_table *tab2 = (tp_table *)malloc(sizeof(struct tp_table));
    memset(tab2, 0, sizeof(tp_table));

    tab->esquema = abreTabela(nome, &objeto, &tab->esquema);
    tab2 = procuraAtributoFK(objeto);

    for(j = 0, temp = c; j < objeto.qtdCampos && temp != NULL; j++, temp = temp->next) {
        switch(tab2[j].chave) {
        case NPK:
            erro = SUCCESS;
            break;

        case PK:
            erro = verificaChavePK(nome, temp , temp->nomeCampo, temp->valorCampo);
            if (erro == ERRO_CHAVE_PRIMARIA) {
                printf("ERROR: duplicate key value violates unique constraint \"%s_pkey\"\nDETAIL:  Key (%s)=(%s) already exists.\n", nome, temp->nomeCampo, temp->valorCampo);

                free(auxT); // Libera a memoria da estrutura.
                //free(temp); // Libera a memoria da estrutura.
                free(tab); // Libera a memoria da estrutura.
                free(tab2); // Libera a memoria da estrutura.
                return ERRO_CHAVE_PRIMARIA;
            }
            if (erro == ERRO_CHAVE_PRIMARIA_NULA) {
                printf("ERROR:  null value in column '%s' violates not-null constraint \"%s_pkey\"\nDETAIL: Primary Key (%s) is null.\n",temp->nomeCampo,nome,temp->nomeCampo);

                free(auxT); // Libera a memoria da estrutura.
                //free(temp); // Libera a memoria da estrutura.
                free(tab); // Libera a memoria da estrutura.
                free(tab2); // Libera a memoria da estrutura.
                return ERRO_CHAVE_PRIMARIA_NULA;
            }

            break;

        case FK:
            if (tab2[j].chave == 2 && strlen(tab2[j].attApt) != 0 && strlen(tab2[j].tabelaApt) != 0) {

                erro = verificaChaveFK(nome, temp, tab2[j].nome, temp->valorCampo, tab2[j].tabelaApt, tab2[j].attApt);

                if (erro != SUCCESS) {
                    printf("ERROR: invalid reference to \"%s.%s\". The value \"%s\" does not exist.\n", tab2[j].tabelaApt,tab2[j].attApt,temp->valorCampo);

                    free(auxT); // Libera a memoria da estrutura.
                    free(temp); // Libera a memoria da estrutura.
                    free(tab); // Libera a memoria da estrutura.
                    free(tab2); // Libera a memoria da estrutura.
                    return ERRO_CHAVE_ESTRANGEIRA;
                }
            }

            break;
        }
    }


    if (erro == ERRO_CHAVE_ESTRANGEIRA) {
        printf("ERROR: unknown foreign key error.\n");

        free(auxT); // Libera a memoria da estrutura.
        free(temp); // Libera a memoria da estrutura.
        free(tab); // Libera a memoria da estrutura.
        free(tab2); // Libera a memoria da estrutura.
        return ERRO_CHAVE_ESTRANGEIRA;
    }

    if (erro == ERRO_CHAVE_PRIMARIA) {
        printf("ERROR: unknown primary key error.\n");

        free(auxT); // Libera a memoria da estrutura.
        free(temp); // Libera a memoria da estrutura.
        free(tab); // Libera a memoria da estrutura.
        free(tab2); // Libera a memoria da estrutura.
        return ERRO_CHAVE_PRIMARIA;
    }
    if (erro == ERRO_DE_PARAMETRO) {
        printf("ERROR: invalid parameter.\n");
        free(auxT); // Libera a memoria da estrutura.
        free(temp); // Libera a memoria da estrutura.
        free(tab); // Libera a memoria da estrutura.
        free(tab2); // Libera a memoria da estrutura.
        return ERRO_DE_PARAMETRO;
    }

    char directory[LEN_DB_NAME*2];
    strcpy(directory, connected.db_directory);
    strcat(directory, dicio.nArquivo);

    if((dados = fopen(directory,"a+b")) == NULL) {
        printf("ERROR: cannot open file.\n");
        free(auxT); // Libera a memoria da estrutura.
        free(temp); // Libera a memoria da estrutura.
        free(tab); // Libera a memoria da estrutura.
        free(tab2); // Libera a memoria da estrutura.
        return ERRO_ABRIR_ARQUIVO;

    }

    for(auxC = c, t = 0; auxC != NULL; auxC = auxC->next, t++) {
        if (t >= dicio.qtdCampos)
            t = 0;



        if (auxT[t].tipo == 'S') { // Grava um dado do tipo string.

            if (sizeof(auxC->valorCampo) > auxT[t].tam && sizeof(auxC->valorCampo) != 8) {
                printf("ERROR: invalid string length.\n");
                free(tab); // Libera a memoria da estrutura.
                free(tab2); // Libera a memoria da estrutura.
                free(auxT); // Libera a memoria da estrutura.
                free(temp); // Libera a memoria da estrutura.
                fclose(dados);
                return ERRO_NO_TAMANHO_STRING;
            }

            if (objcmp(auxC->nomeCampo, auxT[t].nome) != 0) {
                printf("ERROR: column name \"%s\" is not valid.\n", auxC->nomeCampo);
                free(tab); // Libera a memoria da estrutura.
                free(tab2); // Libera a memoria da estrutura.
                free(auxT); // Libera a memoria da estrutura.
                free(temp); // Libera a memoria da estrutura.
                fclose(dados);
                return ERRO_NOME_CAMPO;
            }

            char valorCampo[auxT[t].tam];
            strncpy(valorCampo, auxC->valorCampo, auxT[t].tam);
            strcat(valorCampo, "\0");
            fwrite(&valorCampo,sizeof(valorCampo),1,dados);

        } else if (auxT[t].tipo == 'I') { // Grava um dado do tipo inteiro.
            i = 0;
            while (i < strlen(auxC->valorCampo)) {
                if (auxC->valorCampo[i] < 48 || auxC->valorCampo[i] > 57) {
                    printf("ERROR: column \"%s\" expectet integer.\n", auxC->nomeCampo);
                    free(tab); // Libera a memoria da estrutura.
                    free(tab2); // Libera a memoria da estrutura.
                    free(auxT); // Libera a memoria da estrutura.
                    //free(temp); // Libera a memoria da estrutura.
                    fclose(dados);
                    return ERRO_NO_TIPO_INTEIRO;
                }
                i++;
            }

            int valorInteiro = 0;

            sscanf(auxC->valorCampo,"%d",&valorInteiro);
            fwrite(&valorInteiro,sizeof(valorInteiro),1,dados);

        } else if (auxT[t].tipo == 'D') { // Grava um dado do tipo double.
            x = 0;
            while (x < strlen(auxC->valorCampo)) {
                if((auxC->valorCampo[x] < 48 || auxC->valorCampo[x] > 57) && (auxC->valorCampo[x] != 46)) {
                    printf("ERROR: column \"%s\" expect double.\n", auxC->nomeCampo);
                    free(tab); // Libera a memoria da estrutura.
                    free(tab2); // Libera a memoria da estrutura.
                    free(auxT); // Libera a memoria da estrutura.
                    free(temp); // Libera a memoria da estrutura.
                    fclose(dados);
                    return ERRO_NO_TIPO_DOUBLE;
                }
                x++;
            }

            double valorDouble = convertD(auxC->valorCampo);
            fwrite(&valorDouble,sizeof(valorDouble),1,dados);
        }
        else if (auxT[t].tipo == 'C') { // Grava um dado do tipo char.

            if (strlen(auxC->valorCampo) > (sizeof(char))) {
                printf("ERROR: column \"%s\" expect char.\n", auxC->nomeCampo);
                free(tab); // Libera a memoria da estrutura.
                free(tab2); // Libera a memoria da estrutura.
                free(auxT); // Libera a memoria da estrutura.
                free(temp); // Libera a memoria da estrutura.
                fclose(dados);
                return ERRO_NO_TIPO_CHAR;
            }
            char valorChar = auxC->valorCampo[0];
            fwrite(&valorChar,sizeof(valorChar),1,dados);
        }

    }

    fclose(dados);
    free(tab); // Libera a memoria da estrutura.
    free(tab2); // Libera a memoria da estrutura.
    free(auxT); // Libera a memoria da estrutura.
    free(temp); // Libera a memoria da estrutura.
    return SUCCESS;
}