You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Configuration parameters can be appended via URL-style query (e.g. 'file:test.db?results_as_hash=true')
39
+
# 2. options hash supports:
40
+
# - results_as_hash: controls whether result sets return as hashes (default: arrays)
41
+
db =ChDB::Database.new('file:test.db', results_as_hash:true)
42
+
43
+
# Create a database
44
+
db.execute('CREATE DATABASE IF NOT EXISTS test')
28
45
29
46
# Create a table
47
+
db.execute('DROP TABLE IF EXISTS test.test_table')
30
48
rows = db.execute <<-SQL
31
-
CREATETABLEtest_table(
49
+
CREATETABLEtest.test_table(
32
50
id Int32,
33
51
name String)
34
52
ENGINE = MergeTree()
35
-
ORDER BY id);
53
+
ORDER BY id
36
54
SQL
37
55
38
56
# Execute a few inserts
39
57
{
40
-
1 => 'Alice',
41
-
2 => 'Bob'
58
+
1 => 'Alice',
59
+
2 => 'Bob'
42
60
}.each do |pair|
43
-
db.execute 'INSERT INTO test_table VALUES ( ?, ? )', pair
61
+
db.execute 'INSERT INTO test.test_table VALUES ( ?, ? )', pair
44
62
end
45
63
46
64
# Find a few rows
47
-
db.execute('SELECT * FROM test_table ORDER BY id') do |row|
65
+
db.execute('SELECT * FROM test.test_table ORDER BY id') do |row|
48
66
p row
49
67
end
50
68
# [{ 'id' => '1', 'name' => 'Alice' },
51
69
# { 'id' => '2', 'name' => 'Bob' }]
52
70
71
+
# When you need to open another database, you must first close the previous database
72
+
db.close()
73
+
53
74
# Open another database
54
-
db =ChDB::Database.new'test2.db'
75
+
db =ChDB::Database.new'file:test.db'
55
76
56
77
# Create another table
78
+
db.execute('DROP TABLE IF EXISTS test.test2_table')
57
79
rows = db.execute <<-SQL
58
-
CREATETABLEtest2_table(
80
+
CREATETABLEtest.test2_table(
59
81
id Int32,
60
82
name String)
61
83
ENGINE = MergeTree()
62
-
ORDER BY id");
84
+
ORDER BY id
63
85
SQL
64
86
65
87
# Execute inserts with parameter markers
66
-
db.execute('INSERT INTO test2_table (id, name)
88
+
db.execute('INSERT INTO test.test2_table (id, name)
67
89
VALUES (?, ?)', [3, 'Charlie'])
68
90
69
-
db.execute2('SELECT * FROM test2_table') do |row|
91
+
# Find rows with the first row displaying column names
92
+
db.execute2('SELECT * FROM test.test2_table') do |row|
70
93
p row
71
94
end
72
-
# [['id', 'name'], [3, 'Charlie']],
95
+
# ["id", "name"]
96
+
# ["3", "Charlie"]
97
+
98
+
# Close the database
99
+
db.close()
100
+
101
+
# Use ChDB::Database.open to automatically close the database connection:
102
+
ChDB::Database.open('file:test.db') do |db|
103
+
result = db.execute('SELECT 1')
104
+
p result.to_a # => [["1"]]
105
+
end
106
+
107
+
# Query with specific output formats (CSV, JSON, etc.):
108
+
# See more details at https://clickhouse.com/docs/interfaces/formats.
109
+
ChDB::Database.open(':memory:') do |db|
110
+
csv_data = db.query_with_format('SELECT 1 as a, 2 as b', 'CSV')
111
+
p csv_data
112
+
# "1,2\n"
113
+
114
+
json_data = db.query_with_format('SELECT 1 as a, 2 as b', 'JSON')
115
+
p json_data
116
+
end
73
117
```
74
118
75
119
## Thread Safety
76
120
77
-
When using `ChDB::Database.new` to open a session, all read/write operations within that session are thread-safe. However, currently only one active session is allowed per process. Therefore, when you need to open another session, you must first close the previous session.
78
-
79
-
For example, the following code is fine because only the database
80
-
instance is shared among threads:
121
+
When using `ChDB::Database.new` or `ChDB::Database.open` to open a database connection, all read/write operations within that session are thread-safe. However, currently only one active database connection is allowed per process. Therefore, when you need to open another database connection, you must first close the previous connection.
122
+
**Please note that `ChDB::Database.new`, `ChDB::Database.open`, and `ChDB::Database.close` methods themselves are not thread-safe.** If used in multi-threaded environments, external synchronization must be implemented to prevent concurrent calls to these methods, which could lead to undefined behavior.
0 commit comments