Skip to content

Commit

Permalink
Adds BSQL (#38323)
Browse files Browse the repository at this point in the history
* Add BSQL library v1.1.1.0

* Modify dbcore to use BSQL

* Add missing QDEL_NULL for connectOperation

* Moves BSQL_Shutdown() call to dbcore shutdown

* Fix passing the wrong argument to DBQuery/New()

* Darn it @Jordie0608. Fixes db calls without Connect check

No seriously please make sure I'm not breaking anything

* Queries with a null connection won't runtime

* Fix quoting

* Fix mistake

* Update BSQL to v1.1.2.0

* Update BSQL DMAPI to v1.0.1.0

* Fix connection instatiation

* Does the smart thing in regards to quoting

* Fix braces

* Update BSQL to 1.2.0.0. DMAPI to 1.1.0.0

* Execute/NextRow/MassInsert now have async parameter

* Build BSQL for tests

* Add missing apt source

* Def still need gcc-multilib

* Wut

* Revert "Wut"

This reverts commit d7c98a9.

* Try this then

* Could it really be that simple?

* Literally running out of ideas here

* Update BSQL to v1.2.1.0 DMAPI to v1.1.1.0

* Update BSQL travis version

* Nothing about this makes sense tbqhwyfam

* Whoo boy

* No idea why this isn't working tbh

* Absolute madness

* Ahhhhhhhhhhhhh

* *deep breath*

* "though yet again i was frustrated by failure"

* Add BSQL to Dockerfile

* Pass through MassInsert async param

* BSQL to v1.3.0.0 DMAPI to 1.2.0.0

* Add timeout support

* Wait, something's fucky

* Wtf is this meme?

* Just get good lmao

* Just stop being shit lol

* Stupid verbose logging

* Remove verbosity

* Good god

* BSQL to v1.3.0.1 DMAPI to v1.2.0.1

* BSQL to v1.3.0.2

* Update BSQL travis version

* Update BSQL docker version

* Didn't mean to change that

* Strip connection information from debug logs and make it configgable

* Move this to where CONFIG_GET is defined
  • Loading branch information
Cyberboss authored and vuonojenmustaturska committed Jul 16, 2018
1 parent d209850 commit d210adb
Show file tree
Hide file tree
Showing 21 changed files with 588 additions and 205 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Expand Up @@ -9,6 +9,7 @@ env:
- BYOND_MINOR="1427"
- NODE_VERSION="4"
- RUST_G_VERSION="0.3.0"
- BSQL_VERSION="v1.3.0.2"
matrix:
include:
- env:
Expand Down Expand Up @@ -37,15 +38,20 @@ matrix:
addons:
mariadb: '10.2'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- libstdc++6:i386
- libssl-dev:i386
- gcc-multilib
- g++-7
- g++-7-multilib
- libmariadbclient-dev:i386
cache:
directories:
- $HOME/.cargo
- $HOME/BYOND-${BYOND_MAJOR}.${BYOND_MINOR}
- $HOME/libmariadb
- $HOME/MariaDB
- $HOME/.rustup

install:
Expand Down
Binary file added BSQL.dll
Binary file not shown.
50 changes: 47 additions & 3 deletions Dockerfile
Expand Up @@ -6,12 +6,20 @@ WORKDIR /rust_g

RUN apt-get update && apt-get install -y \
git \
libssl-dev \
ca-certificates \
libc6-dev

FROM build as rust_g

WORKDIR /rust_g

RUN apt-get install -y --no-install-recommends \
libssl-dev \
rustc \
cargo \
pkg-config \
&& git init \
pkg-config

RUN git init \
&& git remote add origin https://github.com/tgstation/rust-g

#TODO: find a way to read these from .travis.yml or a common source eventually
Expand All @@ -21,6 +29,38 @@ RUN git fetch --depth 1 origin $RUST_G_VERSION \
&& git checkout FETCH_HEAD \
&& cargo build --release

FROM base as bsql

WORKDIR /bsql

RUN apt-get install -y --no-install-recommends software-properties-common \
&& add-apt-repository ppa:ubuntu-toolchain-r/test \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
cmake \
make \
g++-7 \
libstdc++6 \
libmariadb-client-lgpl-dev

RUN git init \
&& git remote add origin https://github.com/tgstation/BSQL

#TODO: find a way to read these from .travis.yml or a common source eventually
ENV BSQL_VERSION=v1.3.0.2

RUN git fetch --depth 1 origin $BSQL_VERSION \
&& git checkout FETCH_HEAD

WORKDIR /bsql/artifacts

ENV CC=gcc-7 CXX=g++-7

RUN ln -s /usr/include/mariadb /usr/include/mysql \
&& ln -s /usr/lib/i386-linux-gnu /root/MariaDB \
&& cmake .. \
&& make

FROM base as dm_base

WORKDIR /tgstation
Expand Down Expand Up @@ -64,8 +104,12 @@ RUN apt-get update && apt-get install -y \
&& mkdir -p /root/.byond/bin

COPY --from=rustg /rust_g/target/release/librust_g.so /root/.byond/bin/rust_g
COPY --from=bsql /bsql/artifacts/src/BSQL/libBSQL.so ./
COPY --from=build /deploy ./

#bsql fexists memes
RUN ln -s /tgstation/libBSQL.so /root/.byond/bin/libBSQL.so

VOLUME [ "/tgstation/config", "/tgstation/data" ]

ENTRYPOINT [ "DreamDaemon", "tgstation.dmb", "-port", "1337", "-trusted", "-close", "-verbose" ]
10 changes: 10 additions & 0 deletions code/__DEFINES/_protect.dm
@@ -0,0 +1,10 @@
#define GENERAL_PROTECT_DATUM(Path)\
##Path/can_vv_get(var_name){\
return FALSE;\
}\
##Path/vv_edit_var(var_name, var_value){\
return FALSE;\
}\
##Path/CanProcCall(procname){\
return FALSE;\
}
6 changes: 6 additions & 0 deletions code/__DEFINES/bsql.config.dm
@@ -0,0 +1,6 @@
#define BSQL_EXTERNAL_CONFIGURATION
#define BSQL_DEL_PROC(path) ##path/Destroy()
#define BSQL_DEL_CALL(obj) qdel(##obj)
#define BSQL_IS_DELETED(obj) (QDELETED(obj))
#define BSQL_PROTECT_DATUM(path) GENERAL_PROTECT_DATUM(##path)
#define BSQL_ERROR(message) SSdbcore.ReportError(message)
132 changes: 132 additions & 0 deletions code/__DEFINES/bsql.dm
@@ -0,0 +1,132 @@
//BSQL - DMAPI v1.2.0.1

//types of connections
#define BSQL_CONNECTION_TYPE_MARIADB "MySql"
#define BSQL_CONNECTION_TYPE_SQLSERVER "SqlServer"

#define BSQL_DEFAULT_TIMEOUT 5

//Call this before rebooting or shutting down your world to clean up gracefully. This invalidates all active connection and operation datums
/world/proc/BSQL_Shutdown()
return

/*
Called whenever a library call is made with verbose information, override and do with as you please
message: English debug message
*/
/world/proc/BSQL_Debug(msg)
return

/*
Create a new database connection, does not perform the actual connect
connection_type: The BSQL connection_type to use
asyncTimeout: The timeout to use for normal operations, 0 for infinite, defaults to BSQL_DEFAULT_TIMEOUT
blockingTimeout: The timeout to use for blocking operations, must be less than or equal to asyncTimeout, 0 for infinite, defaults to asyncTimeout
*/
/datum/BSQL_Connection/New(connection_type, asyncTimeout, blockingTimeout)
return ..()

/*
Starts an operation to connect to a database. Should only have 1 successful call
ipaddress: The ip/hostname of the target server
port: The port of the target server
username: The username to login to the target server
password: The password for the target server
database: Optional database to connect to. Must be used when trying to do database operations, `USE x` is not sufficient
Returns: A /datum/BSQL_Operation representing the connection or null if an error occurred
*/
/datum/BSQL_Connection/proc/BeginConnect(ipaddress, port, username, password, database)
return

/*
Properly quotes a string for use by the database. The connection must be open for this proc to succeed
str: The string to quote
Returns: The string quoted on success, null on error
*/
/datum/BSQL_Connection/proc/Quote(str)
return

/*
Starts an operation for a query
query: The text of the query. Only one query allowed per invocation, no semicolons
Returns: A /datum/BSQL_Operation/Query representing the running query and subsequent result set or null if an error occurred
Note for MariaDB: The underlying connection is pooled. In order to use connection state based properties (i.e. LAST_INSERT_ID()) you can guarantee multiple queries will use the same connection by running BSQL_DEL_CALL(query) on the finished /datum/BSQL_Operation/Query and then creating the next one with another call to BeginQuery() with no sleeps in between
*/
/datum/BSQL_Connection/proc/BeginQuery(query)
return

/*
Checks if the operation is complete. This, in some cases must be called multiple times with false return before a result is present regardless of timespan. For best performance check it once per tick
Returns: TRUE if the operation is complete, FALSE if it's not, null on error
*/
/datum/BSQL_Operation/proc/IsComplete()
return

/*
Blocks the entire game until the given operation completes. IsComplete should not be checked after calling this to avoid potential side effects.
Returns: TRUE on success, FALSE if the operation wait time exceeded the connection's blockingTimeout setting
*/
/datum/BSQL_Operation/proc/WaitForCompletion()
return

/*
Get the error message associated with an operation. Should not be used while IsComplete() returns FALSE
Returns: The error message, if any. null otherwise
*/
/datum/BSQL_Operation/proc/GetError()
return

/*
Get the error code associated with an operation. Should not be used while IsComplete() returns FALSE
Returns: The error code, if any. null otherwise
*/
/datum/BSQL_Operation/proc/GetErrorCode()
return

/*
Gets an associated list of column name -> value representation of the most recent row in the query. Only valid if IsComplete() returns TRUE. If this returns null and no errors are present there are no more results in the query. Important to note that once IsComplete() returns TRUE it must not be called again without checking this or the row values may be lost
Returns: An associated list of column name -> value for the row. Values will always be either strings or null
*/
/datum/BSQL_Operation/Query/proc/CurrentRow()
return


/*
Code configuration options below
Define this to avoid modifying this file but the following defines must be declared somewhere else before BSQL/includes.dm is included
*/
#ifndef BSQL_EXTERNAL_CONFIGURATION

//Modify this if you disagree with byond's GC schemes. Ensure this is called for all connections and operations when they are deleted or they will leak native resources until /world/proc/BSQL_Shutdown() is called
#define BSQL_DEL_PROC(path) ##path/Del()

//The equivalent of calling del() in your codebase
#define BSQL_DEL_CALL(obj) del(##obj)

//Returns TRUE if an object is delete
#define BSQL_IS_DELETED(obj) (obj == null)

//Modify this to add protections to the connection and query datums
#define BSQL_PROTECT_DATUM(path)

//Modify this to change up error handling for the library
#define BSQL_ERROR(message) CRASH("BSQL: [##message]")

#endif

/*
Copyright 2018 Jordan Brown
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
11 changes: 1 addition & 10 deletions code/__DEFINES/tgs.config.dm
Expand Up @@ -7,13 +7,4 @@
#define TGS_ERROR_LOG(message) log_world("TGS: Error: [##message]")
#define TGS_NOTIFY_ADMINS(event) message_admins(##event)
#define TGS_CLIENT_COUNT GLOB.clients.len
#define TGS_PROTECT_DATUM(Path)\
##Path/can_vv_get(var_name){\
return FALSE;\
}\
##Path/vv_edit_var(var_name, var_value){\
return FALSE;\
}\
##Path/CanProcCall(procname){\
return FALSE;\
}
#define TGS_PROTECT_DATUM(Path) GENERAL_PROTECT_DATUM(##Path)
3 changes: 1 addition & 2 deletions code/__HELPERS/text.dm
Expand Up @@ -15,8 +15,7 @@

// Run all strings to be used in an SQL query through this proc first to properly escape out injection attempts.
/proc/sanitizeSQL(t)
var/sqltext = SSdbcore.Quote("[t]");
return copytext(sqltext, 2, lentext(sqltext));//Quote() adds quotes around input, we already do that
return SSdbcore.Quote("[t]")

/proc/format_table_name(table as text)
return CONFIG_GET(string/feedback_tableprefix) + table
Expand Down
18 changes: 17 additions & 1 deletion code/controllers/configuration/entries/dbconfig.dm
Expand Up @@ -28,4 +28,20 @@
/datum/config_entry/number/query_debug_log_timeout
config_entry_value = 70
min_val = 1
protection = CONFIG_ENTRY_LOCKED | CONFIG_ENTRY_HIDDEN
protection = CONFIG_ENTRY_LOCKED
deprecated_by = /datum/config_entry/number/blocking_query_timeout

/datum/config_entry/number/query_debug_log_timeout/DeprecationUpdate(value)
return value

/datum/config_entry/number/async_query_timeout
config_entry_value = 10
min_val = 0
protection = CONFIG_ENTRY_LOCKED

/datum/config_entry/number/blocking_query_timeout
config_entry_value = 5
min_val = 0
protection = CONFIG_ENTRY_LOCKED

/datum/config_entry/flag/bsql_debug

0 comments on commit d210adb

Please sign in to comment.