<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Subclassing &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/subclassing/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Wed, 05 Feb 2025 19:00:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Customize and Extend Core Databases Functionality</title>
		<link>https://blog.xojo.com/2025/02/05/customize-and-extend-core-databases-functionality/</link>
		
		<dc:creator><![CDATA[Gabriel Ludosanu]]></dc:creator>
		<pubDate>Wed, 05 Feb 2025 19:00:33 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Method Overriding]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Subclassing]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=14396</guid>

					<description><![CDATA[Have you ever debugged your Xojo database app and wondered about the SQL queries being executed and the parameters used? You are not alone. Whether&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Have you ever debugged your Xojo database app and wondered about the SQL queries being executed and the parameters used? You are not alone. Whether you’re developing a desktop, web, or mobile app, having clear insight into your database interactions is crucial for effective debugging and ensuring your app functions correctly.</p>



<p>This article will show you how to automatically record queries and commands in any Xojo database class to help you debug and improve database operations by using method overriding. This will help you:</p>



<ul class="wp-block-list">
<li>Debug SQL operations faster by seeing exactly what’s being executed.</li>



<li>Spot errors in your SQL queries or parameters before they become runtime issues.</li>



<li>Gain transparency into your app’s database behavior.</li>
</ul>



<p>By the end of this tutorial, you’ll have a clear understanding of:</p>



<ul class="wp-block-list">
<li><strong>Method overriding</strong>: What it is and how you can use it to extend built-in Xojo classes.</li>



<li><strong>Subclassing database classes:</strong>&nbsp;Learn how to develop a custom database class with added logging functionality to improve database management and tracking.</li>



<li><strong>Logging SQL commands and parameters:</strong>&nbsp;Discover how to effectively capture and log database operations such as SELECT, INSERT, and DELETE to enhance visibility and improve database performance.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading" id="what-is-method-overriding"><strong>What is Method Overriding?</strong></h3>



<p>Method overriding, a fundamental concept in object-oriented programming (OOP), lets you customize how a subclass implements its superclass’s methods. In Xojo, this powerful feature allows you to enhance built-in classes without modifying their original code, ensuring you preserve core functionality while adding your own features.</p>



<h4 class="wp-block-heading" id="how-method-overriding-works-in-xojo"><strong>How Method Overriding Works in Xojo</strong></h4>



<p>Let’s break it down step by step with a simple example. Imagine you’re working with the&nbsp;<code>SQLiteDatabase</code>&nbsp;class, and you want to modify the behavior of the&nbsp;<code>ExecuteSQL</code>&nbsp;method to log every SQL command executed.</p>



<ol class="wp-block-list">
<li><strong>Create a Subclass</strong>:
<ul class="wp-block-list">
<li>Create a new class in Xojo and set its Super to&nbsp;<code>SQLiteDatabase</code>. This means your new class inherits all the properties and methods of&nbsp;<code>SQLiteDatabase</code>.</li>
</ul>
</li>



<li><strong>Override the Method</strong>:
<ul class="wp-block-list">
<li>In your new subclass, define a method and from the Method Name picker, select&nbsp;<code>ExecuteSQL</code>.</li>



<li>Add your custom logic, like logging the SQL command to the Xojo debugger.</li>
</ul>
</li>



<li><strong>Call the Original Method</strong>:
<ul class="wp-block-list">
<li>Use the&nbsp;<code>Super</code>&nbsp;keyword to call the original&nbsp;<code>ExecuteSQL</code>&nbsp;method in the superclass. This ensures the original behavior is preserved alongside your custom logic.</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading" id="extending-xojo’s-sqlitedatabase-with-method-overriding"><strong>Extending Xojo’s SQLiteDatabase with Method Overriding</strong></h3>



<p>Let’s enhance Xojo’s <code>SQLiteDatabase</code> class by creating a subclass that automatically logs <code>SELECT</code> queries and their parameters. To make this process easier, use the built-in “Full-Text Searching 5” example project, which already includes various database operations.</p>


<div class="wp-block-image">
<figure class="aligncenter size-medium"><img fetchpriority="high" decoding="async" width="300" height="294" src="https://blog.xojo.com/wp-content/uploads/2025/01/xojo-full-search-database-example-300x294.webp" alt="" class="wp-image-14399" srcset="https://blog.xojo.com/wp-content/uploads/2025/01/xojo-full-search-database-example-300x294.webp 300w, https://blog.xojo.com/wp-content/uploads/2025/01/xojo-full-search-database-example-768x753.webp 768w, https://blog.xojo.com/wp-content/uploads/2025/01/xojo-full-search-database-example.webp 1006w" sizes="(max-width: 300px) 100vw, 300px" /></figure>
</div>


<h4 class="wp-block-heading" id="create-the-mysqlitedatabase-subclass"><strong>1. Create the</strong>&nbsp;<code>MySQLiteDatabase</code>&nbsp;<strong>Subclass</strong></h4>



<ol class="wp-block-list">
<li>From the Insert menu, select Class.</li>



<li>Name the new class&nbsp;<code>MySQLiteDatabase</code>.</li>



<li>In the Inspector, set the Super field to <code>SQLiteDatabase</code>.</li>
</ol>



<p>This tells Xojo that&nbsp;<code>MySQLiteDatabase</code>&nbsp;will inherit all the behavior of&nbsp;<code>SQLiteDatabase</code>, allowing you to override its methods.</p>



<h4 class="wp-block-heading" id="override-the-selectsql-method"><strong>2. Override the</strong>&nbsp;<code>SelectSQL</code>&nbsp;<strong>Method</strong></h4>



<p>Now, you’ll override the&nbsp;<code>SelectSQL</code>&nbsp;method in your&nbsp;<code>MySQLiteDatabase</code>&nbsp;class. This method is used for&nbsp;<code>SELECT</code>&nbsp;queries, so you’ll add logging to display the query and its parameters in the debugger.</p>



<pre class="wp-block-code"><code>Public Function SelectSQL(query As String, ParamArray values() As Variant) As RowSet
  // Log the SELECT query
  System.DebugLog("&#91;DEBUG] SelectSQL called: " + query)
  
  // Log the parameters (if any)
  If values.LastIndex >= 0 Then
    Var stringValues() As String
    For Each v As Variant In values
      stringValues.Add(v.StringValue) // Convert Variant to String
    Next
    System.DebugLog("&#91;DEBUG] Parameters: " + String.FromArray(stringValues, ", "))
  End If

  // Call the original SelectSQL method
  Try
    Return Super.SelectSQL(query, values)
  Catch error As DatabaseException
    // Log any errors
    System.DebugLog("&#91;ERROR] DatabaseException: " + error.Message)
    Raise error // Re-raise the exception so the app can handle it
  End Try
End Function</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading" id="replace-the-database-property"><strong>3. Replace the Database Property</strong></h4>



<p>Now that you’ve created the&nbsp;<code>MySQLiteDatabase</code>&nbsp;subclass, you need to update the example project to use it.</p>



<ol class="wp-block-list">
<li>Open <code>Window1</code> in the Project Browser.</li>



<li>Locate the&nbsp;<code>DB</code>&nbsp;property in&nbsp;<code>Window1</code>. It is currently defined as&nbsp;<code>DB As SQLiteDatabase</code>:</li>



<li>Change the type of&nbsp;<code>DB</code>&nbsp;from&nbsp;<code>SQLiteDatabase</code>&nbsp;to&nbsp;<code>MySQLiteDatabase</code>. To do this:
<ul class="wp-block-list">
<li>Select the&nbsp;<code>DB</code>&nbsp;property.</li>



<li>In the Inspector, update the Type field to <code>MySQLiteDatabase</code>.</li>
</ul>
</li>
</ol>



<p>Additionally, under the Opening event of Window1, change&nbsp;<code>DB = New SQLiteDatabase</code>&nbsp;to&nbsp;<code>DB = New MySQLiteDatabase</code>.</p>



<p>This replaces the original&nbsp;<code>SQLiteDatabase</code>&nbsp;with your custom subclass, ensuring all database operations now use your overridden&nbsp;<code>SelectSQL</code>&nbsp;method.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading" id="test-the-logging"><strong>4. Test the Logging</strong></h4>



<p>With the subclass in place, run the project and test the logging functionality.</p>



<ol class="wp-block-list">
<li>Run the project.</li>



<li>Perform a search in the app’s UI. Each search generates a&nbsp;<code>SELECT</code>&nbsp;query using the&nbsp;<code>SelectSQL</code>&nbsp;method.</li>



<li>Check Xojo’s Messages Panel for output. You should see the SQL query and its parameters logged.</li>
</ol>



<p>Example Output:</p>



<pre class="wp-block-code"><code>&#91;DEBUG] SelectSQL called: SELECT highlight(ftstest, 0, '&lt;', '>') FROM ftstest WHERE ftstest MATCH 'know' ORDER BY rank;</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading" id="additional-enhancements"><strong>Additional Enhancements</strong></h3>



<p>Now that you’ve created a foundation for extending Xojo’s database classes by logging queries with your custom subclass, it’s time to think about how you can take this further. Below are several ideas for additional enhancements that will make your subclass more robust, versatile, and useful for debugging and performance monitoring.</p>



<h4 class="wp-block-heading" id="log-additional-methods"><strong>Log Additional Methods</strong></h4>



<p>While you’ve started with the&nbsp;<code>SelectSQL</code>&nbsp;method, your app likely performs other database operations, such as&nbsp;<code>INSERT</code>,&nbsp;<code>UPDATE</code>,&nbsp;<code>DELETE</code>, or even transactions. You can override methods like&nbsp;<code>ExecuteSQL</code>&nbsp;to log these operations along with their parameters. Extending the logging functionality to these key methods ensures a complete picture of all database activity.</p>



<p>Additionally, logging transactional methods like&nbsp;<code>BeginTransaction</code>,&nbsp;<code>CommitTransaction</code>, and&nbsp;<code>RollbackTransaction</code>&nbsp;can help you trace when database changes are grouped together and ensure data integrity.</p>



<p>Tip: You can incorporate the&nbsp;<a href="https://blog.xojo.com/2024/11/26/log4xojo-a-more-powerful-way-to-manage-your-app-logging/" target="_blank" rel="noreferrer noopener">Log4Xojo project</a>.</p>



<h4 class="wp-block-heading" id="support-for-multiple-database-classes"><strong>Support for Multiple Database Classes</strong></h4>



<p>If your app uses different database types, such as&nbsp;<code>MySQLCommunityServer</code>&nbsp;or&nbsp;<code>PostgreSQLDatabase</code>, you can generalize your logging functionality to work with all of them. This can be achieved by creating a base class for logging that inherits from&nbsp;<code>Database</code>&nbsp;and then extending it for specific database types. This approach ensures consistency and reusability across different database systems.</p>



<h4 class="wp-block-heading" id="monitor-query-performance"><strong>Monitor Query Performance</strong></h4>



<p>Tracking the time it takes for queries to execute can give you valuable insights into your application’s performance. By measuring the duration of each query, you can identify slow queries and optimize them for better efficiency. Additionally, you could log a warning if a query takes longer than a specified threshold, helping you catch performance issues early.</p>



<h4 class="wp-block-heading" id="automatically-retry-failed-queries"><strong>Automatically Retry Failed Queries</strong></h4>



<p>Sometimes, database operations fail due to transient errors, such as network interruptions or locked tables. Implementing retry logic for failed queries can make your app more resilient. For example, you could attempt a query up to three times before logging it as a failure. This is particularly useful for apps that rely on remote database servers, where connectivity issues are more likely.</p>



<h4 class="wp-block-heading" id="add-context-specific-metadata"><strong>Add Context-Specific Metadata</strong></h4>



<p>Including additional context in your logs can make them more meaningful. For example, you might log:</p>



<ul class="wp-block-list">
<li>The name of the method or function that triggered the query.</li>



<li>The user ID or session ID (if applicable) for tracking user-specific database activity.</li>



<li>Tags or labels to group related queries or transactions.</li>
</ul>



<p>This kind of metadata makes it easier to understand the “why” behind a query, not just the “what.”</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading" id="wrapping-it-all-up"><strong>Wrapping It All Up</strong></h3>



<p>Congratulations! You’ve taken a deep dive into one of the most powerful techniques in Xojo development: extending built-in classes, like&nbsp;<code>SQLiteDatabase</code>, to meet your specific needs. By following the steps in this tutorial, you’ve learned how to override methods, create a custom database subclass, and log SQL queries for better debugging and transparency.</p>



<h3 class="wp-block-heading"><strong>What’s Next?</strong></h3>



<p>If you enjoyed this tutorial, we’d love to hear from you! Share your thoughts, your customizations, or any challenges you faced in the&nbsp;<a href="https://forum.xojo.com/" target="_blank" rel="noreferrer noopener">Xojo forums</a>. If you’ve built something amazing using this technique, let us know—we’d love to feature your work in our community showcase.</p>



<p>Keep coding, keep innovating, and as always, happy developing! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p><em>Gabriel is a digital marketing enthusiast who loves coding with Xojo to create cool software tools for any platform. He is always eager to learn and share new ideas!</em></p>



<ul class="wp-block-social-links has-normal-icon-size is-content-justification-center is-layout-flex wp-container-core-social-links-is-layout-16018d1d wp-block-social-links-is-layout-flex"><li class="wp-social-link wp-social-link-facebook  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.facebook.com/goxojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12 2C6.5 2 2 6.5 2 12c0 5 3.7 9.1 8.4 9.9v-7H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.2.2 2.2.2v2.5h-1.3c-1.2 0-1.6.8-1.6 1.6V12h2.8l-.4 2.9h-2.3v7C18.3 21.1 22 17 22 12c0-5.5-4.5-10-10-10z"></path></svg><span class="wp-block-social-link-label screen-reader-text">Facebook</span></a></li>

<li class="wp-social-link wp-social-link-x  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://x.com/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M13.982 10.622 20.54 3h-1.554l-5.693 6.618L8.745 3H3.5l6.876 10.007L3.5 21h1.554l6.012-6.989L15.868 21h5.245l-7.131-10.378Zm-2.128 2.474-.697-.997-5.543-7.93H8l4.474 6.4.697.996 5.815 8.318h-2.387l-4.745-6.787Z" /></svg><span class="wp-block-social-link-label screen-reader-text">X</span></a></li>

<li class="wp-social-link wp-social-link-linkedin  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.linkedin.com/company/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M19.7,3H4.3C3.582,3,3,3.582,3,4.3v15.4C3,20.418,3.582,21,4.3,21h15.4c0.718,0,1.3-0.582,1.3-1.3V4.3 C21,3.582,20.418,3,19.7,3z M8.339,18.338H5.667v-8.59h2.672V18.338z M7.004,8.574c-0.857,0-1.549-0.694-1.549-1.548 c0-0.855,0.691-1.548,1.549-1.548c0.854,0,1.547,0.694,1.547,1.548C8.551,7.881,7.858,8.574,7.004,8.574z M18.339,18.338h-2.669 v-4.177c0-0.996-0.017-2.278-1.387-2.278c-1.389,0-1.601,1.086-1.601,2.206v4.249h-2.667v-8.59h2.559v1.174h0.037 c0.356-0.675,1.227-1.387,2.526-1.387c2.703,0,3.203,1.779,3.203,4.092V18.338z"></path></svg><span class="wp-block-social-link-label screen-reader-text">LinkedIn</span></a></li>

<li class="wp-social-link wp-social-link-github  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://github.com/topics/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z"></path></svg><span class="wp-block-social-link-label screen-reader-text">GitHub</span></a></li>

<li class="wp-social-link wp-social-link-youtube  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.youtube.com/c/XojoInc" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M21.8,8.001c0,0-0.195-1.378-0.795-1.985c-0.76-0.797-1.613-0.801-2.004-0.847c-2.799-0.202-6.997-0.202-6.997-0.202 h-0.009c0,0-4.198,0-6.997,0.202C4.608,5.216,3.756,5.22,2.995,6.016C2.395,6.623,2.2,8.001,2.2,8.001S2,9.62,2,11.238v1.517 c0,1.618,0.2,3.237,0.2,3.237s0.195,1.378,0.795,1.985c0.761,0.797,1.76,0.771,2.205,0.855c1.6,0.153,6.8,0.201,6.8,0.201 s4.203-0.006,7.001-0.209c0.391-0.047,1.243-0.051,2.004-0.847c0.6-0.607,0.795-1.985,0.795-1.985s0.2-1.618,0.2-3.237v-1.517 C22,9.62,21.8,8.001,21.8,8.001z M9.935,14.594l-0.001-5.62l5.404,2.82L9.935,14.594z"></path></svg><span class="wp-block-social-link-label screen-reader-text">YouTube</span></a></li></ul>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
