Skip to content

Commit

Permalink
sqlite3: Add database and pragmas (#667)
Browse files Browse the repository at this point in the history
I added the type definitions needed for my use case for the sqlite3 gem.
https://github.com/sparklemotion/sqlite3-ruby
  • Loading branch information
euglena1215 authored Sep 16, 2024
1 parent a39ccef commit ccdc086
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
27 changes: 27 additions & 0 deletions gems/sqlite3/2.0/_test/test.rb
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
20 changes: 20 additions & 0 deletions gems/sqlite3/2.0/database.rbs
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
5 changes: 5 additions & 0 deletions gems/sqlite3/2.0/pragmas.rbs
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

0 comments on commit ccdc086

Please sign in to comment.