dockerodbcgnucobol

How to connect to a MYSQL Database in cobol using Gnucobol (Via Dockerfile)


I am working on a prototype for a COBOL application in a container. Now I wanted to connect it to a database and I found ODBC. But it did not work our because I got the following error during docker build:

0.141 /function/YTR93.cob: 36: error: SQLCA: No such file or directory
0.141 /function/YTR93.cob: 36: error: syntax error, unexpected Identifier or Literal, expecting .
0.151 /function/YTR93.cob: 34: error: invalid level number 'EXEC'
0.152 cobc: call to cobc_plex_strdup with NULL pointer
0.152 cobc: aborting codegen for /function/YTR92.cob (unknown: unknown)

This is my cobol SQL-Code:

    EXEC SQL
      INCLUDE SQLCA
    END-EXEC.
    EXEC SQL
     CONNECT TO 'MySQL' USER 'admin' IDENTIFIED BY 'ABCDEF'
    END-EXEC.
    EXEC SQL
       SELECT Vorname INTO :host_variable
       FROM table_name
       WHERE condition
     END-EXEC.

This is my Dockerfile:

RUN apt-get update && \
  apt-get install -y \
  g++ \
  make \
  cmake \
  unzip \
  wget \
  gnucobol \
  unixodbc \
  unixodbc-dev \
  wget \
  gnupg \
  && wget https://dev.mysql.com/get/Downloads/Connector-ODBC/8.0/mysql-connector-odbc_8.0.29-1ubuntu20.04_amd64.deb \
  && dpkg -i mysql-connector-odbc_8.0.29-1ubuntu20.04_amd64.deb || apt-get -f install -y \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /mysql-connector-odbc_*.deb


# Set environment variables for MySQL Database configuration
ENV MYSQL_DATABASE=bank1 \
    MYSQL_USER=admin \
    MYSQL_PASSWORD=B:7L1f!r?xP3?8    \
    MYSQL_HOST=database-1.cho40iaoa3pu.eu-central-1.rds.amazonaws.com \
    MYSQL_PORT=3306

# Setup ODBC Driver in odbcinst.ini
RUN echo "[MySQL]\n\
Description=ODBC for MySQL\n\
Driver=/usr/lib/x86_64-linux-gnu/odbc/libmyodbc8a.so\n\
Setup=/usr/lib/x86_64-linux-gnu/odbc/libmyodbc8S.so\n\
FileUsage=1\n\
Server=$MYSQL_HOST\n\
User=$MYSQL_USER\n\
Password=$MYSQL_PASSWORD\n\
Database=$MYSQL_DATABASE\n\
Port=$MYSQL_PORT" > /etc/odbcinst.ini

# Interaktive Frontend Einstellungen zurücksetzen
ENV DEBIAN_FRONTEND=dialog

# Compile the cobol programs
RUN cobc -x --free ${FUNCTION_DIR}/YTR93.cob ${FUNCTION_DIR}/YTR92.cob -o ${FUNCTION_DIR}/cobol-program -lsqlca

Solution

  • For EXEC anything you need to first preprocess the file, in this case with an EXEC SQL preprocessor. For MySQL I'd suggest to use GixSQL.

    This will then convert the EXEC to "pure COBOL" which will then be passed to the COBOL compiler.