Skip to content

Commit ac24dd4

Browse files
authored
Merge pull request #31 from RockfordWei/master
Upgrading to MySQL 8.0.11 while keeping the downcast compatibility to 5.7.21
2 parents be8566f + 4af9641 commit ac24dd4

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

README.md

-6
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ swift package generate-xcodeproj
7676
```
7777
After this you have to set the path for libraries again.
7878

79-
⚠️**NOTE**⚠️ The last mysql homebrew version that Perfect currently supports is 5.7, so please try this command when some missing type issues were found on your build:
80-
81-
```
82-
$ brew install mysql@5.7 && brew link mysql@5.7 --force
83-
```
84-
8579
## Linux Build Notes
8680

8781
Ensure that you have installed libmysqlclient-dev for MySQL version *5.6 or greater*.

Sources/PerfectMySQL/MySQL.swift

+14-6
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,23 @@ public final class MySQL {
100100

101101
/// Commits the transaction
102102
public func commit() -> Bool {
103-
return 1 == mysql_commit(mysqlPtr)
103+
var res = mysql_commit(mysqlPtr)
104+
var FALSE = 0
105+
return memcmp(&res, &FALSE, MemoryLayout.size(ofValue: res)) != 0
104106
}
105107

106108
/// Rolls back the transaction
107109
public func rollback() -> Bool {
108-
return 1 == mysql_rollback(mysqlPtr)
110+
var res = mysql_rollback(mysqlPtr)
111+
var FALSE = 0
112+
return memcmp(&res, &FALSE, MemoryLayout.size(ofValue: res)) != 0
109113
}
110114

111115
/// Checks whether any more results exist
112116
public func moreResults() -> Bool {
113-
return 1 == mysql_more_results(mysqlPtr)
117+
var res = mysql_more_results(mysqlPtr)
118+
var FALSE = 0
119+
return memcmp(&res, &FALSE, MemoryLayout.size(ofValue: res)) != 0
114120
}
115121

116122
/// Returns/initiates the next result in multiple-result executions
@@ -169,22 +175,24 @@ public final class MySQL {
169175
return MYSQL_OPT_WRITE_TIMEOUT
170176
case MySQLOpt.MYSQL_OPT_USE_RESULT:
171177
return MYSQL_OPT_USE_RESULT
178+
/*
172179
case MySQLOpt.MYSQL_OPT_USE_REMOTE_CONNECTION:
173180
return MYSQL_OPT_USE_REMOTE_CONNECTION
174-
case MySQLOpt.MYSQL_OPT_USE_EMBEDDED_CONNECTION:
181+
case MySQLOpt.MYSQL_OPT_USE_EMBEDDED_CONNECTION:
175182
return MYSQL_OPT_USE_EMBEDDED_CONNECTION
176183
case MySQLOpt.MYSQL_OPT_GUESS_CONNECTION:
177184
return MYSQL_OPT_GUESS_CONNECTION
178185
case MySQLOpt.MYSQL_SET_CLIENT_IP:
179186
return MYSQL_SET_CLIENT_IP
180187
case MySQLOpt.MYSQL_SECURE_AUTH:
181188
return MYSQL_SECURE_AUTH
189+
*/
182190
case MySQLOpt.MYSQL_REPORT_DATA_TRUNCATION:
183191
return MYSQL_REPORT_DATA_TRUNCATION
184192
case MySQLOpt.MYSQL_OPT_RECONNECT:
185193
return MYSQL_OPT_RECONNECT
186-
case MySQLOpt.MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
187-
return MYSQL_OPT_SSL_VERIFY_SERVER_CERT
194+
//case MySQLOpt.MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
195+
//return MYSQL_OPT_SSL_VERIFY_SERVER_CERT
188196
case MySQLOpt.MYSQL_PLUGIN_DIR:
189197
return MYSQL_PLUGIN_DIR
190198
case MySQLOpt.MYSQL_DEFAULT_AUTH:

Sources/PerfectMySQL/MySQLStmt.swift

+20-9
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,15 @@ public final class MySQLStmt {
210210
/// Executes a prepared statement, binding parameters if needed
211211
public func execute() -> Bool {
212212
if paramBindsOffset > 0 {
213-
guard let paramBinds = self.paramBinds,
214-
0 == mysql_stmt_bind_param(ptr, paramBinds) else {
213+
guard let paramBinds = self.paramBinds else {
215214
return false
216215
}
216+
var res = mysql_stmt_bind_param(ptr, paramBinds)
217+
var FALSE = 0
218+
let cmp = memcmp(&res, &FALSE, MemoryLayout.size(ofValue: res))
219+
guard cmp == 0 else {
220+
return false
221+
}
217222
}
218223
let r = mysql_stmt_execute(ptr)
219224
return r == 0
@@ -354,7 +359,7 @@ public final class MySQLStmt {
354359
private func genBind<T: UnsignedInteger>(type: enum_field_types, value: T) -> MYSQL_BIND {
355360
var bind = MYSQL_BIND()
356361
bind.buffer_type = type
357-
bind.is_unsigned = 1
362+
memset(&(bind.is_unsigned), 1, MemoryLayout.size(ofValue: bind.is_unsigned))
358363
bind.buffer_length = UInt(MemoryLayout<T>.size)
359364
let b = UnsafeMutablePointer<T>.allocate(capacity: 1)
360365
b.initialize(to: value)
@@ -634,7 +639,9 @@ public final class MySQLStmt {
634639

635640
private func valueForField(_ n: Int) -> Any? {
636641
var bind = binds[n]
637-
guard bind.is_null.pointee == 0 else {
642+
var FALSE = 0
643+
var cmp = memcmp(bind.is_null, &FALSE, MemoryLayout.size(ofValue: bind.is_null.pointee))
644+
guard cmp == 0 else {
638645
return nil
639646
}
640647
let genType = mysqlTypeToGeneralType(bind.buffer_type)
@@ -649,7 +656,8 @@ public final class MySQLStmt {
649656
default: return nil
650657
}
651658
case .integer:
652-
if bind.is_unsigned == 1 {
659+
cmp = memcmp(&(bind.is_unsigned), &FALSE, MemoryLayout.size(ofValue: bind.is_unsigned))
660+
if cmp != 0 {
653661
switch bind.buffer_type {
654662
case MYSQL_TYPE_LONGLONG:
655663
return bind.buffer.assumingMemoryBound(to: UInt64.self).pointee
@@ -766,8 +774,8 @@ public final class MySQLStmt {
766774
var bind = bindField(field)
767775
bind.length = lengthBuffers.advanced(by: i)
768776
bind.length.initialize(to: 0)
769-
bind.is_null = isNullBuffers.advanced(by: i)
770-
bind.is_null.initialize(to: 0)
777+
bind.is_null = unsafeBitCast(isNullBuffers.advanced(by: i), to: type(of: bind.is_null))
778+
memset(bind.is_null, 0, MemoryLayout.size(ofValue: bind.is_null.pointee))
771779

772780
let genType = mysqlTypeToGeneralType(field)
773781
switch genType {
@@ -781,7 +789,7 @@ public final class MySQLStmt {
781789
}
782790
case .integer:
783791
if (f.flags & _UNSIGNED_FLAG) == _UNSIGNED_FLAG {
784-
bind.is_unsigned = 1
792+
memset(&(bind.is_unsigned), 1, MemoryLayout.size(ofValue: bind.is_unsigned))
785793
switch bind.buffer_type {
786794
case MYSQL_TYPE_LONGLONG:
787795
bind = bindBuffer(bind, type: UInt64.self);
@@ -829,7 +837,10 @@ public final class MySQLStmt {
829837
default: break
830838
}
831839
case .integer:
832-
if bind.is_unsigned == 1 {
840+
var FALSE = 0
841+
var res = bind.is_unsigned
842+
let cmp = memcmp(&res, &FALSE, MemoryLayout.size(ofValue: res))
843+
if cmp != 0 {
833844
switch bind.buffer_type {
834845
case MYSQL_TYPE_LONGLONG:
835846
bind.buffer.assumingMemoryBound(to: UInt64.self).deallocate()

Sources/PerfectMySQL/PerfectMySQL.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ public enum MySQLOpt {
9696
MYSQL_SET_CHARSET_DIR, MYSQL_SET_CHARSET_NAME, MYSQL_OPT_LOCAL_INFILE,
9797
MYSQL_OPT_PROTOCOL, MYSQL_SHARED_MEMORY_BASE_NAME, MYSQL_OPT_READ_TIMEOUT,
9898
MYSQL_OPT_WRITE_TIMEOUT, MYSQL_OPT_USE_RESULT,
99-
MYSQL_OPT_USE_REMOTE_CONNECTION, MYSQL_OPT_USE_EMBEDDED_CONNECTION,
100-
MYSQL_OPT_GUESS_CONNECTION, MYSQL_SET_CLIENT_IP, MYSQL_SECURE_AUTH,
99+
//MYSQL_OPT_USE_REMOTE_CONNECTION, MYSQL_OPT_USE_EMBEDDED_CONNECTION,
100+
//MYSQL_OPT_GUESS_CONNECTION, MYSQL_SET_CLIENT_IP, MYSQL_SECURE_AUTH,
101101
MYSQL_REPORT_DATA_TRUNCATION, MYSQL_OPT_RECONNECT,
102-
MYSQL_OPT_SSL_VERIFY_SERVER_CERT, MYSQL_PLUGIN_DIR, MYSQL_DEFAULT_AUTH,
102+
//MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
103+
MYSQL_PLUGIN_DIR, MYSQL_DEFAULT_AUTH,
103104
MYSQL_OPT_BIND,
104105
MYSQL_OPT_SSL_KEY, MYSQL_OPT_SSL_CERT,
105106
MYSQL_OPT_SSL_CA, MYSQL_OPT_SSL_CAPATH, MYSQL_OPT_SSL_CIPHER,

Tests/PerfectMySQLTests/PerfectMySQLTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ class PerfectMySQLTests: XCTestCase {
284284
func testQueryStmt2() {
285285
let mysql = rawMySQL
286286
XCTAssert(mysql.query(statement: "DROP TABLE IF EXISTS all_data_types"))
287-
288287
let qres = mysql.query(statement: "CREATE TABLE `all_data_types` (`varchar` VARCHAR( 22 ),\n`tinyint` TINYINT,\n`text` TEXT,\n`date` DATE,\n`smallint` SMALLINT,\n`mediumint` MEDIUMINT,\n`int` INT,\n`bigint` BIGINT,\n`ubigint` BIGINT UNSIGNED,\n`float` FLOAT( 10, 2 ),\n`double` DOUBLE,\n`decimal` DECIMAL( 10, 2 ),\n`datetime` DATETIME,\n`timestamp` TIMESTAMP,\n`time` TIME,\n`year` YEAR,\n`char` CHAR( 10 ),\n`tinyblob` TINYBLOB,\n`tinytext` TINYTEXT,\n`blob` BLOB,\n`mediumblob` MEDIUMBLOB,\n`mediumtext` MEDIUMTEXT,\n`longblob` LONGBLOB,\n`longtext` LONGTEXT,\n`enum` ENUM( '1', '2', '3' ),\n`set` SET( '1', '2', '3' ),\n`bool` BOOL,\n`binary` BINARY( 20 ),\n`varbinary` VARBINARY( 20 ) ) ENGINE = MYISAM")
289288
XCTAssert(qres == true, mysql.errorMessage())
290289

0 commit comments

Comments
 (0)