vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.86k stars 2.17k forks source link

Windows: ORM select from syntax error #20987

Open XP1 opened 8 months ago

XP1 commented 8 months ago

Describe the bug

V panic: result not set (near "FROM": syntax error (1) (SELECT  FROM `customers` WHERE `country` = ?1 AND `nr_orders` > ?2;); code: 1)
V panic: result not set (near "FROM": syntax error (1) (SELECT  FROM `customers` WHERE `country` IS NULL;); code: 1)
V panic: result not set (near "FROM": syntax error (1) (SELECT  FROM `customers`;); code: 1)

Reproduction Steps

Sample code from:

https://github.com/vlang/v/blob/master/doc/docs.md#orm

import db.sqlite

// sets a custom table name. Default is struct name (case-sensitive)
@[table: 'customers']
struct Customer {
    id        int     @[primary; sql: serial] // a field named `id` of integer type must be the first field
    name      string
    nr_orders int
    country   ?string
}

db := sqlite.connect('customers.db')!

// You can create tables from your struct declarations. For example the next query will issue SQL similar to this:
// CREATE TABLE IF NOT EXISTS `Customer` (
//      `id` INTEGER PRIMARY KEY,
//      `name` TEXT NOT NULL,
//      `nr_orders` INTEGER NOT NULL,
//      `country` TEXT
// )
sql db {
    create table Customer
}!

// insert a new customer:
new_customer := Customer{
    name: 'Bob'
    country: 'uk'
    nr_orders: 10
}
sql db {
    insert new_customer into Customer
}!

us_customer := Customer{
    name: 'Martin'
    country: 'us'
    nr_orders: 5
}
sql db {
    insert us_customer into Customer
}!

none_country_customer := Customer{
    name: 'Dennis'
    country: none
    nr_orders: 2
}
sql db {
    insert none_country_customer into Customer
}!

// update a customer:
sql db {
    update Customer set nr_orders = nr_orders + 1 where name == 'Bob'
}!

// select count(*) from customers
nr_customers := sql db {
    select count from Customer
}!
println('number of all customers: ${nr_customers}')

// V's syntax can be used to build queries:
uk_customers := sql db {
    select from Customer where country == 'uk' && nr_orders > 0
}!
println('We found a total of ${uk_customers.len} customers matching the query.')
for c in uk_customers {
    println('customer: ${c.id}, ${c.name}, ${c.country}, ${c.nr_orders}')
}

none_country_customers := sql db {
    select from Customer where country is none
}!
println('We found a total of ${none_country_customers.len} customers, with no country set.')
for c in none_country_customers {
    println('customer: ${c.id}, ${c.name}, ${c.country}, ${c.nr_orders}')
}

// delete a customer
sql db {
    delete from Customer where name == 'Bob'
}!

Expected Behavior

Run successfully.

Current Behavior

Syntax error panic.

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.4 60b4fb3

Environment details (OS name and version, etc.)

V full version: V 0.4.4 6279e8a.60b4fb3 OS: windows, Microsoft Windows 11 Pro v22631 64-bit

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

felipensp commented 8 months ago

I can't reproduce it. What sqlite lib version are you using?

XP1 commented 8 months ago

Tried again with latest versions on Windows 10 and 11. Still same error.

V weekly.2024.12 https://github.com/vlang/v/releases/download/weekly.2024.12/v_windows.zip

sqlite-amalgamation-3450200.zip https://sqlite.org/2024/sqlite-amalgamation-3450200.zip (2.61 MiB) C source code as an amalgamation, version 3.45.2. (SHA3-256: 8d9c553b52c7b1656a97fec7907cb00fd1419ac45104e45c76b7c2b81ffe0a9d)

JalonSolov commented 8 months ago

So, Windows specific. On Linux, using latest V, I get this:

number of all customers: 3
We found a total of 1 customers matching the query.
customer: 1, Bob, Option('uk'), 11
We found a total of 1 customers, with no country set.
customer: 3, Dennis, Option(none), 2