|
| 1 | +%{ |
| 2 | +#include<stdio.h> |
| 3 | +#include<string.h> |
| 4 | +#include<stdlib.h> |
| 5 | +#include <unistd.h> |
| 6 | +void yyerror(char *); |
| 7 | +extern char * yytext; |
| 8 | + |
| 9 | +void changedir(char *); |
| 10 | +%} |
| 11 | + |
| 12 | +%union { |
| 13 | + char *sval; |
| 14 | +} |
| 15 | + |
| 16 | +%token <sval> OWNER_OBJECT |
| 17 | + |
| 18 | +%token <sval> COMANDO |
| 19 | +%token <sval> CREATE_TABLE |
| 20 | +%token <sval> CREATE_INDEX |
| 21 | +%token <sval> ALTER_TABLE |
| 22 | +%token <sval> FK |
| 23 | +%token <sval> FK_NAME |
| 24 | +%token <sval> ADD_CONSTRAINT |
| 25 | +%token <sval> COLUMN |
| 26 | + |
| 27 | +%token <sval> CHECK |
| 28 | +%token <sval> CHECK_NAME |
| 29 | + |
| 30 | +%token FIM_COMANDO |
| 31 | +%start linhas |
| 32 | + |
| 33 | +%% |
| 34 | +linhas : |
| 35 | + | linhas instrucao |
| 36 | + ; |
| 37 | + |
| 38 | +instrucao: CREATE_TABLE OWNER_OBJECT FIM_COMANDO { |
| 39 | + char *owner; |
| 40 | + char *object; |
| 41 | + owner = strtok ($2,"."); |
| 42 | + object = strtok (NULL, "."); |
| 43 | + char str[1024]; |
| 44 | + strcpy(str,"select case count(1) when 1 then 'ok' else 'error' end from dba_tables t where t.owner='"); |
| 45 | + strcat(str,owner); |
| 46 | + strcat(str,"' and t.table_name='"); |
| 47 | + strcat(str,object); |
| 48 | + strcat(str,"';"); |
| 49 | + printf("\n--CREATE TABLE\n%s\n",str); |
| 50 | + //printf("select case count(1) when 1 then 'ok' else 'error' end from dba_tables t where t.owner='%s' and t.table_name='%s';\n",owner,object); |
| 51 | + } |
| 52 | + |
| 53 | + | CREATE_INDEX OWNER_OBJECT OWNER_OBJECT FIM_COMANDO { |
| 54 | + char *owner; |
| 55 | + char *object; |
| 56 | + owner = strtok ($2,"."); |
| 57 | + object = strtok (NULL, "."); |
| 58 | + char str[1024]; |
| 59 | + strcpy(str,"select case count(1) when 1 then 'ok' else 'error' end from dba_indexes t where t.owner='"); |
| 60 | + strcat(str,owner); |
| 61 | + strcat(str,"' and t.index_name='"); |
| 62 | + strcat(str,object); |
| 63 | + strcat(str,"';"); |
| 64 | + printf("\n--CREATE INDEX\n%s\n",str); |
| 65 | + //printf("select case count(1) when 1 then 'ok' else 'error' end from dba_indexes t where t.owner='%s' and t.index_name='%s';\n",owner,object); |
| 66 | + } |
| 67 | + | ALTER_TABLE OWNER_OBJECT ADD_CONSTRAINT FK_NAME FK OWNER_OBJECT FIM_COMANDO { |
| 68 | + char *owner; |
| 69 | + char *object; |
| 70 | + owner = strtok ($2,"."); |
| 71 | + char str[1024]; |
| 72 | + strcpy(str,"select case count(1) when 1 then 'ok' else 'error' end from dba_constraints t where t.owner='"); |
| 73 | + strcat(str,owner); |
| 74 | + strcat(str,"' and t.constraint_name='"); |
| 75 | + strcat(str,$4); |
| 76 | + strcat(str,"';"); |
| 77 | + printf("\n--ADD CONSTRAINT FK\n%s\n",str); |
| 78 | + //printf("select case count(1) when 1 then 'ok' else 'error' end from dba_constraints t where t.owner='%s' and t.constraint_name='%s';\n",owner,$4); |
| 79 | + } |
| 80 | + |
| 81 | + | ALTER_TABLE OWNER_OBJECT COLUMN FIM_COMANDO { char *owner; |
| 82 | + char *object; |
| 83 | + owner = strtok ($2,"."); |
| 84 | + object = strtok (NULL, "."); |
| 85 | + char str[1024]; |
| 86 | + strcpy(str,"select case count(1) when 1 then 'ok' else 'error' end from dba_tab_columns t where t.owner='"); |
| 87 | + strcat(str,owner); |
| 88 | + strcat(str,"' and t.table_name='"); |
| 89 | + strcat(str,object); |
| 90 | + strcat(str,"' and t.column_name='"); |
| 91 | + strcat(str,$3); |
| 92 | + strcat(str,"';"); |
| 93 | + printf("\n--ADD COLUMN\n%s\n",str); |
| 94 | + //printf("select case count(1) when 1 then 'ok' else 'error' end from dba_tab_columns t where t.owner='%s' and t.table_name='%s' and t.column_name='%s';\n",owner,object,$3); |
| 95 | + } |
| 96 | + |
| 97 | + | ALTER_TABLE OWNER_OBJECT ADD_CONSTRAINT CHECK_NAME CHECK COLUMN FIM_COMANDO { |
| 98 | + char *owner; |
| 99 | + char *object; |
| 100 | + owner = strtok ($2,"."); |
| 101 | + char str[1024]; |
| 102 | + strcpy(str,"select case count(1) when 1 then 'ok' else 'error' end from dba_constraints t where t.owner='"); |
| 103 | + strcat(str,owner); |
| 104 | + strcat(str,"' and t.constraint_name='"); |
| 105 | + strcat(str,$4); |
| 106 | + strcat(str,"';"); |
| 107 | + printf("\n--ADD CHECK CONSTRAINT\n%s\n",str); |
| 108 | + //printf("select case count(1) when 1 then 'ok' else 'error' end from dba_constraints t where t.owner='%s' and t.constraint_name='%s';\n",owner,$4); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + | FIM_COMANDO {} |
| 113 | + | OWNER_OBJECT {} |
| 114 | + | FK_NAME {} |
| 115 | + | COLUMN {} |
| 116 | +// |
| 117 | + ; |
| 118 | + |
| 119 | +%% |
| 120 | + |
| 121 | +int main(int argc, char **argv) |
| 122 | +{ |
| 123 | + |
| 124 | + return yyparse(); |
| 125 | +} |
| 126 | + |
| 127 | +/* função usada pelo bison para dar mensagens de erro */ |
| 128 | +void yyerror(char *msg) |
| 129 | +{ |
| 130 | + fprintf(stderr, "erro: %s \n", msg); |
| 131 | +} |
0 commit comments