21
21
package sqlutils
22
22
23
23
import (
24
+ "fmt"
24
25
"regexp"
25
26
"strings"
26
27
"testing"
@@ -91,7 +92,7 @@ func TestToSqlite3AlterTable(t *testing.T) {
91
92
database_instance
92
93
ADD COLUMN sql_delay INT UNSIGNED NOT NULL AFTER replica_lag_seconds
93
94
`
94
- result := stripSpaces (ToSqlite3Dialect (statement ))
95
+ result := stripSpaces (ToSqlite3Dialect (statement , true ))
95
96
require .Equal (t , result , stripSpaces (`
96
97
ALTER TABLE
97
98
database_instance
@@ -104,7 +105,7 @@ func TestToSqlite3AlterTable(t *testing.T) {
104
105
database_instance
105
106
ADD INDEX source_host_port_idx (source_host, source_port)
106
107
`
107
- result := stripSpaces (ToSqlite3Dialect (statement ))
108
+ result := stripSpaces (ToSqlite3Dialect (statement , true ))
108
109
require .Equal (t , result , stripSpaces (`
109
110
create index
110
111
source_host_port_idx_database_instance
@@ -117,7 +118,7 @@ func TestToSqlite3AlterTable(t *testing.T) {
117
118
topology_recovery
118
119
ADD KEY last_detection_idx (last_detection_id)
119
120
`
120
- result := stripSpaces (ToSqlite3Dialect (statement ))
121
+ result := stripSpaces (ToSqlite3Dialect (statement , true ))
121
122
require .Equal (t , result , stripSpaces (`
122
123
create index
123
124
last_detection_idx_topology_recovery
@@ -134,7 +135,7 @@ func TestCreateIndex(t *testing.T) {
134
135
source_host_port_idx_database_instance
135
136
on database_instance (source_host(128), source_port)
136
137
`
137
- result := stripSpaces (ToSqlite3Dialect (statement ))
138
+ result := stripSpaces (ToSqlite3Dialect (statement , true ))
138
139
require .Equal (t , result , stripSpaces (`
139
140
create index
140
141
source_host_port_idx_database_instance
@@ -173,7 +174,7 @@ func TestToSqlite3Insert(t *testing.T) {
173
174
domain_name=values(domain_name),
174
175
last_registered=values(last_registered)
175
176
`
176
- result := stripSpaces (ToSqlite3Dialect (statement ))
177
+ result := stripSpaces (ToSqlite3Dialect (statement , true ))
177
178
require .Equal (t , result , stripSpaces (`
178
179
replace into
179
180
cluster_domain_name (cluster_name, domain_name, last_registered)
@@ -186,62 +187,62 @@ func TestToSqlite3Insert(t *testing.T) {
186
187
func TestToSqlite3GeneralConversions (t * testing.T ) {
187
188
{
188
189
statement := "select now()"
189
- result := ToSqlite3Dialect (statement )
190
+ result := ToSqlite3Dialect (statement , false )
190
191
require .Equal (t , result , "select datetime('now')" )
191
192
}
192
193
{
193
194
statement := "select now() - interval ? second"
194
- result := ToSqlite3Dialect (statement )
195
+ result := ToSqlite3Dialect (statement , false )
195
196
require .Equal (t , result , "select datetime('now', printf('-%d second', ?))" )
196
197
}
197
198
{
198
199
statement := "select now() + interval ? minute"
199
- result := ToSqlite3Dialect (statement )
200
+ result := ToSqlite3Dialect (statement , false )
200
201
require .Equal (t , result , "select datetime('now', printf('+%d minute', ?))" )
201
202
}
202
203
{
203
204
statement := "select now() + interval 5 minute"
204
- result := ToSqlite3Dialect (statement )
205
+ result := ToSqlite3Dialect (statement , false )
205
206
require .Equal (t , result , "select datetime('now', '+5 minute')" )
206
207
}
207
208
{
208
209
statement := "select some_table.some_column + interval ? minute"
209
- result := ToSqlite3Dialect (statement )
210
+ result := ToSqlite3Dialect (statement , false )
210
211
require .Equal (t , result , "select datetime(some_table.some_column, printf('+%d minute', ?))" )
211
212
}
212
213
{
213
214
statement := "AND primary_instance.last_attempted_check <= primary_instance.last_seen + interval ? minute"
214
- result := ToSqlite3Dialect (statement )
215
+ result := ToSqlite3Dialect (statement , false )
215
216
require .Equal (t , result , "AND primary_instance.last_attempted_check <= datetime(primary_instance.last_seen, printf('+%d minute', ?))" )
216
217
}
217
218
{
218
219
statement := "select concat(primary_instance.port, '') as port"
219
- result := ToSqlite3Dialect (statement )
220
+ result := ToSqlite3Dialect (statement , false )
220
221
require .Equal (t , result , "select (primary_instance.port || '') as port" )
221
222
}
222
223
{
223
224
statement := "select concat( 'abc' , 'def') as s"
224
- result := ToSqlite3Dialect (statement )
225
+ result := ToSqlite3Dialect (statement , false )
225
226
require .Equal (t , result , "select ('abc' || 'def') as s" )
226
227
}
227
228
{
228
229
statement := "select concat( 'abc' , 'def', last.col) as s"
229
- result := ToSqlite3Dialect (statement )
230
+ result := ToSqlite3Dialect (statement , false )
230
231
require .Equal (t , result , "select ('abc' || 'def' || last.col) as s" )
231
232
}
232
233
{
233
234
statement := "select concat(myself.only) as s"
234
- result := ToSqlite3Dialect (statement )
235
+ result := ToSqlite3Dialect (statement , false )
235
236
require .Equal (t , result , "select concat(myself.only) as s" )
236
237
}
237
238
{
238
239
statement := "select concat(1, '2', 3, '4') as s"
239
- result := ToSqlite3Dialect (statement )
240
+ result := ToSqlite3Dialect (statement , false )
240
241
require .Equal (t , result , "select concat(1, '2', 3, '4') as s" )
241
242
}
242
243
{
243
244
statement := "select group_concat( 'abc' , 'def') as s"
244
- result := ToSqlite3Dialect (statement )
245
+ result := ToSqlite3Dialect (statement , false )
245
246
require .Equal (t , result , "select group_concat( 'abc' , 'def') as s" )
246
247
}
247
248
}
@@ -307,27 +308,45 @@ func TestToSqlite3Dialect(t *testing.T) {
307
308
308
309
for _ , test := range tests {
309
310
t .Run (test .input , func (t * testing.T ) {
310
- result := ToSqlite3Dialect (test .input )
311
+ result := ToSqlite3Dialect (test .input , true )
311
312
assert .Equal (t , test .expected , result )
312
313
})
313
314
}
314
315
}
315
316
316
- func BenchmarkToSqlite3Dialect_Insert (b * testing.B ) {
317
- statement := `INSERT ignore INTO database_instance
318
- (alias, hostname, port, last_checked, last_attempted_check, last_check_partial_success, server_id, server_uuid,
319
- version, major_version, version_comment, binlog_server, read_only, binlog_format,
320
- binlog_row_image, log_bin, log_replica_updates, binary_log_file, binary_log_pos, source_host, source_port, replica_net_timeout, heartbeat_interval,
321
- replica_sql_running, replica_io_running, replication_sql_thread_state, replication_io_thread_state, has_replication_filters, supports_oracle_gtid, oracle_gtid, source_uuid, ancestry_uuid, executed_gtid_set, gtid_mode, gtid_purged, gtid_errant, mariadb_gtid, pseudo_gtid,
322
- source_log_file, read_source_log_pos, relay_source_log_file, exec_source_log_pos, relay_log_file, relay_log_pos, last_sql_error, last_io_error, replication_lag_seconds, replica_lag_seconds, sql_delay, data_center, region, physical_environment, replication_depth, is_co_primary, has_replication_credentials, allow_tls, semi_sync_enforced, semi_sync_primary_enabled, semi_sync_primary_timeout, semi_sync_primary_wait_for_replica_count, semi_sync_replica_enabled, semi_sync_primary_status, semi_sync_primary_clients, semi_sync_replica_status, last_discovery_latency, last_seen)
317
+ func buildToSqlite3Dialect_Insert (instances int ) string {
318
+ var rows []string
319
+ for i := 0 ; i < instances ; i ++ {
320
+ rows = append (rows , `(?, ?, ?, NOW(), NOW(), 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())` )
321
+ }
322
+
323
+ return fmt .Sprintf (`INSERT ignore INTO database_instance
324
+ (alias, hostname, port, last_checked, last_attempted_check, last_check_partial_success, server_id, server_uuid,
325
+ version, major_version, version_comment, binlog_server, read_only, binlog_format,
326
+ binlog_row_image, log_bin, log_replica_updates, binary_log_file, binary_log_pos, source_host, source_port, replica_net_timeout, heartbeat_interval,
327
+ replica_sql_running, replica_io_running, replication_sql_thread_state, replication_io_thread_state, has_replication_filters, supports_oracle_gtid, oracle_gtid, source_uuid, ancestry_uuid, executed_gtid_set, gtid_mode, gtid_purged, gtid_errant, mariadb_gtid, pseudo_gtid,
328
+ source_log_file, read_source_log_pos, relay_source_log_file, exec_source_log_pos, relay_log_file, relay_log_pos, last_sql_error, last_io_error, replication_lag_seconds, replica_lag_seconds, sql_delay, data_center, region, physical_environment, replication_depth, is_co_primary, has_replication_credentials, allow_tls, semi_sync_enforced, semi_sync_primary_enabled, semi_sync_primary_timeout, semi_sync_primary_wait_for_replica_count, semi_sync_replica_enabled, semi_sync_primary_status, semi_sync_primary_clients, semi_sync_replica_status, last_discovery_latency, last_seen)
323
329
VALUES
324
- (?, ?, ?, NOW(), NOW(), 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
330
+ %s
325
331
ON DUPLICATE KEY UPDATE
326
- alias=VALUES(alias), hostname=VALUES(hostname), port=VALUES(port), last_checked=VALUES(last_checked), last_attempted_check=VALUES(last_attempted_check), last_check_partial_success=VALUES(last_check_partial_success), server_id=VALUES(server_id), server_uuid=VALUES(server_uuid), version=VALUES(version), major_version=VALUES(major_version), version_comment=VALUES(version_comment), binlog_server=VALUES(binlog_server), read_only=VALUES(read_only), binlog_format=VALUES(binlog_format), binlog_row_image=VALUES(binlog_row_image), log_bin=VALUES(log_bin), log_replica_updates=VALUES(log_replica_updates), binary_log_file=VALUES(binary_log_file), binary_log_pos=VALUES(binary_log_pos), source_host=VALUES(source_host), source_port=VALUES(source_port), replica_net_timeout=VALUES(replica_net_timeout), heartbeat_interval=VALUES(heartbeat_interval), replica_sql_running=VALUES(replica_sql_running), replica_io_running=VALUES(replica_io_running), replication_sql_thread_state=VALUES(replication_sql_thread_state), replication_io_thread_state=VALUES(replication_io_thread_state), has_replication_filters=VALUES(has_replication_filters), supports_oracle_gtid=VALUES(supports_oracle_gtid), oracle_gtid=VALUES(oracle_gtid), source_uuid=VALUES(source_uuid), ancestry_uuid=VALUES(ancestry_uuid), executed_gtid_set=VALUES(executed_gtid_set), gtid_mode=VALUES(gtid_mode), gtid_purged=VALUES(gtid_purged), gtid_errant=VALUES(gtid_errant), mariadb_gtid=VALUES(mariadb_gtid), pseudo_gtid=VALUES(pseudo_gtid), source_log_file=VALUES(source_log_file), read_source_log_pos=VALUES(read_source_log_pos), relay_source_log_file=VALUES(relay_source_log_file), exec_source_log_pos=VALUES(exec_source_log_pos), relay_log_file=VALUES(relay_log_file), relay_log_pos=VALUES(relay_log_pos), last_sql_error=VALUES(last_sql_error), last_io_error=VALUES(last_io_error), replication_lag_seconds=VALUES(replication_lag_seconds), replica_lag_seconds=VALUES(replica_lag_seconds), sql_delay=VALUES(sql_delay), data_center=VALUES(data_center), region=VALUES(region), physical_environment=VALUES(physical_environment), replication_depth=VALUES(replication_depth), is_co_primary=VALUES(is_co_primary), has_replication_credentials=VALUES(has_replication_credentials), allow_tls=VALUES(allow_tls),
327
- semi_sync_enforced=VALUES(semi_sync_enforced), semi_sync_primary_enabled=VALUES(semi_sync_primary_enabled), semi_sync_primary_timeout=VALUES(semi_sync_primary_timeout), semi_sync_primary_wait_for_replica_count=VALUES(semi_sync_primary_wait_for_replica_count), semi_sync_replica_enabled=VALUES(semi_sync_replica_enabled), semi_sync_primary_status=VALUES(semi_sync_primary_status), semi_sync_primary_clients=VALUES(semi_sync_primary_clients), semi_sync_replica_status=VALUES(semi_sync_replica_status),
328
- last_discovery_latency=VALUES(last_discovery_latency), last_seen=VALUES(last_seen)
329
- `
332
+ alias=VALUES(alias), hostname=VALUES(hostname), port=VALUES(port), last_checked=VALUES(last_checked), last_attempted_check=VALUES(last_attempted_check), last_check_partial_success=VALUES(last_check_partial_success), server_id=VALUES(server_id), server_uuid=VALUES(server_uuid), version=VALUES(version), major_version=VALUES(major_version), version_comment=VALUES(version_comment), binlog_server=VALUES(binlog_server), read_only=VALUES(read_only), binlog_format=VALUES(binlog_format), binlog_row_image=VALUES(binlog_row_image), log_bin=VALUES(log_bin), log_replica_updates=VALUES(log_replica_updates), binary_log_file=VALUES(binary_log_file), binary_log_pos=VALUES(binary_log_pos), source_host=VALUES(source_host), source_port=VALUES(source_port), replica_net_timeout=VALUES(replica_net_timeout), heartbeat_interval=VALUES(heartbeat_interval), replica_sql_running=VALUES(replica_sql_running), replica_io_running=VALUES(replica_io_running), replication_sql_thread_state=VALUES(replication_sql_thread_state), replication_io_thread_state=VALUES(replication_io_thread_state), has_replication_filters=VALUES(has_replication_filters), supports_oracle_gtid=VALUES(supports_oracle_gtid), oracle_gtid=VALUES(oracle_gtid), source_uuid=VALUES(source_uuid), ancestry_uuid=VALUES(ancestry_uuid), executed_gtid_set=VALUES(executed_gtid_set), gtid_mode=VALUES(gtid_mode), gtid_purged=VALUES(gtid_purged), gtid_errant=VALUES(gtid_errant), mariadb_gtid=VALUES(mariadb_gtid), pseudo_gtid=VALUES(pseudo_gtid), source_log_file=VALUES(source_log_file), read_source_log_pos=VALUES(read_source_log_pos), relay_source_log_file=VALUES(relay_source_log_file), exec_source_log_pos=VALUES(exec_source_log_pos), relay_log_file=VALUES(relay_log_file), relay_log_pos=VALUES(relay_log_pos), last_sql_error=VALUES(last_sql_error), last_io_error=VALUES(last_io_error), replication_lag_seconds=VALUES(replication_lag_seconds), replica_lag_seconds=VALUES(replica_lag_seconds), sql_delay=VALUES(sql_delay), data_center=VALUES(data_center), region=VALUES(region), physical_environment=VALUES(physical_environment), replication_depth=VALUES(replication_depth), is_co_primary=VALUES(is_co_primary), has_replication_credentials=VALUES(has_replication_credentials), allow_tls=VALUES(allow_tls),
333
+ semi_sync_enforced=VALUES(semi_sync_enforced), semi_sync_primary_enabled=VALUES(semi_sync_primary_enabled), semi_sync_primary_timeout=VALUES(semi_sync_primary_timeout), semi_sync_primary_wait_for_replica_count=VALUES(semi_sync_primary_wait_for_replica_count), semi_sync_replica_enabled=VALUES(semi_sync_replica_enabled), semi_sync_primary_status=VALUES(semi_sync_primary_status), semi_sync_primary_clients=VALUES(semi_sync_primary_clients), semi_sync_replica_status=VALUES(semi_sync_replica_status),
334
+ last_discovery_latency=VALUES(last_discovery_latency), last_seen=VALUES(last_seen)
335
+ ` , strings .Join (rows , "\n \t \t \t \t " ))
336
+ }
337
+
338
+ func BenchmarkToSqlite3Dialect_Insert1000 (b * testing.B ) {
330
339
for i := 0 ; i < b .N ; i ++ {
331
- ToSqlite3Dialect (statement )
340
+ b .StopTimer ()
341
+ statement := buildToSqlite3Dialect_Insert (1000 )
342
+ b .StartTimer ()
343
+ ToSqlite3Dialect (statement , true )
344
+ }
345
+ }
346
+
347
+ func BenchmarkToSqlite3Dialect_Select (b * testing.B ) {
348
+ for i := 0 ; i < b .N ; i ++ {
349
+ statement := "select now() - interval ? second"
350
+ ToSqlite3Dialect (statement , false )
332
351
}
333
352
}
0 commit comments