ホーム › System.Search › SQLNativeSql
SQLNativeSql
関数ドライバが解釈するネイティブSQL文に変換する。
シグネチャ
// ODBC32.dll (ANSI / -A)
#include <windows.h>
SHORT SQLNativeSql(
void* hdbc,
BYTE* szSqlStrIn,
INT cchSqlStrIn,
BYTE* szSqlStr, // optional
INT cchSqlStrMax,
INT* pcbSqlStr
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hdbc | void* | inout | 対象の接続ハンドル。ドライバ固有のSQLへ変換する。 |
| szSqlStrIn | BYTE* | in | 入力するSQL文字列。ANSI文字列。 |
| cchSqlStrIn | INT | in | szSqlStrInの文字数。SQL_NTSでNUL終端を示す。 |
| szSqlStr | BYTE* | outoptional | 変換後のネイティブSQLを受け取るバッファ。ANSI文字列。 |
| cchSqlStrMax | INT | in | szSqlStrバッファの文字数長。 |
| pcbSqlStr | INT* | inout | 変換後SQLの実際の文字数を受け取るポインタ。NULL可。 |
戻り値の型: SHORT
各言語での呼び出し定義
// ODBC32.dll (ANSI / -A)
#include <windows.h>
SHORT SQLNativeSql(
void* hdbc,
BYTE* szSqlStrIn,
INT cchSqlStrIn,
BYTE* szSqlStr, // optional
INT cchSqlStrMax,
INT* pcbSqlStr
);[DllImport("ODBC32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern short SQLNativeSql(
IntPtr hdbc, // void* in/out
IntPtr szSqlStrIn, // BYTE*
int cchSqlStrIn, // INT
IntPtr szSqlStr, // BYTE* optional, out
int cchSqlStrMax, // INT
ref int pcbSqlStr // INT* in/out
);<DllImport("ODBC32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function SQLNativeSql(
hdbc As IntPtr, ' void* in/out
szSqlStrIn As IntPtr, ' BYTE*
cchSqlStrIn As Integer, ' INT
szSqlStr As IntPtr, ' BYTE* optional, out
cchSqlStrMax As Integer, ' INT
ByRef pcbSqlStr As Integer ' INT* in/out
) As Short
End Function' hdbc : void* in/out
' szSqlStrIn : BYTE*
' cchSqlStrIn : INT
' szSqlStr : BYTE* optional, out
' cchSqlStrMax : INT
' pcbSqlStr : INT* in/out
Declare PtrSafe Function SQLNativeSql Lib "odbc32" ( _
ByVal hdbc As LongPtr, _
ByVal szSqlStrIn As LongPtr, _
ByVal cchSqlStrIn As Long, _
ByVal szSqlStr As LongPtr, _
ByVal cchSqlStrMax As Long, _
ByRef pcbSqlStr As Long) As Integer
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SQLNativeSql = ctypes.windll.odbc32.SQLNativeSql
SQLNativeSql.restype = ctypes.c_short
SQLNativeSql.argtypes = [
ctypes.POINTER(None), # hdbc : void* in/out
ctypes.POINTER(ctypes.c_ubyte), # szSqlStrIn : BYTE*
ctypes.c_int, # cchSqlStrIn : INT
ctypes.POINTER(ctypes.c_ubyte), # szSqlStr : BYTE* optional, out
ctypes.c_int, # cchSqlStrMax : INT
ctypes.POINTER(ctypes.c_int), # pcbSqlStr : INT* in/out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ODBC32.dll')
SQLNativeSql = Fiddle::Function.new(
lib['SQLNativeSql'],
[
Fiddle::TYPE_VOIDP, # hdbc : void* in/out
Fiddle::TYPE_VOIDP, # szSqlStrIn : BYTE*
Fiddle::TYPE_INT, # cchSqlStrIn : INT
Fiddle::TYPE_VOIDP, # szSqlStr : BYTE* optional, out
Fiddle::TYPE_INT, # cchSqlStrMax : INT
Fiddle::TYPE_VOIDP, # pcbSqlStr : INT* in/out
],
Fiddle::TYPE_SHORT)#[link(name = "odbc32")]
extern "system" {
fn SQLNativeSql(
hdbc: *mut (), // void* in/out
szSqlStrIn: *mut u8, // BYTE*
cchSqlStrIn: i32, // INT
szSqlStr: *mut u8, // BYTE* optional, out
cchSqlStrMax: i32, // INT
pcbSqlStr: *mut i32 // INT* in/out
) -> i16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ODBC32.dll", CharSet = CharSet.Ansi)]
public static extern short SQLNativeSql(IntPtr hdbc, IntPtr szSqlStrIn, int cchSqlStrIn, IntPtr szSqlStr, int cchSqlStrMax, ref int pcbSqlStr);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ODBC32_SQLNativeSql' -Namespace Win32 -PassThru
# $api::SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr)#uselib "ODBC32.dll"
#func global SQLNativeSql "SQLNativeSql" sptr, sptr, sptr, sptr, sptr, sptr
; SQLNativeSql hdbc, varptr(szSqlStrIn), cchSqlStrIn, varptr(szSqlStr), cchSqlStrMax, varptr(pcbSqlStr) ; 戻り値は stat
; hdbc : void* in/out -> "sptr"
; szSqlStrIn : BYTE* -> "sptr"
; cchSqlStrIn : INT -> "sptr"
; szSqlStr : BYTE* optional, out -> "sptr"
; cchSqlStrMax : INT -> "sptr"
; pcbSqlStr : INT* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "ODBC32.dll" #cfunc global SQLNativeSql "SQLNativeSql" sptr, var, int, var, int, var ; res = SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr) ; hdbc : void* in/out -> "sptr" ; szSqlStrIn : BYTE* -> "var" ; cchSqlStrIn : INT -> "int" ; szSqlStr : BYTE* optional, out -> "var" ; cchSqlStrMax : INT -> "int" ; pcbSqlStr : INT* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ODBC32.dll" #cfunc global SQLNativeSql "SQLNativeSql" sptr, sptr, int, sptr, int, sptr ; res = SQLNativeSql(hdbc, varptr(szSqlStrIn), cchSqlStrIn, varptr(szSqlStr), cchSqlStrMax, varptr(pcbSqlStr)) ; hdbc : void* in/out -> "sptr" ; szSqlStrIn : BYTE* -> "sptr" ; cchSqlStrIn : INT -> "int" ; szSqlStr : BYTE* optional, out -> "sptr" ; cchSqlStrMax : INT -> "int" ; pcbSqlStr : INT* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; SHORT SQLNativeSql(void* hdbc, BYTE* szSqlStrIn, INT cchSqlStrIn, BYTE* szSqlStr, INT cchSqlStrMax, INT* pcbSqlStr) #uselib "ODBC32.dll" #cfunc global SQLNativeSql "SQLNativeSql" intptr, var, int, var, int, var ; res = SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr) ; hdbc : void* in/out -> "intptr" ; szSqlStrIn : BYTE* -> "var" ; cchSqlStrIn : INT -> "int" ; szSqlStr : BYTE* optional, out -> "var" ; cchSqlStrMax : INT -> "int" ; pcbSqlStr : INT* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; SHORT SQLNativeSql(void* hdbc, BYTE* szSqlStrIn, INT cchSqlStrIn, BYTE* szSqlStr, INT cchSqlStrMax, INT* pcbSqlStr) #uselib "ODBC32.dll" #cfunc global SQLNativeSql "SQLNativeSql" intptr, intptr, int, intptr, int, intptr ; res = SQLNativeSql(hdbc, varptr(szSqlStrIn), cchSqlStrIn, varptr(szSqlStr), cchSqlStrMax, varptr(pcbSqlStr)) ; hdbc : void* in/out -> "intptr" ; szSqlStrIn : BYTE* -> "intptr" ; cchSqlStrIn : INT -> "int" ; szSqlStr : BYTE* optional, out -> "intptr" ; cchSqlStrMax : INT -> "int" ; pcbSqlStr : INT* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
odbc32 = windows.NewLazySystemDLL("ODBC32.dll")
procSQLNativeSql = odbc32.NewProc("SQLNativeSql")
)
// hdbc (void* in/out), szSqlStrIn (BYTE*), cchSqlStrIn (INT), szSqlStr (BYTE* optional, out), cchSqlStrMax (INT), pcbSqlStr (INT* in/out)
r1, _, err := procSQLNativeSql.Call(
uintptr(hdbc),
uintptr(szSqlStrIn),
uintptr(cchSqlStrIn),
uintptr(szSqlStr),
uintptr(cchSqlStrMax),
uintptr(pcbSqlStr),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // SHORTfunction SQLNativeSql(
hdbc: Pointer; // void* in/out
szSqlStrIn: Pointer; // BYTE*
cchSqlStrIn: Integer; // INT
szSqlStr: Pointer; // BYTE* optional, out
cchSqlStrMax: Integer; // INT
pcbSqlStr: Pointer // INT* in/out
): Smallint; stdcall;
external 'ODBC32.dll' name 'SQLNativeSql';result := DllCall("ODBC32\SQLNativeSql"
, "Ptr", hdbc ; void* in/out
, "Ptr", szSqlStrIn ; BYTE*
, "Int", cchSqlStrIn ; INT
, "Ptr", szSqlStr ; BYTE* optional, out
, "Int", cchSqlStrMax ; INT
, "Ptr", pcbSqlStr ; INT* in/out
, "Short") ; return: SHORT●SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr) = DLL("ODBC32.dll", "int SQLNativeSql(void*, void*, int, void*, int, void*)")
# 呼び出し: SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr)
# hdbc : void* in/out -> "void*"
# szSqlStrIn : BYTE* -> "void*"
# cchSqlStrIn : INT -> "int"
# szSqlStr : BYTE* optional, out -> "void*"
# cchSqlStrMax : INT -> "int"
# pcbSqlStr : INT* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "odbc32" fn SQLNativeSql(
hdbc: ?*anyopaque, // void* in/out
szSqlStrIn: [*c]u8, // BYTE*
cchSqlStrIn: i32, // INT
szSqlStr: [*c]u8, // BYTE* optional, out
cchSqlStrMax: i32, // INT
pcbSqlStr: [*c]i32 // INT* in/out
) callconv(std.os.windows.WINAPI) i16;proc SQLNativeSql(
hdbc: pointer, # void* in/out
szSqlStrIn: ptr uint8, # BYTE*
cchSqlStrIn: int32, # INT
szSqlStr: ptr uint8, # BYTE* optional, out
cchSqlStrMax: int32, # INT
pcbSqlStr: ptr int32 # INT* in/out
): int16 {.importc: "SQLNativeSql", stdcall, dynlib: "ODBC32.dll".}pragma(lib, "odbc32");
extern(Windows)
short SQLNativeSql(
void* hdbc, // void* in/out
ubyte* szSqlStrIn, // BYTE*
int cchSqlStrIn, // INT
ubyte* szSqlStr, // BYTE* optional, out
int cchSqlStrMax, // INT
int* pcbSqlStr // INT* in/out
);ccall((:SQLNativeSql, "ODBC32.dll"), stdcall, Int16,
(Ptr{Cvoid}, Ptr{UInt8}, Int32, Ptr{UInt8}, Int32, Ptr{Int32}),
hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr)
# hdbc : void* in/out -> Ptr{Cvoid}
# szSqlStrIn : BYTE* -> Ptr{UInt8}
# cchSqlStrIn : INT -> Int32
# szSqlStr : BYTE* optional, out -> Ptr{UInt8}
# cchSqlStrMax : INT -> Int32
# pcbSqlStr : INT* in/out -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int16_t SQLNativeSql(
void* hdbc,
uint8_t* szSqlStrIn,
int32_t cchSqlStrIn,
uint8_t* szSqlStr,
int32_t cchSqlStrMax,
int32_t* pcbSqlStr);
]]
local odbc32 = ffi.load("odbc32")
-- odbc32.SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr)
-- hdbc : void* in/out
-- szSqlStrIn : BYTE*
-- cchSqlStrIn : INT
-- szSqlStr : BYTE* optional, out
-- cchSqlStrMax : INT
-- pcbSqlStr : INT* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ODBC32.dll');
const SQLNativeSql = lib.func('__stdcall', 'SQLNativeSql', 'int16_t', ['void *', 'uint8_t *', 'int32_t', 'uint8_t *', 'int32_t', 'int32_t *']);
// SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr)
// hdbc : void* in/out -> 'void *'
// szSqlStrIn : BYTE* -> 'uint8_t *'
// cchSqlStrIn : INT -> 'int32_t'
// szSqlStr : BYTE* optional, out -> 'uint8_t *'
// cchSqlStrMax : INT -> 'int32_t'
// pcbSqlStr : INT* in/out -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ODBC32.dll", {
SQLNativeSql: { parameters: ["pointer", "pointer", "i32", "pointer", "i32", "pointer"], result: "i16" },
});
// lib.symbols.SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr)
// hdbc : void* in/out -> "pointer"
// szSqlStrIn : BYTE* -> "pointer"
// cchSqlStrIn : INT -> "i32"
// szSqlStr : BYTE* optional, out -> "pointer"
// cchSqlStrMax : INT -> "i32"
// pcbSqlStr : INT* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int16_t SQLNativeSql(
void* hdbc,
uint8_t* szSqlStrIn,
int32_t cchSqlStrIn,
uint8_t* szSqlStr,
int32_t cchSqlStrMax,
int32_t* pcbSqlStr);
C, "ODBC32.dll");
// $ffi->SQLNativeSql(hdbc, szSqlStrIn, cchSqlStrIn, szSqlStr, cchSqlStrMax, pcbSqlStr);
// hdbc : void* in/out
// szSqlStrIn : BYTE*
// cchSqlStrIn : INT
// szSqlStr : BYTE* optional, out
// cchSqlStrMax : INT
// pcbSqlStr : INT* in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Odbc32 extends StdCallLibrary {
Odbc32 INSTANCE = Native.load("odbc32", Odbc32.class, W32APIOptions.ASCII_OPTIONS);
short SQLNativeSql(
Pointer hdbc, // void* in/out
byte[] szSqlStrIn, // BYTE*
int cchSqlStrIn, // INT
byte[] szSqlStr, // BYTE* optional, out
int cchSqlStrMax, // INT
IntByReference pcbSqlStr // INT* in/out
);
}@[Link("odbc32")]
lib LibODBC32
fun SQLNativeSql = SQLNativeSql(
hdbc : Void*, # void* in/out
szSqlStrIn : UInt8*, # BYTE*
cchSqlStrIn : Int32, # INT
szSqlStr : UInt8*, # BYTE* optional, out
cchSqlStrMax : Int32, # INT
pcbSqlStr : Int32* # INT* in/out
) : Int16
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SQLNativeSqlNative = Int16 Function(Pointer<Void>, Pointer<Uint8>, Int32, Pointer<Uint8>, Int32, Pointer<Int32>);
typedef SQLNativeSqlDart = int Function(Pointer<Void>, Pointer<Uint8>, int, Pointer<Uint8>, int, Pointer<Int32>);
final SQLNativeSql = DynamicLibrary.open('ODBC32.dll')
.lookupFunction<SQLNativeSqlNative, SQLNativeSqlDart>('SQLNativeSql');
// hdbc : void* in/out -> Pointer<Void>
// szSqlStrIn : BYTE* -> Pointer<Uint8>
// cchSqlStrIn : INT -> Int32
// szSqlStr : BYTE* optional, out -> Pointer<Uint8>
// cchSqlStrMax : INT -> Int32
// pcbSqlStr : INT* in/out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SQLNativeSql(
hdbc: Pointer; // void* in/out
szSqlStrIn: Pointer; // BYTE*
cchSqlStrIn: Integer; // INT
szSqlStr: Pointer; // BYTE* optional, out
cchSqlStrMax: Integer; // INT
pcbSqlStr: Pointer // INT* in/out
): Smallint; stdcall;
external 'ODBC32.dll' name 'SQLNativeSql';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SQLNativeSql"
c_SQLNativeSql :: Ptr () -> Ptr Word8 -> Int32 -> Ptr Word8 -> Int32 -> Ptr Int32 -> IO Int16
-- hdbc : void* in/out -> Ptr ()
-- szSqlStrIn : BYTE* -> Ptr Word8
-- cchSqlStrIn : INT -> Int32
-- szSqlStr : BYTE* optional, out -> Ptr Word8
-- cchSqlStrMax : INT -> Int32
-- pcbSqlStr : INT* in/out -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let sqlnativesql =
foreign "SQLNativeSql"
((ptr void) @-> (ptr uint8_t) @-> int32_t @-> (ptr uint8_t) @-> int32_t @-> (ptr int32_t) @-> returning int16_t)
(* hdbc : void* in/out -> (ptr void) *)
(* szSqlStrIn : BYTE* -> (ptr uint8_t) *)
(* cchSqlStrIn : INT -> int32_t *)
(* szSqlStr : BYTE* optional, out -> (ptr uint8_t) *)
(* cchSqlStrMax : INT -> int32_t *)
(* pcbSqlStr : INT* in/out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library odbc32 (t "ODBC32.dll"))
(cffi:use-foreign-library odbc32)
(cffi:defcfun ("SQLNativeSql" sqlnative-sql :convention :stdcall) :int16
(hdbc :pointer) ; void* in/out
(sz-sql-str-in :pointer) ; BYTE*
(cch-sql-str-in :int32) ; INT
(sz-sql-str :pointer) ; BYTE* optional, out
(cch-sql-str-max :int32) ; INT
(pcb-sql-str :pointer)) ; INT* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SQLNativeSql = Win32::API::More->new('ODBC32',
'short SQLNativeSql(LPVOID hdbc, LPVOID szSqlStrIn, int cchSqlStrIn, LPVOID szSqlStr, int cchSqlStrMax, LPVOID pcbSqlStr)');
# my $ret = $SQLNativeSql->Call($hdbc, $szSqlStrIn, $cchSqlStrIn, $szSqlStr, $cchSqlStrMax, $pcbSqlStr);
# hdbc : void* in/out -> LPVOID
# szSqlStrIn : BYTE* -> LPVOID
# cchSqlStrIn : INT -> int
# szSqlStr : BYTE* optional, out -> LPVOID
# cchSqlStrMax : INT -> int
# pcbSqlStr : INT* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f SQLNativeSqlA (ANSI版) — SQL文をドライバ固有のネイティブSQLに変換する。
- f SQLNativeSqlW (Unicode版) — ドライバが解釈するネイティブSQL文に変換する(Unicode)。