Module db.mssql stdlib

db.mssql
Version:
0.3.3
License:
MIT
Dependencies from vmod:
0
Imports:
0
Imported by:
0
Repository:
OS-specific
Show selected OS-specific symbols.
Backend-specific
Show selected Backend-specific symbols.

Dependencies defined in v.mod

This section is empty.

Imports

This section is empty.

Imported by

This section is empty.

Overview

  • This is a V wrapper of SQL Server ODBC C/C++ library

Dependencies

Windows Notes

Using msvc

  • Make sure cl.exe of msvc is accessible from command line. You can run v commands in Visual Studio 2019 Developer Command Prompt to be safe.
  • C Headers and dlls can be automatically resolved by msvc.

Using tcc

  • Copy those headers to @VEXEROOT\thirdparty\mssql\include. The version number 10.0.18362.0 might differ on your system. Command Prompt commands:
copy "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\sql.h" thirdparty\mssql\include
copy "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\sqlext.h" thirdparty\mssql\include
copy "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\sqltypes.h" thirdparty\mssql\include
copy "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\sqlucode.h" thirdparty\mssql\include
copy "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared\sal.h" thirdparty\mssql\include
copy "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared\concurrencysal.h" thirdparty\mssql\include
  • dlls can be automatically resolved by tcc

TODO

  • Support Mac
  • Support ORM

Usage

import mssql

fn test_example() ? {
    // connect to server
    config := mssql.Config{
        driver: 'ODBC Driver 17 for SQL Server'
        server: 'tcp:localhost'
        uid: '<your username>'
        pwd: '<your password>'
    }

    mut conn := mssql.Connection{}

    conn.connect(config.get_conn_str())?

    defer {
        conn.close()
    }

    // get current db name
    mut query := 'SELECT DB_NAME()'
    mut res := conn.query(query)?
    assert res == mssql.Result{
        rows: [mssql.Row{
            vals: ['master']
        }]
        num_rows_affected: -1
    }
}

Aliases

This section is empty.

Constants

This section is empty.

Sum types

This section is empty.

Functions

This section is empty.

Structs

#struct Config

pub struct Config {
pub:
	driver string
	server string
	uid    string
	pwd    string
	// if dbname empty, conn str will not contain Database info,
	// and it is up to the server to choose which db to connect to.
	dbname string
}

Config TODO

#fn (Config) get_conn_str

fn (cfg Config) get_conn_str() string

#struct Connection

pub struct Connection {
mut:
	henv C.SQLHENV = C.SQLHENV(C.SQL_NULL_HENV) // Environment
	hdbc C.SQLHDBC = C.SQLHDBC(C.SQL_NULL_HDBC) // Connection handle
pub mut:
	conn_str string
}

#fn (&Connection) connect

fn (mut conn &Connection) connect(conn_str string) !bool

connect to db

#fn (&Connection) close

fn (mut conn &Connection) close()

close - closes the connection.

#fn (&Connection) query

fn (mut conn &Connection) query(q string) !Result

query executes a sql query

#struct Row

pub struct Row {
pub mut:
	vals []string
}

#struct Result

pub struct Result {
pub mut:
	rows []Row
	// the number of rows affected by sql statement
	num_rows_affected int
}

Interfaces

This section is empty.

Enums

This section is empty.