isqlodbc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #ifdef WIN32
  5. #include <io.h>
  6. #include <windows.h>
  7. #endif
  8. #ifdef UNIX
  9. #include <sqltypes.h>
  10. #endif
  11. #include <sql.h>
  12. #include <sqlext.h>
  13. #define BUF_LENGTH 65000
  14. /* environment variable */
  15. SQLHENV env=NULL;
  16. SQLHDBC dbc=NULL;
  17. SQLHSTMT stmt=NULL;
  18. SQLHSTMT cstmt=NULL;
  19. unsigned char *dsn;
  20. unsigned char *user;
  21. unsigned char *pass;
  22. RETCODE retcod;
  23. /*description a columns of result of request */
  24. SQLSMALLINT ColumnCount;
  25. unsigned int ColNumber;
  26. unsigned char ColName[SQL_MAX_COLUMN_NAME_LEN];
  27. unsigned int Length;
  28. unsigned int Type;
  29. unsigned int Size;
  30. unsigned int Digits;
  31. unsigned int Nullable;
  32. unsigned char data_buf[BUF_LENGTH];
  33. unsigned long OutData;
  34. /* function print error message*/
  35. void PrintError(HENV env,HDBC dbc,HSTMT stmt,RETCODE retcod)
  36. {
  37. SQLINTEGER nError;
  38. SQLSMALLINT TextLength;
  39. unsigned char BufErrMsg[SQL_MAX_MESSAGE_LENGTH+1];
  40. unsigned char SqlState[128];
  41. SQLError(env,dbc,stmt,SqlState,&nError,BufErrMsg,512, &TextLength);
  42. printf("%s\n" ,BufErrMsg);
  43. }
  44. void sqlquery(SQLHDBC dbc,SQLHSTMT stmt, unsigned char *strquery)
  45. {
  46. retcod=SQLAllocStmt(dbc, &stmt);
  47. retcod=SQLExecDirect(stmt,strquery,SQL_NTS);
  48. if(retcod!=SQL_SUCCESS)
  49. { PrintError(env,dbc,stmt,retcod);}
  50. SQLNumResultCols(stmt,&ColumnCount);
  51. while(SQLFetch(stmt)==SQL_SUCCESS)
  52. {
  53. for(ColNumber=1; ColNumber<=ColumnCount ; ColNumber++)
  54. {
  55. SQLGetData(stmt,ColNumber,SQL_CHAR,data_buf,BUF_LENGTH,&OutData);
  56. printf("%s|",data_buf);
  57. }
  58. printf("\n",data_buf);
  59. strcpy(data_buf,"");
  60. }
  61. SQLFreeStmt( stmt, SQL_DROP );
  62. }
  63. /* isqlodbc dsn[[,user][,pass]] ["SQLCMD"] */
  64. int main(int argc, char *argv[])
  65. {
  66. unsigned char qbuf[64000];
  67. unsigned char *ptr=NULL;
  68. /* Allocate environment and database connection handles */
  69. retcod=SQLAllocEnv( &env );
  70. if(retcod!=SQL_SUCCESS)
  71. {
  72. PrintError(env,dbc,stmt,retcod);
  73. SQLFreeEnv(env);
  74. return (-1);
  75. }
  76. retcod = SQLAllocConnect( env, &dbc );
  77. if(retcod!=SQL_SUCCESS)
  78. {
  79. PrintError(env,dbc,stmt,retcod);
  80. SQLFreeConnect( dbc );
  81. return (-1);
  82. }
  83. if(argc > 1 )
  84. {
  85. /* parsing command line and get parametrs */
  86. dsn = strtok(argv[1],",");
  87. user = strtok(NULL, ",");
  88. pass = strtok(NULL, ",");
  89. /* Connect from DSN */
  90. retcod=SQLConnect(dbc,dsn,SQL_NTS,user,SQL_NTS,pass,SQL_NTS);
  91. if(retcod!=SQL_SUCCESS)
  92. { PrintError(env,dbc,stmt,retcod); }
  93. else
  94. {
  95. if (argc > 2)
  96. {
  97. /*sql cmd from command line*/
  98. sqlquery(dbc,stmt,argv[2]);
  99. }
  100. else
  101. {
  102. /*sql cmd from stdin */
  103. if( isatty(0) ){ printf(".tables - list table\n.q - exit\nsql>"); }
  104. while(fgets(qbuf,63000,stdin) != NULL )
  105. {
  106. ptr=strrchr(qbuf,';');
  107. if (ptr!=NULL)
  108. {
  109. sqlquery(dbc,stmt,qbuf);
  110. }
  111. else
  112. {
  113. /*cmd exit*/
  114. if (strstr(qbuf,".q")){ break; };
  115. /*cmd table list*/
  116. if (strstr(qbuf,".tables"))
  117. {
  118. retcod=SQLAllocStmt(dbc, &stmt);
  119. if(retcod!=SQL_SUCCESS){ PrintError(env,dbc,stmt,retcod); }
  120. else
  121. {
  122. retcod=SQLTables(stmt,NULL,0,NULL,0,NULL,0,NULL,0);
  123. if(retcod !=SQL_SUCCESS) { PrintError(env,dbc,stmt,retcod);}
  124. while(SQLFetch(stmt)==SQL_SUCCESS)
  125. {
  126. SQLGetData(stmt,3,SQL_CHAR,data_buf,BUF_LENGTH,&OutData);
  127. printf("%s|",data_buf);
  128. /*list columns */
  129. retcod=SQLAllocStmt(dbc, &cstmt);
  130. retcod=SQLColumns(cstmt,NULL,0,NULL,0,data_buf,strlen(data_buf),NULL,0);
  131. if(retcod !=SQL_SUCCESS) { PrintError(env,dbc,stmt,retcod);}
  132. else
  133. {
  134. printf("create table %s (",data_buf);
  135. while(SQLFetch(cstmt)==SQL_SUCCESS)
  136. {
  137. SQLGetData(cstmt,4,SQL_CHAR,data_buf,BUF_LENGTH,&OutData);
  138. printf("%s ",data_buf);
  139. SQLGetData(cstmt,6,SQL_CHAR,data_buf,BUF_LENGTH,&OutData);
  140. printf("%s, ",data_buf);
  141. }
  142. printf(");\n");
  143. SQLFreeStmt( cstmt, SQL_DROP );
  144. }/*end list columns*/
  145. }/*end while SQLFetch */
  146. SQLFreeStmt( stmt, SQL_DROP );
  147. }
  148. }/*end if (strstr(qbuf,".tables")) */
  149. } /*end else cmd*/
  150. if( isatty(0) ){ printf("sql>"); }
  151. } /*end while*/
  152. }
  153. }
  154. SQLDisconnect(dbc);
  155. } /* if (argc > 2) */
  156. else
  157. {
  158. printf("isqlodbc dsn[[,user][,pass]] [\"SQLCMD\"]\n");
  159. }
  160. SQLFreeConnect( dbc );
  161. SQLFreeEnv( env );
  162. return 0;
  163. }