kjkjsakjdsaha9dusadijsalksadsd

This commit is contained in:
Fijxu 2024-11-08 20:37:17 -03:00
commit 9bb808ae7f
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
9 changed files with 185 additions and 0 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Fijxu <fijxu@nadeko.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

27
README.md Normal file
View file

@ -0,0 +1,27 @@
# crystal-jsonb-test
TODO: Write a description here
## Installation
TODO: Write installation instructions here
## Usage
TODO: Write usage instructions here
## Development
TODO: Write development instructions here
## Contributing
1. Fork it (<https://github.com/your-github-user/crystal-jsonb-test/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [Fijxu](https://github.com/your-github-user) - creator and maintainer

10
shard.lock Normal file
View file

@ -0,0 +1,10 @@
version: 2.0
shards:
db:
git: https://github.com/crystal-lang/crystal-db.git
version: 0.13.1
pg:
git: https://github.com/will/crystal-pg.git
version: 0.29.0

19
shard.yml Normal file
View file

@ -0,0 +1,19 @@
name: crystal-jsonb-test
version: 0.1.0
authors:
- Fijxu <fijxu@nadeko.net>
targets:
crystal-jsonb-test:
main: src/crystal-jsonb-test.cr
dependencies:
db:
github: crystal-lang/crystal-db
pg:
github: will/crystal-pg
crystal: '>= 1.13.3'
license: MIT

View file

@ -0,0 +1,9 @@
require "./spec_helper"
describe Crystal::Jsonb::Test do
# TODO: Write tests
it "works" do
false.should eq(true)
end
end

2
spec/spec_helper.cr Normal file
View file

@ -0,0 +1,2 @@
require "spec"
require "../src/crystal-jsonb-test"

30
src/crystal-jsonb-test.cr Normal file
View file

@ -0,0 +1,30 @@
require "db"
require "pg"
require "json"
PG_DB = DB.open "postgres://postgres:postgres@127.0.0.1:5432/postgres"
PG_DB.exec("CREATE TABLE IF NOT EXISTS test4 (id SERIAL PRIMARY KEY, data JSONB)")
json = JSON.build do |json|
json.object do
json.field "xd", "xd"
end
end
insert = <<-SQL
INSERT INTO test4 (data) VALUES ($1)
SQL
PG_DB.exec(insert, json)
result = PG_DB.query_one("SELECT data FROM test4 WHERE id = 1", as: JSON::Any)
puts result["xd"]
# update = <<-SQL
# UPDATE test4
# SET data = array_append(data, $1)
# WHERE id = $2
# SQL
# PG_DB.exec(update, json, 1)

22
src/real.cr Normal file
View file

@ -0,0 +1,22 @@
require "db"
require "pg"
require "json"
PG_DB = DB.open "postgres://postgres:postgres@127.0.0.1:5432/postgres"
PG_DB.exec("CREATE TABLE IF NOT EXISTS test2 (id SERIAL PRIMARY KEY, data JSONB)")
# json = %({ "xd": "xd" })
json = JSON.build do |json|
json.object do
json.field "xd", "xd"
end
end
PG_DB.exec("INSERT INTO test2 (data) VALUES ($1)", json)
result = PG_DB.query_one("SELECT data FROM test2 WHERE id = 1", as: JSON::Any)
puts result["xd"]

45
src/xd.cr Normal file
View file

@ -0,0 +1,45 @@
require "db"
require "pg"
require "json"
PG_DB = DB.open "postgres://postgres:postgres@127.0.0.1:5432/postgres"
PG_DB.exec("CREATE TABLE IF NOT EXISTS test (id TEXT PRIMARY KEY, data _JSONB)")
json : String = %({ "name": "John", "age": 30, "city": "New York" })
# # json = JSON.build do |json|
# # json.object do
# # json.field "name", "John"
# # json.field "age", 30
# # json.field "city", "New York"
# # end
# # end
# PG_DB.exec("INSERT INTO test (id, data) VALUES ('fijxu', '#{json}')")
# # PG_DB.exec("INSERT INTO test (id, data) VALUES ('fijxu', '#{json}')")
# # result = PG_DB.query_one("SELECT data FROM test WHERE id = 1", as: JSON::Any)
# # puts result["name"] # => "John"
# request = <<-SQL
# UPDATE test
# SET data = array_append(data, $1)
# WHERE id = $2
# SQL
# # PG_DB.exec(request, json, "fijxu")
# result = PG_DB.query_one("SELECT data FROM test WHERE id = $1", "fijxu", as: JSON::Any)
# # puts result["name"] # => "John"
# pp result