├── README.md ├── gcc ├── makefile ├── objects.mk ├── sources.mk └── src │ └── subdir.mk ├── qt └── mysql.pro ├── src ├── mysql.cpp └── mysql.h ├── vs2013 └── mysql │ └── mysql.vcxproj └── vs2015 └── mysql └── mysql.vcxproj /README.md: -------------------------------------------------------------------------------- 1 | # mysql 2 | Simple crossplatform C++ MySQL class library 3 | -------------------------------------------------------------------------------- /gcc/makefile: -------------------------------------------------------------------------------- 1 | -include ../makefile.init 2 | 3 | RM := rm -rf 4 | 5 | # All of the sources participating in the build are defined here 6 | -include sources.mk 7 | -include src/subdir.mk 8 | -include subdir.mk 9 | -include objects.mk 10 | 11 | ifneq ($(MAKECMDGOALS),clean) 12 | ifneq ($(strip $(C++_DEPS)),) 13 | -include $(C++_DEPS) 14 | endif 15 | ifneq ($(strip $(C_DEPS)),) 16 | -include $(C_DEPS) 17 | endif 18 | ifneq ($(strip $(CC_DEPS)),) 19 | -include $(CC_DEPS) 20 | endif 21 | ifneq ($(strip $(CPP_DEPS)),) 22 | -include $(CPP_DEPS) 23 | endif 24 | ifneq ($(strip $(CXX_DEPS)),) 25 | -include $(CXX_DEPS) 26 | endif 27 | ifneq ($(strip $(C_UPPER_DEPS)),) 28 | -include $(C_UPPER_DEPS) 29 | endif 30 | endif 31 | 32 | -include ../makefile.defs 33 | 34 | # Add inputs and outputs from these tool invocations to the build variables 35 | 36 | # All Target 37 | all: libmysql.a 38 | 39 | # Tool invocations 40 | libmysql.a: $(OBJS) $(USER_OBJS) 41 | @echo 'Building target: $@' 42 | @echo 'Invoking: Cross GCC Archiver' 43 | ar -r "libmysql.a" $(OBJS) $(USER_OBJS) $(LIBS) 44 | @echo 'Finished building target: $@' 45 | @echo ' ' 46 | 47 | # Other Targets 48 | clean: 49 | -$(RM) $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(ARCHIVES)$(CPP_DEPS)$(CXX_DEPS)$(C_UPPER_DEPS) libmysql.a 50 | -@echo ' ' 51 | 52 | .PHONY: all clean dependents 53 | .SECONDARY: 54 | 55 | -include ../makefile.targets 56 | -------------------------------------------------------------------------------- /gcc/objects.mk: -------------------------------------------------------------------------------- 1 | USER_OBJS := 2 | 3 | LIBS := 4 | 5 | -------------------------------------------------------------------------------- /gcc/sources.mk: -------------------------------------------------------------------------------- 1 | O_SRCS := 2 | CPP_SRCS := 3 | C_UPPER_SRCS := 4 | C_SRCS := 5 | S_UPPER_SRCS := 6 | OBJ_SRCS := 7 | ASM_SRCS := 8 | CXX_SRCS := 9 | C++_SRCS := 10 | CC_SRCS := 11 | C++_DEPS := 12 | OBJS := 13 | C_DEPS := 14 | CC_DEPS := 15 | ARCHIVES := 16 | CPP_DEPS := 17 | CXX_DEPS := 18 | C_UPPER_DEPS := 19 | 20 | # Every subdirectory with source files must be described here 21 | SUBDIRS := \ 22 | src \ 23 | 24 | -------------------------------------------------------------------------------- /gcc/src/subdir.mk: -------------------------------------------------------------------------------- 1 | # Add inputs and outputs from these tool invocations to the build variables 2 | CPP_SRCS += \ 3 | ../src/mysql.cpp 4 | 5 | OBJS += \ 6 | ./src/mysql.o 7 | 8 | CPP_DEPS += \ 9 | ./src/mysql.d 10 | 11 | 12 | # Each subdirectory must supply rules for building sources it contributes 13 | src/%.o: ../src/%.cpp 14 | @echo 'Building file: $<' 15 | @echo 'Invoking: Cross G++ Compiler' 16 | g++ -D__GXX_EXPERIMENTAL_CXX0X__ -I"../../core/src" -I"../../lib/mysql-connector-c/include" -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<" 17 | @echo 'Finished building: $<' 18 | @echo ' ' 19 | 20 | 21 | -------------------------------------------------------------------------------- /qt/mysql.pro: -------------------------------------------------------------------------------- 1 | QT -= core gui 2 | 3 | TARGET = mysql 4 | TEMPLATE = lib 5 | CONFIG += staticlib 6 | 7 | INCLUDEPATH += ../../core/src 8 | INCLUDEPATH += ../../logger/src 9 | INCLUDEPATH += ../../network/src 10 | INCLUDEPATH += ../../../lib/mysqlconn/include 11 | 12 | 13 | HEADERS += \ 14 | ../src/mysql.h 15 | 16 | SOURCES += \ 17 | ../src/mysql.cpp 18 | -------------------------------------------------------------------------------- /src/mysql.cpp: -------------------------------------------------------------------------------- 1 | #include "mysql.h" 2 | 3 | namespace mysql { 4 | 5 | //-------------------------------------------------------------------------------------------------- 6 | //---------- class MySQL --------------------------------------------------------- 7 | //-------------------------------------------------------------------------------------------------- 8 | MySQL::MySQL() { 9 | conn = NULL; 10 | res = NULL; 11 | fields = NULL; 12 | 13 | logger = new logger::Logger("mysql.log"); 14 | } 15 | MySQL::~MySQL() { 16 | if (res != NULL) mysql_free_result(res); 17 | if (conn != NULL) mysql_close(conn); 18 | } 19 | bool MySQL::init() { 20 | conn = mysql_init(NULL); 21 | if (conn == NULL) { 22 | logger->out("Error: can't create MySQL-descriptor"); 23 | return false; 24 | } 25 | logger->out("MySQL-descriptor was created!"); 26 | return true; 27 | } 28 | bool MySQL::connect(String host, String user, String password, String db) { 29 | logger->out("MySQL connect"); 30 | try { 31 | string host8 = host.to_string(); 32 | string user8 = user.to_string(); 33 | string pswd8 = password.to_string(); 34 | string db8 = db.to_string(); 35 | if (!mysql_real_connect(conn, host.to_string().c_str(), user.to_string().c_str(), password.to_string().c_str(), db.to_string().c_str(), NULL, NULL, 0)) { 36 | String error = mysql_error(conn); 37 | logger->out("Error: can't connect to database"); 38 | return false; 39 | } 40 | } 41 | catch (...) { 42 | logger->out("connect catch"); 43 | return false; 44 | } 45 | logger->out("Connected to database!"); 46 | return true; 47 | } 48 | bool MySQL::query(String sql) { 49 | if (res != NULL) mysql_free_result(res); 50 | if (mysql_query(conn, sql.to_string().c_str()) != 0) { 51 | logger->out("Error: mistake in SQL"); 52 | return false; 53 | } 54 | return true; 55 | } 56 | bool MySQL::exec(String sql) { 57 | if (mysql_query(conn, sql.to_string().c_str()) != 0) 58 | { 59 | return false; 60 | } 61 | return true; 62 | } 63 | int MySQL::active(String sql) { 64 | if (exec(sql)) { 65 | if (storeResult()) { 66 | int count = getRowCount(); 67 | return count; 68 | } 69 | } 70 | return 0; 71 | } 72 | bool MySQL::storeResult() { 73 | if (res != NULL) mysql_free_result(res); 74 | res = mysql_store_result(conn); 75 | fields = mysql_fetch_fields(res); 76 | if (res) return true; 77 | return false; 78 | } 79 | void MySQL::freeResult() { 80 | if (res != NULL) mysql_free_result(res); 81 | res = NULL; 82 | fields = NULL; 83 | } 84 | String MySQL::getEscapeString(String s) { 85 | int len = s.getLength(); 86 | char *res = (char*)malloc(len * 2 + 1); 87 | //MYSQL *mysql, char *to,const char *from, unsigned long length 88 | mysql_real_escape_string(conn, res, s.to_string().c_str(), len); 89 | String sRes = res; 90 | free(res); 91 | 92 | return sRes; 93 | } 94 | String MySQL::getEscapeString(char *s, int len) { 95 | char *res = (char*)malloc(len * 2 + 1); 96 | //MYSQL *mysql, char *to,const char *from, unsigned long length 97 | mysql_real_escape_string(conn, res, s, len); 98 | String sRes = res; 99 | free(res); 100 | 101 | return sRes; 102 | } 103 | int MySQL::getRowCount() { 104 | return mysql_num_rows(res); 105 | } 106 | int MySQL::getFieldCount() { 107 | int count = mysql_num_fields(res); 108 | return count; 109 | } 110 | String MySQL::getFieldValue(int rowIndex, int fieldIndex) { 111 | mysql_data_seek(res, rowIndex); 112 | MYSQL_ROW row = mysql_fetch_row(res); 113 | if (row[fieldIndex] == NULL) return ""; 114 | return (String)row[fieldIndex]; 115 | } 116 | String MySQL::getFieldValue(int rowIndex, String fieldName) { 117 | // MYSQL_ROW row = mysql_fetch_row(res); 118 | int count = mysql_num_fields(res); 119 | for (int i = 0; i < count; i++) { 120 | if (fieldName == (String)fields[i].name) { 121 | return getFieldValue(rowIndex, i); 122 | } 123 | } 124 | return ""; 125 | } 126 | int MySQL::getLastId() { 127 | String sql = "select LAST_INSERT_ID();"; 128 | if (this->exec(sql)) { 129 | if (this->storeResult()) { 130 | int count = this->getRowCount(); 131 | if (count > 0) { 132 | String lastSiteId = this->getFieldValue(0, "LAST_INSERT_ID()"); 133 | return lastSiteId.toInt(); 134 | } 135 | } 136 | } 137 | return 0; 138 | } 139 | 140 | } 141 | -------------------------------------------------------------------------------- /src/mysql.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "core.h" 4 | using namespace core; 5 | #include "logger.h" 6 | 7 | namespace mysql { 8 | 9 | //#include <../include/mysql.h> 10 | #include 11 | 12 | #ifdef OS_WINDOWS 13 | //#pragma comment (lib, "libmysql.lib") 14 | #endif 15 | 16 | 17 | class MySQL : public Object { 18 | protected: 19 | MYSQL *conn; 20 | MYSQL_RES *res; 21 | MYSQL_FIELD *fields; 22 | 23 | logger::Logger *logger; 24 | public: 25 | MySQL(); 26 | ~MySQL(); 27 | virtual bool init(); 28 | virtual bool connect(String host, String user, String password, String db); 29 | virtual bool query(String sql); 30 | virtual bool exec(String sql); 31 | virtual int active(String sql); 32 | virtual bool storeResult(); 33 | virtual void freeResult(); 34 | 35 | virtual String getEscapeString(String s); 36 | virtual String getEscapeString(char *s, int len); 37 | 38 | virtual int getRowCount(); 39 | virtual int getFieldCount(); 40 | virtual String getFieldValue(int rowIndex, int fieldIndex); 41 | virtual String getFieldValue(int rowIndex, String fieldName); 42 | 43 | virtual int getLastId(); 44 | }; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /vs2013/mysql/mysql.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {9FEEA133-3A97-4FE0-A482-0FFDAC3BE310} 15 | Win32Proj 16 | cjMySQL 17 | 18 | 19 | 20 | StaticLibrary 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | StaticLibrary 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | ..\..\..\libraries\mysqlconn\include;..\..\..\core\src;$(IncludePath) 44 | $(LibraryPath) 45 | 46 | 47 | ..\..\..\cjCore\src;..\..\..\..\libraries\mysqlconn\include;$(IncludePath) 48 | ..\..\cj\Debug;$(LibraryPath) 49 | 50 | 51 | 52 | NotUsing 53 | Level3 54 | Disabled 55 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 56 | true 57 | 58 | MultiThreadedDebugDLL 59 | 60 | 61 | Windows 62 | true 63 | 64 | 65 | 66 | 67 | Level3 68 | NotUsing 69 | MaxSpeed 70 | true 71 | true 72 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 73 | true 74 | 75 | MultiThreaded 76 | 77 | 78 | Windows 79 | true 80 | true 81 | true 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /vs2015/mysql/mysql.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {9FEEA133-3A97-4FE0-A482-0FFDAC3BE310} 15 | Win32Proj 16 | cjMySQL 17 | 18 | 19 | 20 | StaticLibrary 21 | true 22 | v140 23 | Unicode 24 | 25 | 26 | StaticLibrary 27 | false 28 | v140 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | ..\..\..\libraries\mysqlconn\include;..\..\..\core\src;$(IncludePath) 44 | $(LibraryPath) 45 | 46 | 47 | ..\..\..\cjCore\src;..\..\..\..\libraries\mysqlconn\include;$(IncludePath) 48 | ..\..\cj\Debug;$(LibraryPath) 49 | 50 | 51 | 52 | NotUsing 53 | Level3 54 | Disabled 55 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 56 | true 57 | 58 | MultiThreadedDebugDLL 59 | 60 | 61 | Windows 62 | true 63 | 64 | 65 | 66 | 67 | Level3 68 | NotUsing 69 | MaxSpeed 70 | true 71 | true 72 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 73 | true 74 | 75 | MultiThreaded 76 | 77 | 78 | Windows 79 | true 80 | true 81 | true 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | --------------------------------------------------------------------------------