static int conn_connect(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); luaL_argcheck(L, lua_istable(L, 2), 1, "argument must be table"); lua_getfield(L, 2, "host"); const char* host = luaL_checkstring(L, -1); lua_getfield(L, 2, "user"); const char* user = luaL_checkstring(L, -1); lua_getfield(L, 2, "passwd"); const char* passwd = luaL_checkstring(L, -1); lua_getfield(L, 2, "db"); const char* db = lua_tostring(L, -1); lua_getfield(L, 2, "port"); unsigned int port = (unsigned int)luaL_optinteger(L, -1, 3306); lua_getfield(L, 2, "unix_socket"); const char* unix_socket = lua_tostring(L, -1); lua_getfield(L, 2, "client_flag"); unsigned long flags = (unsigned long)luaL_optinteger(L, -1, 0); lua_pop(L, 7); if (mysql_real_connect(&conn->my_conn, host, user, passwd, db, port, unix_socket, flags) != &conn->my_conn) { return luaL_error(L, "connect failed, %s\n", mysql_error(&conn->my_conn)); } return 0; }
static int conn_tostring(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn, 1, "invalid Connection object"); lua_pushfstring(L, "Connection* (%p)", conn); return 1; }
static int conn_rollback(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); if (mysql_rollback(&conn->my_conn) != 0) { return luaL_error(L, "rollback failed, %s\n", mysql_error(&conn->my_conn)); } return 0; }
static int conn_set_compress(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); int err = mysql_options(&conn->my_conn, MYSQL_OPT_COMPRESS, NULL); if (err != 0) { return luaL_error(L, "%d: %s\n", err, mysql_error(&conn->my_conn)); } return 0; }
static int conn_close(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn, 1, "invalid Connection object"); if (!conn->closed) { mysql_close(&conn->my_conn); conn->closed = 1; } return 0; }
static int conn_set_protocol(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); unsigned int protocol = (unsigned int)luaL_checkinteger(L, 2); int err = mysql_options(&conn->my_conn, MYSQL_OPT_PROTOCOL, &protocol); if (err != 0) { return luaL_error(L, "%d: %s\n", err, mysql_error(&conn->my_conn)); } return 0; }
static int conn_set_write_timeout(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); uint32_t timeout = (unsigned int)luaL_checkinteger(L, 2); int err = mysql_options(&conn->my_conn, MYSQL_OPT_WRITE_TIMEOUT, &timeout); if (err != 0) { return luaL_error(L, "%d: %s\n", err, mysql_error(&conn->my_conn)); } return 0; }
static int conn_set_reconnect(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); my_bool val = (my_bool)lua_toboolean(L, 2); int err = mysql_options(&conn->my_conn, MYSQL_OPT_RECONNECT, &val); if (err != 0) { return luaL_error(L, "%d: %s\n", err, mysql_error(&conn->my_conn)); } return 0; }
static int conn_set_charset(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); const char* charset = luaL_checkstring(L, 2); int err = mysql_options(&conn->my_conn, MYSQL_SET_CHARSET_NAME, charset); if (err != 0) { return luaL_error(L, "%d: %s\n", err, mysql_error(&conn->my_conn)); } return 0; }
static int conn_ping(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); MYSQL* my_conn = &conn->my_conn; if (my_conn->methods == NULL) { int err = mysql_ping(my_conn); if (err != 0) { return luaL_error(L, "%d: %s\n", err, mysql_error(&conn->my_conn)); } } return 0; }
static int conn_escape_string(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); size_t length = 0; const char* stmt = luaL_checklstring(L, 2, &length); char* dest = malloc(length * 2 + 1); if (dest == NULL) { return luaL_error(L, "malloc() failed."); } unsigned long newlen = mysql_real_escape_string(&conn->my_conn, dest, stmt, (unsigned long)length); lua_pushlstring(L, dest, newlen); free(dest); return 1; }
static int conn_execute(lua_State* L) { Connection* conn = check_conn(L); luaL_argcheck(L, conn && !conn->closed, 1, "invalid Connection object"); size_t length = 0; const char* stmt = luaL_checklstring(L, 2, &length); const char* fetch_opt = lua_tostring(L, 3); int fetch_all = 1; if (fetch_opt) { if (strcmp(fetch_opt, "store") == 0) fetch_all = 1; else if (strcmp(fetch_opt, "use") == 0) fetch_all = 0; } MYSQL* my_conn = &conn->my_conn; int err = mysql_real_query(my_conn, stmt, (unsigned long)length); if (err != 0) { return luaL_error(L, "%d: %s\n", err, mysql_error(my_conn)); } MYSQL_RES* res = NULL; if (fetch_all) res = mysql_store_result(my_conn); else res = mysql_use_result(my_conn); unsigned int numcols = mysql_field_count(my_conn); if (res) { return create_cursor(L, 1, res, numcols, fetch_all); } else { if (numcols == 0) // query does not return data (not SELECT) { lua_pushinteger(L, (lua_Integer)mysql_affected_rows(my_conn)); return 1; } } return 0; }
//Initialise the device byte ITG3200::init() { isconnected = check_conn(); if(!isconnected) return 0; //Set proper mode of operation Wire.beginTransmission(address); Wire.write((byte)DLPF_FS); //select register 22, DLPF_FS register Wire.write((byte)0x19); //00011001b Wire.endTransmission(); //Set the divider (sampling at 1kHz, dividing by divider +1) Wire.beginTransmission(address); Wire.write((byte)SMPLRT_DIV); Wire.write((byte)rate); Wire.endTransmission(); return 1; }
//Initialise the device byte HMC5883L::init() { isconnected = check_conn(); if(!isconnected) return 0; //Put the HMC5883 IC into the correct operating mode Wire.beginTransmission(address); Wire.write((byte)CONF_REG_A); //Configuration register A Wire.write((byte)((average&0x03)<<5)|((rate&0x07)<<2)); //Set the avg and rate Wire.endTransmission(); Wire.beginTransmission(address); Wire.write((byte)CONF_REG_B); //Configuration register B Wire.write((byte)((gain&0x07)<<5)); //Set the gain Wire.endTransmission(); Wire.beginTransmission(address); //open communication with HMC5883 Wire.write((byte)MODE_REG); //select mode register Wire.write((byte)0x00); //continuous measurement mode Wire.endTransmission(); return 1; }
int main(int argc, char **argv) { char *pghost, *pgport, *pgoptions, *pgtty; char *dbName1, *dbName2; char *tblName; int nFields; int i, j; PGconn *conn1, *conn2; /* * PGresult *res1, *res2; */ PGresult *res1; if (argc != 4) { fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]); fprintf(stderr, " compares two tables in two databases\n"); exit(1); } tblName = argv[1]; dbName1 = argv[2]; dbName2 = argv[3]; /* * begin, by setting the parameters for a backend connection if the * parameters are null, then the system will try to use reasonable * defaults by looking up environment variables or, failing that, using * hardwired constants */ pghost = NULL; /* host name of the backend server */ pgport = NULL; /* port of the backend server */ pgoptions = NULL; /* special options to start up the backend * server */ pgtty = NULL; /* debugging tty for the backend server */ /* make a connection to the database */ conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1); check_conn(conn1, dbName1); conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2); check_conn(conn2, dbName2); /* start a transaction block */ res1 = PQexec(conn1, "BEGIN"); if (PQresultStatus(res1) != PGRES_COMMAND_OK) { fprintf(stderr, "BEGIN command failed\n"); PQclear(res1); exit_nicely(conn1, conn2); } /* * make sure to PQclear() a PGresult whenever it is no longer needed to * avoid memory leaks */ PQclear(res1); /* * fetch instances from the pg_database, the system catalog of databases */ res1 = PQexec(conn1, "DECLARE myportal CURSOR FOR select * from pg_database"); if (PQresultStatus(res1) != PGRES_COMMAND_OK) { fprintf(stderr, "DECLARE CURSOR command failed\n"); PQclear(res1); exit_nicely(conn1, conn2); } PQclear(res1); res1 = PQexec(conn1, "FETCH ALL in myportal"); if (PQresultStatus(res1) != PGRES_TUPLES_OK) { fprintf(stderr, "FETCH ALL command didn't return tuples properly\n"); PQclear(res1); exit_nicely(conn1, conn2); } /* first, print out the attribute names */ nFields = PQnfields(res1); for (i = 0; i < nFields; i++) printf("%-15s", PQfname(res1, i)); printf("\n\n"); /* next, print out the instances */ for (i = 0; i < PQntuples(res1); i++) { for (j = 0; j < nFields; j++) printf("%-15s", PQgetvalue(res1, i, j)); printf("\n"); } PQclear(res1); /* close the portal */ res1 = PQexec(conn1, "CLOSE myportal"); PQclear(res1); /* end the transaction */ res1 = PQexec(conn1, "END"); PQclear(res1); /* close the connections to the database and cleanup */ PQfinish(conn1); PQfinish(conn2); /* fclose(debug); */ return 0; }