|
| 1 | +#include<ecpgtype.h> |
| 2 | +#include<ecpglib.h> |
| 3 | +#include<ecpgerrno.h> |
| 4 | +#include"extern.h" |
| 5 | +#include<sqlca.h> |
| 6 | + |
| 7 | +staticstructconnection*all_connections=NULL,*actual_connection=NULL; |
| 8 | + |
| 9 | +structconnection* |
| 10 | +get_connection(constchar*connection_name) |
| 11 | +{ |
| 12 | +structconnection*con=all_connections; |
| 13 | + |
| 14 | +if (connection_name==NULL||strcmp(connection_name,"CURRENT")==0) |
| 15 | +returnactual_connection; |
| 16 | + |
| 17 | +for (;con&&strcmp(connection_name,con->name)!=0;con=con->next); |
| 18 | +if (con) |
| 19 | +returncon; |
| 20 | +else |
| 21 | +returnNULL; |
| 22 | +} |
| 23 | + |
| 24 | +staticvoid |
| 25 | +ecpg_finish(structconnection*act) |
| 26 | +{ |
| 27 | +if (act!=NULL) |
| 28 | +{ |
| 29 | +ECPGlog("ecpg_finish: finishing %s.\n",act->name); |
| 30 | +PQfinish(act->connection); |
| 31 | + |
| 32 | +/* remove act from the list */ |
| 33 | +if (act==all_connections) |
| 34 | +all_connections=act->next; |
| 35 | +else |
| 36 | +{ |
| 37 | +structconnection*con; |
| 38 | + |
| 39 | +for (con=all_connections;con->next&&con->next!=act;con=con->next); |
| 40 | +if (con->next) |
| 41 | +con->next=act->next; |
| 42 | +} |
| 43 | + |
| 44 | +if (actual_connection==act) |
| 45 | +actual_connection=all_connections; |
| 46 | + |
| 47 | +free(act->name); |
| 48 | +free(act); |
| 49 | +} |
| 50 | +else |
| 51 | +ECPGlog("ecpg_finish: called an extra time.\n"); |
| 52 | +} |
| 53 | + |
| 54 | +bool |
| 55 | +ECPGsetcommit(intlineno,constchar*mode,constchar*connection_name) |
| 56 | +{ |
| 57 | +structconnection*con=get_connection(connection_name); |
| 58 | +PGresult*results; |
| 59 | + |
| 60 | +if (!ecpg_init(con,connection_name,lineno)) |
| 61 | +return(false); |
| 62 | + |
| 63 | +ECPGlog("ECPGsetcommit line %d action = %s connection = %s\n",lineno,mode,con->name); |
| 64 | + |
| 65 | +if (con->autocommit== true&&strncmp(mode,"off",strlen("off"))==0) |
| 66 | +{ |
| 67 | +if (con->committed) |
| 68 | +{ |
| 69 | +if ((results=PQexec(con->connection,"begin transaction"))==NULL) |
| 70 | +{ |
| 71 | +ECPGraise(lineno,ECPG_TRANS,NULL); |
| 72 | +return false; |
| 73 | +} |
| 74 | +PQclear(results); |
| 75 | +con->committed= false; |
| 76 | +} |
| 77 | +con->autocommit= false; |
| 78 | +} |
| 79 | +elseif (con->autocommit== false&&strncmp(mode,"on",strlen("on"))==0) |
| 80 | +{ |
| 81 | +if (!con->committed) |
| 82 | +{ |
| 83 | +if ((results=PQexec(con->connection,"commit"))==NULL) |
| 84 | +{ |
| 85 | +ECPGraise(lineno,ECPG_TRANS,NULL); |
| 86 | +return false; |
| 87 | +} |
| 88 | +PQclear(results); |
| 89 | +con->committed= true; |
| 90 | +} |
| 91 | +con->autocommit= true; |
| 92 | +} |
| 93 | + |
| 94 | +return true; |
| 95 | +} |
| 96 | + |
| 97 | +bool |
| 98 | +ECPGsetconn(intlineno,constchar*connection_name) |
| 99 | +{ |
| 100 | +structconnection*con=get_connection(connection_name); |
| 101 | + |
| 102 | +if (!ecpg_init(con,connection_name,lineno)) |
| 103 | +return(false); |
| 104 | + |
| 105 | +actual_connection=con; |
| 106 | +return true; |
| 107 | +} |
| 108 | + |
| 109 | +bool |
| 110 | +ECPGconnect(intlineno,constchar*dbname,constchar*user,constchar*passwd,constchar*connection_name,intautocommit) |
| 111 | +{ |
| 112 | +structconnection*this; |
| 113 | + |
| 114 | +init_sqlca(); |
| 115 | + |
| 116 | +if ((this= (structconnection*)ecpg_alloc(sizeof(structconnection),lineno))==NULL) |
| 117 | +return false; |
| 118 | + |
| 119 | +if (dbname==NULL&&connection_name==NULL) |
| 120 | +connection_name="DEFAULT"; |
| 121 | + |
| 122 | +/* add connection to our list */ |
| 123 | +if (connection_name!=NULL) |
| 124 | +this->name=ecpg_strdup(connection_name,lineno); |
| 125 | +else |
| 126 | +this->name=ecpg_strdup(dbname,lineno); |
| 127 | + |
| 128 | +if (all_connections==NULL) |
| 129 | +this->next=NULL; |
| 130 | +else |
| 131 | +this->next=all_connections; |
| 132 | + |
| 133 | +actual_connection=all_connections=this; |
| 134 | + |
| 135 | +ECPGlog("ECPGconnect: opening database %s %s%s\n",dbname ?dbname :"<DEFAULT>",user ?"for user " :"",user ?user :""); |
| 136 | + |
| 137 | +this->connection=PQsetdbLogin(NULL,NULL,NULL,NULL,dbname,user,passwd); |
| 138 | + |
| 139 | +if (PQstatus(this->connection)==CONNECTION_BAD) |
| 140 | +{ |
| 141 | +ecpg_finish(this); |
| 142 | +ECPGlog("connect: could not open database %s %s%s in line %d\n",dbname ?dbname :"<DEFAULT>",user ?"for user " :"",user ?user :"",lineno); |
| 143 | +ECPGraise(lineno,ECPG_CONNECT,dbname ?dbname :"<DEFAULT>"); |
| 144 | +return false; |
| 145 | +} |
| 146 | + |
| 147 | +this->committed= true; |
| 148 | +this->autocommit=autocommit; |
| 149 | + |
| 150 | +return true; |
| 151 | +} |
| 152 | + |
| 153 | +bool |
| 154 | +ECPGdisconnect(intlineno,constchar*connection_name) |
| 155 | +{ |
| 156 | +structconnection*con; |
| 157 | + |
| 158 | +if (strcmp(connection_name,"ALL")==0) |
| 159 | +{ |
| 160 | +init_sqlca(); |
| 161 | +for (con=all_connections;con;) |
| 162 | +{ |
| 163 | +structconnection*f=con; |
| 164 | + |
| 165 | +con=con->next; |
| 166 | +ecpg_finish(f); |
| 167 | +} |
| 168 | +} |
| 169 | +else |
| 170 | +{ |
| 171 | +con=get_connection(connection_name); |
| 172 | + |
| 173 | +if (!ecpg_init(con,connection_name,lineno)) |
| 174 | +return(false); |
| 175 | +else |
| 176 | +ecpg_finish(con); |
| 177 | +} |
| 178 | + |
| 179 | +return true; |
| 180 | +} |