private void WriteBuffer(PythonBuffer b) { _bytes.Add((byte)'s'); List <byte> newBytes = new List <byte> (); for (int i = 0; i < b.Size; i++) { if (b[i] is string) { string str = b[i] as string; byte[] utfBytes = Encoding.UTF8.GetBytes(str); if (utfBytes.Length != str.Length) { newBytes.AddRange(utfBytes); } else { byte[] strBytes = PythonAsciiEncoding.Instance.GetBytes(str); newBytes.AddRange(strBytes); } } else { newBytes.Add((byte)b[i]); } } WriteInt32(newBytes.Count); _bytes.AddRange(newBytes); }
private void BindParameter(CodeContext context, int index, object arg) { int rc; if (arg == null) { rc = Sqlite3.sqlite3_bind_null(st, index); } else if (arg is int) { rc = Sqlite3.sqlite3_bind_int(st, index, (int)arg); } else if (arg is bool) { rc = Sqlite3.sqlite3_bind_int(st, index, (bool)arg ? 1 : 0); } else if (arg is long) { rc = Sqlite3.sqlite3_bind_int64(st, index, (long)arg); } else if (arg is System.Numerics.BigInteger) { rc = Sqlite3.sqlite3_bind_int64(st, index, (long)((System.Numerics.BigInteger)arg)); } else if (arg is float) { rc = Sqlite3.sqlite3_bind_double(st, index, (float)arg); } else if (arg is double) { rc = Sqlite3.sqlite3_bind_double(st, index, (double)arg); } else if (arg is string) { rc = Sqlite3.sqlite3_bind_text(st, index, (string)arg, -1, Sqlite3.SQLITE_TRANSIENT); } else if (arg is byte[]) { rc = Sqlite3.sqlite3_bind_blob(this.st, index, (byte[])arg, -1, Sqlite3.SQLITE_TRANSIENT); } else if (arg is PythonBuffer) { //TODO: see if there is a better way to do this PythonBuffer buffer = (PythonBuffer)arg; string s = buffer[new Slice(0, null)].ToString(); byte[] bytes = PythonSQLite.Latin1.GetBytes(s); rc = Sqlite3.sqlite3_bind_blob(this.st, index, bytes, -1, Sqlite3.SQLITE_TRANSIENT); } else { throw PythonSQLite.MakeInterfaceError("Unable to bind parameter {0} - unsupported type {1}".Format(index, arg.GetType())); } if (rc != Sqlite3.SQLITE_OK) { throw PythonSQLite.MakeInterfaceError("Unable to bind parameter {0}: {1}".Format(index, Sqlite3.sqlite3_errmsg(db))); } }
public static void SetCharArrayValue(_Array arr, object value) { PythonBuffer buf = value as PythonBuffer; if (buf != null && buf._object is string) { value = buf.ToString(); } arr.NativeType.SetValue(arr._memHolder, 0, value); }
private static void setResult(Sqlite3.sqlite3_context ctx, object result) { if (result == null) { Sqlite3.sqlite3_result_null(ctx); } else if (result is bool) { Sqlite3.sqlite3_result_int64(ctx, ((bool)result) ? 1 : 0); } else if (result is int) { Sqlite3.sqlite3_result_int64(ctx, (int)result); } else if (result is long) { Sqlite3.sqlite3_result_int64(ctx, (long)result); } else if (result is System.Numerics.BigInteger) { Sqlite3.sqlite3_result_int64(ctx, (long)((System.Numerics.BigInteger)result)); } else if (result is float) { Sqlite3.sqlite3_result_double(ctx, (float)result); } else if (result is double) { Sqlite3.sqlite3_result_double(ctx, (double)result); } else if (result is string) { Sqlite3.sqlite3_result_text(ctx, (string)result, -1, Sqlite3.SQLITE_TRANSIENT); } else if (result is byte[]) { byte[] b = (byte[])result; string s = Latin1.GetString(b, 0, b.Length); Sqlite3.sqlite3_result_blob(ctx, s, s.Length, Sqlite3.SQLITE_TRANSIENT); } else if (result is PythonBuffer) { PythonBuffer buffer = (PythonBuffer)result; string s = buffer.__getslice__(0, null).ToString(); Sqlite3.sqlite3_result_blob(ctx, s, s.Length, Sqlite3.SQLITE_TRANSIENT); } else { // TODO raise error } }
private static object[] buildPyParams(CodeContext context, Sqlite3.sqlite3_context ctx, int argc, sqlite3_value[] argv) { object[] args = new object[argc]; for (int i = 0; i < argc; ++i) { sqlite3_value cur_value = argv[i]; object cur_py_value = null; switch (Sqlite3.sqlite3_value_type(cur_value)) { case Sqlite3.SQLITE_INTEGER: cur_py_value = (int)Sqlite3.sqlite3_value_int64(cur_value); break; case Sqlite3.SQLITE_FLOAT: cur_py_value = Sqlite3.sqlite3_value_double(cur_value); break; case Sqlite3.SQLITE_TEXT: cur_py_value = Sqlite3.sqlite3_value_text(cur_value); break; case Sqlite3.SQLITE_BLOB: byte[] result = Sqlite3.sqlite3_value_blob(cur_value); PythonBuffer buffer = new PythonBuffer(context, result); cur_py_value = buffer; break; case Sqlite3.SQLITE_NULL: default: cur_py_value = null; break; } args[i] = cur_py_value; } return(args); }
public override BigInteger write(CodeContext /*!*/ context, object b) { byte[] bArray = b as byte[]; if (bArray != null) { return(write(bArray)); } Bytes bBytes = b as Bytes; if (bBytes != null) { return(write(bBytes)); } PythonBuffer bBuffer = b as PythonBuffer; if (bBuffer != null) { return(write(bBuffer.ToString())); } ArrayModule.array bPythonArray = b as ArrayModule.array; if (bPythonArray != null) { return(write(bPythonArray.ToByteArray())); } ICollection <byte> bCollection = b as ICollection <byte>; if (bCollection != null) { return(write(bCollection)); } EnsureWritable(); throw PythonOps.TypeError("expected a readable buffer object"); }
public static void SetWCharArrayRaw(_Array arr, object value) { PythonBuffer buf = value as PythonBuffer; if (buf != null && (buf._object is string || buf._object is Bytes)) { value = buf.ToString(); } MemoryView view = value as MemoryView; if ((object)view != null) { string strVal = view.tobytes().ToString(); if (strVal.Length > arr.__len__()) { throw PythonOps.ValueError("string too long"); } value = strVal; } arr.NativeType.SetValue(arr._memHolder, 0, value); }
public new void write(PythonBuffer data) { ThrowIfClosed(); base.write(data); }
private object fetchOneRow(CodeContext context) { int numcols = Sqlite3.sqlite3_data_count(this.statement.st); object[] row = new object[numcols]; object converter = null; for (int i = 0; i < numcols; ++i) { object converted = null; if (this.connection.detect_types != 0) { converter = row_cast_map[i]; } else { converter = null; } if (converter != null) { byte[] val = Sqlite3.sqlite3_column_blob(this.statement.st, i); if (val == null) { converted = null; } else { string item = Latin1.GetString(val, 0, val.Length); converted = PythonCalls.Call(context, converter, item); } } else { int coltype = Sqlite3.sqlite3_column_type(this.statement.st, i); switch (coltype) { case Sqlite3.SQLITE_NULL: converted = null; break; case Sqlite3.SQLITE_INTEGER: long l = Sqlite3.sqlite3_column_int64(this.statement.st, i); if (l < int.MinValue || l > int.MaxValue) { converted = l; } else { converted = (int)l; } break; case Sqlite3.SQLITE_FLOAT: converted = Sqlite3.sqlite3_column_double(this.statement.st, i); break; case Sqlite3.SQLITE_TEXT: converted = Sqlite3.sqlite3_column_text(this.statement.st, i); break; case Sqlite3.SQLITE_BLOB: default: byte[] blob = Sqlite3.sqlite3_column_blob(this.statement.st, i); PythonBuffer buffer = new PythonBuffer(context, blob); converted = buffer; break; } } row[i] = converted; } return(new PythonTuple(row)); }
public static Sha256Object sha256(PythonBuffer data) { return(new Sha256Object((IList <byte>)data)); }
public static Sha256Object sha256(PythonBuffer data) { return(new Sha256Object(data)); }
public void update(PythonBuffer newBytes) { update((IList <byte>)newBytes); }
internal MD5Type(PythonBuffer initialBuffer) { _bytes = new byte[0]; update(initialBuffer); }
public static MD5Type md5(PythonBuffer data) { return new MD5Type(data); }
public void update(PythonBuffer buffer) { update(buffer.ToString().MakeByteArray()); }
public void update(PythonBuffer newData) { update((IList <byte>)newData); }
public void update(PythonBuffer buffer) { update((IList <byte>)buffer); }
public static Sha384Object sha384(PythonBuffer data) { return(new Sha384Object(data)); }
public static Sha512Object sha512(PythonBuffer data) { return(new Sha512Object(data)); }
internal Sha512Object(PythonBuffer initialBuffer) { _bytes = new byte[0]; update(initialBuffer); }
public static MD5Type @new(PythonBuffer data) { return(new MD5Type(data)); }
public void update(PythonBuffer newData) { update(newData.ToString().MakeByteArray()); }
public BigInteger readinto([NotNull] PythonBuffer buffer) { EnsureReadable(); throw PythonOps.TypeError("buffer is read-only"); }
public void write([NotNull] PythonBuffer buffer) { _sr.Write(buffer.ToString()); }
public static string hexlify([NotNull] PythonBuffer data) { return(hexlify(data.ToString())); }
public static MD5Object md5(PythonBuffer data) { return(new MD5Object(data)); }
public static sha sha1(PythonBuffer data) { return(new sha(data)); }
internal sha(PythonBuffer initialBuffer) { _bytes = new byte[0]; update(initialBuffer); }