Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PostgreSQL #112

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions .github/workflows/ci-run-test-extensions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: ci-run-test-extensions

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
test-on-ubuntu-2204:
name: test-on-ubuntu-22.04
runs-on: ubuntu-latest
container:
image: docker.io/ubuntu:22.04
env:
TZ: Asia/Shanghai
DEBIAN_FRONTEND: noninteractive
volumes:
- ${{ github.workspace }}:${{ github.workspace }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

steps:
- uses: actions/checkout@v4

- name: Intall Deps
run: |
export DEBIAN_FRONTEND=noninteractive
echo "Asia/Shanghai" > /etc/timezone
apt-get update
apt-get install -y gcc make cmake autoconf sudo lsb-release curl ca-certificates tzdata
dpkg-reconfigure --frontend noninteractive tzdata

- name: Install database
run: |
install -d /usr/share/postgresql-common/pgdg
curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
apt-get update
apt-get install -y postgresql-15 libpq-dev

- name: Start database
run: |
sudo -u postgres echo "123456" > /tmp/pg.pwfile
sudo -u postgres /usr/lib/postgresql/15/bin/initdb -D /tmp/data -U postgres -A md5 --pwfile=/tmp/pg.pwfile
sudo -u postgres /usr/lib/postgresql/15/bin/pg_ctl -D /tmp/data -l /tmp/data/logfile start
PGPASSWORD=123456 psql -h 127.0.0.1 -U postgres -c "SELECT 1"

- name: Configure And make
working-directory: ${{ github.workspace }}
run: |
# autoconf
./configure --with-pgsql \
--with-pgsql-incdir=/usr/include/postgresql \
--with-pgsql-libdir=/usr/lib/x86_64-linux-gnu
make

- name: Test
working-directory: ${{ github.workspace }}
run: |
cd tests/
make test
make test-ext
make clean
cd ../
make clean

- name: Cmake And make
working-directory: ${{ github.workspace }}
run: |
mkdir build/ && cd build/
cmake .. \
-DWITH_PGSQL=ON \
-DWITH_PGSQL_INCLUDE_DIR=/usr/include/postgresql \
-DWITH_PGSQL_LIBRARY_DIR=/usr/lib/x86_64-linux-gnu
make && make install

- name: Cmake Test
working-directory: ${{ github.workspace }}
run: |
cd build/
make test
31 changes: 28 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ IF (NOT CMAKE_USE_PTHREADS_INIT)
MESSAGE(FATAL_ERROR "Couldn't find pthreads.")
ENDIF()

IF(NOT "${WITH_PGSQL}" STREQUAL "")
message(STATUS "with_pgsql: ${WITH_PGSQL}")
# check "libpq-fe.h"
if(EXISTS "${WITH_PGSQL_INCLUDE_DIR}/libpq-fe.h")
message(STATUS "found 'libpq-fe.h' header.")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_PGSQL")
else()
message(FATAL_ERROR "Cannot find 'libpq-fe.h' header. Use '-DWITH_PGSQL_INCLUDE_DIR=DIR' to specify the directory where 'libpq-fe.h' is located.")
endif()

# check "libpq.so"
if(EXISTS "${WITH_PGSQL_LIBRARY_DIR}/libpq.so")
message(STATUS "found 'libpq.so' library.")
SET(QLIBC_LINK_LIBS "pq")
else()
message(FATAL_ERROR "Cannot find 'libpq.so' library. Use '-DWITH_PGSQL_LIBRARY_DIR=DIR' to specify the directory where 'libpq.so' is located.")
endif()
ENDIF()

SET(SRC_SUBPATHS
containers/*.c
utilities/*.c
Expand Down Expand Up @@ -80,20 +99,26 @@ FILE(GLOB_RECURSE SRC_LIB_EXT
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${qlibc_SOURCE_DIR}/lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${qlibc_SOURCE_DIR}/lib)

LIST(APPEND QLIBC_INCLUDE_DIRECTORIES ${qlibc_SOURCE_DIR}/src/internal)
LIST(APPEND QLIBC_INCLUDE_DIRECTORIES ${WITH_PGSQL_INCLUDE_DIR})
LIST(APPEND QLIBC_LINK_DIRECTORIES ${WITH_PGSQL_LIBRARY_DIR})

INCLUDE_DIRECTORIES(${QLIBC_INCLUDE_DIRECTORIES})
LINK_DIRECTORIES(${QLIBC_LINK_DIRECTORIES})

ADD_LIBRARY(qlibc-static STATIC ${SRC_LIB})
ADD_LIBRARY(qlibc SHARED ${SRC_LIB})
ADD_LIBRARY(qlibcext-static STATIC ${SRC_LIB_EXT})
ADD_LIBRARY(qlibcext SHARED ${SRC_LIB_EXT})

INCLUDE_DIRECTORIES(${qlibc_SOURCE_DIR}/src/internal)
TARGET_INCLUDE_DIRECTORIES(qlibc-static PUBLIC ${qlibc_SOURCE_DIR}/include/qlibc)
TARGET_INCLUDE_DIRECTORIES(qlibc PUBLIC ${qlibc_SOURCE_DIR}/include/qlibc)
TARGET_INCLUDE_DIRECTORIES(qlibcext-static PUBLIC ${qlibc_SOURCE_DIR}/include/qlibc)

TARGET_LINK_LIBRARIES(qlibc-static PRIVATE ${CMAKE_THREAD_LIBS_INIT})
TARGET_LINK_LIBRARIES(qlibc PRIVATE ${CMAKE_THREAD_LIBS_INIT})
TARGET_LINK_LIBRARIES(qlibcext-static PRIVATE ${CMAKE_THREAD_LIBS_INIT})
TARGET_LINK_LIBRARIES(qlibcext PUBLIC qlibc)
TARGET_LINK_LIBRARIES(qlibcext-static PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${QLIBC_LINK_LIBS})
TARGET_LINK_LIBRARIES(qlibcext PUBLIC qlibc ${QLIBC_LINK_LIBS})

SET(QLIBC_HEADER "${qlibc_SOURCE_DIR}/include/qlibc")
INSTALL(DIRECTORY ${QLIBC_HEADER} DESTINATION include)
Expand Down
4 changes: 3 additions & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ all:
test: all
make -C tests test

test-ext: all
make -C tests test-ext

install:
make -C src install

Expand All @@ -44,7 +47,6 @@ uninstall:
clean:
make -C src clean


distclean: clean
@for DIR in src tests examples; do \
echo "===> $${DIR}"; \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Please refer the LICENSE document included in the package for more details.
* INI-style Configuration File Parser.
* HTTP client.
* Rotating File Logger.
* Database(MySQL) interface.
* Database(MySQL/PostgreSQL) interface.
* [Token-Bucket](https://en.wikipedia.org/wiki/Token_bucket)

## qLibc Tables at a Glance
Expand Down
103 changes: 103 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,9 @@ enable_ext_qhttpclient
enable_ext_qdatabase
with_openssl
with_mysql
with_pgsql
with_pgsql_incdir
with_pgsql_libdir
'
ac_precious_vars='build_alias
host_alias
Expand Down Expand Up @@ -1372,6 +1375,11 @@ Optional Packages:
extension API. When it's enabled, user applications
need to link mysql client library. (ex:
-lmysqlclient)
--with-pgsql This will enable PostgreSQL database support in
qdatabase extension API. When it's enabled, user
applications need to link libpq library. (ex: -lpq)
--with-pgsql-incdir=DIR site header files for PostgreSQL in DIR.
--with-pgsql-libdir=DIR site library files for PostgreSQL in DIR

Some influential environment variables:
CC C compiler command
Expand Down Expand Up @@ -4843,6 +4851,101 @@ See \`config.log' for more details" "$LINENO" 5; }
fi
fi


# Check whether --with-pgsql was given.
if test "${with_pgsql+set}" = set; then :
withval=$with_pgsql;
else
withval=no
fi

if test "$withval" = yes; then
# check libpq-fe.h
as_ac_File=`$as_echo "ac_cv_file_$with_pgsql_incdir/libpq-fe.h" | $as_tr_sh`
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $with_pgsql_incdir/libpq-fe.h" >&5
$as_echo_n "checking for $with_pgsql_incdir/libpq-fe.h... " >&6; }
if eval \${$as_ac_File+:} false; then :
$as_echo_n "(cached) " >&6
else
test "$cross_compiling" = yes &&
as_fn_error $? "cannot check for file existence when cross compiling" "$LINENO" 5
if test -r "$with_pgsql_incdir/libpq-fe.h"; then
eval "$as_ac_File=yes"
else
eval "$as_ac_File=no"
fi
fi
eval ac_res=\$$as_ac_File
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
$as_echo "$ac_res" >&6; }
if eval test \"x\$"$as_ac_File"\" = x"yes"; then :
withval=yes
else
withval=no
fi

if test "$withval" = yes; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: find '$with_pgsql_incdir/libpq-fe.h'" >&5
$as_echo "$as_me: find '$with_pgsql_incdir/libpq-fe.h'" >&6;}
CPPFLAGS="$CPPFLAGS -DENABLE_PGSQL -I$with_pgsql_incdir"
else
{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
as_fn_error $? "Cannot find 'libpq-fe.h' header. Use --with-pgsql-incdir=DIR to specify the directory where 'libpq-fe.h' is located.
See \`config.log' for more details" "$LINENO" 5; }
fi
# check libpq.so
as_ac_File=`$as_echo "ac_cv_file_$with_pgsql_libdir/libpq.so" | $as_tr_sh`
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $with_pgsql_libdir/libpq.so" >&5
$as_echo_n "checking for $with_pgsql_libdir/libpq.so... " >&6; }
if eval \${$as_ac_File+:} false; then :
$as_echo_n "(cached) " >&6
else
test "$cross_compiling" = yes &&
as_fn_error $? "cannot check for file existence when cross compiling" "$LINENO" 5
if test -r "$with_pgsql_libdir/libpq.so"; then
eval "$as_ac_File=yes"
else
eval "$as_ac_File=no"
fi
fi
eval ac_res=\$$as_ac_File
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
$as_echo "$ac_res" >&6; }
if eval test \"x\$"$as_ac_File"\" = x"yes"; then :
withval=yes
else
withval=no
fi

if test "$withval" = yes; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: find '$with_pgsql_libdir/libpq.so'" >&5
$as_echo "$as_me: find '$with_pgsql_libdir/libpq.so'" >&6;}
CPPFLAGS="$CPPFLAGS -Wl,-rpath,$with_pgsql_libdir -L$with_pgsql_libdir -lpq"
else
{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
as_fn_error $? "Cannot find 'libpq.so' library. Use --with-pgsql-libdir=DIR to specify the directory where 'libpq.so' is located.
See \`config.log' for more details" "$LINENO" 5; }
fi
fi

# Check whether --with-pgsql_incdir was given.
if test "${with_pgsql_incdir+set}" = set; then :
withval=$with_pgsql_incdir;
else
withval=no
fi


# Check whether --with-pgsql_libdir was given.
if test "${with_pgsql_libdir+set}" = set; then :
withval=$with_pgsql_libdir;
else
withval=no
fi


{ $as_echo "$as_me:${as_lineno-$LINENO}: CFLAGS $CFLAGS" >&5
$as_echo "$as_me: CFLAGS $CFLAGS" >&6;}
{ $as_echo "$as_me:${as_lineno-$LINENO}: CPPFLAGS $CPPFLAGS" >&5
Expand Down
22 changes: 22 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ if test "$withval" = yes; then
fi
fi

AC_ARG_WITH([pgsql],[AS_HELP_STRING([--with-pgsql], [This will enable PostgreSQL database support in qdatabase extension API. When it's enabled, user applications need to link libpq library. (ex: -lpq)])],[],[withval=no])
if test "$withval" = yes; then
# check libpq-fe.h
AC_CHECK_FILE([$with_pgsql_incdir/libpq-fe.h],[withval=yes],[withval=no])
if test "$withval" = yes; then
AC_MSG_NOTICE([find '$with_pgsql_incdir/libpq-fe.h'])
CPPFLAGS="$CPPFLAGS -DENABLE_PGSQL -I$with_pgsql_incdir"
else
AC_MSG_FAILURE([Cannot find 'libpq-fe.h' header. Use --with-pgsql-incdir=DIR to specify the directory where 'libpq-fe.h' is located.])
fi
# check libpq.so
AC_CHECK_FILE([$with_pgsql_libdir/libpq.so],[withval=yes],[withval=no])
if test "$withval" = yes; then
AC_MSG_NOTICE([find '$with_pgsql_libdir/libpq.so'])
CPPFLAGS="$CPPFLAGS -Wl,-rpath,$with_pgsql_libdir -L$with_pgsql_libdir -lpq"
else
AC_MSG_FAILURE([Cannot find 'libpq.so' library. Use --with-pgsql-libdir=DIR to specify the directory where 'libpq.so' is located.])
fi
fi
AC_ARG_WITH([pgsql_incdir],[AS_HELP_STRING([--with-pgsql-incdir=DIR], [site header files for PostgreSQL in DIR.])],[],[withval=no])
AC_ARG_WITH([pgsql_libdir],[AS_HELP_STRING([--with-pgsql-libdir=DIR], [site library files for PostgreSQL in DIR])],[],[withval=no])

AC_MSG_NOTICE([CFLAGS $CFLAGS])
AC_MSG_NOTICE([CPPFLAGS $CPPFLAGS])
#AC_MSG_NOTICE([LIBS $LIBS])
Expand Down
Loading
Loading