-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlite3: Add database and pragmas (#667)
I added the type definitions needed for my use case for the sqlite3 gem. https://github.com/sparklemotion/sqlite3-ruby
- Loading branch information
1 parent
a39ccef
commit ccdc086
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Write Ruby code to test the RBS. | ||
# It is type checked by `steep check` command. | ||
|
||
require "sqlite3" | ||
|
||
SQLite3::Database.new("test.db").execute("SELECT * FROM users").each do |row| | ||
row[0] # Array | ||
end | ||
SQLite3::Database.new("test.db").execute("SELECT * FROM users") do |row| | ||
row[0] # Array | ||
end | ||
|
||
SQLite3::Database.new("test.db", results_as_hash: true).execute("SELECT * FROM users").each do |row| | ||
row['id'] # Hash | ||
end | ||
|
||
SQLite3::Database.new("test.db", results_as_hash: false).execute("SELECT * FROM users").each do |row| | ||
row[0] # Array | ||
end | ||
|
||
SQLite3::Database.new("test.db") do |db| | ||
db.busy_timeout = 5000 | ||
end | ||
|
||
db = SQLite3::Database.new("test.db") | ||
db.execute("INSERT INTO users (name) VALUES ('john')") | ||
db.changes + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module SQLite3 | ||
type row_value_type = String | Integer | Float | nil | ||
type result_as_hash = Hash[String, row_value_type] | ||
type result_as_array = Array[row_value_type] | ||
|
||
class Database[ResultType] | ||
include Pragmas | ||
|
||
def self.new: (String file, results_as_hash: true, **untyped) ?{ (Database[result_as_hash]) -> void } -> Database[result_as_hash] | ||
| (String file, results_as_hash: false, **untyped) ?{ (Database[result_as_array]) -> void } -> Database[result_as_array] | ||
| (String file, **untyped) ?{ (Database[result_as_array]) -> void } -> Database[result_as_array] | ||
|
||
def execute: (String sql, *untyped) -> Array[ResultType] | ||
| (String sql, *untyped) { (ResultType) -> void } -> void | ||
|
||
def get_first_row: (String sql, *untyped) -> ResultType? | ||
|
||
def changes: () -> Integer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module SQLite3 | ||
module Pragmas | ||
def busy_timeout=: (Integer) -> void | ||
end | ||
end |