diff --git a/plugins/include/dbi.inc b/plugins/include/dbi.inc index d1140bbf8..4556fe792 100644 --- a/plugins/include/dbi.inc +++ b/plugins/include/dbi.inc @@ -701,18 +701,7 @@ native SQL_TQuery(Handle:database, SQLTCallback:callback, const String:query[], * * @return A transaction handle. */ -native Handle:SQL_CreateTransaction(); - -/** - * Adds a query to a transaction object. - * - * @param txn A transaction handle. - * @param query Query string. - * @param data Extra data value to pass to the final callback. - * @return The index of the query in the transaction's query list. - * @error Invalid transaction handle. - */ -native SQL_AddQuery(Handle:txn, const String:query[], any:data=0); +native Transaction:SQL_CreateTransaction(); /** * Callback for a successful transaction. @@ -739,6 +728,20 @@ functag public SQLTxnSuccess(Handle:db, any:data, numQueries, Handle:results[], */ functag public SQLTxnFailure(Handle:db, any:data, numQueries, const String:error[], failIndex, any:queryData[]); +methodmap Transaction < Handle +{ + /** + * Adds a query to a transaction object. + * + * @param txn A transaction handle. + * @param query Query string. + * @param data Extra data value to pass to the final callback. + * @return The index of the query in the transaction's query list. + * @error Invalid transaction handle. + */ + AddQuery = native SQL_AddQuery(Transaction:txn, const String:query[], any:data=0); +}; + /** * Sends a transaction to the database thread. The transaction handle is * automatically closed. When the transaction completes, the optional @@ -755,7 +758,7 @@ functag public SQLTxnFailure(Handle:db, any:data, numQueries, const String:error */ native SQL_ExecuteTransaction( Handle:db, - Handle:txn, + Transaction:txn, SQLTxnSuccess:onSuccess=SQLTxnSuccess:-1, SQLTxnFailure:onError=SQLTxnFailure:-1, any:data=0, diff --git a/plugins/include/handles.inc b/plugins/include/handles.inc index b09703d04..1305cc0bb 100644 --- a/plugins/include/handles.inc +++ b/plugins/include/handles.inc @@ -76,6 +76,15 @@ native bool:CloseHandle(Handle:hndl); */ native Handle:CloneHandle(Handle:hndl, Handle:plugin=INVALID_HANDLE); +/** + * Helper for object-oriented syntax. + */ +methodmap Handle +{ + Clone = CloneHandle; + Close = CloseHandle; +}; + /** * Do not use this function. Returns if a Handle and its contents * are readable, whereas INVALID_HANDLE only checks for the absence diff --git a/plugins/testsuite/sqltest.sp b/plugins/testsuite/sqltest.sp index e615713ff..1f5ac9787 100644 --- a/plugins/testsuite/sqltest.sp +++ b/plugins/testsuite/sqltest.sp @@ -279,10 +279,10 @@ public Action:Command_TestTxn(args) SetTestContext("CreateTransaction"); - new Handle:txn = SQL_CreateTransaction(); - AssertEq("AddQuery", SQL_AddQuery(txn, "INSERT INTO egg (id) VALUES (4)", 50), 0); - AssertEq("AddQuery", SQL_AddQuery(txn, "INSERT INTO egg (id) VALUES (5)", 60), 1); - AssertEq("AddQuery", SQL_AddQuery(txn, "SELECT COUNT(id) FROM egg", 70), 2); + new Transaction:txn = SQL_CreateTransaction(); + AssertEq("AddQuery", txn.AddQuery("INSERT INTO egg (id) VALUES (4)", 50), 0); + AssertEq("AddQuery", txn.AddQuery("INSERT INTO egg (id) VALUES (5)", 60), 1); + AssertEq("AddQuery", txn.AddQuery("SELECT COUNT(id) FROM egg", 70), 2); SQL_ExecuteTransaction( db, txn, @@ -292,9 +292,9 @@ public Action:Command_TestTxn(args) ); txn = SQL_CreateTransaction(); - AssertEq("AddQuery", SQL_AddQuery(txn, "INSERT INTO egg (id) VALUES (6)", 50), 0); - AssertEq("AddQuery", SQL_AddQuery(txn, "INSERT INTO egg (id) VALUES (6)", 60), 1); - AssertEq("AddQuery", SQL_AddQuery(txn, "SELECT COUNT(id) FROM egg", 70), 2); + AssertEq("AddQuery", txn.AddQuery("INSERT INTO egg (id) VALUES (6)", 50), 0); + AssertEq("AddQuery", txn.AddQuery("INSERT INTO egg (id) VALUES (6)", 60), 1); + AssertEq("AddQuery", txn.AddQuery("SELECT COUNT(id) FROM egg", 70), 2); SQL_ExecuteTransaction( db, txn, @@ -306,13 +306,13 @@ public Action:Command_TestTxn(args) // Make sure the transaction was rolled back - COUNT should be 5. txn = SQL_CreateTransaction(); AssertEq("CloneHandle", _:CloneHandle(txn), _:INVALID_HANDLE); - SQL_AddQuery(txn, "SELECT COUNT(id) FROM egg"); + txn.AddQuery("SELECT COUNT(id) FROM egg"); SQL_ExecuteTransaction( db, txn, Txn_Test3_OnSuccess ); - CloseHandle(db); + db.Close(); return Plugin_Handled; } diff --git a/sourcepawn/compiler/tests/fail-array-on-implicit-this.sp b/sourcepawn/compiler/tests/fail-array-on-implicit-this.sp new file mode 100644 index 000000000..d796c0b21 --- /dev/null +++ b/sourcepawn/compiler/tests/fail-array-on-implicit-this.sp @@ -0,0 +1,10 @@ +native CloseHandle(Handle:this[]); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ +} diff --git a/sourcepawn/compiler/tests/fail-bad-upcast.sp b/sourcepawn/compiler/tests/fail-bad-upcast.sp new file mode 100644 index 000000000..43e8024ab --- /dev/null +++ b/sourcepawn/compiler/tests/fail-bad-upcast.sp @@ -0,0 +1,15 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Close = CloseHandle; +}; + +methodmap Crab { +}; + +public main() +{ + new Crab:x; + new Handle:y; + x = y; +} diff --git a/sourcepawn/compiler/tests/fail-method-on-array.sp b/sourcepawn/compiler/tests/fail-method-on-array.sp new file mode 100644 index 000000000..9e1f8f909 --- /dev/null +++ b/sourcepawn/compiler/tests/fail-method-on-array.sp @@ -0,0 +1,13 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ + new Handle:x[2]; + + x.Close(); +} diff --git a/sourcepawn/compiler/tests/fail-method-on-function.sp b/sourcepawn/compiler/tests/fail-method-on-function.sp new file mode 100644 index 000000000..653eb66e0 --- /dev/null +++ b/sourcepawn/compiler/tests/fail-method-on-function.sp @@ -0,0 +1,11 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ + main.Close(); +} diff --git a/sourcepawn/compiler/tests/fail-mismatch-on-implicit-this.sp b/sourcepawn/compiler/tests/fail-mismatch-on-implicit-this.sp new file mode 100644 index 000000000..eeb502464 --- /dev/null +++ b/sourcepawn/compiler/tests/fail-mismatch-on-implicit-this.sp @@ -0,0 +1,10 @@ +native CloseHandle(HandleEgg:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ +} diff --git a/sourcepawn/compiler/tests/fail-multi-tag-on-implicit-this.sp b/sourcepawn/compiler/tests/fail-multi-tag-on-implicit-this.sp new file mode 100644 index 000000000..e9e05742b --- /dev/null +++ b/sourcepawn/compiler/tests/fail-multi-tag-on-implicit-this.sp @@ -0,0 +1,10 @@ +native CloseHandle({Handle, Egg}:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ +} diff --git a/sourcepawn/compiler/tests/fail-no-tag-on-implicit-this.sp b/sourcepawn/compiler/tests/fail-no-tag-on-implicit-this.sp new file mode 100644 index 000000000..31a0b280a --- /dev/null +++ b/sourcepawn/compiler/tests/fail-no-tag-on-implicit-this.sp @@ -0,0 +1,10 @@ +native CloseHandle(this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ +} diff --git a/sourcepawn/compiler/tests/fail-ref-on-implicit-this.sp b/sourcepawn/compiler/tests/fail-ref-on-implicit-this.sp new file mode 100644 index 000000000..8a842e6ba --- /dev/null +++ b/sourcepawn/compiler/tests/fail-ref-on-implicit-this.sp @@ -0,0 +1,10 @@ +native CloseHandle(&Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ +} diff --git a/sourcepawn/compiler/tests/ok-inheritance.sp b/sourcepawn/compiler/tests/ok-inheritance.sp new file mode 100644 index 000000000..1eb085c4a --- /dev/null +++ b/sourcepawn/compiler/tests/ok-inheritance.sp @@ -0,0 +1,15 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +methodmap Crab < Handle { +}; + +public main() +{ + new Crab:x; + x.Close(); +} diff --git a/sourcepawn/compiler/tests/ok-method-on-const.sp b/sourcepawn/compiler/tests/ok-method-on-const.sp new file mode 100644 index 000000000..2f50181f4 --- /dev/null +++ b/sourcepawn/compiler/tests/ok-method-on-const.sp @@ -0,0 +1,15 @@ +native CloseHandle(Handle:this); + +enum Handle { + INVALID_HANDLE = 0, +}; + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ + INVALID_HANDLE.Close(); +} diff --git a/sourcepawn/compiler/tests/ok-method-on-constref.sp b/sourcepawn/compiler/tests/ok-method-on-constref.sp new file mode 100644 index 000000000..156e8b216 --- /dev/null +++ b/sourcepawn/compiler/tests/ok-method-on-constref.sp @@ -0,0 +1,17 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +f(const &Handle:x) +{ + x.Close() +} + +public main() +{ + new Handle:x; + f(x) +} diff --git a/sourcepawn/compiler/tests/ok-method-on-element.sp b/sourcepawn/compiler/tests/ok-method-on-element.sp new file mode 100644 index 000000000..326d56a8d --- /dev/null +++ b/sourcepawn/compiler/tests/ok-method-on-element.sp @@ -0,0 +1,12 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ + new Handle:x[2]; + x[1].Close(); +} diff --git a/sourcepawn/compiler/tests/ok-method-on-ref.sp b/sourcepawn/compiler/tests/ok-method-on-ref.sp new file mode 100644 index 000000000..900ffd13d --- /dev/null +++ b/sourcepawn/compiler/tests/ok-method-on-ref.sp @@ -0,0 +1,17 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +f(&Handle:x) +{ + x.Close() +} + +public main() +{ + new Handle:x; + f(x) +} diff --git a/sourcepawn/compiler/tests/ok-method-on-scalar.sp b/sourcepawn/compiler/tests/ok-method-on-scalar.sp new file mode 100644 index 000000000..eaebfcbeb --- /dev/null +++ b/sourcepawn/compiler/tests/ok-method-on-scalar.sp @@ -0,0 +1,12 @@ +native CloseHandle(Handle:this); + +methodmap Handle { + Clone = native Handle:CloneHandle(Handle:this); + Close = CloseHandle; +}; + +public main() +{ + new Handle:x; + x.Close(); +}