Skip to content

导出go语言标准库database, 由generator 工具生成在手动修改的仓库,导出功能到脚本域中使用,支持多种数据库驱动。

Notifications You must be signed in to change notification settings

php-any/database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

database

编译进go执行器中

使用sql.Load(vm)启用数据库功能

package main

import (
	"fmt"
	"github.com/php-any/database/sql"
	"os"

	_ "github.com/go-sql-driver/mysql" // 使用 mysql
	"github.com/php-any/origami/parser"
	"github.com/php-any/origami/runtime"
	"github.com/php-any/origami/std"
)

func main() {
	// 创建解析器
	p := parser.NewParser()

	// 创建程序运行的环境
	vm := runtime.NewVM(p)
	std.Load(vm)
	sql.Load(vm)
	
	// 获取脚本路径参数
	scriptPath := os.Args[1]

	// 检查文件是否存在
	if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
		_, _ = fmt.Fprintf(os.Stderr, "错误: 文件 '%s' 不存在\n\n", scriptPath)
		os.Exit(1)
	}

	_, err := vm.LoadAndRun(scriptPath)
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "错误: %v\n", err)
		if !parser.InLSP {
			panic(err)
		}
	}
}


脚本中使用示例

namespace test;

use database\sql\open;

$db = open("mysql", "root:root@/temp");

$db->ping();

$rows = $db->query("SELECT id, age, name, coin, create_at FROM users");

for ;$rows->next(); {
    $rows->scan($id, $age, $name, $coin, $create_at);

    echo "id: {$id}, age: {$age}, name: {$name}, coin: {$coin}, create_at: {$create_at}\n";
}

About

导出go语言标准库database, 由generator 工具生成在手动修改的仓库,导出功能到脚本域中使用,支持多种数据库驱动。

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages