⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glpsql.c

📁 著名的大规模线性规划求解器源码GLPK.C语言版本,可以修剪.内有详细帮助文档.
💻 C
📖 第 1 页 / 共 3 页
字号:
/* glpsql.c *//************************************************************************  This code is part of GLPK (GNU Linear Programming Kit).**  Author: Heinrich Schuchardt <xypron.glpk@gmx.de>.**  Copyright (C) 2000,01,02,03,04,05,06,07,08,2009 Andrew Makhorin,*  Department for Applied Informatics, Moscow Aviation Institute,*  Moscow, Russia. All rights reserved. E-mail: <mao@mai2.rcnet.ru>.**  GLPK is free software: you can redistribute it and/or modify it*  under the terms of the GNU General Public License as published by*  the Free Software Foundation, either version 3 of the License, or*  (at your option) any later version.**  GLPK is distributed in the hope that it will be useful, but WITHOUT*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public*  License for more details.**  You should have received a copy of the GNU General Public License*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.***********************************************************************/#define _GLPSTD_STDIO#include "glpmpl.h"#include "glpsql.h"#ifdef ODBC_DLNAME#define HAVE_ODBC#define libodbc ODBC_DLNAME#define h_odbc (lib_link_env()->h_odbc)#endif#ifdef MYSQL_DLNAME#define HAVE_MYSQL#define libmysql MYSQL_DLNAME#define h_mysql (lib_link_env()->h_mysql)#endif/**********************************************************************/#if defined(HAVE_ODBC) || defined(HAVE_MYSQL)#define SQL_FIELD_MAX 100/* maximal field count */#define SQL_FDLEN_MAX 255/* maximal field length */static int db_escaped_string_length(const char* from)/* length of escaped string */{   int         count;   const char *pointer;    for (pointer = from, count = 0; *pointer != (char) '\0'; pointer++,         count++)    {      switch (*pointer)      {         case '\'':            count++;            break;      }    }    return count;}static int db_escape_string (char *to, const char *from)/* escape string*/{   const char *source = from;   char *target = to;   unsigned int remaining;   remaining = strlen(from);   if (to == NULL)     to = (char *) (from + remaining);   while (remaining > 0)   {      switch (*source)      {         case '\'':            *target = '\'';            target++;            *target = '\'';            break;         default:            *target = *source;            }      source++;      target++;      remaining--;      }   /* Write the terminating NUL character. */   *target = '\0';   return target - to;}static char *db_generate_select_stmt(TABDCA *dca)/* generate select statement */{   char        *arg;   char const  *field;   char        *query;   int          j;   int          narg;   int          nf;   int          total;   total = 50;   nf = mpl_tab_num_flds(dca);   narg = mpl_tab_num_args(dca);   for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)   {      field = mpl_tab_get_name(dca, j);      total += strlen(field);      total += 2;   }   arg = (char *) mpl_tab_get_arg(dca, narg);   total += strlen(arg);   query = xmalloc( total * sizeof(char));   strcpy (query, "SELECT ");   for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)   {      field = mpl_tab_get_name(dca, j);      strcat(query, field);      if ( j < nf )         strcat(query, ", ");   }   strcat(query, " FROM ");   strcat(query, arg);   return query;}static char *db_generate_insert_stmt(TABDCA *dca)/* generate insert statement */{   char        *arg;   char const  *field;   char        *query;   int          j;   int          narg;   int          nf;   int          total;   total = 50;   nf = mpl_tab_num_flds(dca);   narg = mpl_tab_num_args(dca);   for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)   {      field = mpl_tab_get_name(dca, j);      total += strlen(field);      total += 5;   }   arg = (char *) mpl_tab_get_arg(dca, narg);   total += strlen(arg);   query = xmalloc( (total+1) * sizeof(char));   strcpy (query, "INSERT INTO ");   strcat(query, arg);   strcat(query, " ( ");   for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)   {      field = mpl_tab_get_name(dca, j);      strcat(query, field);      if ( j < nf )         strcat(query, ", ");   }   strcat(query, " ) VALUES ( ");   for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)   {      strcat(query, "?");      if ( j < nf )         strcat(query, ", ");   }   strcat(query, " )");   return query;}#endif/**********************************************************************/#ifndef HAVE_ODBCvoid *db_iodbc_open(TABDCA *dca, int mode){     xassert(dca == dca);      xassert(mode == mode);      xprintf("iODBC table driver not supported\n");      return NULL;}int db_iodbc_read(TABDCA *dca, void *link){     xassert(dca != dca);      xassert(link != link);      return 0;}int db_iodbc_write(TABDCA *dca, void *link){     xassert(dca != dca);      xassert(link != link);      return 0;}int db_iodbc_close(TABDCA *dca, void *link){     xassert(dca != dca);      xassert(link != link);      return 0;}#else#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__WOE__)#include <windows.h>#endif#include <sql.h>#include <sqlext.h>struct db_odbc{   int              mode;         /*'R' = Read, 'W' = Write*/   SQLHDBC          hdbc;         /*connection handle*/   SQLHENV          henv;         /*environment handle*/   SQLHSTMT         hstmt;        /*statement handle*/   SQLSMALLINT      nresultcols;  /* columns in result*/   SQLULEN          collen[SQL_FIELD_MAX+1];   SQLLEN           outlen[SQL_FIELD_MAX+1];   SQLSMALLINT      coltype[SQL_FIELD_MAX+1];   SQLCHAR          data[SQL_FIELD_MAX+1][SQL_FDLEN_MAX+1];   SQLCHAR          colname[SQL_FIELD_MAX+1][SQL_FDLEN_MAX+1];   int              isnumeric[SQL_FIELD_MAX+1];   int              nf;   /* number of fields in the csv file */   int              ref[1+SQL_FIELD_MAX];   /* ref[k] = k', if k-th field of the csv file corresponds to      k'-th field in the table statement; if ref[k] = 0, k-th field      of the csv file is ignored */   SQLCHAR         *query;   /* query generated by db_iodbc_open */};SQLRETURN SQL_API dl_SQLAllocHandle (   SQLSMALLINT           HandleType,   SQLHANDLE             InputHandle,   SQLHANDLE            *OutputHandle){      typedef SQLRETURN SQL_API ep_SQLAllocHandle(         SQLSMALLINT           HandleType,         SQLHANDLE             InputHandle,         SQLHANDLE            *OutputHandle);      ep_SQLAllocHandle *fn;      fn = (ep_SQLAllocHandle *) xdlsym(h_odbc, "SQLAllocHandle");      xassert(fn != NULL);      return (*fn)(HandleType, InputHandle, OutputHandle);}SQLRETURN SQL_API dl_SQLBindCol (   SQLHSTMT              StatementHandle,   SQLUSMALLINT          ColumnNumber,   SQLSMALLINT           TargetType,   SQLPOINTER            TargetValue,   SQLLEN                BufferLength,   SQLLEN               *StrLen_or_Ind){      typedef SQLRETURN SQL_API ep_SQLBindCol(         SQLHSTMT              StatementHandle,         SQLUSMALLINT          ColumnNumber,         SQLSMALLINT           TargetType,         SQLPOINTER            TargetValue,         SQLLEN                BufferLength,         SQLLEN               *StrLen_or_Ind);      ep_SQLBindCol *fn;      fn = (ep_SQLBindCol *) xdlsym(h_odbc, "SQLBindCol");      xassert(fn != NULL);      return (*fn)(StatementHandle, ColumnNumber, TargetType,         TargetValue, BufferLength, StrLen_or_Ind);}SQLRETURN SQL_API dl_SQLCloseCursor (   SQLHSTMT              StatementHandle){      typedef SQLRETURN SQL_API ep_SQLCloseCursor (         SQLHSTMT              StatementHandle);      ep_SQLCloseCursor *fn;      fn = (ep_SQLCloseCursor *) xdlsym(h_odbc, "SQLCloseCursor");      xassert(fn != NULL);      return (*fn)(StatementHandle);}SQLRETURN SQL_API dl_SQLDisconnect (   SQLHDBC               ConnectionHandle){      typedef SQLRETURN SQL_API ep_SQLDisconnect(         SQLHDBC               ConnectionHandle);      ep_SQLDisconnect *fn;      fn = (ep_SQLDisconnect *) xdlsym(h_odbc, "SQLDisconnect");      xassert(fn != NULL);      return (*fn)(ConnectionHandle);}SQLRETURN SQL_API dl_SQLDriverConnect (   SQLHDBC               hdbc,   SQLHWND               hwnd,   SQLCHAR              *szConnStrIn,   SQLSMALLINT           cbConnStrIn,   SQLCHAR              *szConnStrOut,   SQLSMALLINT           cbConnStrOutMax,   SQLSMALLINT          *pcbConnStrOut,   SQLUSMALLINT          fDriverCompletion){      typedef SQLRETURN SQL_API ep_SQLDriverConnect(         SQLHDBC               hdbc,         SQLHWND               hwnd,         SQLCHAR             * szConnStrIn,         SQLSMALLINT           cbConnStrIn,         SQLCHAR             * szConnStrOut,         SQLSMALLINT           cbConnStrOutMax,         SQLSMALLINT         * pcbConnStrOut,         SQLUSMALLINT          fDriverCompletion);      ep_SQLDriverConnect *fn;      fn = (ep_SQLDriverConnect *) xdlsym(h_odbc, "SQLDriverConnect");      xassert(fn != NULL);      return (*fn)(hdbc, hwnd, szConnStrIn, cbConnStrIn, szConnStrOut,         cbConnStrOutMax, pcbConnStrOut, fDriverCompletion);}SQLRETURN SQL_API dl_SQLEndTran (   SQLSMALLINT           HandleType,   SQLHANDLE             Handle,   SQLSMALLINT           CompletionType){      typedef SQLRETURN SQL_API ep_SQLEndTran (         SQLSMALLINT           HandleType,         SQLHANDLE             Handle,         SQLSMALLINT           CompletionType);      ep_SQLEndTran *fn;      fn = (ep_SQLEndTran *) xdlsym(h_odbc, "SQLEndTran");      xassert(fn != NULL);      return (*fn)(HandleType, Handle, CompletionType);}SQLRETURN SQL_API dl_SQLExecDirect (   SQLHSTMT              StatementHandle,   SQLCHAR             * StatementText,   SQLINTEGER            TextLength){      typedef SQLRETURN SQL_API ep_SQLExecDirect (         SQLHSTMT              StatementHandle,         SQLCHAR             * StatementText,         SQLINTEGER            TextLength);      ep_SQLExecDirect *fn;      fn = (ep_SQLExecDirect *) xdlsym(h_odbc, "SQLExecDirect");      xassert(fn != NULL);      return (*fn)(StatementHandle, StatementText, TextLength);}SQLRETURN SQL_API dl_SQLFetch (   SQLHSTMT              StatementHandle){      typedef SQLRETURN SQL_API ep_SQLFetch (         SQLHSTMT              StatementHandle);      ep_SQLFetch *fn;      fn = (ep_SQLFetch*) xdlsym(h_odbc, "SQLFetch");      xassert(fn != NULL);      return (*fn)(StatementHandle);}SQLRETURN SQL_API dl_SQLFreeHandle (   SQLSMALLINT           HandleType,   SQLHANDLE             Handle){      typedef SQLRETURN SQL_API ep_SQLFreeHandle (         SQLSMALLINT           HandleType,         SQLHANDLE             Handle);      ep_SQLFreeHandle *fn;      fn = (ep_SQLFreeHandle *) xdlsym(h_odbc, "SQLFreeHandle");      xassert(fn != NULL);      return (*fn)(HandleType, Handle);}SQLRETURN SQL_API dl_SQLDescribeCol (   SQLHSTMT              StatementHandle,   SQLUSMALLINT          ColumnNumber,   SQLCHAR             * ColumnName,   SQLSMALLINT           BufferLength,   SQLSMALLINT         * NameLength,   SQLSMALLINT         * DataType,   SQLULEN             * ColumnSize,   SQLSMALLINT         * DecimalDigits,   SQLSMALLINT         * Nullable){      typedef SQLRETURN SQL_API ep_SQLDescribeCol (         SQLHSTMT              StatementHandle,         SQLUSMALLINT          ColumnNumber,         SQLCHAR              *ColumnName,         SQLSMALLINT           BufferLength,         SQLSMALLINT          *NameLength,         SQLSMALLINT          *DataType,         SQLULEN              *ColumnSize,         SQLSMALLINT          *DecimalDigits,         SQLSMALLINT          *Nullable);      ep_SQLDescribeCol *fn;      fn = (ep_SQLDescribeCol *) xdlsym(h_odbc, "SQLDescribeCol");      xassert(fn != NULL);      return (*fn)(StatementHandle, ColumnNumber, ColumnName,         BufferLength, NameLength,         DataType, ColumnSize, DecimalDigits, Nullable);}SQLRETURN SQL_API dl_SQLGetDiagRec (   SQLSMALLINT           HandleType,   SQLHANDLE             Handle,   SQLSMALLINT           RecNumber,   SQLCHAR              *Sqlstate,   SQLINTEGER           *NativeError,   SQLCHAR              *MessageText,   SQLSMALLINT           BufferLength,   SQLSMALLINT          *TextLength){      typedef SQLRETURN SQL_API ep_SQLGetDiagRec (         SQLSMALLINT           HandleType,         SQLHANDLE             Handle,         SQLSMALLINT           RecNumber,         SQLCHAR              *Sqlstate,         SQLINTEGER           *NativeError,         SQLCHAR              *MessageText,         SQLSMALLINT           BufferLength,         SQLSMALLINT          *TextLength);      ep_SQLGetDiagRec *fn;      fn = (ep_SQLGetDiagRec *) xdlsym(h_odbc, "SQLGetDiagRec");      xassert(fn != NULL);      return (*fn)(HandleType, Handle, RecNumber, Sqlstate,         NativeError, MessageText, BufferLength, TextLength);}SQLRETURN SQL_API dl_SQLGetInfo (   SQLHDBC               ConnectionHandle,   SQLUSMALLINT          InfoType,   SQLPOINTER            InfoValue,   SQLSMALLINT           BufferLength,   SQLSMALLINT          *StringLength){      typedef SQLRETURN SQL_API ep_SQLGetInfo (         SQLHDBC               ConnectionHandle,         SQLUSMALLINT          InfoType,         SQLPOINTER            InfoValue,         SQLSMALLINT           BufferLength,         SQLSMALLINT          *StringLength);      ep_SQLGetInfo *fn;      fn = (ep_SQLGetInfo *) xdlsym(h_odbc, "SQLGetInfo");      xassert(fn != NULL);      return (*fn)(ConnectionHandle, InfoType, InfoValue, BufferLength,         StringLength);}SQLRETURN SQL_API dl_SQLNumResultCols (   SQLHSTMT              StatementHandle,   SQLSMALLINT          *ColumnCount)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -