Skip to content

Commit

Permalink
merge: mysqli_report()
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Oct 8, 2022
1 parent f3cc084 commit 070bbed
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 63 deletions.
6 changes: 6 additions & 0 deletions src/Peachpie.Library.MySql/MySqlConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ internal MySqlConnectionResource CreateConnection(IDbConnection dbconnection)

return connection;
}

public virtual void ReportException(Exception exception, string exceptionMessage)
{
// MySql outputs the error to php error handler
PhpException.Throw(PhpError.Warning, exceptionMessage);
}
}
}
21 changes: 21 additions & 0 deletions src/Peachpie.Library.MySql/MySqlConnectionResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,26 @@ internal long LastInsertedId
return command != null ? MySqlExtensions.LastInsertedId(command) : -1;
}
}

public override int GetLastErrorNumber()
{
if (LastException == null)
{
return (int)MySqlErrorCode.None; // success
}
else if (LastException is MySqlException me)
{
return (int)me.ErrorCode;
}
else
{
return (int)MySqlErrorCode.UnknownError; // unk erro number
}
}

protected override void ReportException(Exception exception, string exceptionMessage)
{
_manager.ReportException(exception, exceptionMessage);
}
}
}
30 changes: 20 additions & 10 deletions src/Peachpie.Library.MySql/MySqli/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,30 @@ public static class Constants
/// <summary></summary>
public const int MYSQLI_SET_CHARSET_NAME = 7;

//MYSQLI_REPORT_INDEX
//Report if no index or bad index was used in a query.
/// <summary>
/// Turns reporting off.
/// </summary>
public const int MYSQLI_REPORT_OFF = 0;

//MYSQLI_REPORT_ERROR
//Report errors from mysqli function calls.
/// <summary>
/// Report errors from mysqli function calls.
/// </summary>
public const int MYSQLI_REPORT_ERROR = 1;

//MYSQLI_REPORT_STRICT
//Throw a mysqli_sql_exception for errors instead of warnings.
/// <summary>
/// Throw a <see cref="mysqli_sql_exception"/> for errors instead of warnings.
/// </summary>
public const int MYSQLI_REPORT_STRICT = 2;

//MYSQLI_REPORT_ALL
//Set all options on (report all).
/// <summary>
/// Report if no index or bad index was used in a query.
/// </summary>
public const int MYSQLI_REPORT_INDEX = 4;

//MYSQLI_REPORT_OFF
//Turns reporting off.
/// <summary>
/// Set all options on (report all).
/// </summary>
public const int MYSQLI_REPORT_ALL = 0xff;

//MYSQLI_DEBUG_TRACE_ENABLED
//Is set to 1 if mysqli_debug() functionality is enabled.
Expand Down
23 changes: 8 additions & 15 deletions src/Peachpie.Library.MySql/MySqli/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ namespace Peachpie.Library.MySql.MySqli
[PhpExtension(Constants.ExtensionName)]
public static class Functions
{
/// <summary>
/// Internal object used to store per-request (context) data.
/// </summary>
internal class MySqliContextData
{
public static MySqliContextData/*!*/GetContextData(Context ctx) => ctx.GetStatic<MySqliContextData>();

public string LastConnectionError { get; set; }
}

/// <summary>
/// Initializes MySQLi and returns a resource for use with mysqli_real_connect().
/// </summary>
Expand Down Expand Up @@ -96,15 +86,15 @@ public static mysqli mysqli_connect(Context ctx, IDbConnection dbconnection/*, b
/// The connection error message. Otherwise <c>null</c>.
/// </summary>
public static string mysqli_connect_error(Context ctx, mysqli link = null)
=> (link != null) ? link.connect_error : MySqliContextData.GetContextData(ctx).LastConnectionError;
=> (link != null) ? link.connect_error : MySqliConnectionManager.GetInstance(ctx).LastConnectionError;

/// <summary>
/// Returns the error code from last connect call.
/// </summary>
public static int mysqli_connect_errno(Context ctx, mysqli link = null)
=> (link != null)
? link.connect_errno
: string.IsNullOrEmpty(MySqliContextData.GetContextData(ctx).LastConnectionError) ? 0 : -1;
: string.IsNullOrEmpty(MySqliConnectionManager.GetInstance(ctx).LastConnectionError) ? 0 : -1;

/// <summary>
/// Returns the error code for the most recent function call.
Expand Down Expand Up @@ -369,10 +359,13 @@ public static bool mysqli_ssl_set(mysqli link, string key = null, string cert =
/// <summary>
/// Sets mysqli error reporting mode.
/// </summary>
public static bool mysqli_report(int flags)
public static bool mysqli_report(Context ctx, ReportMode flags)
{
PhpException.FunctionNotSupported(nameof(mysqli_report));
return false;
var manager = MySqliConnectionManager.GetInstance(ctx);

manager.ReportMode = flags;

return true;
}
}
}
69 changes: 69 additions & 0 deletions src/Peachpie.Library.MySql/MySqli/MySqliConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySqlConnector;
using Pchp.Core;

namespace Peachpie.Library.MySql.MySqli
{
class MySqliConnectionManager : MySqlConnectionManager
{
/// <summary>
/// Gets the manager singleton within runtime context.
/// </summary>
public static new MySqliConnectionManager GetInstance(Context ctx) => ctx.GetStatic<MySqliConnectionManager>();

public string LastConnectionError { get; set; }

/// <summary>
/// The error handling mode.
/// </summary>
public ReportMode ReportMode { get; set; } = ReportMode.Default;

public override void ReportException(Exception exception, string exceptionMessage)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}

if ((ReportMode & ReportMode.Error) != 0)
{
if ((ReportMode & ReportMode.Strict) != 0)
{
var errcode = exception is MySqlException me ? me.ErrorCode : MySqlErrorCode.UnknownError;

throw new mysqli_sql_exception(exceptionMessage, (int)errcode);
}
else
{
// outputs the error to php error handler
PhpException.Throw(PhpError.Warning, exceptionMessage);
}
}
}
}

/// <summary>
/// MYSQLI_REPORT_*** constants.
/// </summary>
[PhpHidden, Flags]
public enum ReportMode
{
/// <summary><see cref="Constants.MYSQLI_REPORT_OFF"/></summary>
Off = Constants.MYSQLI_REPORT_OFF,
/// <summary><see cref="Constants.MYSQLI_REPORT_ERROR"/></summary>
Error = Constants.MYSQLI_REPORT_ERROR,
/// <summary><see cref="Constants.MYSQLI_REPORT_STRICT"/></summary>
Strict = Constants.MYSQLI_REPORT_STRICT,
/// <summary><see cref="Constants.MYSQLI_REPORT_INDEX"/></summary>
Index = Constants.MYSQLI_REPORT_INDEX,
/// <summary><see cref="Constants.MYSQLI_REPORT_ALL"/></summary>
All = Constants.MYSQLI_REPORT_ALL,

/// <summary>Default error handling flags.</summary>
Default = Error | Strict, // as it is in PHP 8.1
}
}
11 changes: 5 additions & 6 deletions src/Peachpie.Library.MySql/MySqli/mysqli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal mysqli(Context ctx, IDbConnection dbconnection)
{
// create connection resource and
// register it in the list of active connections
this.Connection = MySqlConnectionManager
this.Connection = MySqliConnectionManager
.GetInstance(ctx)
.CreateConnection(dbconnection);
}
Expand Down Expand Up @@ -306,6 +306,7 @@ public PhpValue query(PhpString query, int resultmode = Constants.MYSQLI_STORE_R
public bool real_connect(Context ctx, string host = null, string username = null, string passwd = null, string dbname = "", int port = -1, string socket = null, int flags = 0)
{
var config = ctx.Configuration.Get<MySqlConfiguration>();
var manager = MySqliConnectionManager.GetInstance(ctx);

// string $host = ini_get("mysqli.default_host")
// string $username = ini_get("mysqli.default_user")
Expand Down Expand Up @@ -346,12 +347,12 @@ public bool real_connect(Context ctx, string host = null, string username = null
}
}

Connection = MySqlConnectionManager.GetInstance(ctx)
.CreateConnection(connection_string.ToString(), true, -1, out bool success);
Connection = manager.CreateConnection(connection_string.ToString(), true, -1, out bool success);

if (success)
{
Connection.Server = host;
manager.LastConnectionError = null;

if (!string.IsNullOrEmpty(dbname))
{
Expand All @@ -360,9 +361,7 @@ public bool real_connect(Context ctx, string host = null, string username = null
}
else
{
MySqliContextData.GetContextData(ctx).LastConnectionError
= connect_error
= Connection.GetLastErrorMessage();
manager.LastConnectionError = connect_error = Connection.GetLastErrorMessage();
}

//
Expand Down
9 changes: 4 additions & 5 deletions src/Peachpie.Library/Database/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public abstract class ConnectionManager<TConnection> : IStaticInit where TConnec
/// <summary>
/// Associated runtime context.
/// </summary>
public Context Context => _ctx;
Context _ctx;
public Context Context { get; private set; }

/// <summary>
/// List of connections established by the manager.
Expand Down Expand Up @@ -69,7 +68,7 @@ public TConnection CreateConnection(string connectionString, bool newConnection,
if ((success = connection.Connect()) == true)
{
AddConnection(connection);
_ctx.RegisterDisposable(connection);
this.Context.RegisterDisposable(connection);
}

return connection;
Expand Down Expand Up @@ -108,7 +107,7 @@ protected void AddConnection(TConnection connection)
public bool RemoveConnection(TConnection connection)
{
//
_ctx.UnregisterDisposable(connection);
this.Context.UnregisterDisposable(connection);

//
if (_connections.Count != 0)
Expand All @@ -132,7 +131,7 @@ public TConnection GetLastConnection()

void IStaticInit.Init(Context ctx)
{
_ctx = ctx;
this.Context = ctx;
}
}
}
Loading

0 comments on commit 070bbed

Please sign in to comment.