mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: move the mysql/sqlite/pg/mssql modules under vlib/db (#16820)
This commit is contained in:
@@ -1,52 +1,4 @@
|
||||
## Description:
|
||||
|
||||
`pg` is a wrapper for the PostgreSQL client library. It provides access to a PostgreSQL
|
||||
database server.
|
||||
|
||||
Before you can use this module, you must first have PostgreSQL installed on your system.
|
||||
To do this, find your OS and perform the actions listed.
|
||||
|
||||
**NOTE**: These instructions are meant only as a convenience. If your OS is not listed
|
||||
or you need extra help, [go here](https://www.postgresql.org/download/).
|
||||
|
||||
### Fedora 31
|
||||
```
|
||||
sudo dnf install postgresql-server postgresql-contrib
|
||||
sudo systemctl enable postgresql # to autostart on startup
|
||||
sudo systemctl start postgresql
|
||||
```
|
||||
|
||||
### Ubuntu/Debian
|
||||
```
|
||||
sudo apt-get install postgresql postgresql-client
|
||||
sudo systemctl enable postgresql # to autostart on startup
|
||||
sudo systemctl start postgresql
|
||||
```
|
||||
|
||||
### MacOSX (Homebrew)
|
||||
```
|
||||
brew install postgresql
|
||||
brew services start postgresql
|
||||
```
|
||||
|
||||
### MacOSX (MacPorts)
|
||||
```
|
||||
gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
|
||||
```
|
||||
|
||||
## Installing libpq-dev or its equivalent for your OS: ##
|
||||
|
||||
**Ubuntu/Debian**: `sudo apt-get install libpq-dev`
|
||||
|
||||
**Red Hat Linux (RHEL)**: `yum install postgresql-devel`
|
||||
|
||||
**OpenSuse**: `zypper in postgresql-devel`
|
||||
|
||||
**ArchLinux**: `pacman -S postgresql-libs`
|
||||
|
||||
##Getting Started with [PostgreSQL](https://www.postgresqltutorial.com/postgresql-getting-started)
|
||||
|
||||
Read this section to learn how to install and connect to PostgreSQL
|
||||
*[Windows](https://www.postgresqltutorial.com/install-postgresql)*;
|
||||
*[Linux](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-linux)*;
|
||||
*[macOS](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-macos)*.
|
||||
The `pg` module has been moved to `db.pg`.
|
||||
Update your code to do: `import db.pg` instead.
|
||||
|
||||
@@ -1,226 +0,0 @@
|
||||
module main
|
||||
|
||||
import orm
|
||||
import pg
|
||||
import time
|
||||
|
||||
struct TestCustomSqlType {
|
||||
id int [primary; sql: serial]
|
||||
custom string [sql_type: 'TEXT']
|
||||
custom1 string [sql_type: 'VARCHAR(191)']
|
||||
custom2 string [sql_type: 'TIMESTAMP']
|
||||
custom3 string [sql_type: 'uuid']
|
||||
}
|
||||
|
||||
struct TestCustomWrongSqlType {
|
||||
id int [primary; sql: serial]
|
||||
custom string
|
||||
custom1 string [sql_type: 'VARCHAR']
|
||||
custom2 string [sql_type: 'money']
|
||||
custom3 string [sql_type: 'xml']
|
||||
}
|
||||
|
||||
struct TestTimeType {
|
||||
mut:
|
||||
id int [primary; sql: serial]
|
||||
username string
|
||||
created_at time.Time [sql_type: 'TIMESTAMP']
|
||||
updated_at string [sql_type: 'TIMESTAMP']
|
||||
deleted_at time.Time
|
||||
}
|
||||
|
||||
struct TestDefaultAtribute {
|
||||
id string [default: 'gen_random_uuid()'; primary; sql_type: 'uuid']
|
||||
name string
|
||||
created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
|
||||
}
|
||||
|
||||
fn test_pg_orm() {
|
||||
mut db := pg.connect(
|
||||
host: 'localhost'
|
||||
user: 'postgres'
|
||||
password: 'password'
|
||||
dbname: 'postgres'
|
||||
) or { panic(err) }
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
}
|
||||
|
||||
db.create('Test', [
|
||||
orm.TableField{
|
||||
name: 'id'
|
||||
typ: typeof[string]().idx
|
||||
is_time: false
|
||||
default_val: ''
|
||||
is_arr: false
|
||||
attrs: [
|
||||
StructAttribute{
|
||||
name: 'primary'
|
||||
has_arg: false
|
||||
arg: ''
|
||||
kind: .plain
|
||||
},
|
||||
StructAttribute{
|
||||
name: 'sql'
|
||||
has_arg: true
|
||||
arg: 'serial'
|
||||
kind: .plain
|
||||
},
|
||||
]
|
||||
},
|
||||
orm.TableField{
|
||||
name: 'name'
|
||||
typ: typeof[string]().idx
|
||||
is_time: false
|
||||
default_val: ''
|
||||
is_arr: false
|
||||
attrs: []
|
||||
},
|
||||
orm.TableField{
|
||||
name: 'age'
|
||||
typ: typeof[i64]().idx
|
||||
is_time: false
|
||||
default_val: ''
|
||||
is_arr: false
|
||||
attrs: []
|
||||
},
|
||||
]) or { panic(err) }
|
||||
|
||||
db.insert('Test', orm.QueryData{
|
||||
fields: ['name', 'age']
|
||||
data: [orm.string_to_primitive('Louis'), orm.int_to_primitive(101)]
|
||||
}) or { panic(err) }
|
||||
|
||||
res := db.@select(orm.SelectConfig{
|
||||
table: 'Test'
|
||||
is_count: false
|
||||
has_where: true
|
||||
has_order: false
|
||||
order: ''
|
||||
order_type: .asc
|
||||
has_limit: false
|
||||
primary: 'id'
|
||||
has_offset: false
|
||||
fields: ['id', 'name', 'age']
|
||||
types: [typeof[int]().idx, typeof[string]().idx, typeof[i64]().idx]
|
||||
}, orm.QueryData{}, orm.QueryData{
|
||||
fields: ['name', 'age']
|
||||
data: [orm.Primitive('Louis'), orm.Primitive(101)]
|
||||
types: []
|
||||
kinds: [.eq, .eq]
|
||||
is_and: [true]
|
||||
}) or { panic(err) }
|
||||
|
||||
id := res[0][0]
|
||||
name := res[0][1]
|
||||
age := res[0][2]
|
||||
|
||||
assert id is int
|
||||
if id is int {
|
||||
assert id == 1
|
||||
}
|
||||
|
||||
assert name is string
|
||||
if name is string {
|
||||
assert name == 'Louis'
|
||||
}
|
||||
|
||||
assert age is i64
|
||||
if age is i64 {
|
||||
assert age == 101
|
||||
}
|
||||
|
||||
/** test orm sql type
|
||||
* - verify if all type create by attribute sql_type has created
|
||||
*/
|
||||
|
||||
sql db {
|
||||
create table TestCustomSqlType
|
||||
}
|
||||
|
||||
mut result_custom_sql := db.exec("
|
||||
SELECT DATA_TYPE
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME = 'TestCustomSqlType'
|
||||
ORDER BY ORDINAL_POSITION
|
||||
") or {
|
||||
println(err)
|
||||
panic(err)
|
||||
}
|
||||
mut information_schema_data_types_results := []string{}
|
||||
information_schema_custom_sql := ['integer', 'text', 'character varying',
|
||||
'timestamp without time zone', 'uuid']
|
||||
for data_type in result_custom_sql {
|
||||
information_schema_data_types_results << data_type.vals[0]
|
||||
}
|
||||
|
||||
sql db {
|
||||
drop table TestCustomSqlType
|
||||
}
|
||||
|
||||
assert information_schema_data_types_results == information_schema_custom_sql
|
||||
|
||||
/** test_orm_time_type
|
||||
* - test time.Time v type with sql_type: 'TIMESTAMP'
|
||||
* - test string v type with sql_type: 'TIMESTAMP'
|
||||
* - test time.Time v type without
|
||||
*/
|
||||
today := time.parse('2022-07-16 15:13:27') or {
|
||||
println(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
model := TestTimeType{
|
||||
username: 'hitalo'
|
||||
created_at: today
|
||||
updated_at: today.str()
|
||||
deleted_at: today
|
||||
}
|
||||
|
||||
sql db {
|
||||
create table TestTimeType
|
||||
}
|
||||
|
||||
sql db {
|
||||
insert model into TestTimeType
|
||||
}
|
||||
|
||||
results := sql db {
|
||||
select from TestTimeType where username == 'hitalo'
|
||||
}
|
||||
|
||||
sql db {
|
||||
drop table TestTimeType
|
||||
}
|
||||
|
||||
assert results[0].username == model.username
|
||||
assert results[0].created_at == model.created_at
|
||||
assert results[0].updated_at == model.updated_at
|
||||
assert results[0].deleted_at == model.deleted_at
|
||||
|
||||
/** test default attribute
|
||||
*/
|
||||
sql db {
|
||||
create table TestDefaultAtribute
|
||||
}
|
||||
|
||||
mut result_defaults := db.exec("
|
||||
SELECT column_default
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME = 'TestDefaultAtribute'
|
||||
ORDER BY ORDINAL_POSITION
|
||||
") or {
|
||||
println(err)
|
||||
panic(err)
|
||||
}
|
||||
mut information_schema_defaults_results := []string{}
|
||||
|
||||
for defaults in result_defaults {
|
||||
information_schema_defaults_results << defaults.vals[0]
|
||||
}
|
||||
sql db {
|
||||
drop table TestDefaultAtribute
|
||||
}
|
||||
assert ['gen_random_uuid()', '', 'CURRENT_TIMESTAMP'] == information_schema_defaults_results
|
||||
}
|
||||
3
vlib/pg/z_deprecated.v
Normal file
3
vlib/pg/z_deprecated.v
Normal file
@@ -0,0 +1,3 @@
|
||||
[deprecated: 'import db.pg instead']
|
||||
[deprecated_after: '2023-02-01']
|
||||
module pg
|
||||
Reference in New Issue
Block a user